blob: a78cd8316624c8a8fbbaf009fd2260d43d0edf30 [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) {
Stan Iliev500a0c32016-10-26 10:30:09 -040078 mEglManager.damageFrame(frame, dirty);
79
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;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070092 } else {
93 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -040094 }
Greg Danielac2d2322017-07-12 11:30:15 -040095
Greg Danielc9da8e82018-03-21 10:50:24 -040096 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040097
98 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
99
100 SkASSERT(mRenderThread.getGrContext() != nullptr);
101 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Derek Sollenberger25833d22019-01-14 13:55:55 -0500102 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400103 mSurfaceColorSpace, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400104
Fedor Kudasov90df0562019-06-19 11:41:34 +0100105 LightingInfo::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -0500106 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
John Reck0fa0cbc2019-04-05 16:57:46 -0700107 SkMatrix::I());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400108
109 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700110 if (CC_UNLIKELY(Properties::showDirtyRegions ||
111 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400112 SkCanvas* profileCanvas = surface->getCanvas();
113 SkiaProfileRenderer profileRenderer(profileCanvas);
114 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400115 }
116
Greg Danielbe2803a2021-02-19 18:32:16 -0500117 {
118 ATRACE_NAME("flush commands");
119 surface->flushAndSubmit();
120 }
121 layerUpdateQueue->clear();
122
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500123 // Log memory statistics
124 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
125 dumpResourceCacheUsage();
126 }
127
Stan Iliev500a0c32016-10-26 10:30:09 -0400128 return true;
129}
130
John Reck1bcacfd2017-11-03 10:12:19 -0700131bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
132 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400133 GL_CHECKPOINT(LOW);
134
135 // Even if we decided to cancel the frame, from the perspective of jank
136 // metrics the frame was swapped at this point
137 currentFrameInfo->markSwapBuffers();
138
139 *requireSwap = drew || mEglManager.damageRequiresSwap();
140
141 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
142 return false;
143 }
144
145 return *requireSwap;
146}
147
Stan Iliev500a0c32016-10-26 10:30:09 -0400148DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700149 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000150 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400151}
152
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400153void SkiaOpenGLPipeline::onContextDestroyed() {
154 if (mEglSurface != EGL_NO_SURFACE) {
155 mEglManager.destroySurface(mEglSurface);
156 mEglSurface = EGL_NO_SURFACE;
157 }
158}
159
Stan Iliev500a0c32016-10-26 10:30:09 -0400160void SkiaOpenGLPipeline::onStop() {
161 if (mEglManager.isCurrent(mEglSurface)) {
162 mEglManager.makeCurrent(EGL_NO_SURFACE);
163 }
164}
165
John Reck8ddbc592020-05-07 16:11:18 -0700166bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400167 if (mEglSurface != EGL_NO_SURFACE) {
168 mEglManager.destroySurface(mEglSurface);
169 mEglSurface = EGL_NO_SURFACE;
170 }
171
172 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700173 mRenderThread.requireGlContext();
Derek Sollenberger1863d942020-02-05 15:41:51 -0500174 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
John Reck8e539ca2018-11-15 15:21:29 -0800175 if (!newSurface) {
176 return false;
177 }
178 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400179 }
180
181 if (mEglSurface != EGL_NO_SURFACE) {
182 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
183 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
184 return true;
185 }
186
187 return false;
188}
189
190bool SkiaOpenGLPipeline::isSurfaceReady() {
191 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
192}
193
194bool SkiaOpenGLPipeline::isContextReady() {
195 return CC_LIKELY(mEglManager.hasEglContext());
196}
197
198void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
199 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
200 if (thread.eglManager().hasEglContext()) {
201 mode = DrawGlInfo::kModeProcess;
202 }
203
204 (*functor)(mode, nullptr);
205
206 // If there's no context we don't need to reset as there's no gl state to save/restore
207 if (mode != DrawGlInfo::kModeProcessNoContext) {
208 thread.getGrContext()->resetContext();
209 }
210}
211
Stan Iliev500a0c32016-10-26 10:30:09 -0400212} /* namespace skiapipeline */
213} /* namespace uirenderer */
214} /* namespace android */