blob: b5c1b490182fc1b9e28e572097dca70e740d9e08 [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 Sollenberger289380f2022-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.
60 if (!isSurfaceReady() && mNativeWindow) {
61 setSurface(mNativeWindow.get(), mSwapBehavior);
62 }
63
Stan Iliev500a0c32016-10-26 10:30:09 -040064 EGLint error = 0;
65 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
66 return MakeCurrentResult::AlreadyCurrent;
67 }
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070068
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070069 EGLint majorVersion = 0;
70 eglQueryContext(eglGetCurrentDisplay(), eglGetCurrentContext(), EGL_CONTEXT_CLIENT_VERSION, &majorVersion);
71
72 // 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 -070073 // disagree on the draw/read buffer state if the default framebuffer transitions from a surface
74 // to EGL_NO_SURFACE and vice-versa. There was a related discussion within Khronos on this topic.
75 // See https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13534.
76 // The discussion was not resolved with a clear consensus
Shih-hsin Lid82c91d2023-04-21 12:13:47 -070077 if (error == 0 && (majorVersion > 2) && wasSurfaceless && mEglSurface != EGL_NO_SURFACE) {
Shih-Hsin Li4a012f52021-09-21 18:11:23 -070078 GLint curReadFB = 0;
79 GLint curDrawFB = 0;
80 glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &curReadFB);
81 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curDrawFB);
82
83 GLint buffer = GL_NONE;
84 glBindFramebuffer(GL_FRAMEBUFFER, 0);
85 glGetIntegerv(GL_DRAW_BUFFER0, &buffer);
86 if (buffer == GL_NONE) {
87 const GLenum drawBuffer = GL_BACK;
88 glDrawBuffers(1, &drawBuffer);
89 }
90
91 glGetIntegerv(GL_READ_BUFFER, &buffer);
92 if (buffer == GL_NONE) {
93 glReadBuffer(GL_BACK);
94 }
95
96 glBindFramebuffer(GL_READ_FRAMEBUFFER, curReadFB);
97 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, curDrawFB);
98
99 GL_CHECKPOINT(LOW);
100 }
101
Stan Iliev500a0c32016-10-26 10:30:09 -0400102 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
103}
104
105Frame SkiaOpenGLPipeline::getFrame() {
106 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -0700107 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -0400108 return mEglManager.beginFrame(mEglSurface);
109}
110
Alec Mouri3afb3972022-05-27 22:03:11 +0000111IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
112 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
113 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
114 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
115 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler) {
John Reck76005182021-06-09 22:43:05 -0400116 if (!isCapturingSkp()) {
117 mEglManager.damageFrame(frame, dirty);
118 }
Stan Iliev500a0c32016-10-26 10:30:09 -0400119
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700120 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -0400121 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -0400122 GrGLFramebufferInfo fboInfo;
123 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700124 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -0400125 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700126 } else if (colorType == kN32_SkColorType) {
127 // Note: The default preference of pixel format is RGBA_8888, when other
128 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -0400129 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -0700130 } else if (colorType == kRGBA_1010102_SkColorType) {
131 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500132 } else if (colorType == kAlpha_8_SkColorType) {
133 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700134 } else {
135 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -0400136 }
Greg Danielac2d2322017-07-12 11:30:15 -0400137
Greg Danielc9da8e82018-03-21 10:50:24 -0400138 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400139
140 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
141
142 SkASSERT(mRenderThread.getGrContext() != nullptr);
143 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Derek Sollenberger25833d22019-01-14 13:55:55 -0500144 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400145 mSurfaceColorSpace, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400146
Fedor Kudasov90df0562019-06-19 11:41:34 +0100147 LightingInfo::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500148 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
John Reck0fa0cbc2019-04-05 16:57:46 -0700149 SkMatrix::I());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400150
151 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700152 if (CC_UNLIKELY(Properties::showDirtyRegions ||
153 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400154 SkCanvas* profileCanvas = surface->getCanvas();
155 SkiaProfileRenderer profileRenderer(profileCanvas);
156 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400157 }
158
Greg Danielbe2803a2021-02-19 18:32:16 -0500159 {
160 ATRACE_NAME("flush commands");
161 surface->flushAndSubmit();
162 }
163 layerUpdateQueue->clear();
164
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500165 // Log memory statistics
166 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
167 dumpResourceCacheUsage();
168 }
169
Alec Mouri3afb3972022-05-27 22:03:11 +0000170 return {true, IRenderPipeline::DrawResult::kUnknownTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400171}
172
John Reck1bcacfd2017-11-03 10:12:19 -0700173bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
174 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400175 GL_CHECKPOINT(LOW);
176
177 // Even if we decided to cancel the frame, from the perspective of jank
178 // metrics the frame was swapped at this point
179 currentFrameInfo->markSwapBuffers();
180
181 *requireSwap = drew || mEglManager.damageRequiresSwap();
182
183 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
184 return false;
185 }
186
187 return *requireSwap;
188}
189
Stan Iliev500a0c32016-10-26 10:30:09 -0400190DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700191 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000192 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400193}
194
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400195void SkiaOpenGLPipeline::onContextDestroyed() {
196 if (mEglSurface != EGL_NO_SURFACE) {
197 mEglManager.destroySurface(mEglSurface);
198 mEglSurface = EGL_NO_SURFACE;
199 }
200}
201
Stan Iliev500a0c32016-10-26 10:30:09 -0400202void SkiaOpenGLPipeline::onStop() {
203 if (mEglManager.isCurrent(mEglSurface)) {
204 mEglManager.makeCurrent(EGL_NO_SURFACE);
205 }
206}
207
John Reck8ddbc592020-05-07 16:11:18 -0700208bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger289380f2022-08-30 14:24:22 -0400209 mNativeWindow = surface;
210 mSwapBehavior = swapBehavior;
211
Stan Iliev500a0c32016-10-26 10:30:09 -0400212 if (mEglSurface != EGL_NO_SURFACE) {
213 mEglManager.destroySurface(mEglSurface);
214 mEglSurface = EGL_NO_SURFACE;
215 }
216
217 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700218 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500219 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800220 if (!newSurface) {
221 return false;
222 }
223 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400224 }
225
226 if (mEglSurface != EGL_NO_SURFACE) {
227 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
Derek Sollenberger289380f2022-08-30 14:24:22 -0400228 const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
229 ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior.");
Stan Iliev500a0c32016-10-26 10:30:09 -0400230 return true;
231 }
232
233 return false;
234}
235
236bool SkiaOpenGLPipeline::isSurfaceReady() {
237 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
238}
239
240bool SkiaOpenGLPipeline::isContextReady() {
241 return CC_LIKELY(mEglManager.hasEglContext());
242}
243
244void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
245 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
246 if (thread.eglManager().hasEglContext()) {
247 mode = DrawGlInfo::kModeProcess;
248 }
249
250 (*functor)(mode, nullptr);
251
252 // If there's no context we don't need to reset as there's no gl state to save/restore
253 if (mode != DrawGlInfo::kModeProcessNoContext) {
254 thread.getGrContext()->resetContext();
255 }
256}
257
Stan Iliev500a0c32016-10-26 10:30:09 -0400258} /* namespace skiapipeline */
259} /* namespace uirenderer */
260} /* namespace android */