blob: 56f1e64396af93ae10c0d68a962bcf6c785dca79 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkiaOpenGLPipeline.h"
18
Alec Mouri3afb3972022-05-27 22:03:11 +000019#include <GrBackendSurface.h>
20#include <SkBlendMode.h>
21#include <SkImageInfo.h>
22#include <cutils/properties.h>
rnleece9762b2021-05-21 15:40:53 -070023#include <gui/TraceUtils.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000024#include <strings.h>
25
Stan Iliev500a0c32016-10-26 10:30:09 -040026#include "DeferredLayerUpdater.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000027#include "FrameInfo.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050028#include "LayerDrawable.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010029#include "LightingInfo.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040030#include "SkiaPipeline.h"
31#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070032#include "hwui/Bitmap.h"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040033#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070034#include "renderstate/RenderState.h"
35#include "renderthread/EglManager.h"
36#include "renderthread/Frame.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000037#include "renderthread/IRenderPipeline.h"
John Reckd9d7f122018-05-03 14:40:56 -070038#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040039
Stan Iliev500a0c32016-10-26 10:30:09 -040040using namespace android::uirenderer::renderthread;
41
42namespace android {
43namespace uirenderer {
44namespace skiapipeline {
45
46SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
Derek Sollenberger5a5a6482018-09-19 13:52:13 -040047 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
48 thread.renderState().registerContextCallback(this);
49}
50
51SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
52 mRenderThread.renderState().removeContextCallback(this);
53}
Stan Iliev500a0c32016-10-26 10:30:09 -040054
55MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070056 bool wasSurfaceless = mEglManager.isCurrent(EGL_NO_SURFACE);
57
Derek Sollenbergerb8307722022-08-30 14:24:22 -040058 // In case the surface was destroyed (e.g. a previous trimMemory call) we
59 // need to recreate it here.
Nader Jawada3521852023-01-30 20:23:46 -080060 if (mHardwareBuffer) {
61 mRenderThread.requireGlContext();
62 } else if (!isSurfaceReady() && mNativeWindow) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -040063 setSurface(mNativeWindow.get(), mSwapBehavior);
64 }
65
Stan Iliev500a0c32016-10-26 10:30:09 -040066 EGLint error = 0;
67 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
68 return MakeCurrentResult::AlreadyCurrent;
69 }
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070070
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070071 EGLint majorVersion = 0;
72 eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &majorVersion);
73
74 // Make sure read/draw buffer state of default framebuffer is GL_BACK for ES 3.X. Vendor implementations
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070075 // disagree on the draw/read buffer state if the default framebuffer transitions from a surface
76 // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic.
77 // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534.
78 // The discussion was not resolved with a clear consensus
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070079 if (error == 0 && (majorVersion > 2) && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070080 GLint curReadFB = 0;
81 GLint curDrawFB = 0;
82 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB);
83 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB);
84
85 GLint buffer = GL_NONE;
86 glBindFramebuffer(GL_FRAMEBUFFER, 0);
87 glGetIntegerv(GL_DRAW_BUFFER0, &buffer);
88 if (buffer == GL_NONE) {
89 const GLenum drawBuffer = GL_BACK;
90 glDrawBuffers(1, &drawBuffer);
91 }
92
93 glGetIntegerv(GL_READ_BUFFER, &buffer);
94 if (buffer == GL_NONE) {
95 glReadBuffer(GL_BACK);
96 }
97
98 glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB);
99 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB);
100
101 GL_CHECKPOINT(LOW);
102 }
103
Stan Iliev500a0c32016-10-26 10:30:09 -0400104 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
105}
106
107Frame SkiaOpenGLPipeline::getFrame() {
Nader Jawad24617222023-03-30 12:29:01 -0700108 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
109 "drawRenderNode called on a context with no surface!");
110 return mEglManager.beginFrame(mEglSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400111}
112
Alec Mouri3afb3972022-05-27 22:03:11 +0000113IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
114 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
115 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
116 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
Nader Jawada3521852023-01-30 20:23:46 -0800117 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
Jiang Tian8e6a8462024-01-09 11:52:05 +0800118 const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) {
Nader Jawada3521852023-01-30 20:23:46 -0800119 if (!isCapturingSkp() && !mHardwareBuffer) {
John Reck76005182021-06-09 22:43:05 -0400120 mEglManager.damageFrame(frame, dirty);
121 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400122
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700123 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -0400124 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -0400125 GrGLFramebufferInfo fboInfo;
126 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700127 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -0400128 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700129 } else if (colorType == kN32_SkColorType) {
130 // Note: The default preference of pixel format is RGBA_8888, when other
131 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -0400132 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -0700133 } else if (colorType == kRGBA_1010102_SkColorType) {
134 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500135 } else if (colorType == kAlpha_8_SkColorType) {
136 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700137 } else {
138 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -0400139 }
Greg Danielac2d2322017-07-12 11:30:15 -0400140
Greg Danielc9da8e82018-03-21 10:50:24 -0400141 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400142
John Reckb026f352023-04-05 13:19:15 -0400143 SkSurfaceProps props(mColorMode == ColorMode::Default ? 0 : SkSurfaceProps::kAlwaysDither_Flag,
144 kUnknown_SkPixelGeometry);
Stan Iliev500a0c32016-10-26 10:30:09 -0400145
146 SkASSERT(mRenderThread.getGrContext() != nullptr);
Nader Jawada3521852023-01-30 20:23:46 -0800147 sk_sp<SkSurface> surface;
148 SkMatrix preTransform;
149 if (mHardwareBuffer) {
150 surface = getBufferSkSurface(bufferParams);
151 preTransform = bufferParams.getTransform();
152 } else {
153 surface = SkSurface::MakeFromBackendRenderTarget(mRenderThread.getGrContext(), backendRT,
154 getSurfaceOrigin(), colorType,
155 mSurfaceColorSpace, &props);
156 preTransform = SkMatrix::I();
157 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400158
Nader Jawada3521852023-01-30 20:23:46 -0800159 SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y);
160 LightGeometry localGeometry = lightGeometry;
161 localGeometry.center.x = lightCenter.fX;
162 localGeometry.center.y = lightCenter.fY;
163 LightingInfo::updateLighting(localGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500164 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
Nader Jawada3521852023-01-30 20:23:46 -0800165 preTransform);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400166
167 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700168 if (CC_UNLIKELY(Properties::showDirtyRegions ||
169 ProfileType::None != Properties::getProfileType())) {
Jiang Tian8e6a8462024-01-09 11:52:05 +0800170 std::scoped_lock lock(profilerLock);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400171 SkCanvas* profileCanvas = surface->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400172 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400173 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400174 }
175
Greg Danielbe2803a2021-02-19 18:32:16 -0500176 {
177 ATRACE_NAME("flush commands");
178 surface->flushAndSubmit();
179 }
180 layerUpdateQueue->clear();
181
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500182 // Log memory statistics
183 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
184 dumpResourceCacheUsage();
185 }
186
Alec Mouri3afb3972022-05-27 22:03:11 +0000187 return {true, IRenderPipeline::DrawResult::kUnknownTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400188}
189
John Reck1bcacfd2017-11-03 10:12:19 -0700190bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
191 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400192 GL_CHECKPOINT(LOW);
193
194 // Even if we decided to cancel the frame, from the perspective of jank
195 // metrics the frame was swapped at this point
196 currentFrameInfo->markSwapBuffers();
197
Nader Jawada3521852023-01-30 20:23:46 -0800198 if (mHardwareBuffer) {
199 return false;
200 }
201
Stan Iliev500a0c32016-10-26 10:30:09 -0400202 *requireSwap = drew || mEglManager.damageRequiresSwap();
203
204 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
205 return false;
206 }
207
208 return *requireSwap;
209}
210
Stan Iliev500a0c32016-10-26 10:30:09 -0400211DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700212 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000213 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400214}
215
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400216void SkiaOpenGLPipeline::onContextDestroyed() {
217 if (mEglSurface != EGL_NO_SURFACE) {
218 mEglManager.destroySurface(mEglSurface);
219 mEglSurface = EGL_NO_SURFACE;
220 }
221}
222
Stan Iliev500a0c32016-10-26 10:30:09 -0400223void SkiaOpenGLPipeline::onStop() {
224 if (mEglManager.isCurrent(mEglSurface)) {
225 mEglManager.makeCurrent(EGL_NO_SURFACE);
226 }
227}
228
John Reck8ddbc592020-05-07 16:11:18 -0700229bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400230 mNativeWindow = surface;
231 mSwapBehavior = swapBehavior;
232
Stan Iliev500a0c32016-10-26 10:30:09 -0400233 if (mEglSurface != EGL_NO_SURFACE) {
234 mEglManager.destroySurface(mEglSurface);
235 mEglSurface = EGL_NO_SURFACE;
236 }
237
238 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700239 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500240 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800241 if (!newSurface) {
242 return false;
243 }
244 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400245 }
246
247 if (mEglSurface != EGL_NO_SURFACE) {
248 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400249 const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
250 ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior.");
Stan Iliev500a0c32016-10-26 10:30:09 -0400251 return true;
252 }
253
254 return false;
255}
256
Nader Jawada3521852023-01-30 20:23:46 -0800257[[nodiscard]] android::base::unique_fd SkiaOpenGLPipeline::flush() {
258 int fence = -1;
259 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
260 mEglManager.createReleaseFence(true, &sync, &fence);
261 // If a sync object is returned here then the device does not support native
262 // fences, we block on the returned sync and return -1 as a file descriptor
263 if (sync != EGL_NO_SYNC_KHR) {
264 EGLDisplay display = mEglManager.eglDisplay();
265 EGLint result = eglClientWaitSyncKHR(display, sync, 0, 1000000000);
266 if (result == EGL_FALSE) {
267 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
268 eglGetError());
269 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
270 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
271 }
272 eglDestroySyncKHR(display, sync);
273 }
274 return android::base::unique_fd(fence);
275}
276
Stan Iliev500a0c32016-10-26 10:30:09 -0400277bool SkiaOpenGLPipeline::isSurfaceReady() {
278 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
279}
280
281bool SkiaOpenGLPipeline::isContextReady() {
282 return CC_LIKELY(mEglManager.hasEglContext());
283}
284
285void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
286 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
287 if (thread.eglManager().hasEglContext()) {
288 mode = DrawGlInfo::kModeProcess;
289 }
290
291 (*functor)(mode, nullptr);
292
293 // If there's no context we don't need to reset as there's no gl state to save/restore
294 if (mode != DrawGlInfo::kModeProcessNoContext) {
295 thread.getGrContext()->resetContext();
296 }
297}
298
Stan Iliev500a0c32016-10-26 10:30:09 -0400299} /* namespace skiapipeline */
300} /* namespace uirenderer */
301} /* namespace android */