blob: d58b59e8338068f66b273cf41ffaa70163b40b28 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
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"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050020#include "LayerDrawable.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040021#include "SkiaPipeline.h"
22#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "hwui/Bitmap.h"
24#include "renderstate/RenderState.h"
25#include "renderthread/EglManager.h"
26#include "renderthread/Frame.h"
John Reckd9d7f122018-05-03 14:40:56 -070027#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040028#include "utils/TraceUtils.h"
29
Greg Danielac2d2322017-07-12 11:30:15 -040030#include <GrBackendSurface.h>
Derek Sollenberger23249912018-05-03 16:12:18 -040031#include <SkBlendMode.h>
John Reck437f6fb2018-05-07 08:14:14 -070032#include <SkImageInfo.h>
Greg Danielac2d2322017-07-12 11:30:15 -040033
Stan Iliev500a0c32016-10-26 10:30:09 -040034#include <cutils/properties.h>
35#include <strings.h>
36
37using namespace android::uirenderer::renderthread;
38
39namespace android {
40namespace uirenderer {
41namespace skiapipeline {
42
43SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070044 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {}
Stan Iliev500a0c32016-10-26 10:30:09 -040045
46MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() {
47 // TODO: Figure out why this workaround is needed, see b/13913604
48 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
49 EGLint error = 0;
50 if (!mEglManager.makeCurrent(mEglSurface, &error)) {
51 return MakeCurrentResult::AlreadyCurrent;
52 }
53 return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded;
54}
55
56Frame SkiaOpenGLPipeline::getFrame() {
57 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -070058 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -040059 return mEglManager.beginFrame(mEglSurface);
60}
61
John Reck1bcacfd2017-11-03 10:12:19 -070062bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070063 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070064 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
John Reck437f6fb2018-05-07 08:14:14 -070065 bool opaque, bool wideColorGamut, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070066 const std::vector<sp<RenderNode>>& renderNodes,
67 FrameInfoVisualizer* profiler) {
Stan Iliev500a0c32016-10-26 10:30:09 -040068 mEglManager.damageFrame(frame, dirty);
69
Greg Danielc9da8e82018-03-21 10:50:24 -040070 SkColorType colorType;
Stan Iliev500a0c32016-10-26 10:30:09 -040071 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040072 GrGLFramebufferInfo fboInfo;
73 fboInfo.fFBOID = 0;
Greg Danielc9da8e82018-03-21 10:50:24 -040074 if (wideColorGamut) {
75 fboInfo.fFormat = GL_RGBA16F;
76 colorType = kRGBA_F16_SkColorType;
77 } else {
78 fboInfo.fFormat = GL_RGBA8;
79 colorType = kN32_SkColorType;
80 }
Greg Danielac2d2322017-07-12 11:30:15 -040081
Greg Danielc9da8e82018-03-21 10:50:24 -040082 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040083
84 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
85
86 SkASSERT(mRenderThread.getGrContext() != nullptr);
87 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Greg Danielc9da8e82018-03-21 10:50:24 -040088 mRenderThread.getGrContext(), backendRT, kBottomLeft_GrSurfaceOrigin, colorType,
89 nullptr, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -040090
91 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
John Reck1bcacfd2017-11-03 10:12:19 -070092 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut, contentDrawBounds,
93 surface);
Stan Iliev500a0c32016-10-26 10:30:09 -040094 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040095
96 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070097 if (CC_UNLIKELY(Properties::showDirtyRegions ||
98 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -040099 SkCanvas* profileCanvas = surface->getCanvas();
100 SkiaProfileRenderer profileRenderer(profileCanvas);
101 profiler->draw(profileRenderer);
102 profileCanvas->flush();
103 }
104
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500105 // Log memory statistics
106 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
107 dumpResourceCacheUsage();
108 }
109
Stan Iliev500a0c32016-10-26 10:30:09 -0400110 return true;
111}
112
John Reck1bcacfd2017-11-03 10:12:19 -0700113bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
114 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400115 GL_CHECKPOINT(LOW);
116
117 // Even if we decided to cancel the frame, from the perspective of jank
118 // metrics the frame was swapped at this point
119 currentFrameInfo->markSwapBuffers();
120
121 *requireSwap = drew || mEglManager.damageRequiresSwap();
122
123 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
124 return false;
125 }
126
127 return *requireSwap;
128}
129
Stan Iliev500a0c32016-10-26 10:30:09 -0400130DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700131 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000132 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400133}
134
135void SkiaOpenGLPipeline::onStop() {
136 if (mEglManager.isCurrent(mEglSurface)) {
137 mEglManager.makeCurrent(EGL_NO_SURFACE);
138 }
139}
140
Romain Guy26a2b972017-04-17 09:39:51 -0700141bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700142 ColorMode colorMode) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400143 if (mEglSurface != EGL_NO_SURFACE) {
144 mEglManager.destroySurface(mEglSurface);
145 mEglSurface = EGL_NO_SURFACE;
146 }
147
148 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700149 mRenderThread.requireGlContext();
Romain Guy26a2b972017-04-17 09:39:51 -0700150 const bool wideColorGamut = colorMode == ColorMode::WideColorGamut;
151 mEglSurface = mEglManager.createSurface(surface, wideColorGamut);
Stan Iliev500a0c32016-10-26 10:30:09 -0400152 }
153
154 if (mEglSurface != EGL_NO_SURFACE) {
155 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
156 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
157 return true;
158 }
159
160 return false;
161}
162
163bool SkiaOpenGLPipeline::isSurfaceReady() {
164 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
165}
166
167bool SkiaOpenGLPipeline::isContextReady() {
168 return CC_LIKELY(mEglManager.hasEglContext());
169}
170
171void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
172 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
173 if (thread.eglManager().hasEglContext()) {
174 mode = DrawGlInfo::kModeProcess;
175 }
176
177 (*functor)(mode, nullptr);
178
179 // If there's no context we don't need to reset as there's no gl state to save/restore
180 if (mode != DrawGlInfo::kModeProcessNoContext) {
181 thread.getGrContext()->resetContext();
182 }
183}
184
Stan Iliev500a0c32016-10-26 10:30:09 -0400185} /* namespace skiapipeline */
186} /* namespace uirenderer */
187} /* namespace android */