blob: 530c654797a98a2bfe2847bc2d0a30731fa2c384 [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>
Kevin Lubick867a79c2023-08-14 14:19:11 +000020#include <include/gpu/gl/GrGLTypes.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000021#include <GrBackendSurface.h>
22#include <SkBlendMode.h>
23#include <SkImageInfo.h>
24#include <cutils/properties.h>
rnleece9762b2021-05-21 15:40:53 -070025#include <gui/TraceUtils.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000026#include <strings.h>
27
Stan Iliev500a0c32016-10-26 10:30:09 -040028#include "DeferredLayerUpdater.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000029#include "FrameInfo.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050030#include "LayerDrawable.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010031#include "LightingInfo.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040032#include "SkiaPipeline.h"
33#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070034#include "hwui/Bitmap.h"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040035#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070036#include "renderstate/RenderState.h"
37#include "renderthread/EglManager.h"
38#include "renderthread/Frame.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000039#include "renderthread/IRenderPipeline.h"
John Reckd9d7f122018-05-03 14:40:56 -070040#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040041
Stan Iliev500a0c32016-10-26 10:30:09 -040042using namespace android::uirenderer::renderthread;
43
44namespace android {
45namespace uirenderer {
46namespace skiapipeline {
47
48SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
Derek Sollenberger5a5a6482018-09-19 13:52:13 -040049 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
50 thread.renderState().registerContextCallback(this);
51}
52
53SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
54 mRenderThread.renderState().removeContextCallback(this);
55}
Stan Iliev500a0c32016-10-26 10:30:09 -040056
57MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070058 bool wasSurfaceless = mEglManager.isCurrent(EGL_NO_SURFACE);
59
Derek Sollenbergerb8307722022-08-30 14:24:22 -040060 // In case the surface was destroyed (e.g. a previous trimMemory call) we
61 // need to recreate it here.
Nader Jawada3521852023-01-30 20:23:46 -080062 if (mHardwareBuffer) {
63 mRenderThread.requireGlContext();
64 } else if (!isSurfaceReady() && mNativeWindow) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -040065 setSurface(mNativeWindow.get(), mSwapBehavior);
66 }
67
Stan Iliev500a0c32016-10-26 10:30:09 -040068 EGLint error = 0;
69 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
70 return MakeCurrentResult::AlreadyCurrent;
71 }
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070072
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070073 EGLint majorVersion = 0;
74 eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &majorVersion);
75
76 // 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 -070077 // disagree on the draw/read buffer state if the default framebuffer transitions from a surface
78 // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic.
79 // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534.
80 // The discussion was not resolved with a clear consensus
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070081 if (error == 0 && (majorVersion > 2) && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070082 GLint curReadFB = 0;
83 GLint curDrawFB = 0;
84 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB);
85 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB);
86
87 GLint buffer = GL_NONE;
88 glBindFramebuffer(GL_FRAMEBUFFER, 0);
89 glGetIntegerv(GL_DRAW_BUFFER0, &buffer);
90 if (buffer == GL_NONE) {
91 const GLenum drawBuffer = GL_BACK;
92 glDrawBuffers(1, &drawBuffer);
93 }
94
95 glGetIntegerv(GL_READ_BUFFER, &buffer);
96 if (buffer == GL_NONE) {
97 glReadBuffer(GL_BACK);
98 }
99
100 glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB);
101 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB);
102
103 GL_CHECKPOINT(LOW);
104 }
105
Stan Iliev500a0c32016-10-26 10:30:09 -0400106 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
107}
108
109Frame SkiaOpenGLPipeline::getFrame() {
Nader Jawad24617222023-03-30 12:29:01 -0700110 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
111 "drawRenderNode called on a context with no surface!");
112 return mEglManager.beginFrame(mEglSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400113}
114
Alec Mouri3afb3972022-05-27 22:03:11 +0000115IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
116 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
117 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
118 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
Nader Jawada3521852023-01-30 20:23:46 -0800119 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
120 const HardwareBufferRenderParams& bufferParams) {
121 if (!isCapturingSkp() && !mHardwareBuffer) {
John Reck76005182021-06-09 22:43:05 -0400122 mEglManager.damageFrame(frame, dirty);
123 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400124
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700125 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -0400126 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -0400127 GrGLFramebufferInfo fboInfo;
128 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700129 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -0400130 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700131 } else if (colorType == kN32_SkColorType) {
132 // Note: The default preference of pixel format is RGBA_8888, when other
133 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -0400134 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -0700135 } else if (colorType == kRGBA_1010102_SkColorType) {
136 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500137 } else if (colorType == kAlpha_8_SkColorType) {
138 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700139 } else {
140 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -0400141 }
Greg Danielac2d2322017-07-12 11:30:15 -0400142
Greg Danielc9da8e82018-03-21 10:50:24 -0400143 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400144
John Reckb026f352023-04-05 13:19:15 -0400145 SkSurfaceProps props(mColorMode == ColorMode::Default ? 0 : SkSurfaceProps::kAlwaysDither_Flag,
146 kUnknown_SkPixelGeometry);
Stan Iliev500a0c32016-10-26 10:30:09 -0400147
148 SkASSERT(mRenderThread.getGrContext() != nullptr);
Nader Jawada3521852023-01-30 20:23:46 -0800149 sk_sp<SkSurface> surface;
150 SkMatrix preTransform;
151 if (mHardwareBuffer) {
152 surface = getBufferSkSurface(bufferParams);
153 preTransform = bufferParams.getTransform();
154 } else {
Kevin Lubick6817fc42023-05-12 19:27:20 +0000155 surface = SkSurfaces::WrapBackendRenderTarget(mRenderThread.getGrContext(), backendRT,
156 getSurfaceOrigin(), colorType,
157 mSurfaceColorSpace, &props);
Nader Jawada3521852023-01-30 20:23:46 -0800158 preTransform = SkMatrix::I();
159 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400160
Nader Jawada3521852023-01-30 20:23:46 -0800161 SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y);
162 LightGeometry localGeometry = lightGeometry;
163 localGeometry.center.x = lightCenter.fX;
164 localGeometry.center.y = lightCenter.fY;
165 LightingInfo::updateLighting(localGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500166 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
Nader Jawada3521852023-01-30 20:23:46 -0800167 preTransform);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400168
169 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700170 if (CC_UNLIKELY(Properties::showDirtyRegions ||
171 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400172 SkCanvas* profileCanvas = surface->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400173 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400174 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400175 }
176
Greg Danielbe2803a2021-02-19 18:32:16 -0500177 {
178 ATRACE_NAME("flush commands");
Kevin Lubickaa592d02023-05-30 15:07:06 +0000179 skgpu::ganesh::FlushAndSubmit(surface);
Greg Danielbe2803a2021-02-19 18:32:16 -0500180 }
181 layerUpdateQueue->clear();
182
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500183 // Log memory statistics
184 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
185 dumpResourceCacheUsage();
186 }
187
Alec Mouri3afb3972022-05-27 22:03:11 +0000188 return {true, IRenderPipeline::DrawResult::kUnknownTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400189}
190
John Reck1bcacfd2017-11-03 10:12:19 -0700191bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
192 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400193 GL_CHECKPOINT(LOW);
194
195 // Even if we decided to cancel the frame, from the perspective of jank
196 // metrics the frame was swapped at this point
197 currentFrameInfo->markSwapBuffers();
198
Nader Jawada3521852023-01-30 20:23:46 -0800199 if (mHardwareBuffer) {
200 return false;
201 }
202
Stan Iliev500a0c32016-10-26 10:30:09 -0400203 *requireSwap = drew || mEglManager.damageRequiresSwap();
204
205 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
206 return false;
207 }
208
209 return *requireSwap;
210}
211
Stan Iliev500a0c32016-10-26 10:30:09 -0400212DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700213 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000214 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400215}
216
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400217void SkiaOpenGLPipeline::onContextDestroyed() {
218 if (mEglSurface != EGL_NO_SURFACE) {
219 mEglManager.destroySurface(mEglSurface);
220 mEglSurface = EGL_NO_SURFACE;
221 }
222}
223
Stan Iliev500a0c32016-10-26 10:30:09 -0400224void SkiaOpenGLPipeline::onStop() {
225 if (mEglManager.isCurrent(mEglSurface)) {
226 mEglManager.makeCurrent(EGL_NO_SURFACE);
227 }
228}
229
John Reck8ddbc592020-05-07 16:11:18 -0700230bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400231 mNativeWindow = surface;
232 mSwapBehavior = swapBehavior;
233
Stan Iliev500a0c32016-10-26 10:30:09 -0400234 if (mEglSurface != EGL_NO_SURFACE) {
235 mEglManager.destroySurface(mEglSurface);
236 mEglSurface = EGL_NO_SURFACE;
237 }
238
239 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700240 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500241 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800242 if (!newSurface) {
243 return false;
244 }
245 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400246 }
247
248 if (mEglSurface != EGL_NO_SURFACE) {
249 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
John Reck3c260882023-05-30 17:39:21 -0400250 mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
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 */