5package org.qtproject.qt.android;
7import android.annotation.SuppressLint;
8import android.content.Context;
9import android.content.res.TypedArray;
10import android.graphics.PixelFormat;
11import android.graphics.Rect;
12import android.opengl.EGL14;
13import android.opengl.EGLConfig;
14import android.opengl.EGLContext;
15import android.opengl.EGLDisplay;
16import android.opengl.EGLSurface;
17import android.opengl.GLES20;
18import android.os.Build;
19import android.util.Log;
20import android.view.Surface;
21import android.view.SurfaceControl;
22import android.view.SurfaceHolder;
23import android.view.SurfaceView;
25@SuppressLint(
"ViewConstructor")
26class QtSurface extends SurfaceView implements SurfaceHolder.Callback2
28 private final int m_format;
30 private boolean m_backgroundPrimed =
false;
31 private boolean m_primeUnconsumed =
false;
32 private SurfaceControl m_renderControl;
33 private Surface m_renderSurface;
39 setFocusableInTouchMode(
false);
40 setZOrderMediaOverlay(onTop);
41 m_surfaceCallback = surfaceCallback;
42 m_format = imageDepth == 16 ? PixelFormat.RGB_565 :
PixelFormat.RGBA_8888;
43 SurfaceHolder holder = getHolder();
44 holder.setFormat(m_format);
45 holder.addCallback(
this);
49 public void surfaceCreated(SurfaceHolder holder)
54 final Rect frame = holder.getSurfaceFrame();
56 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && canPrime)
57 m_backgroundPrimed = paintBackgroundColor(holder.getSurface());
58 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
60 final SurfaceControl control =
new SurfaceControl.Builder()
62 .setParent(getSurfaceControl())
63 .setBufferSize(Math.max(1, getWidth()), Math.max(1, getHeight()))
67 initializeRenderSurface(control,
new Surface(control));
68 }
catch (RuntimeException |
Error failure) {
71 }
catch (RuntimeException |
Error cleanupFailure) {
72 failure.addSuppressed(cleanupFailure);
77 if (m_surfaceCallback !=
null)
78 m_surfaceCallback.onSurfaceCreated(
79 m_renderSurface !=
null ? m_renderSurface : holder.getSurface());
80 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && canPrime)
81 m_backgroundPrimed = m_primeUnconsumed = paintBackgroundColor(holder.getSurface());
84 private void initializeRenderSurface(SurfaceControl control, Surface surface)
86 try (SurfaceControl.Transaction transaction =
new SurfaceControl.Transaction()) {
87 transaction.setVisibility(control,
true).apply();
88 }
catch (RuntimeException |
Error failure) {
91 }
catch (RuntimeException |
Error cleanupFailure) {
92 failure.addSuppressed(cleanupFailure);
96 m_renderControl = control;
97 m_renderSurface = surface;
101 public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int height)
103 if (m_renderControl !=
null) {
104 try (SurfaceControl.Transaction transaction =
new SurfaceControl.Transaction()) {
105 transaction.setBufferSize(m_renderControl,
width,
height).apply();
109 if (!m_backgroundPrimed && m_renderSurface !=
null &&
width > 0 &&
height > 0)
110 m_backgroundPrimed = m_primeUnconsumed = paintBackgroundColor(holder.getSurface());
114 public void surfaceRedrawNeeded(SurfaceHolder holder)
120 public void surfaceRedrawNeededAsync(SurfaceHolder holder, Runnable drawingFinished)
123 if (m_renderSurface !=
null) {
124 if (m_primeUnconsumed)
125 m_primeUnconsumed =
false;
127 paintBackgroundColor(holder.getSurface());
129 if (m_surfaceCallback !=
null)
130 m_surfaceCallback.onSurfaceRedrawNeeded(drawingFinished);
132 drawingFinished.run();
136 public void surfaceDestroyed(SurfaceHolder holder)
139 m_backgroundPrimed = m_primeUnconsumed =
false;
140 final Surface surface = m_renderSurface;
141 final SurfaceControl control = m_renderControl;
142 m_renderSurface =
null;
143 m_renderControl =
null;
144 final Runnable released = () -> {
150 Throwable detachFailure =
null;
152 if (control !=
null) {
153 try (SurfaceControl.Transaction transaction =
new SurfaceControl.Transaction()) {
154 transaction.reparent(control,
null).apply();
157 }
catch (RuntimeException |
Error failure) {
158 detachFailure = failure;
162 if (m_surfaceCallback !=
null)
163 m_surfaceCallback.onSurfaceDestroyed(released);
166 }
catch (RuntimeException |
Error failure) {
167 if (detachFailure ==
null)
169 detachFailure.addSuppressed(failure);
176 private boolean paintBackgroundColor(Surface surface)
178 if (surface ==
null || !surface.isValid()) {
179 Log.w(
QtNative.QtTAG,
"Cannot prime an invalid background Surface");
184 if (
display == EGL14.EGL_NO_DISPLAY)
185 return backgroundEglFailure(
"get display");
187 final int[] version =
new int[2];
188 if (!EGL14.eglInitialize(
display, version, 0, version, 1))
189 return backgroundEglFailure(
"initialize display");
191 final EGLConfig[] configs =
new EGLConfig[1];
192 final int[] matching =
new int[1];
193 final int[] configAttribs = {
194 EGL14.EGL_SURFACE_TYPE, EGL14.EGL_WINDOW_BIT,
195 EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
196 EGL14.EGL_RED_SIZE, 8,
197 EGL14.EGL_GREEN_SIZE, 8,
198 EGL14.EGL_BLUE_SIZE, 8,
199 EGL14.EGL_ALPHA_SIZE, 8,
203 if (!EGL14.eglChooseConfig(
display, configAttribs, 0, configs, 0, 1, matching, 0))
204 return backgroundEglFailure(
"choose config");
206 if (matching[0] == 0) {
207 Log.w(
QtNative.QtTAG,
"No EGL config for the background Surface");
211 final int[] contextAttribs = { EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE };
212 final EGLContext
context = EGL14.eglCreateContext(
display, configs[0],
213 EGL14.EGL_NO_CONTEXT, contextAttribs, 0);
214 if (context == EGL14.EGL_NO_CONTEXT)
215 return backgroundEglFailure(
"create context");
218 surface,
new int[] { EGL14.EGL_NONE }, 0);
219 if (eglSurface == EGL14.EGL_NO_SURFACE) {
220 backgroundEglFailure(
"create window surface");
221 if (!EGL14.eglDestroyContext(
display, context))
222 backgroundEglFailure(
"destroy context");
227 final EGLDisplay previousDisplay = EGL14.eglGetCurrentDisplay();
228 final EGLContext previousContext = EGL14.eglGetCurrentContext();
229 final EGLSurface previousDraw = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
230 final EGLSurface previousRead = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
231 boolean madeCurrent =
false;
233 madeCurrent = EGL14.eglMakeCurrent(
display, eglSurface, eglSurface, context);
235 Log.w(
QtNative.QtTAG,
"Cannot bind the background EGL context: 0x"
236 +
Integer.toHexString(EGL14.eglGetError()));
239 final int color = resolveBackgroundColor();
241 ((
color >> 16) & 0xff) / 255
f,
242 ((
color >> 8) & 0xff) / 255
f,
244 ((
color >>> 24) & 0xff) / 255
f);
245 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
246 if (!EGL14.eglSwapBuffers(
display, eglSurface)) {
247 Log.w(
QtNative.QtTAG,
"Cannot post the background EGL buffer: 0x"
248 +
Integer.toHexString(EGL14.eglGetError()));
254 final EGLDisplay restoreDisplay = previousDisplay.equals(EGL14.EGL_NO_DISPLAY)
256 if (!EGL14.eglMakeCurrent(restoreDisplay, previousDraw,
257 previousRead, previousContext)) {
258 Log.w(
QtNative.QtTAG,
"Cannot restore the UI thread EGL binding: 0x"
259 +
Integer.toHexString(EGL14.eglGetError()));
260 if (!EGL14.eglMakeCurrent(
display, EGL14.EGL_NO_SURFACE,
261 EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT)) {
262 Log.w(
QtNative.QtTAG,
"Cannot release the background EGL binding: 0x"
263 +
Integer.toHexString(EGL14.eglGetError()));
267 if (!EGL14.eglDestroySurface(
display, eglSurface)) {
268 Log.w(
QtNative.QtTAG,
"Cannot destroy the background EGL surface: 0x"
269 +
Integer.toHexString(EGL14.eglGetError()));
271 if (!EGL14.eglDestroyContext(
display, context)) {
272 Log.w(
QtNative.QtTAG,
"Cannot destroy the background EGL context: 0x"
273 +
Integer.toHexString(EGL14.eglGetError()));
278 private static boolean backgroundEglFailure(String operation)
280 Log.w(
QtNative.QtTAG,
"Cannot " + operation +
" for the background EGL buffer: 0x"
281 +
Integer.toHexString(EGL14.eglGetError()));
285 private int resolveBackgroundColor()
287 TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(
289 android.R.attr.windowSplashScreenBackground,
290 android.R.attr.colorBackground
294 final int splash = typedArray.getColor(0, 0);
296 return splash | 0xff000000;
297 return typedArray.getColor(1, 0xff000000) | 0xff000000;
299 typedArray.recycle();
struct wl_display * display
Napi::TypedArray TypedArray
QVideoFrameFormat::PixelFormat PixelFormat
typedef EGLSurface(EGLAPIENTRYP PFNEGLCREATESTREAMPRODUCERSURFACEKHRPROC)(EGLDisplay dpy
typedef EGLDisplay(EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(EGLenum platform
GLuint color
[setDefaultFactory]