1 /*
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
19 #include <gui/TraceUtils.h>
20 #include "DeferredLayerUpdater.h"
21 #include "LayerDrawable.h"
22 #include "LightingInfo.h"
23 #include "SkiaPipeline.h"
24 #include "SkiaProfileRenderer.h"
25 #include "hwui/Bitmap.h"
26 #include "private/hwui/DrawGlInfo.h"
27 #include "renderstate/RenderState.h"
28 #include "renderthread/EglManager.h"
29 #include "renderthread/Frame.h"
30 #include "utils/GLUtils.h"
31
32 #include <GLES3/gl3.h>
33
34 #include <GrBackendSurface.h>
35 #include <SkBlendMode.h>
36 #include <SkImageInfo.h>
37
38 #include <cutils/properties.h>
39 #include <strings.h>
40
41 using namespace android::uirenderer::renderthread;
42
43 namespace android {
44 namespace uirenderer {
45 namespace skiapipeline {
46
SkiaOpenGLPipeline(RenderThread & thread)47 SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
48 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
49 thread.renderState().registerContextCallback(this);
50 }
51
~SkiaOpenGLPipeline()52 SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
53 mRenderThread.renderState().removeContextCallback(this);
54 }
55
makeCurrent()56 MakeCurrentResult 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
getFrame()66 Frame SkiaOpenGLPipeline::getFrame() {
67 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
68 "drawRenderNode called on a context with no surface!");
69 return mEglManager.beginFrame(mEglSurface);
70 }
71
draw(const Frame & frame,const SkRect & screenDirty,const SkRect & dirty,const LightGeometry & lightGeometry,LayerUpdateQueue * layerUpdateQueue,const Rect & contentDrawBounds,bool opaque,const LightInfo & lightInfo,const std::vector<sp<RenderNode>> & renderNodes,FrameInfoVisualizer * profiler)72 bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
73 const LightGeometry& lightGeometry,
74 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
75 bool opaque, const LightInfo& lightInfo,
76 const std::vector<sp<RenderNode>>& renderNodes,
77 FrameInfoVisualizer* profiler) {
78 if (!isCapturingSkp()) {
79 mEglManager.damageFrame(frame, dirty);
80 }
81
82 SkColorType colorType = getSurfaceColorType();
83 // setup surface for fbo0
84 GrGLFramebufferInfo fboInfo;
85 fboInfo.fFBOID = 0;
86 if (colorType == kRGBA_F16_SkColorType) {
87 fboInfo.fFormat = GL_RGBA16F;
88 } 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.
91 fboInfo.fFormat = GL_RGBA8;
92 } else if (colorType == kRGBA_1010102_SkColorType) {
93 fboInfo.fFormat = GL_RGB10_A2;
94 } else {
95 LOG_ALWAYS_FATAL("Unsupported color type.");
96 }
97
98 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
99
100 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
101
102 SkASSERT(mRenderThread.getGrContext() != nullptr);
103 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
104 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
105 mSurfaceColorSpace, &props));
106
107 LightingInfo::updateLighting(lightGeometry, lightInfo);
108 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
109 SkMatrix::I());
110
111 // Draw visual debugging features
112 if (CC_UNLIKELY(Properties::showDirtyRegions ||
113 ProfileType::None != Properties::getProfileType())) {
114 SkCanvas* profileCanvas = surface->getCanvas();
115 SkiaProfileRenderer profileRenderer(profileCanvas);
116 profiler->draw(profileRenderer);
117 }
118
119 {
120 ATRACE_NAME("flush commands");
121 surface->flushAndSubmit();
122 }
123 layerUpdateQueue->clear();
124
125 // Log memory statistics
126 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
127 dumpResourceCacheUsage();
128 }
129
130 return true;
131 }
132
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)133 bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
134 FrameInfo* currentFrameInfo, bool* requireSwap) {
135 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
createTextureLayer()150 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
151 mRenderThread.requireGlContext();
152 return new DeferredLayerUpdater(mRenderThread.renderState());
153 }
154
onContextDestroyed()155 void SkiaOpenGLPipeline::onContextDestroyed() {
156 if (mEglSurface != EGL_NO_SURFACE) {
157 mEglManager.destroySurface(mEglSurface);
158 mEglSurface = EGL_NO_SURFACE;
159 }
160 }
161
onStop()162 void SkiaOpenGLPipeline::onStop() {
163 if (mEglManager.isCurrent(mEglSurface)) {
164 mEglManager.makeCurrent(EGL_NO_SURFACE);
165 }
166 }
167
setSurface(ANativeWindow * surface,SwapBehavior swapBehavior)168 bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
169 if (mEglSurface != EGL_NO_SURFACE) {
170 mEglManager.destroySurface(mEglSurface);
171 mEglSurface = EGL_NO_SURFACE;
172 }
173
174 if (surface) {
175 mRenderThread.requireGlContext();
176 auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
177 if (!newSurface) {
178 return false;
179 }
180 mEglSurface = newSurface.unwrap();
181 }
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
isSurfaceReady()192 bool SkiaOpenGLPipeline::isSurfaceReady() {
193 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
194 }
195
isContextReady()196 bool SkiaOpenGLPipeline::isContextReady() {
197 return CC_LIKELY(mEglManager.hasEglContext());
198 }
199
invokeFunctor(const RenderThread & thread,Functor * functor)200 void 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
214 } /* namespace skiapipeline */
215 } /* namespace uirenderer */
216 } /* namespace android */
217