blob: 62e42b8e1863cbcd5f6088a7c0f36bf8141d87b2 [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() {
56 // TODO: Figure out why this workaround is needed, see b/13913604
57 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
58 EGLint error = 0;
59 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
60 return MakeCurrentResult::AlreadyCurrent;
61 }
62 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
63}
64
65Frame SkiaOpenGLPipeline::getFrame() {
66 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -070067 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -040068 return mEglManager.beginFrame(mEglSurface);
69}
70
Alec Mouri3afb3972022-05-27 22:03:11 +000071IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
72 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
73 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
74 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
75 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler) {
John Reck76005182021-06-09 22:43:05 -040076 if (!isCapturingSkp()) {
77 mEglManager.damageFrame(frame, dirty);
78 }
Stan Iliev500a0c32016-10-26 10:30:09 -040079
Peiyong Lin1f6aa122018-09-10 16:28:08 -070080 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -040081 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040082 GrGLFramebufferInfo fboInfo;
83 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070084 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -040085 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070086 } else if (colorType == kN32_SkColorType) {
87 // Note: The default preference of pixel format is RGBA_8888, when other
88 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -040089 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -070090 } else if (colorType == kRGBA_1010102_SkColorType) {
91 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -050092 } else if (colorType == kAlpha_8_SkColorType) {
93 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070094 } else {
95 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -040096 }
Greg Danielac2d2322017-07-12 11:30:15 -040097
Greg Danielc9da8e82018-03-21 10:50:24 -040098 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040099
100 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
101
102 SkASSERT(mRenderThread.getGrContext() != nullptr);
103 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Derek Sollenberger25833d22019-01-14 13:55:55 -0500104 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400105 mSurfaceColorSpace, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400106
Fedor Kudasov90df0562019-06-19 11:41:34 +0100107 LightingInfo::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500108 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
John Reck0fa0cbc2019-04-05 16:57:46 -0700109 SkMatrix::I());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400110
111 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700112 if (CC_UNLIKELY(Properties::showDirtyRegions ||
113 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400114 SkCanvas* profileCanvas = surface->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400115 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400116 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400117 }
118
Greg Danielbe2803a2021-02-19 18:32:16 -0500119 {
120 ATRACE_NAME("flush commands");
121 surface->flushAndSubmit();
122 }
123 layerUpdateQueue->clear();
124
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500125 // Log memory statistics
126 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
127 dumpResourceCacheUsage();
128 }
129
Alec Mouri3afb3972022-05-27 22:03:11 +0000130 return {true, IRenderPipeline::DrawResult::kUnknownTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400131}
132
John Reck1bcacfd2017-11-03 10:12:19 -0700133bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
134 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400135 GL_CHECKPOINT(LOW);
136
137 // Even if we decided to cancel the frame, from the perspective of jank
138 // metrics the frame was swapped at this point
139 currentFrameInfo->markSwapBuffers();
140
141 *requireSwap = drew || mEglManager.damageRequiresSwap();
142
143 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
144 return false;
145 }
146
147 return *requireSwap;
148}
149
Stan Iliev500a0c32016-10-26 10:30:09 -0400150DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700151 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000152 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400153}
154
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400155void SkiaOpenGLPipeline::onContextDestroyed() {
156 if (mEglSurface != EGL_NO_SURFACE) {
157 mEglManager.destroySurface(mEglSurface);
158 mEglSurface = EGL_NO_SURFACE;
159 }
160}
161
Stan Iliev500a0c32016-10-26 10:30:09 -0400162void SkiaOpenGLPipeline::onStop() {
163 if (mEglManager.isCurrent(mEglSurface)) {
164 mEglManager.makeCurrent(EGL_NO_SURFACE);
165 }
166}
167
John Reck8ddbc592020-05-07 16:11:18 -0700168bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400169 if (mEglSurface != EGL_NO_SURFACE) {
170 mEglManager.destroySurface(mEglSurface);
171 mEglSurface = EGL_NO_SURFACE;
172 }
173
174 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700175 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500176 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800177 if (!newSurface) {
178 return false;
179 }
180 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400181 }
182
183 if (mEglSurface != EGL_NO_SURFACE) {
184 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
185 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
186 return true;
187 }
188
189 return false;
190}
191
192bool SkiaOpenGLPipeline::isSurfaceReady() {
193 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
194}
195
196bool SkiaOpenGLPipeline::isContextReady() {
197 return CC_LIKELY(mEglManager.hasEglContext());
198}
199
200void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
201 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
202 if (thread.eglManager().hasEglContext()) {
203 mode = DrawGlInfo::kModeProcess;
204 }
205
206 (*functor)(mode, nullptr);
207
208 // If there's no context we don't need to reset as there's no gl state to save/restore
209 if (mode != DrawGlInfo::kModeProcessNoContext) {
210 thread.getGrContext()->resetContext();
211 }
212}
213
Stan Iliev500a0c32016-10-26 10:30:09 -0400214} /* namespace skiapipeline */
215} /* namespace uirenderer */
216} /* namespace android */