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