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 <GrBackendSurface.h>
20 #include <SkBlendMode.h>
21 #include <SkImageInfo.h>
22 #include <cutils/properties.h>
23 #include <gui/TraceUtils.h>
24 #include <strings.h>
25
26 #include "DeferredLayerUpdater.h"
27 #include "FrameInfo.h"
28 #include "LayerDrawable.h"
29 #include "LightingInfo.h"
30 #include "SkiaPipeline.h"
31 #include "SkiaProfileRenderer.h"
32 #include "hwui/Bitmap.h"
33 #include "private/hwui/DrawGlInfo.h"
34 #include "renderstate/RenderState.h"
35 #include "renderthread/EglManager.h"
36 #include "renderthread/Frame.h"
37 #include "renderthread/IRenderPipeline.h"
38 #include "utils/GLUtils.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 // In case the surface was destroyed (e.g. a previous trimMemory call) we
57 // need to recreate it here.
58 if (!isSurfaceReady() && mNativeWindow) {
59 setSurface(mNativeWindow.get(), mSwapBehavior);
60 }
61
62 EGLint error = 0;
63 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
64 return MakeCurrentResult::AlreadyCurrent;
65 }
66 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
67 }
68
getFrame()69 Frame SkiaOpenGLPipeline::getFrame() {
70 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
71 "drawRenderNode called on a context with no surface!");
72 return mEglManager.beginFrame(mEglSurface);
73 }
74
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)75 IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw(
76 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
77 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
78 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
79 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler) {
80 if (!isCapturingSkp()) {
81 mEglManager.damageFrame(frame, dirty);
82 }
83
84 SkColorType colorType = getSurfaceColorType();
85 // setup surface for fbo0
86 GrGLFramebufferInfo fboInfo;
87 fboInfo.fFBOID = 0;
88 if (colorType == kRGBA_F16_SkColorType) {
89 fboInfo.fFormat = GL_RGBA16F;
90 } else if (colorType == kN32_SkColorType) {
91 // Note: The default preference of pixel format is RGBA_8888, when other
92 // pixel format is available, we should branch out and do more check.
93 fboInfo.fFormat = GL_RGBA8;
94 } else if (colorType == kRGBA_1010102_SkColorType) {
95 fboInfo.fFormat = GL_RGB10_A2;
96 } else if (colorType == kAlpha_8_SkColorType) {
97 fboInfo.fFormat = GL_R8;
98 } else {
99 LOG_ALWAYS_FATAL("Unsupported color type.");
100 }
101
102 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
103
104 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
105
106 SkASSERT(mRenderThread.getGrContext() != nullptr);
107 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
108 mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType,
109 mSurfaceColorSpace, &props));
110
111 LightingInfo::updateLighting(lightGeometry, lightInfo);
112 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface,
113 SkMatrix::I());
114
115 // Draw visual debugging features
116 if (CC_UNLIKELY(Properties::showDirtyRegions ||
117 ProfileType::None != Properties::getProfileType())) {
118 SkCanvas* profileCanvas = surface->getCanvas();
119 SkiaProfileRenderer profileRenderer(profileCanvas);
120 profiler->draw(profileRenderer);
121 }
122
123 {
124 ATRACE_NAME("flush commands");
125 surface->flushAndSubmit();
126 }
127 layerUpdateQueue->clear();
128
129 // Log memory statistics
130 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
131 dumpResourceCacheUsage();
132 }
133
134 return {true, IRenderPipeline::DrawResult::kUnknownTime};
135 }
136
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)137 bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
138 FrameInfo* currentFrameInfo, bool* requireSwap) {
139 GL_CHECKPOINT(LOW);
140
141 // Even if we decided to cancel the frame, from the perspective of jank
142 // metrics the frame was swapped at this point
143 currentFrameInfo->markSwapBuffers();
144
145 *requireSwap = drew || mEglManager.damageRequiresSwap();
146
147 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
148 return false;
149 }
150
151 return *requireSwap;
152 }
153
createTextureLayer()154 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
155 mRenderThread.requireGlContext();
156 return new DeferredLayerUpdater(mRenderThread.renderState());
157 }
158
onContextDestroyed()159 void SkiaOpenGLPipeline::onContextDestroyed() {
160 if (mEglSurface != EGL_NO_SURFACE) {
161 mEglManager.destroySurface(mEglSurface);
162 mEglSurface = EGL_NO_SURFACE;
163 }
164 }
165
onStop()166 void SkiaOpenGLPipeline::onStop() {
167 if (mEglManager.isCurrent(mEglSurface)) {
168 mEglManager.makeCurrent(EGL_NO_SURFACE);
169 }
170 }
171
setSurface(ANativeWindow * surface,SwapBehavior swapBehavior)172 bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
173 mNativeWindow = surface;
174 mSwapBehavior = swapBehavior;
175
176 if (mEglSurface != EGL_NO_SURFACE) {
177 mEglManager.destroySurface(mEglSurface);
178 mEglSurface = EGL_NO_SURFACE;
179 }
180
181 if (surface) {
182 mRenderThread.requireGlContext();
183 auto newSurface = mEglManager.createSurface(surface, mColorMode, 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 const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
193 ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior.");
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