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