Off-screen rendering
An off-screen browser renders into a pixel buffer that your code draws, instead of into a surface on screen. Use it to compose web content into your own views, textures or a car or kiosk UI. This guide covers 0.21.0.
Frames
CefriumBrowser browser = new CefriumBrowser(width, height, scale);
browser.setRenderHandler(new CefriumRenderHandler() {
@Override
public void onPaint(int width, int height, ByteBuffer pixels) {
// RGBA, premultiplied, width * height * 4 bytes. Copy it before returning.
}
});
browser.loadUrl("https://example.com");
Width and height are physical pixels; the page sees them divided by
scale, so scale picks the layout (2.75 on a 1080
pixel wide phone gives a 393 CSS pixel mobile page).
When the view that shows the frames changes size, call
browser.setSize(width, height). The page lays out again, gets a
resize event, and the next frames come at the new size.
Before 0.15.0 an off-screen browser kept its first size.
Input
sendTap, sendTouchDown/Move/Up,
sendSwipe and the mouse calls take coordinates in the browser's
pixels. A long press on text selects it; see
Text selection.
A host that gets desktop-style input (a game engine, a JCEF port) maps it to the mouse and key calls one to one:
browser.sendMouseMove(x, y, modifiers);
browser.sendMouseDown(x, y, CefriumBrowser.MOUSE_BUTTON_LEFT, modifiers);
browser.sendMouseUp(x, y, CefriumBrowser.MOUSE_BUTTON_LEFT, modifiers);
browser.sendMouseClick(x, y, button, clickCount, modifiers);
browser.sendMouseWheel(x, y, deltaX, deltaY, modifiers);
browser.sendMouseLeave(x, y);
browser.sendKeyEvent(androidKeyEvent); // android.view.KeyEvent
Signatures and constants are in the CefriumBrowser reference.
A quick swipe keeps scrolling after the finger lifts, as in a window (0.16.0).
Text goes in through the IME calls (imeSetComposition,
imeCommitText), which reach the focused field in any frame.
An app that keeps its own text field and writes the page's field from it
with a script should run that script with
evaluateJavaScriptInFocusedFrame (0.16.0):
evaluateJavaScript runs in the main frame, where the focused
element of a page with the field inside an iframe is the iframe.
Text selection
Selection works on the page itself, not on the frames: the engine knows
the text, so there is nothing to recognise in the pixels. Forward a long
press with sendTouchEvent and the engine selects the word under
it. Three callbacks of CefriumRenderHandler do the rest:
onTextSelectionChanged(text)gives the selected text.onQuickMenu(x, y, width, height, flags, callback)says where to show a Copy / Cut / Paste menu and which of them apply; callcallback.copy()(orcut,paste,cancel) once.onTouchHandleChanged(handle)(0.17.0) says where to draw each selection handle: itsid,orientation(LEFTfor the start,RIGHTfor the end,CENTERfor a caret),x,y,alphaand whether it isenabled. Draw itTouchHandle.SIZEsquare from (x, y). A drag that starts there moves that end of the selection; you only forward the touches.
Keep the menu non-modal, or the handles cannot be dragged while it
shows. Before 0.17.0 the handle callback reported a moved handle as hidden.
The OSR IME sample in cefrium-sample does all of this.
Pickers and dialogs
Android Chromium shows <select>, the date and time inputs,
<input type=color> and alert(),
confirm() and prompt() as Android dialogs on the
browser's window. An off-screen browser has no window, so it needs one of
these:
// The standard Android dialogs, on your activity.
browser.setDialogActivity(activity);
// Or your own UI.
browser.setPickerHandler(new CefriumPickerHandler() {
@Override
public boolean onShowSelect(SelectItem[] items, int selected, boolean multiple,
SelectCallback callback) {
// Show the items; then callback.select(new int[] {index}) or callback.cancel().
return true;
}
});
With neither, the picker is cancelled and the page keeps its value, and a
JS dialog returns as if dismissed. onShowDateTime receives the
value as Chromium represents it: milliseconds since midnight for a time,
months since January 1970 for a month, otherwise milliseconds since 1970 UTC.
A <select multiple> is a list inside the page and needs no
picker. JS dialogs can also be handled with
setJsDialogHandler.
Media
| Feature | Off-screen |
|---|---|
<video>, hardware (H.264) and software (VP9) decoded | Frames in onPaint since 0.15.0 |
Video from a MediaStream, canvas, WebGL, OffscreenCanvas | Rendered |
<audio> and video sound | Played on the device. setAudioHandler takes the audio instead |
Sound that starts without a tap (autoplay, play()) | Blocked by default, as in Chrome; setMediaPlaybackRequiresUserGesture(false) allows it (0.16.0) |
| Fullscreen | Enters and leaves; screen orientation lock is not supported off-screen |
| Picture-in-picture | Not supported (the request is rejected) |
Video, audio and fullscreen are covered by on-device tests in the release suite; the other rows were measured on a device.