blob: cf31173d266e888904ec50755b64e372a43f6810 [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,
118 const HardwareBufferRenderParams& bufferParams) {
119 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())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400170 SkCanvas* profileCanvas = surface->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400171 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400172 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400173 }
174
Greg Danielbe2803a2021-02-19 18:32:16 -0500175 {
176 ATRACE_NAME("flush commands");
177 surface->flushAndSubmit();
178 }
179 layerUpdateQueue->clear();
180
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500181 // Log memory statistics
182 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
183 dumpResourceCacheUsage();
184 }
185
Alec Mouri3afb3972022-05-27 22:03:11 +0000186 return {true, IRenderPipeline::DrawResult::kUnknownTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400187}
188
John Reck1bcacfd2017-11-03 10:12:19 -0700189bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
190 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400191 GL_CHECKPOINT(LOW);
192
193 // Even if we decided to cancel the frame, from the perspective of jank
194 // metrics the frame was swapped at this point
195 currentFrameInfo->markSwapBuffers();
196
Nader Jawada3521852023-01-30 20:23:46 -0800197 if (mHardwareBuffer) {
198 return false;
199 }
200
Stan Iliev500a0c32016-10-26 10:30:09 -0400201 *requireSwap = drew || mEglManager.damageRequiresSwap();
202
203 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
204 return false;
205 }
206
207 return *requireSwap;
208}
209
Stan Iliev500a0c32016-10-26 10:30:09 -0400210DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700211 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000212 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400213}
214
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400215void SkiaOpenGLPipeline::onContextDestroyed() {
216 if (mEglSurface != EGL_NO_SURFACE) {
217 mEglManager.destroySurface(mEglSurface);
218 mEglSurface = EGL_NO_SURFACE;
219 }
220}
221
Stan Iliev500a0c32016-10-26 10:30:09 -0400222void SkiaOpenGLPipeline::onStop() {
223 if (mEglManager.isCurrent(mEglSurface)) {
224 mEglManager.makeCurrent(EGL_NO_SURFACE);
225 }
226}
227
John Reck8ddbc592020-05-07 16:11:18 -0700228bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400229 mNativeWindow = surface;
230 mSwapBehavior = swapBehavior;
231
Stan Iliev500a0c32016-10-26 10:30:09 -0400232 if (mEglSurface != EGL_NO_SURFACE) {
233 mEglManager.destroySurface(mEglSurface);
234 mEglSurface = EGL_NO_SURFACE;
235 }
236
237 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700238 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500239 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800240 if (!newSurface) {
241 return false;
242 }
243 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400244 }
245
246 if (mEglSurface != EGL_NO_SURFACE) {
247 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400248 const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
249 ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior.");
Stan Iliev500a0c32016-10-26 10:30:09 -0400250 return true;
251 }
252
253 return false;
254}
255
Nader Jawada3521852023-01-30 20:23:46 -0800256[[nodiscard]] android::base::unique_fd SkiaOpenGLPipeline::flush() {
257 int fence = -1;
258 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
259 mEglManager.createReleaseFence(true, &sync, &fence);
260 // If a sync object is returned here then the device does not support native
261 // fences, we block on the returned sync and return -1 as a file descriptor
262 if (sync != EGL_NO_SYNC_KHR) {
263 EGLDisplay display = mEglManager.eglDisplay();
264 EGLint result = eglClientWaitSyncKHR(display, sync, 0, 1000000000);
265 if (result == EGL_FALSE) {
266 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
267 eglGetError());
268 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
269 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
270 }
271 eglDestroySyncKHR(display, sync);
272 }
273 return android::base::unique_fd(fence);
274}
275
Stan Iliev500a0c32016-10-26 10:30:09 -0400276bool SkiaOpenGLPipeline::isSurfaceReady() {
277 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
278}
279
280bool SkiaOpenGLPipeline::isContextReady() {
281 return CC_LIKELY(mEglManager.hasEglContext());
282}
283
284void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
285 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
286 if (thread.eglManager().hasEglContext()) {
287 mode = DrawGlInfo::kModeProcess;
288 }
289
290 (*functor)(mode, nullptr);
291
292 // If there's no context we don't need to reset as there's no gl state to save/restore
293 if (mode != DrawGlInfo::kModeProcessNoContext) {
294 thread.getGrContext()->resetContext();
295 }
296}
297
Stan Iliev500a0c32016-10-26 10:30:09 -0400298} /* namespace skiapipeline */
299} /* namespace uirenderer */
300} /* namespace android */