blob: 3684bc1e6a1f7231a7775554a2ec2d549fa37719 [file] [log] [blame]
Stan Iliev021693b2016-10-17 16:26:15 -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 "GLFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <GrContext.h>
19#include <private/hwui/DrawGlInfo.h>
Stan Iliev021693b2016-10-17 16:26:15 -040020#include "GlFunctorLifecycleListener.h"
21#include "RenderNode.h"
Stan Iliev6a3b05532017-08-01 18:51:37 -040022#include "SkAndroidFrameworkUtils.h"
Stan Iliev021693b2016-10-17 16:26:15 -040023#include "SkClipStack.h"
Derek Sollenberger0fba15b2018-05-30 18:08:57 -040024#include "SkRect.h"
25#include "GrBackendSurface.h"
26#include "GrRenderTarget.h"
27#include "GrRenderTargetContext.h"
28#include "GrGLTypes.h"
Stan Iliev021693b2016-10-17 16:26:15 -040029
30namespace android {
31namespace uirenderer {
32namespace skiapipeline {
33
34GLFunctorDrawable::~GLFunctorDrawable() {
John Reck1bcacfd2017-11-03 10:12:19 -070035 if (mListener.get() != nullptr) {
Stan Iliev021693b2016-10-17 16:26:15 -040036 mListener->onGlFunctorReleased(mFunctor);
37 }
38}
39
40void GLFunctorDrawable::syncFunctor() const {
41 (*mFunctor)(DrawGlInfo::kModeSync, nullptr);
42}
43
44static void setScissor(int viewportHeight, const SkIRect& clip) {
45 SkASSERT(!clip.isEmpty());
46 // transform to Y-flipped GL space, and prevent negatives
47 GLint y = viewportHeight - clip.fBottom;
48 GLint height = (viewportHeight - clip.fTop) - y;
49 glScissor(clip.fLeft, y, clip.width(), height);
50}
51
Derek Sollenberger0fba15b2018-05-30 18:08:57 -040052static bool GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) {
53 GrRenderTargetContext *renderTargetContext =
54 canvas->internal_private_accessTopLayerRenderTargetContext();
55 if (!renderTargetContext) {
56 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
57 return false;
58 }
59
60 GrRenderTarget *renderTarget = renderTargetContext->accessRenderTarget();
61 if (!renderTarget) {
62 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
63 return false;
64 }
65
66 GrBackendRenderTarget backendTarget = renderTarget->getBackendRenderTarget();
67 const GrGLFramebufferInfo* fboInfo = backendTarget.getGLFramebufferInfo();
68
69 if (!fboInfo) {
70 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
71 return false;
72 }
73
74 *outFboID = fboInfo->fFBOID;
75 *outFboSize = SkISize::Make(renderTargetContext->width(), renderTargetContext->height());
76 return true;
77}
78
Stan Iliev021693b2016-10-17 16:26:15 -040079void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
80 if (canvas->getGrContext() == nullptr) {
81 SkDEBUGF(("Attempting to draw GLFunctor into an unsupported surface"));
82 return;
83 }
84
Greg Daniel45ec62b2017-01-04 14:27:00 -050085 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
86 canvas->clear(SK_ColorRED);
87 return;
88 }
89
Derek Sollenberger0fba15b2018-05-30 18:08:57 -040090 GLuint fboID = 0;
91 SkISize fboSize;
92 if (!GetFboDetails(canvas, &fboID, &fboSize)) {
93 return;
94 }
95
96 SkIRect surfaceBounds = canvas->internal_private_getTopLayerBounds();
97 SkIRect clipBounds = canvas->getDeviceClipBounds();
Stan Iliev021693b2016-10-17 16:26:15 -040098 SkMatrix44 mat4(canvas->getTotalMatrix());
Stan Iliev021693b2016-10-17 16:26:15 -040099 SkRegion clipRegion;
Stan Iliev98d251b2017-01-19 18:08:38 -0500100 canvas->temporary_internal_getRgnClip(&clipRegion);
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400101
102 sk_sp<SkSurface> tmpSurface;
103 // we are in a state where there is an unclipped saveLayer
104 if (fboID != 0 && !surfaceBounds.contains(clipBounds)) {
105
106 // create an offscreen layer and clear it
107 SkImageInfo surfaceInfo = canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height());
108 tmpSurface = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes,
109 surfaceInfo);
110 tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
111
112 GrBackendObject backendObject;
113 if (!tmpSurface->getRenderTargetHandle(&backendObject, SkSurface::kFlushWrite_BackendHandleAccess)) {
114 ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor");
115 return;
116 }
117
118 fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height());
119 fboID = (GLuint)backendObject;
120
121 // update the matrix and clip that we pass to the WebView to match the coordinates of
122 // the offscreen layer
123 mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop, 0);
124 clipBounds.offsetTo(0, 0);
125 clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop);
126
127 } else if (fboID != 0) {
128 // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix
129 // from device coordinates to the layer's coordinates
130 clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop);
131 mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop, 0);
132 }
133
134 DrawGlInfo info;
135 info.clipLeft = clipBounds.fLeft;
136 info.clipTop = clipBounds.fTop;
137 info.clipRight = clipBounds.fRight;
138 info.clipBottom = clipBounds.fBottom;
139 info.isLayer = fboID != 0;
140 info.width = fboSize.width();
141 info.height = fboSize.height();
142 mat4.asColMajorf(&info.transform[0]);
143
144 // ensure that the framebuffer that the webview will render into is bound before we clear
145 // the stencil and/or draw the functor.
Stan Iliev357c63d2018-05-23 15:29:09 -0400146 canvas->flush();
Stan Iliev357c63d2018-05-23 15:29:09 -0400147 glViewport(0, 0, info.width, info.height);
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400148 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
149
150 // apply a simple clip with a scissor or a complex clip with a stencil
151 bool clearStencilAfterFunctor = false;
Stan Iliev021693b2016-10-17 16:26:15 -0400152 if (CC_UNLIKELY(clipRegion.isComplex())) {
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400153 // clear the stencil
Stan Iliev357c63d2018-05-23 15:29:09 -0400154 //TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
Stan Iliev021693b2016-10-17 16:26:15 -0400155 glDisable(GL_SCISSOR_TEST);
Stan Iliev6a3b05532017-08-01 18:51:37 -0400156 glStencilMask(0x1);
Stan Iliev021693b2016-10-17 16:26:15 -0400157 glClearStencil(0);
158 glClear(GL_STENCIL_BUFFER_BIT);
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400159
160 // notify Skia that we just updated the FBO and stencil
161 const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState;
162 canvas->getGrContext()->resetContext(grState);
163
164 SkCanvas* tmpCanvas = canvas;
165 if (tmpSurface) {
166 tmpCanvas = tmpSurface->getCanvas();
167 // set the clip on the new canvas
168 tmpCanvas->clipRegion(clipRegion);
169 }
170
Stan Iliev357c63d2018-05-23 15:29:09 -0400171 // GL ops get inserted here if previous flush is missing, which could dirty the stencil
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400172 bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas);
173 tmpCanvas->flush(); //need this flush for the single op that draws into the stencil
174
175 // ensure that the framebuffer that the webview will render into is bound before after we
176 // draw into the stencil
Stan Iliev357c63d2018-05-23 15:29:09 -0400177 glViewport(0, 0, info.width, info.height);
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400178 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
179
Stan Iliev6a3b05532017-08-01 18:51:37 -0400180 if (stencilWritten) {
181 glStencilMask(0x1);
182 glStencilFunc(GL_EQUAL, 0x1, 0x1);
183 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
184 clearStencilAfterFunctor = true;
185 glEnable(GL_STENCIL_TEST);
186 } else {
187 glDisable(GL_STENCIL_TEST);
Stan Iliev021693b2016-10-17 16:26:15 -0400188 }
Stan Iliev021693b2016-10-17 16:26:15 -0400189 } else if (clipRegion.isEmpty()) {
190 glDisable(GL_STENCIL_TEST);
191 glDisable(GL_SCISSOR_TEST);
192 } else {
193 glDisable(GL_STENCIL_TEST);
194 glEnable(GL_SCISSOR_TEST);
195 setScissor(info.height, clipRegion.getBounds());
196 }
197
198 (*mFunctor)(DrawGlInfo::kModeDraw, &info);
199
Stan Iliev6a3b05532017-08-01 18:51:37 -0400200 if (clearStencilAfterFunctor) {
John Reck1bcacfd2017-11-03 10:12:19 -0700201 // clear stencil buffer as it may be used by Skia
Stan Iliev6a3b05532017-08-01 18:51:37 -0400202 glDisable(GL_SCISSOR_TEST);
203 glDisable(GL_STENCIL_TEST);
204 glStencilMask(0x1);
205 glClearStencil(0);
206 glClear(GL_STENCIL_BUFFER_BIT);
207 }
208
Stan Iliev021693b2016-10-17 16:26:15 -0400209 canvas->getGrContext()->resetContext();
Derek Sollenberger0fba15b2018-05-30 18:08:57 -0400210
211 // if there were unclipped save layers involved we draw our offscreen surface to the canvas
212 if (tmpSurface) {
213 SkAutoCanvasRestore acr(canvas, true);
214 SkMatrix invertedMatrix;
215 if (!canvas->getTotalMatrix().invert(&invertedMatrix)) {
216 ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw");
217 return;
218 }
219 canvas->concat(invertedMatrix);
220
221 const SkIRect deviceBounds = canvas->getDeviceClipBounds();
222 tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop, nullptr);
223 }
John Reck1bcacfd2017-11-03 10:12:19 -0700224}
Stan Iliev021693b2016-10-17 16:26:15 -0400225
John Reck1bcacfd2017-11-03 10:12:19 -0700226}; // namespace skiapipeline
227}; // namespace uirenderer
228}; // namespace android