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 "GlLayer.h"
21 #include "LayerDrawable.h"
22 #include "renderthread/EglManager.h"
23 #include "renderthread/Frame.h"
24 #include "renderstate/RenderState.h"
25 #include "SkiaPipeline.h"
26 #include "SkiaProfileRenderer.h"
27 #include "utils/TraceUtils.h"
28 
29 #include <cutils/properties.h>
30 #include <strings.h>
31 
32 using namespace android::uirenderer::renderthread;
33 
34 namespace android {
35 namespace uirenderer {
36 namespace skiapipeline {
37 
SkiaOpenGLPipeline(RenderThread & thread)38 SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
39         : SkiaPipeline(thread)
40         , mEglManager(thread.eglManager()) {
41 }
42 
makeCurrent()43 MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
44     // TODO: Figure out why this workaround is needed, see b/13913604
45     // In the meantime this matches the behavior of GLRenderer, so it is not a regression
46     EGLint error = 0;
47     if (!mEglManager.makeCurrent(mEglSurface, &error)) {
48         return MakeCurrentResult::AlreadyCurrent;
49     }
50     return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
51 }
52 
getFrame()53 Frame SkiaOpenGLPipeline::getFrame() {
54     LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
55                 "drawRenderNode called on a context with no surface!");
56     return mEglManager.beginFrame(mEglSurface);
57 }
58 
draw(const Frame & frame,const SkRect & screenDirty,const SkRect & dirty,const FrameBuilder::LightGeometry & lightGeometry,LayerUpdateQueue * layerUpdateQueue,const Rect & contentDrawBounds,bool opaque,const BakedOpRenderer::LightInfo & lightInfo,const std::vector<sp<RenderNode>> & renderNodes,FrameInfoVisualizer * profiler)59 bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty,
60         const SkRect& dirty,
61         const FrameBuilder::LightGeometry& lightGeometry,
62         LayerUpdateQueue* layerUpdateQueue,
63         const Rect& contentDrawBounds, bool opaque,
64         const BakedOpRenderer::LightInfo& lightInfo,
65         const std::vector<sp<RenderNode>>& renderNodes,
66         FrameInfoVisualizer* profiler) {
67 
68     mEglManager.damageFrame(frame, dirty);
69 
70     // setup surface for fbo0
71     GrBackendRenderTargetDesc renderTargetDesc;
72     renderTargetDesc.fWidth = frame.width();
73     renderTargetDesc.fHeight = frame.height();
74     renderTargetDesc.fConfig = kRGBA_8888_GrPixelConfig;
75     renderTargetDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
76     renderTargetDesc.fSampleCnt = 0;
77     renderTargetDesc.fStencilBits = STENCIL_BUFFER_SIZE;
78     renderTargetDesc.fRenderTargetHandle = 0;
79 
80     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
81 
82     SkASSERT(mRenderThread.getGrContext() != nullptr);
83     sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
84             mRenderThread.getGrContext(), renderTargetDesc, &props));
85 
86     SkiaPipeline::updateLighting(lightGeometry, lightInfo);
87     renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface);
88     layerUpdateQueue->clear();
89 
90     // Draw visual debugging features
91     if (CC_UNLIKELY(Properties::showDirtyRegions
92             || ProfileType::None != Properties::getProfileType())) {
93         SkCanvas* profileCanvas = surface->getCanvas();
94         SkiaProfileRenderer profileRenderer(profileCanvas);
95         profiler->draw(profileRenderer);
96         profileCanvas->flush();
97     }
98 
99     // Log memory statistics
100     if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
101         dumpResourceCacheUsage();
102     }
103 
104     return true;
105 }
106 
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)107 bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew,
108         const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
109 
110     GL_CHECKPOINT(LOW);
111 
112     // Even if we decided to cancel the frame, from the perspective of jank
113     // metrics the frame was swapped at this point
114     currentFrameInfo->markSwapBuffers();
115 
116     *requireSwap = drew || mEglManager.damageRequiresSwap();
117 
118     if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
119         return false;
120     }
121 
122     return *requireSwap;
123 }
124 
copyLayerInto(DeferredLayerUpdater * deferredLayer,SkBitmap * bitmap)125 bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) {
126     if (!mRenderThread.getGrContext()) {
127         return false;
128     }
129 
130     // acquire most recent buffer for drawing
131     deferredLayer->updateTexImage();
132     deferredLayer->apply();
133 
134     SkCanvas canvas(*bitmap);
135     Layer* layer = deferredLayer->backingLayer();
136     return LayerDrawable::DrawLayer(mRenderThread.getGrContext(), &canvas, layer);
137 }
138 
createLayer(RenderState & renderState,uint32_t layerWidth,uint32_t layerHeight,SkColorFilter * colorFilter,int alpha,SkBlendMode mode,bool blend)139 static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
140         SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
141     GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha,
142             mode, blend);
143     layer->generateTexture();
144     return layer;
145 }
146 
createTextureLayer()147 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
148     mEglManager.initialize();
149     return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
150 }
151 
onStop()152 void SkiaOpenGLPipeline::onStop() {
153     if (mEglManager.isCurrent(mEglSurface)) {
154         mEglManager.makeCurrent(EGL_NO_SURFACE);
155     }
156 }
157 
setSurface(Surface * surface,SwapBehavior swapBehavior)158 bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
159 
160     if (mEglSurface != EGL_NO_SURFACE) {
161         mEglManager.destroySurface(mEglSurface);
162         mEglSurface = EGL_NO_SURFACE;
163     }
164 
165     if (surface) {
166         mEglSurface = mEglManager.createSurface(surface);
167     }
168 
169     if (mEglSurface != EGL_NO_SURFACE) {
170         const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
171         mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
172         return true;
173     }
174 
175     return false;
176 }
177 
isSurfaceReady()178 bool SkiaOpenGLPipeline::isSurfaceReady() {
179     return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
180 }
181 
isContextReady()182 bool SkiaOpenGLPipeline::isContextReady() {
183     return CC_LIKELY(mEglManager.hasEglContext());
184 }
185 
invokeFunctor(const RenderThread & thread,Functor * functor)186 void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
187     DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
188     if (thread.eglManager().hasEglContext()) {
189         mode = DrawGlInfo::kModeProcess;
190     }
191 
192     (*functor)(mode, nullptr);
193 
194     // If there's no context we don't need to reset as there's no gl state to save/restore
195     if (mode != DrawGlInfo::kModeProcessNoContext) {
196         thread.getGrContext()->resetContext();
197     }
198 }
199 
200 } /* namespace skiapipeline */
201 } /* namespace uirenderer */
202 } /* namespace android */
203