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 "SkiaPipeline.h"
22 #include "SkiaProfileRenderer.h"
23 #include "hwui/Bitmap.h"
24 #include "private/hwui/DrawGlInfo.h"
25 #include "renderstate/RenderState.h"
26 #include "renderthread/EglManager.h"
27 #include "renderthread/Frame.h"
28 #include "utils/GLUtils.h"
29 #include "utils/TraceUtils.h"
30 
31 #include <GLES3/gl3.h>
32 
33 #include <GrBackendSurface.h>
34 #include <SkBlendMode.h>
35 #include <SkImageInfo.h>
36 
37 #include <cutils/properties.h>
38 #include <strings.h>
39 
40 using namespace android::uirenderer::renderthread;
41 
42 namespace android {
43 namespace uirenderer {
44 namespace skiapipeline {
45 
SkiaOpenGLPipeline(RenderThread & thread)46 SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
47         : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
48     thread.renderState().registerContextCallback(this);
49 }
50 
~SkiaOpenGLPipeline()51 SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
52     mRenderThread.renderState().removeContextCallback(this);
53 }
54 
makeCurrent()55 MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
56     // TODO: Figure out why this workaround is needed, see b/13913604
57     // In the meantime this matches the behavior of GLRenderer, so it is not a regression
58     EGLint error = 0;
59     if (!mEglManager.makeCurrent(mEglSurface, &error)) {
60         return MakeCurrentResult::AlreadyCurrent;
61     }
62     return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
63 }
64 
getFrame()65 Frame SkiaOpenGLPipeline::getFrame() {
66     LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
67                         "drawRenderNode called on a context with no surface!");
68     return mEglManager.beginFrame(mEglSurface);
69 }
70 
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)71 bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
72                               const LightGeometry& lightGeometry,
73                               LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
74                               bool opaque, const LightInfo& lightInfo,
75                               const std::vector<sp<RenderNode>>& renderNodes,
76                               FrameInfoVisualizer* profiler) {
77     mEglManager.damageFrame(frame, dirty);
78 
79     SkColorType colorType = getSurfaceColorType();
80     // setup surface for fbo0
81     GrGLFramebufferInfo fboInfo;
82     fboInfo.fFBOID = 0;
83     if (colorType == kRGBA_F16_SkColorType) {
84         fboInfo.fFormat = GL_RGBA16F;
85     } else if (colorType == kN32_SkColorType) {
86         // Note: The default preference of pixel format is RGBA_8888, when other
87         // pixel format is available, we should branch out and do more check.
88         fboInfo.fFormat = GL_RGBA8;
89     } else {
90         LOG_ALWAYS_FATAL("Unsupported color type.");
91     }
92 
93     GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
94 
95     SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
96 
97     SkASSERT(mRenderThread.getGrContext() != nullptr);
98     sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
99             mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
100             mSurfaceColorSpace, &props));
101 
102     SkiaPipeline::updateLighting(lightGeometry, lightInfo);
103     renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
104                 SkMatrix::I());
105     layerUpdateQueue->clear();
106 
107     // Draw visual debugging features
108     if (CC_UNLIKELY(Properties::showDirtyRegions ||
109                     ProfileType::None != Properties::getProfileType())) {
110         SkCanvas* profileCanvas = surface->getCanvas();
111         SkiaProfileRenderer profileRenderer(profileCanvas);
112         profiler->draw(profileRenderer);
113         profileCanvas->flush();
114     }
115 
116     // Log memory statistics
117     if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
118         dumpResourceCacheUsage();
119     }
120 
121     return true;
122 }
123 
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)124 bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
125                                      FrameInfo* currentFrameInfo, bool* requireSwap) {
126     GL_CHECKPOINT(LOW);
127 
128     // Even if we decided to cancel the frame, from the perspective of jank
129     // metrics the frame was swapped at this point
130     currentFrameInfo->markSwapBuffers();
131 
132     *requireSwap = drew || mEglManager.damageRequiresSwap();
133 
134     if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
135         return false;
136     }
137 
138     return *requireSwap;
139 }
140 
createTextureLayer()141 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
142     mRenderThread.requireGlContext();
143     return new DeferredLayerUpdater(mRenderThread.renderState());
144 }
145 
onContextDestroyed()146 void SkiaOpenGLPipeline::onContextDestroyed() {
147     if (mEglSurface != EGL_NO_SURFACE) {
148         mEglManager.destroySurface(mEglSurface);
149         mEglSurface = EGL_NO_SURFACE;
150     }
151 }
152 
onStop()153 void SkiaOpenGLPipeline::onStop() {
154     if (mEglManager.isCurrent(mEglSurface)) {
155         mEglManager.makeCurrent(EGL_NO_SURFACE);
156     }
157 }
158 
setBufferCount(ANativeWindow * window,uint32_t extraBuffers)159 static void setBufferCount(ANativeWindow* window, uint32_t extraBuffers) {
160     int query_value;
161     int err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
162     if (err != 0 || query_value < 0) {
163         ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
164         return;
165     }
166     auto min_undequeued_buffers = static_cast<uint32_t>(query_value);
167 
168     int bufferCount = min_undequeued_buffers + 2 + extraBuffers;
169     native_window_set_buffer_count(window, bufferCount);
170 }
171 
setSurface(ANativeWindow * surface,SwapBehavior swapBehavior,ColorMode colorMode,uint32_t extraBuffers)172 bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior,
173                                     ColorMode colorMode, uint32_t extraBuffers) {
174     if (mEglSurface != EGL_NO_SURFACE) {
175         mEglManager.destroySurface(mEglSurface);
176         mEglSurface = EGL_NO_SURFACE;
177     }
178 
179     setSurfaceColorProperties(colorMode);
180 
181     if (surface) {
182         mRenderThread.requireGlContext();
183         auto newSurface = mEglManager.createSurface(surface, colorMode, mSurfaceColorSpace);
184         if (!newSurface) {
185             return false;
186         }
187         mEglSurface = newSurface.unwrap();
188     }
189 
190     if (mEglSurface != EGL_NO_SURFACE) {
191         const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
192         mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
193         setBufferCount(surface, extraBuffers);
194         return true;
195     }
196 
197     return false;
198 }
199 
isSurfaceReady()200 bool SkiaOpenGLPipeline::isSurfaceReady() {
201     return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
202 }
203 
isContextReady()204 bool SkiaOpenGLPipeline::isContextReady() {
205     return CC_LIKELY(mEglManager.hasEglContext());
206 }
207 
invokeFunctor(const RenderThread & thread,Functor * functor)208 void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
209     DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
210     if (thread.eglManager().hasEglContext()) {
211         mode = DrawGlInfo::kModeProcess;
212     }
213 
214     (*functor)(mode, nullptr);
215 
216     // If there's no context we don't need to reset as there's no gl state to save/restore
217     if (mode != DrawGlInfo::kModeProcessNoContext) {
218         thread.getGrContext()->resetContext();
219     }
220 }
221 
222 } /* namespace skiapipeline */
223 } /* namespace uirenderer */
224 } /* namespace android */
225