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 "DeferredLayerUpdater.h"
20 #include "LayerDrawable.h"
21 #include "LightingInfo.h"
22 #include "SkiaPipeline.h"
23 #include "SkiaProfileRenderer.h"
24 #include "hwui/Bitmap.h"
25 #include "private/hwui/DrawGlInfo.h"
26 #include "renderstate/RenderState.h"
27 #include "renderthread/EglManager.h"
28 #include "renderthread/Frame.h"
29 #include "utils/GLUtils.h"
30 #include "utils/TraceUtils.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     mEglManager.damageFrame(frame, dirty);
79 
80     SkColorType colorType = getSurfaceColorType();
81     // setup surface for fbo0
82     GrGLFramebufferInfo fboInfo;
83     fboInfo.fFBOID = 0;
84     if (colorType == kRGBA_F16_SkColorType) {
85         fboInfo.fFormat = GL_RGBA16F;
86     } 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.
89         fboInfo.fFormat = GL_RGBA8;
90     } else {
91         LOG_ALWAYS_FATAL("Unsupported color type.");
92     }
93 
94     GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
95 
96     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
97 
98     SkASSERT(mRenderThread.getGrContext() != nullptr);
99     sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
100             mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
101             mSurfaceColorSpace, &props));
102 
103     LightingInfo::updateLighting(lightGeometry, lightInfo);
104     renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
105                 SkMatrix::I());
106     layerUpdateQueue->clear();
107 
108     // Draw visual debugging features
109     if (CC_UNLIKELY(Properties::showDirtyRegions ||
110                     ProfileType::None != Properties::getProfileType())) {
111         SkCanvas* profileCanvas = surface->getCanvas();
112         SkiaProfileRenderer profileRenderer(profileCanvas);
113         profiler->draw(profileRenderer);
114         profileCanvas->flush();
115     }
116 
117     // Log memory statistics
118     if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
119         dumpResourceCacheUsage();
120     }
121 
122     return true;
123 }
124 
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)125 bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
126                                      FrameInfo* currentFrameInfo, bool* requireSwap) {
127     GL_CHECKPOINT(LOW);
128 
129     // Even if we decided to cancel the frame, from the perspective of jank
130     // metrics the frame was swapped at this point
131     currentFrameInfo->markSwapBuffers();
132 
133     *requireSwap = drew || mEglManager.damageRequiresSwap();
134 
135     if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
136         return false;
137     }
138 
139     return *requireSwap;
140 }
141 
createTextureLayer()142 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
143     mRenderThread.requireGlContext();
144     return new DeferredLayerUpdater(mRenderThread.renderState());
145 }
146 
onContextDestroyed()147 void SkiaOpenGLPipeline::onContextDestroyed() {
148     if (mEglSurface != EGL_NO_SURFACE) {
149         mEglManager.destroySurface(mEglSurface);
150         mEglSurface = EGL_NO_SURFACE;
151     }
152 }
153 
onStop()154 void SkiaOpenGLPipeline::onStop() {
155     if (mEglManager.isCurrent(mEglSurface)) {
156         mEglManager.makeCurrent(EGL_NO_SURFACE);
157     }
158 }
159 
setSurface(ANativeWindow * surface,SwapBehavior swapBehavior)160 bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
161     if (mEglSurface != EGL_NO_SURFACE) {
162         mEglManager.destroySurface(mEglSurface);
163         mEglSurface = EGL_NO_SURFACE;
164     }
165 
166     if (surface) {
167         mRenderThread.requireGlContext();
168         auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace);
169         if (!newSurface) {
170             return false;
171         }
172         mEglSurface = newSurface.unwrap();
173     }
174 
175     if (mEglSurface != EGL_NO_SURFACE) {
176         const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
177         mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
178         return true;
179     }
180 
181     return false;
182 }
183 
isSurfaceReady()184 bool SkiaOpenGLPipeline::isSurfaceReady() {
185     return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
186 }
187 
isContextReady()188 bool SkiaOpenGLPipeline::isContextReady() {
189     return CC_LIKELY(mEglManager.hasEglContext());
190 }
191 
invokeFunctor(const RenderThread & thread,Functor * functor)192 void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
193     DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
194     if (thread.eglManager().hasEglContext()) {
195         mode = DrawGlInfo::kModeProcess;
196     }
197 
198     (*functor)(mode, nullptr);
199 
200     // If there's no context we don't need to reset as there's no gl state to save/restore
201     if (mode != DrawGlInfo::kModeProcessNoContext) {
202         thread.getGrContext()->resetContext();
203     }
204 }
205 
206 } /* namespace skiapipeline */
207 } /* namespace uirenderer */
208 } /* namespace android */
209