blob: 744739accb2c225cc39b0d2c456f0e79781f3796 [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
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040020#include "DeferredLayerUpdater.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050021#include "LayerDrawable.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010022#include "LightingInfo.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040023#include "SkiaPipeline.h"
24#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070025#include "hwui/Bitmap.h"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040026#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070027#include "renderstate/RenderState.h"
28#include "renderthread/EglManager.h"
29#include "renderthread/Frame.h"
John Reckd9d7f122018-05-03 14:40:56 -070030#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040031
John Reckbdc9f1b2018-09-14 15:22:35 -070032#include <GLES3/gl3.h>
33
Greg Danielac2d2322017-07-12 11:30:15 -040034#include <GrBackendSurface.h>
Derek Sollenberger23249912018-05-03 16:12:18 -040035#include <SkBlendMode.h>
John Reck437f6fb2018-05-07 08:14:14 -070036#include <SkImageInfo.h>
Greg Danielac2d2322017-07-12 11:30:15 -040037
Stan Iliev500a0c32016-10-26 10:30:09 -040038#include <cutils/properties.h>
39#include <strings.h>
40
41using 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() {
57 // TODO: Figure out why this workaround is needed, see b/13913604
58 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
59 EGLint error = 0;
60 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
61 return MakeCurrentResult::AlreadyCurrent;
62 }
63 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
64}
65
66Frame SkiaOpenGLPipeline::getFrame() {
67 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -070068 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -040069 return mEglManager.beginFrame(mEglSurface);
70}
71
John Reck1bcacfd2017-11-03 10:12:19 -070072bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070073 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070074 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070075 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070076 const std::vector<sp<RenderNode>>& renderNodes,
77 FrameInfoVisualizer* profiler) {
John Reck76005182021-06-09 22:43:05 -040078 if (!isCapturingSkp()) {
79 mEglManager.damageFrame(frame, dirty);
80 }
Stan Iliev500a0c32016-10-26 10:30:09 -040081
Peiyong Lin1f6aa122018-09-10 16:28:08 -070082 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -040083 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040084 GrGLFramebufferInfo fboInfo;
85 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070086 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -040087 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070088 } else if (colorType == kN32_SkColorType) {
89 // Note: The default preference of pixel format is RGBA_8888, when other
90 // pixel format is available, we should branch out and do more check.
Greg Danielc9da8e82018-03-21 10:50:24 -040091 fboInfo.fFormat = GL_RGBA8;
John Reckb36bfdd2020-07-23 13:47:49 -070092 } else if (colorType == kRGBA_1010102_SkColorType) {
93 fboInfo.fFormat = GL_RGB10_A2;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -050094 } else if (colorType == kAlpha_8_SkColorType) {
95 fboInfo.fFormat = GL_R8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070096 } else {
97 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -040098 }
Greg Danielac2d2322017-07-12 11:30:15 -040099
Greg Danielc9da8e82018-03-21 10:50:24 -0400100 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -0400101
102 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
103
104 SkASSERT(mRenderThread.getGrContext() != nullptr);
105 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Derek Sollenberger25833d22019-01-14 13:55:55 -0500106 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400107 mSurfaceColorSpace, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400108
Fedor Kudasov90df0562019-06-19 11:41:34 +0100109 LightingInfo::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500110 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
John Reck0fa0cbc2019-04-05 16:57:46 -0700111 SkMatrix::I());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400112
113 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700114 if (CC_UNLIKELY(Properties::showDirtyRegions ||
115 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400116 SkCanvas* profileCanvas = surface->getCanvas();
117 SkiaProfileRenderer profileRenderer(profileCanvas);
118 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400119 }
120
Greg Danielbe2803a2021-02-19 18:32:16 -0500121 {
122 ATRACE_NAME("flush commands");
123 surface->flushAndSubmit();
124 }
125 layerUpdateQueue->clear();
126
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500127 // Log memory statistics
128 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
129 dumpResourceCacheUsage();
130 }
131
Stan Iliev500a0c32016-10-26 10:30:09 -0400132 return true;
133}
134
John Reck1bcacfd2017-11-03 10:12:19 -0700135bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
136 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400137 GL_CHECKPOINT(LOW);
138
139 // Even if we decided to cancel the frame, from the perspective of jank
140 // metrics the frame was swapped at this point
141 currentFrameInfo->markSwapBuffers();
142
143 *requireSwap = drew || mEglManager.damageRequiresSwap();
144
145 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
146 return false;
147 }
148
149 return *requireSwap;
150}
151
Stan Iliev500a0c32016-10-26 10:30:09 -0400152DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700153 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000154 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400155}
156
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400157void SkiaOpenGLPipeline::onContextDestroyed() {
158 if (mEglSurface != EGL_NO_SURFACE) {
159 mEglManager.destroySurface(mEglSurface);
160 mEglSurface = EGL_NO_SURFACE;
161 }
162}
163
Stan Iliev500a0c32016-10-26 10:30:09 -0400164void SkiaOpenGLPipeline::onStop() {
165 if (mEglManager.isCurrent(mEglSurface)) {
166 mEglManager.makeCurrent(EGL_NO_SURFACE);
167 }
168}
169
John Reck8ddbc592020-05-07 16:11:18 -0700170bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400171 if (mEglSurface != EGL_NO_SURFACE) {
172 mEglManager.destroySurface(mEglSurface);
173 mEglSurface = EGL_NO_SURFACE;
174 }
175
176 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700177 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500178 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800179 if (!newSurface) {
180 return false;
181 }
182 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400183 }
184
185 if (mEglSurface != EGL_NO_SURFACE) {
186 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
187 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
188 return true;
189 }
190
191 return false;
192}
193
194bool SkiaOpenGLPipeline::isSurfaceReady() {
195 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
196}
197
198bool SkiaOpenGLPipeline::isContextReady() {
199 return CC_LIKELY(mEglManager.hasEglContext());
200}
201
202void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
203 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
204 if (thread.eglManager().hasEglContext()) {
205 mode = DrawGlInfo::kModeProcess;
206 }
207
208 (*functor)(mode, nullptr);
209
210 // If there's no context we don't need to reset as there's no gl state to save/restore
211 if (mode != DrawGlInfo::kModeProcessNoContext) {
212 thread.getGrContext()->resetContext();
213 }
214}
215
Stan Iliev500a0c32016-10-26 10:30:09 -0400216} /* namespace skiapipeline */
217} /* namespace uirenderer */
218} /* namespace android */