blob: 0768f457972bd6f0cf8a779fc3e57f5696a932d2 [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
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000017#include "pipeline/skia/SkiaOpenGLPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040018
Alec Mouri3afb3972022-05-27 22:03:11 +000019#include <SkBlendMode.h>
20#include <SkImageInfo.h>
21#include <cutils/properties.h>
rnleece9762b2021-05-21 15:40:53 -070022#include <gui/TraceUtils.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040023#include <include/gpu/ganesh/GrBackendSurface.h>
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000024#include <include/gpu/ganesh/SkSurfaceGanesh.h>
25#include <include/gpu/ganesh/gl/GrGLBackendSurface.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040026#include <include/gpu/ganesh/gl/GrGLTypes.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000027#include <strings.h>
28
Stan Iliev500a0c32016-10-26 10:30:09 -040029#include "DeferredLayerUpdater.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000030#include "FrameInfo.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010031#include "LightingInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070032#include "hwui/Bitmap.h"
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000033#include "pipeline/skia/LayerDrawable.h"
34#include "pipeline/skia/SkiaGpuPipeline.h"
35#include "pipeline/skia/SkiaProfileRenderer.h"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040036#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070037#include "renderstate/RenderState.h"
38#include "renderthread/EglManager.h"
39#include "renderthread/Frame.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000040#include "renderthread/IRenderPipeline.h"
John Reckd9d7f122018-05-03 14:40:56 -070041#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040042
Stan Iliev500a0c32016-10-26 10:30:09 -040043using namespace android::uirenderer::renderthread;
44
45namespace android {
46namespace uirenderer {
47namespace skiapipeline {
48
49SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
Jerome Gaillardbea67ce2024-03-28 14:21:16 +000050 : SkiaGpuPipeline(thread), mEglManager(thread.eglManager()) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -040051 thread.renderState().registerContextCallback(this);
52}
53
54SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
55 mRenderThread.renderState().removeContextCallback(this);
56}
Stan Iliev500a0c32016-10-26 10:30:09 -040057
58MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070059 bool wasSurfaceless = mEglManager.isCurrent(EGL_NO_SURFACE);
60
Derek Sollenbergerb8307722022-08-30 14:24:22 -040061 // In case the surface was destroyed (e.g. a previous trimMemory call) we
62 // need to recreate it here.
Nader Jawada3521852023-01-30 20:23:46 -080063 if (mHardwareBuffer) {
64 mRenderThread.requireGlContext();
65 } else if (!isSurfaceReady() && mNativeWindow) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -040066 setSurface(mNativeWindow.get(), mSwapBehavior);
67 }
68
Stan Iliev500a0c32016-10-26 10:30:09 -040069 EGLint error = 0;
70 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
71 return MakeCurrentResult::AlreadyCurrent;
72 }
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070073
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070074 EGLint majorVersion = 0;
75 eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &majorVersion);
76
77 // 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 -070078 // disagree on the draw/read buffer state if the default framebuffer transitions from a surface
79 // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic.
80 // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534.
81 // The discussion was not resolved with a clear consensus
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070082 if (error == 0 && (majorVersion > 2) && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070083 GLint curReadFB = 0;
84 GLint curDrawFB = 0;
85 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB);
86 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB);
87
88 GLint buffer = GL_NONE;
89 glBindFramebuffer(GL_FRAMEBUFFER, 0);
90 glGetIntegerv(GL_DRAW_BUFFER0, &buffer);
91 if (buffer == GL_NONE) {
92 const GLenum drawBuffer = GL_BACK;
93 glDrawBuffers(1, &drawBuffer);
94 }
95
96 glGetIntegerv(GL_READ_BUFFER, &buffer);
97 if (buffer == GL_NONE) {
98 glReadBuffer(GL_BACK);
99 }
100
101 glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB);
102 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB);
103
104 GL_CHECKPOINT(LOW);
105 }
106
Stan Iliev500a0c32016-10-26 10:30:09 -0400107 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
108}
109
110Frame SkiaOpenGLPipeline::getFrame() {
Nader Jawad24617222023-03-30 12:29:01 -0700111 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
112 "drawRenderNode called on a context with no surface!");
113 return mEglManager.beginFrame(mEglSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400114}
115
Alec Mouri3afb3972022-05-27 22:03:11 +0000116IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
117 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
118 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
119 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
Nader Jawada3521852023-01-30 20:23:46 -0800120 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
Jiang Tiane0edc3a2024-01-09 11:52:05 +0800121 const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) {
Nader Jawada3521852023-01-30 20:23:46 -0800122 if (!isCapturingSkp() && !mHardwareBuffer) {
John Reck76005182021-06-09 22:43:05 -0400123 mEglManager.damageFrame(frame, dirty);
124 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400125
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700126 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -0400127 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -0400128 GrGLFramebufferInfo fboInfo;
129 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700130 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -0400131 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700132 } else if (colorType == kN32_SkColorType) {
133 // Note: The default preference of pixel format is RGBA_8888, when other
134 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -0400135 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -0700136 } else if (colorType == kRGBA_1010102_SkColorType) {
137 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500138 } else if (colorType == kAlpha_8_SkColorType) {
139 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700140 } else {
141 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -0400142 }
Greg Danielac2d2322017-07-12 11:30:15 -0400143
Kevin Lubick27576942023-08-16 12:50:44 +0000144 auto backendRT = GrBackendRenderTargets::MakeGL(frame.width(), frame.height(), 0,
145 STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400146
John Reckb026f352023-04-05 13:19:15 -0400147 SkSurfaceProps props(mColorMode == ColorMode::Default ? 0 : SkSurfaceProps::kAlwaysDither_Flag,
148 kUnknown_SkPixelGeometry);
Stan Iliev500a0c32016-10-26 10:30:09 -0400149
150 SkASSERT(mRenderThread.getGrContext() != nullptr);
Nader Jawada3521852023-01-30 20:23:46 -0800151 sk_sp<SkSurface> surface;
152 SkMatrix preTransform;
153 if (mHardwareBuffer) {
154 surface = getBufferSkSurface(bufferParams);
155 preTransform = bufferParams.getTransform();
156 } else {
Kevin Lubick6817fc42023-05-12 19:27:20 +0000157 surface = SkSurfaces::WrapBackendRenderTarget(mRenderThread.getGrContext(), backendRT,
158 getSurfaceOrigin(), colorType,
159 mSurfaceColorSpace, &props);
Nader Jawada3521852023-01-30 20:23:46 -0800160 preTransform = SkMatrix::I();
161 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400162
Nader Jawada3521852023-01-30 20:23:46 -0800163 SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y);
164 LightGeometry localGeometry = lightGeometry;
165 localGeometry.center.x = lightCenter.fX;
166 localGeometry.center.y = lightCenter.fY;
167 LightingInfo::updateLighting(localGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500168 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
Nader Jawada3521852023-01-30 20:23:46 -0800169 preTransform);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400170
171 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700172 if (CC_UNLIKELY(Properties::showDirtyRegions ||
173 ProfileType::None != Properties::getProfileType())) {
Jiang Tiane0edc3a2024-01-09 11:52:05 +0800174 std::scoped_lock lock(profilerLock);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400175 SkCanvas* profileCanvas = surface->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400176 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400177 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400178 }
179
Greg Danielbe2803a2021-02-19 18:32:16 -0500180 {
181 ATRACE_NAME("flush commands");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000182 skgpu::ganesh::FlushAndSubmit(surface);
Greg Danielbe2803a2021-02-19 18:32:16 -0500183 }
184 layerUpdateQueue->clear();
185
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500186 // Log memory statistics
187 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
188 dumpResourceCacheUsage();
189 }
190
John Reck5d3fac12023-11-08 23:08:10 -0500191 return {true, IRenderPipeline::DrawResult::kUnknownTime, android::base::unique_fd{}};
Stan Iliev500a0c32016-10-26 10:30:09 -0400192}
193
John Reck5d3fac12023-11-08 23:08:10 -0500194bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, IRenderPipeline::DrawResult& drawResult,
195 const SkRect& screenDirty, FrameInfo* currentFrameInfo,
196 bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400197 GL_CHECKPOINT(LOW);
198
199 // Even if we decided to cancel the frame, from the perspective of jank
200 // metrics the frame was swapped at this point
201 currentFrameInfo->markSwapBuffers();
202
Nader Jawada3521852023-01-30 20:23:46 -0800203 if (mHardwareBuffer) {
204 return false;
205 }
206
John Reck5d3fac12023-11-08 23:08:10 -0500207 *requireSwap = drawResult.success || mEglManager.damageRequiresSwap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400208
209 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
210 return false;
211 }
212
213 return *requireSwap;
214}
215
Stan Iliev500a0c32016-10-26 10:30:09 -0400216DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700217 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000218 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400219}
220
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400221void SkiaOpenGLPipeline::onContextDestroyed() {
222 if (mEglSurface != EGL_NO_SURFACE) {
223 mEglManager.destroySurface(mEglSurface);
224 mEglSurface = EGL_NO_SURFACE;
225 }
226}
227
Stan Iliev500a0c32016-10-26 10:30:09 -0400228void SkiaOpenGLPipeline::onStop() {
229 if (mEglManager.isCurrent(mEglSurface)) {
230 mEglManager.makeCurrent(EGL_NO_SURFACE);
231 }
232}
233
John Reck8ddbc592020-05-07 16:11:18 -0700234bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400235 mNativeWindow = surface;
236 mSwapBehavior = swapBehavior;
237
Stan Iliev500a0c32016-10-26 10:30:09 -0400238 if (mEglSurface != EGL_NO_SURFACE) {
239 mEglManager.destroySurface(mEglSurface);
240 mEglSurface = EGL_NO_SURFACE;
241 }
242
243 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700244 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500245 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800246 if (!newSurface) {
247 return false;
248 }
249 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400250 }
251
252 if (mEglSurface != EGL_NO_SURFACE) {
253 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
John Reck3c260882023-05-30 17:39:21 -0400254 mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
Stan Iliev500a0c32016-10-26 10:30:09 -0400255 return true;
256 }
257
258 return false;
259}
260
Nader Jawada3521852023-01-30 20:23:46 -0800261[[nodiscard]] android::base::unique_fd SkiaOpenGLPipeline::flush() {
262 int fence = -1;
263 EGLSyncKHR sync = EGL_NO_SYNC_KHR;
264 mEglManager.createReleaseFence(true, &sync, &fence);
265 // If a sync object is returned here then the device does not support native
266 // fences, we block on the returned sync and return -1 as a file descriptor
267 if (sync != EGL_NO_SYNC_KHR) {
268 EGLDisplay display = mEglManager.eglDisplay();
269 EGLint result = eglClientWaitSyncKHR(display, sync, 0, 1000000000);
270 if (result == EGL_FALSE) {
271 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
272 eglGetError());
273 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
274 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
275 }
276 eglDestroySyncKHR(display, sync);
277 }
278 return android::base::unique_fd(fence);
279}
280
Stan Iliev500a0c32016-10-26 10:30:09 -0400281bool SkiaOpenGLPipeline::isSurfaceReady() {
282 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
283}
284
285bool SkiaOpenGLPipeline::isContextReady() {
286 return CC_LIKELY(mEglManager.hasEglContext());
287}
288
289void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
290 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
291 if (thread.eglManager().hasEglContext()) {
292 mode = DrawGlInfo::kModeProcess;
293 }
294
295 (*functor)(mode, nullptr);
296
297 // If there's no context we don't need to reset as there's no gl state to save/restore
298 if (mode != DrawGlInfo::kModeProcessNoContext) {
299 thread.getGrContext()->resetContext();
300 }
301}
302
Stan Iliev500a0c32016-10-26 10:30:09 -0400303} /* namespace skiapipeline */
304} /* namespace uirenderer */
305} /* namespace android */