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 "GLFunctorDrawable.h"
18 #include <GrContext.h>
19 #include <private/hwui/DrawGlInfo.h>
20 #include "GlFunctorLifecycleListener.h"
21 #include "RenderNode.h"
22 #include "SkAndroidFrameworkUtils.h"
23 #include "SkClipStack.h"
24 #include "SkRect.h"
25 #include "GrBackendSurface.h"
26 #include "GrRenderTarget.h"
27 #include "GrRenderTargetContext.h"
28 #include "GrGLTypes.h"
29 
30 namespace android {
31 namespace uirenderer {
32 namespace skiapipeline {
33 
~GLFunctorDrawable()34 GLFunctorDrawable::~GLFunctorDrawable() {
35     if (mListener.get() != nullptr) {
36         mListener->onGlFunctorReleased(mFunctor);
37     }
38 }
39 
syncFunctor() const40 void GLFunctorDrawable::syncFunctor() const {
41     (*mFunctor)(DrawGlInfo::kModeSync, nullptr);
42 }
43 
setScissor(int viewportHeight,const SkIRect & clip)44 static 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 
GetFboDetails(SkCanvas * canvas,GLuint * outFboID,SkISize * outFboSize)52 static 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 
onDraw(SkCanvas * canvas)79 void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
80     if (canvas->getGrContext() == nullptr) {
81         SkDEBUGF(("Attempting to draw GLFunctor into an unsupported surface"));
82         return;
83     }
84 
85     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
86         canvas->clear(SK_ColorRED);
87         return;
88     }
89 
90     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();
98     SkMatrix44 mat4(canvas->getTotalMatrix());
99     SkRegion clipRegion;
100     canvas->temporary_internal_getRgnClip(&clipRegion);
101 
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.
146     canvas->flush();
147     glViewport(0, 0, info.width, info.height);
148     glBindFramebuffer(GL_FRAMEBUFFER, fboID);
149 
150     // apply a simple clip with a scissor or a complex clip with a stencil
151     bool clearStencilAfterFunctor = false;
152     if (CC_UNLIKELY(clipRegion.isComplex())) {
153         // clear the stencil
154         //TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
155         glDisable(GL_SCISSOR_TEST);
156         glStencilMask(0x1);
157         glClearStencil(0);
158         glClear(GL_STENCIL_BUFFER_BIT);
159 
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 
171         // GL ops get inserted here if previous flush is missing, which could dirty the stencil
172         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
177         glViewport(0, 0, info.width, info.height);
178         glBindFramebuffer(GL_FRAMEBUFFER, fboID);
179 
180         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);
188         }
189     } 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 
200     if (clearStencilAfterFunctor) {
201         // clear stencil buffer as it may be used by Skia
202         glDisable(GL_SCISSOR_TEST);
203         glDisable(GL_STENCIL_TEST);
204         glStencilMask(0x1);
205         glClearStencil(0);
206         glClear(GL_STENCIL_BUFFER_BIT);
207     }
208 
209     canvas->getGrContext()->resetContext();
210 
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     }
224 }
225 
226 };  // namespace skiapipeline
227 };  // namespace uirenderer
228 };  // namespace android
229