blob: 23b3074435cfdd36ccb4d243c02bc4d663329aec [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
Kevin Lubick6817fc42023-05-12 19:27:20 +000019#include <include/gpu/ganesh/SkSurfaceGanesh.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000020#include <GrBackendSurface.h>
21#include <SkBlendMode.h>
22#include <SkImageInfo.h>
23#include <cutils/properties.h>
rnleece9762b2021-05-21 15:40:53 -070024#include <gui/TraceUtils.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000025#include <strings.h>
26
Stan Iliev500a0c32016-10-26 10:30:09 -040027#include "DeferredLayerUpdater.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000028#include "FrameInfo.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050029#include "LayerDrawable.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010030#include "LightingInfo.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040031#include "SkiaPipeline.h"
32#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070033#include "hwui/Bitmap.h"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040034#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070035#include "renderstate/RenderState.h"
36#include "renderthread/EglManager.h"
37#include "renderthread/Frame.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000038#include "renderthread/IRenderPipeline.h"
John Reckd9d7f122018-05-03 14:40:56 -070039#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040040
Stan Iliev500a0c32016-10-26 10:30:09 -040041using namespace android::uirenderer::renderthread;
42
43namespace android {
44namespace uirenderer {
45namespace skiapipeline {
46
47SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
Derek Sollenberger5a5a6482018-09-19 13:52:13 -040048 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
49 thread.renderState().registerContextCallback(this);
50}
51
52SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
53 mRenderThread.renderState().removeContextCallback(this);
54}
Stan Iliev500a0c32016-10-26 10:30:09 -040055
56MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070057 bool wasSurfaceless = mEglManager.isCurrent(EGL_NO_SURFACE);
58
Derek Sollenbergerb8307722022-08-30 14:24:22 -040059 // In case the surface was destroyed (e.g. a previous trimMemory call) we
60 // need to recreate it here.
Nader Jawada3521852023-01-30 20:23:46 -080061 if (mHardwareBuffer) {
62 mRenderThread.requireGlContext();
63 } else if (!isSurfaceReady() && mNativeWindow) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -040064 setSurface(mNativeWindow.get(), mSwapBehavior);
65 }
66
Stan Iliev500a0c32016-10-26 10:30:09 -040067 EGLint error = 0;
68 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
69 return MakeCurrentResult::AlreadyCurrent;
70 }
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070071
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070072 EGLint majorVersion = 0;
73 eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &majorVersion);
74
75 // 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 -070076 // disagree on the draw/read buffer state if the default framebuffer transitions from a surface
77 // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic.
78 // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534.
79 // The discussion was not resolved with a clear consensus
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070080 if (error == 0 && (majorVersion > 2) && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070081 GLint curReadFB = 0;
82 GLint curDrawFB = 0;
83 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB);
84 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB);
85
86 GLint buffer = GL_NONE;
87 glBindFramebuffer(GL_FRAMEBUFFER, 0);
88 glGetIntegerv(GL_DRAW_BUFFER0, &buffer);
89 if (buffer == GL_NONE) {
90 const GLenum drawBuffer = GL_BACK;
91 glDrawBuffers(1, &drawBuffer);
92 }
93
94 glGetIntegerv(GL_READ_BUFFER, &buffer);
95 if (buffer == GL_NONE) {
96 glReadBuffer(GL_BACK);
97 }
98
99 glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB);
100 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB);
101
102 GL_CHECKPOINT(LOW);
103 }
104
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
106}
107
108Frame SkiaOpenGLPipeline::getFrame() {
Nader Jawad24617222023-03-30 12:29:01 -0700109 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
110 "drawRenderNode called on a context with no surface!");
111 return mEglManager.beginFrame(mEglSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400112}
113
Alec Mouri3afb3972022-05-27 22:03:11 +0000114IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
115 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
116 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
117 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
Nader Jawada3521852023-01-30 20:23:46 -0800118 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
119 const HardwareBufferRenderParams& bufferParams) {
120 if (!isCapturingSkp() && !mHardwareBuffer) {
John Reck76005182021-06-09 22:43:05 -0400121 mEglManager.damageFrame(frame, dirty);
122 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400123
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700124 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -0400125 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -0400126 GrGLFramebufferInfo fboInfo;
127 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700128 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -0400129 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700130 } else if (colorType == kN32_SkColorType) {
131 // Note: The default preference of pixel format is RGBA_8888, when other
132 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -0400133 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -0700134 } else if (colorType == kRGBA_1010102_SkColorType) {
135 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500136 } else if (colorType == kAlpha_8_SkColorType) {
137 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700138 } else {
139 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -0400140 }
Greg Danielac2d2322017-07-12 11:30:15 -0400141
Greg Danielc9da8e82018-03-21 10:50:24 -0400142 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400143
John Reckb026f352023-04-05 13:19:15 -0400144 SkSurfaceProps props(mColorMode == ColorMode::Default ? 0 : SkSurfaceProps::kAlwaysDither_Flag,
145 kUnknown_SkPixelGeometry);
Stan Iliev500a0c32016-10-26 10:30:09 -0400146
147 SkASSERT(mRenderThread.getGrContext() != nullptr);
Nader Jawada3521852023-01-30 20:23:46 -0800148 sk_sp<SkSurface> surface;
149 SkMatrix preTransform;
150 if (mHardwareBuffer) {
151 surface = getBufferSkSurface(bufferParams);
152 preTransform = bufferParams.getTransform();
153 } else {
Kevin Lubick6817fc42023-05-12 19:27:20 +0000154 surface = SkSurfaces::WrapBackendRenderTarget(mRenderThread.getGrContext(), backendRT,
155 getSurfaceOrigin(), colorType,
156 mSurfaceColorSpace, &props);
Nader Jawada3521852023-01-30 20:23:46 -0800157 preTransform = SkMatrix::I();
158 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400159
Nader Jawada3521852023-01-30 20:23:46 -0800160 SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y);
161 LightGeometry localGeometry = lightGeometry;
162 localGeometry.center.x = lightCenter.fX;
163 localGeometry.center.y = lightCenter.fY;
164 LightingInfo::updateLighting(localGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500165 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
Nader Jawada3521852023-01-30 20:23:46 -0800166 preTransform);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400167
168 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700169 if (CC_UNLIKELY(Properties::showDirtyRegions ||
170 ProfileType::None != Properties::getProfileType())) {
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");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000178 skgpu::ganesh::FlushAndSubmit(surface);
Greg Danielbe2803a2021-02-19 18:32:16 -0500179 }
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);
John Reck3c260882023-05-30 17:39:21 -0400249 mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
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 */