blob: 6ae59990d0ab86f7d5567a94d8d81c548dc6ec72 [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"
Derek Sollenberger28a4d992018-09-20 13:37:24 -040024#include "private/hwui/DrawGlInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070025#include "renderstate/RenderState.h"
26#include "renderthread/EglManager.h"
27#include "renderthread/Frame.h"
John Reckd9d7f122018-05-03 14:40:56 -070028#include "utils/GLUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029#include "utils/TraceUtils.h"
30
John Reckbdc9f1b2018-09-14 15:22:35 -070031#include <GLES3/gl3.h>
32
Greg Danielac2d2322017-07-12 11:30:15 -040033#include <GrBackendSurface.h>
Derek Sollenberger23249912018-05-03 16:12:18 -040034#include <SkBlendMode.h>
John Reck437f6fb2018-05-07 08:14:14 -070035#include <SkImageInfo.h>
Greg Danielac2d2322017-07-12 11:30:15 -040036
Stan Iliev500a0c32016-10-26 10:30:09 -040037#include <cutils/properties.h>
38#include <strings.h>
39
40using namespace android::uirenderer::renderthread;
41
42namespace android {
43namespace uirenderer {
44namespace skiapipeline {
45
46SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread)
Derek Sollenberger5a5a6482018-09-19 13:52:13 -040047 : SkiaPipeline(thread), mEglManager(thread.eglManager()) {
48 thread.renderState().registerContextCallback(this);
49}
50
51SkiaOpenGLPipeline::~SkiaOpenGLPipeline() {
52 mRenderThread.renderState().removeContextCallback(this);
53}
Stan Iliev500a0c32016-10-26 10:30:09 -040054
55MakeCurrentResult 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
65Frame SkiaOpenGLPipeline::getFrame() {
66 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -070067 "drawRenderNode called on a context with no surface!");
Stan Iliev500a0c32016-10-26 10:30:09 -040068 return mEglManager.beginFrame(mEglSurface);
69}
70
John Reck1bcacfd2017-11-03 10:12:19 -070071bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070072 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070073 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070074 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070075 const std::vector<sp<RenderNode>>& renderNodes,
76 FrameInfoVisualizer* profiler) {
Stan Iliev500a0c32016-10-26 10:30:09 -040077 mEglManager.damageFrame(frame, dirty);
78
Peiyong Lin1f6aa122018-09-10 16:28:08 -070079 SkColorType colorType = getSurfaceColorType();
Stan Iliev500a0c32016-10-26 10:30:09 -040080 // setup surface for fbo0
Greg Danielac2d2322017-07-12 11:30:15 -040081 GrGLFramebufferInfo fboInfo;
82 fboInfo.fFBOID = 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070083 if (colorType == kRGBA_F16_SkColorType) {
Greg Danielc9da8e82018-03-21 10:50:24 -040084 fboInfo.fFormat = GL_RGBA16F;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070085 } 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.
Greg Danielc9da8e82018-03-21 10:50:24 -040088 fboInfo.fFormat = GL_RGBA8;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070089 } else {
90 LOG_ALWAYS_FATAL("Unsupported color type.");
Greg Danielc9da8e82018-03-21 10:50:24 -040091 }
Greg Danielac2d2322017-07-12 11:30:15 -040092
Greg Danielc9da8e82018-03-21 10:50:24 -040093 GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo);
Stan Iliev500a0c32016-10-26 10:30:09 -040094
95 SkSurfaceProps props(0, kUnknown_SkPixelGeometry);
96
97 SkASSERT(mRenderThread.getGrContext() != nullptr);
98 sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(
Greg Danielc9da8e82018-03-21 10:50:24 -040099 mRenderThread.getGrContext(), backendRT, kBottomLeft_GrSurfaceOrigin, colorType,
100 nullptr, &props));
Stan Iliev500a0c32016-10-26 10:30:09 -0400101
102 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700103 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400104 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400105
106 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700107 if (CC_UNLIKELY(Properties::showDirtyRegions ||
108 ProfileType::None != Properties::getProfileType())) {
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400109 SkCanvas* profileCanvas = surface->getCanvas();
110 SkiaProfileRenderer profileRenderer(profileCanvas);
111 profiler->draw(profileRenderer);
112 profileCanvas->flush();
113 }
114
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500115 // Log memory statistics
116 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
117 dumpResourceCacheUsage();
118 }
119
Stan Iliev500a0c32016-10-26 10:30:09 -0400120 return true;
121}
122
John Reck1bcacfd2017-11-03 10:12:19 -0700123bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
124 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400125 GL_CHECKPOINT(LOW);
126
127 // Even if we decided to cancel the frame, from the perspective of jank
128 // metrics the frame was swapped at this point
129 currentFrameInfo->markSwapBuffers();
130
131 *requireSwap = drew || mEglManager.damageRequiresSwap();
132
133 if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) {
134 return false;
135 }
136
137 return *requireSwap;
138}
139
Stan Iliev500a0c32016-10-26 10:30:09 -0400140DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
John Reck1e510712018-04-23 08:15:03 -0700141 mRenderThread.requireGlContext();
Stan Iliev564ca3e2018-09-04 22:00:00 +0000142 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400143}
144
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400145void SkiaOpenGLPipeline::onContextDestroyed() {
146 if (mEglSurface != EGL_NO_SURFACE) {
147 mEglManager.destroySurface(mEglSurface);
148 mEglSurface = EGL_NO_SURFACE;
149 }
150}
151
Stan Iliev500a0c32016-10-26 10:30:09 -0400152void SkiaOpenGLPipeline::onStop() {
153 if (mEglManager.isCurrent(mEglSurface)) {
154 mEglManager.makeCurrent(EGL_NO_SURFACE);
155 }
156}
157
Romain Guy26a2b972017-04-17 09:39:51 -0700158bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700159 ColorMode colorMode) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400160 if (mEglSurface != EGL_NO_SURFACE) {
161 mEglManager.destroySurface(mEglSurface);
162 mEglSurface = EGL_NO_SURFACE;
163 }
164
165 if (surface) {
John Reck1e510712018-04-23 08:15:03 -0700166 mRenderThread.requireGlContext();
John Reck8e539ca2018-11-15 15:21:29 -0800167 auto newSurface = mEglManager.createSurface(surface, colorMode);
168 if (!newSurface) {
169 return false;
170 }
171 mEglSurface = newSurface.unwrap();
Stan Iliev500a0c32016-10-26 10:30:09 -0400172 }
173
Peiyong Lin189021b2018-09-27 16:41:40 -0700174 if (colorMode == ColorMode::SRGB) {
175 mSurfaceColorType = SkColorType::kN32_SkColorType;
176 } else if (colorMode == ColorMode::WideColorGamut) {
177 mSurfaceColorType = SkColorType::kRGBA_F16_SkColorType;
178 }
179
Stan Iliev500a0c32016-10-26 10:30:09 -0400180 if (mEglSurface != EGL_NO_SURFACE) {
181 const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer);
182 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
183 return true;
184 }
185
186 return false;
187}
188
189bool SkiaOpenGLPipeline::isSurfaceReady() {
190 return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE);
191}
192
193bool SkiaOpenGLPipeline::isContextReady() {
194 return CC_LIKELY(mEglManager.hasEglContext());
195}
196
197void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
198 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
199 if (thread.eglManager().hasEglContext()) {
200 mode = DrawGlInfo::kModeProcess;
201 }
202
203 (*functor)(mode, nullptr);
204
205 // If there's no context we don't need to reset as there's no gl state to save/restore
206 if (mode != DrawGlInfo::kModeProcessNoContext) {
207 thread.getGrContext()->resetContext();
208 }
209}
210
Stan Iliev500a0c32016-10-26 10:30:09 -0400211} /* namespace skiapipeline */
212} /* namespace uirenderer */
213} /* namespace android */