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 <GrDirectContext.h>
19 #include <private/hwui/DrawGlInfo.h>
20 #include "FunctorDrawable.h"
21 #include "GrBackendSurface.h"
22 #include "RenderNode.h"
23 #include "SkAndroidFrameworkUtils.h"
24 #include "SkClipStack.h"
25 #include "SkRect.h"
26 #include "SkM44.h"
27 #include "utils/GLUtils.h"
28
29 namespace android {
30 namespace uirenderer {
31 namespace skiapipeline {
32
setScissor(int viewportHeight,const SkIRect & clip)33 static void setScissor(int viewportHeight, const SkIRect& clip) {
34 SkASSERT(!clip.isEmpty());
35 // transform to Y-flipped GL space, and prevent negatives
36 GLint y = viewportHeight - clip.fBottom;
37 GLint height = (viewportHeight - clip.fTop) - y;
38 glScissor(clip.fLeft, y, clip.width(), height);
39 }
40
GetFboDetails(SkCanvas * canvas,GLuint * outFboID,SkISize * outFboSize)41 static void GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) {
42 GrBackendRenderTarget renderTarget = canvas->topLayerBackendRenderTarget();
43 GrGLFramebufferInfo fboInfo;
44 LOG_ALWAYS_FATAL_IF(!renderTarget.getGLFramebufferInfo(&fboInfo),
45 "getGLFrameBufferInfo failed");
46
47 *outFboID = fboInfo.fFBOID;
48 *outFboSize = renderTarget.dimensions();
49 }
50
onDraw(SkCanvas * canvas)51 void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
52 GrDirectContext* directContext = GrAsDirectContext(canvas->recordingContext());
53 if (directContext == nullptr) {
54 // We're dumping a picture, render a light-blue rectangle instead
55 // TODO: Draw the WebView text on top? Seemingly complicated as SkPaint doesn't
56 // seem to have a default typeface that works. We only ever use drawGlyphs, which
57 // requires going through minikin & hwui's canvas which we don't have here.
58 SkPaint paint;
59 paint.setColor(0xFF81D4FA);
60 canvas->drawRect(mBounds, paint);
61 return;
62 }
63
64 // flush will create a GrRenderTarget if not already present.
65 canvas->flush();
66
67 GLuint fboID = 0;
68 SkISize fboSize;
69 GetFboDetails(canvas, &fboID, &fboSize);
70
71 SkIRect surfaceBounds = canvas->topLayerBounds();
72 SkIRect clipBounds = canvas->getDeviceClipBounds();
73 SkM44 mat4(canvas->getLocalToDevice());
74 SkRegion clipRegion;
75 canvas->temporary_internal_getRgnClip(&clipRegion);
76
77 sk_sp<SkSurface> tmpSurface;
78 // we are in a state where there is an unclipped saveLayer
79 if (fboID != 0 && !surfaceBounds.contains(clipBounds)) {
80 // create an offscreen layer and clear it
81 SkImageInfo surfaceInfo =
82 canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height());
83 tmpSurface =
84 SkSurface::MakeRenderTarget(directContext, SkBudgeted::kYes, surfaceInfo);
85 tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
86
87 GrGLFramebufferInfo fboInfo;
88 if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess)
89 .getGLFramebufferInfo(&fboInfo)) {
90 ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor");
91 return;
92 }
93
94 fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height());
95 fboID = fboInfo.fFBOID;
96
97 // update the matrix and clip that we pass to the WebView to match the coordinates of
98 // the offscreen layer
99 mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop);
100 clipBounds.offsetTo(0, 0);
101 clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop);
102
103 } else if (fboID != 0) {
104 // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix
105 // from device coordinates to the layer's coordinates
106 clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop);
107 mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop);
108 }
109
110 DrawGlInfo info;
111 info.clipLeft = clipBounds.fLeft;
112 info.clipTop = clipBounds.fTop;
113 info.clipRight = clipBounds.fRight;
114 info.clipBottom = clipBounds.fBottom;
115 info.isLayer = fboID != 0;
116 info.width = fboSize.width();
117 info.height = fboSize.height();
118 mat4.getColMajor(&info.transform[0]);
119 info.color_space_ptr = canvas->imageInfo().colorSpace();
120
121 // ensure that the framebuffer that the webview will render into is bound before we clear
122 // the stencil and/or draw the functor.
123 glViewport(0, 0, info.width, info.height);
124 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
125
126 // apply a simple clip with a scissor or a complex clip with a stencil
127 bool clearStencilAfterFunctor = false;
128 if (CC_UNLIKELY(clipRegion.isComplex())) {
129 // clear the stencil
130 // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
131 glDisable(GL_SCISSOR_TEST);
132 glStencilMask(0x1);
133 glClearStencil(0);
134 glClear(GL_STENCIL_BUFFER_BIT);
135
136 // notify Skia that we just updated the FBO and stencil
137 const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState;
138 directContext->resetContext(grState);
139
140 SkCanvas* tmpCanvas = canvas;
141 if (tmpSurface) {
142 tmpCanvas = tmpSurface->getCanvas();
143 // set the clip on the new canvas
144 tmpCanvas->clipRegion(clipRegion);
145 }
146
147 // GL ops get inserted here if previous flush is missing, which could dirty the stencil
148 bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas);
149 tmpCanvas->flush(); // need this flush for the single op that draws into the stencil
150
151 // ensure that the framebuffer that the webview will render into is bound before after we
152 // draw into the stencil
153 glViewport(0, 0, info.width, info.height);
154 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
155
156 if (stencilWritten) {
157 glStencilMask(0x1);
158 glStencilFunc(GL_EQUAL, 0x1, 0x1);
159 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
160 clearStencilAfterFunctor = true;
161 glEnable(GL_STENCIL_TEST);
162 } else {
163 glDisable(GL_STENCIL_TEST);
164 }
165 } else if (clipRegion.isEmpty()) {
166 glDisable(GL_STENCIL_TEST);
167 glDisable(GL_SCISSOR_TEST);
168 } else {
169 glDisable(GL_STENCIL_TEST);
170 glEnable(GL_SCISSOR_TEST);
171 setScissor(info.height, clipRegion.getBounds());
172 }
173
174 // WebView may swallow GL errors, so catch them here
175 GL_CHECKPOINT(LOW);
176 mWebViewHandle->drawGl(info);
177
178 if (clearStencilAfterFunctor) {
179 // clear stencil buffer as it may be used by Skia
180 glDisable(GL_SCISSOR_TEST);
181 glDisable(GL_STENCIL_TEST);
182 glStencilMask(0x1);
183 glClearStencil(0);
184 glClear(GL_STENCIL_BUFFER_BIT);
185 }
186
187 directContext->resetContext();
188
189 // if there were unclipped save layers involved we draw our offscreen surface to the canvas
190 if (tmpSurface) {
191 SkAutoCanvasRestore acr(canvas, true);
192 SkMatrix invertedMatrix;
193 if (!canvas->getTotalMatrix().invert(&invertedMatrix)) {
194 ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw");
195 return;
196 }
197 canvas->concat(invertedMatrix);
198
199 const SkIRect deviceBounds = canvas->getDeviceClipBounds();
200 tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop);
201 }
202 }
203
204 } // namespace skiapipeline
205 } // namespace uirenderer
206 } // namespace android
207