Skip to content

Commit

Permalink
Add rendered image callback for CefBrowserOsr/CefRenderer (fixes chro…
Browse files Browse the repository at this point in the history
  • Loading branch information
Osiris-Team authored and magreenblatt committed Jan 11, 2024
1 parent 566dc4e commit ade64c3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
4 changes: 3 additions & 1 deletion java/org/cef/SystemBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class SystemBootstrap {
/**
* Simple interface for how a library by name should be loaded.
*/
static public interface Loader { public void loadLibrary(String libname); }
static public interface Loader {
public void loadLibrary(String libname);
}

/**
* Default implementation is to call System.loadLibrary
Expand Down
28 changes: 28 additions & 0 deletions java/org/cef/browser/CefBrowserOsr.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
Expand All @@ -84,6 +86,9 @@ class CefBrowserOsr extends CefBrowser_N implements CefRenderHandler {
private int depth_per_component = 8;
private boolean isTransparent_;

private CopyOnWriteArrayList<Consumer<CefPaintEvent>> onPaintListeners =
new CopyOnWriteArrayList<>();

CefBrowserOsr(CefClient client, String url, boolean transparent, CefRequestContext context) {
this(client, url, transparent, context, null, null);
}
Expand Down Expand Up @@ -354,6 +359,22 @@ public void onPopupSize(CefBrowser browser, Rectangle size) {
renderer_.onPopupSize(size);
}

@Override
public void addOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.add(listener);
}

@Override
public void setOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.clear();
onPaintListeners.add(listener);
}

@Override
public void removeOnPaintListener(Consumer<CefPaintEvent> listener) {
onPaintListeners.remove(listener);
}

@Override
public void onPaint(CefBrowser browser, boolean popup, Rectangle[] dirtyRects,
ByteBuffer buffer, int width, int height) {
Expand All @@ -376,6 +397,13 @@ public void run() {
canvas_.display();
}
});
if (!onPaintListeners.isEmpty()) {
CefPaintEvent paintEvent =
new CefPaintEvent(browser, popup, dirtyRects, buffer, width, height);
for (Consumer<CefPaintEvent> l : onPaintListeners) {
l.accept(paintEvent);
}
}
}

@Override
Expand Down
51 changes: 51 additions & 0 deletions java/org/cef/browser/CefPaintEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2024 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

package org.cef.browser;

import java.awt.*;
import java.nio.ByteBuffer;

public class CefPaintEvent {
private final CefBrowser browser;
private final boolean popup;
private final Rectangle[] dirtyRects;
private final ByteBuffer renderedFrame;
private final int width;
private final int height;

public CefPaintEvent(CefBrowser browser, boolean popup, Rectangle[] dirtyRects,
ByteBuffer renderedFrame, int width, int height) {
this.browser = browser;
this.popup = popup;
this.dirtyRects = dirtyRects;
this.renderedFrame = renderedFrame;
this.width = width;
this.height = height;
}

public CefBrowser getBrowser() {
return browser;
}

public boolean getPopup() {
return popup;
}

public Rectangle[] getDirtyRects() {
return dirtyRects;
}

public ByteBuffer getRenderedFrame() {
return renderedFrame;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}
7 changes: 4 additions & 3 deletions java/org/cef/handler/CefAppHandlerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public void onBeforeCommandLineProcessing(String process_type, CefCommandLine co
continue;
}
// Arguments with '--', '-' and, on Windows, '/' prefixes are considered switches.
int switchCnt = arg.startsWith("--")
? 2
: arg.startsWith("/") ? 1 : arg.startsWith("-") ? 1 : 0;
int switchCnt = arg.startsWith("--") ? 2
: arg.startsWith("/") ? 1
: arg.startsWith("-") ? 1
: 0;
switch (switchCnt) {
case 2:
// An argument of "--" will terminate switch parsing with all subsequent
Expand Down
20 changes: 20 additions & 0 deletions java/org/cef/handler/CefRenderHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package org.cef.handler;

import org.cef.browser.CefBrowser;
import org.cef.browser.CefPaintEvent;
import org.cef.callback.CefDragData;

import java.awt.Point;
import java.awt.Rectangle;
import java.nio.ByteBuffer;
import java.util.function.Consumer;

/**
* Implement this interface to handle events when window rendering is disabled.
Expand Down Expand Up @@ -65,6 +67,24 @@ public interface CefRenderHandler {
public void onPaint(CefBrowser browser, boolean popup, Rectangle[] dirtyRects,
ByteBuffer buffer, int width, int height);

/**
* Add provided listener.
* @param listener Code that gets executed after a frame was rendered.
*/
public void addOnPaintListener(Consumer<CefPaintEvent> listener);

/**
* Remove existing listeners and replace with provided listener.
* @param listener Code that gets executed after a frame was rendered.
*/
public void setOnPaintListener(Consumer<CefPaintEvent> listener);

/**
* Remove provided listener.
* @param listener Code that gets executed after a frame was rendered.
*/
public void removeOnPaintListener(Consumer<CefPaintEvent> listener);

/**
* Handle cursor changes.
* @param browser The browser generating the event.
Expand Down

0 comments on commit ade64c3

Please sign in to comment.