blob: a1b2b18195bc3ea11972bd35ae3d77961e2e9603 [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>
John Reck283bb462018-12-13 16:40:14 -080020#include "FunctorDrawable.h"
Stan Iliev021693b2016-10-17 16:26:15 -040021#include "GlFunctorLifecycleListener.h"
John Reck283bb462018-12-13 16:40:14 -080022#include "GrBackendSurface.h"
23#include "GrRenderTarget.h"
24#include "GrRenderTargetContext.h"
Stan Iliev021693b2016-10-17 16:26:15 -040025#include "RenderNode.h"
Stan Iliev6a3b05532017-08-01 18:51:37 -040026#include "SkAndroidFrameworkUtils.h"
Stan Iliev021693b2016-10-17 16:26:15 -040027#include "SkClipStack.h"
Derek Sollenberger02456f02018-05-30 18:08:57 -040028#include "SkRect.h"
Stan Iliev021693b2016-10-17 16:26:15 -040029
30namespace android {
31namespace uirenderer {
32namespace skiapipeline {
33
34GLFunctorDrawable::~GLFunctorDrawable() {
John Reck283bb462018-12-13 16:40:14 -080035 if (auto lp = std::get_if<LegacyFunctor>(&mAnyFunctor)) {
36 if (lp->listener) {
37 lp->listener->onGlFunctorReleased(lp->functor);
38 }
Stan Iliev021693b2016-10-17 16:26:15 -040039 }
40}
41
Stan Iliev021693b2016-10-17 16:26:15 -040042static void setScissor(int viewportHeight, const SkIRect& clip) {
43 SkASSERT(!clip.isEmpty());
44 // transform to Y-flipped GL space, and prevent negatives
45 GLint y = viewportHeight - clip.fBottom;
46 GLint height = (viewportHeight - clip.fTop) - y;
47 glScissor(clip.fLeft, y, clip.width(), height);
48}
49
Derek Sollenberger02456f02018-05-30 18:08:57 -040050static bool GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) {
John Reck283bb462018-12-13 16:40:14 -080051 GrRenderTargetContext* renderTargetContext =
Derek Sollenberger02456f02018-05-30 18:08:57 -040052 canvas->internal_private_accessTopLayerRenderTargetContext();
53 if (!renderTargetContext) {
54 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
55 return false;
56 }
57
John Reck283bb462018-12-13 16:40:14 -080058 GrRenderTarget* renderTarget = renderTargetContext->accessRenderTarget();
Derek Sollenberger02456f02018-05-30 18:08:57 -040059 if (!renderTarget) {
60 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
61 return false;
62 }
63
64 GrGLFramebufferInfo fboInfo;
65 if (!renderTarget->getBackendRenderTarget().getGLFramebufferInfo(&fboInfo)) {
66 ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw");
67 return false;
68 }
69
70 *outFboID = fboInfo.fFBOID;
71 *outFboSize = SkISize::Make(renderTargetContext->width(), renderTargetContext->height());
72 return true;
73}
74
Stan Iliev021693b2016-10-17 16:26:15 -040075void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
76 if (canvas->getGrContext() == nullptr) {
John Reck5cca8f22018-12-10 17:06:22 -080077 // We're dumping a picture, render a light-blue rectangle instead
78 // TODO: Draw the WebView text on top? Seemingly complicated as SkPaint doesn't
79 // seem to have a default typeface that works. We only ever use drawGlyphs, which
80 // requires going through minikin & hwui's canvas which we don't have here.
81 SkPaint paint;
82 paint.setColor(0xFF81D4FA);
83 canvas->drawRect(mBounds, paint);
Stan Iliev021693b2016-10-17 16:26:15 -040084 return;
85 }
86
Derek Sollenberger02456f02018-05-30 18:08:57 -040087 GLuint fboID = 0;
88 SkISize fboSize;
89 if (!GetFboDetails(canvas, &fboID, &fboSize)) {
90 return;
91 }
92
93 SkIRect surfaceBounds = canvas->internal_private_getTopLayerBounds();
94 SkIRect clipBounds = canvas->getDeviceClipBounds();
Stan Iliev021693b2016-10-17 16:26:15 -040095 SkMatrix44 mat4(canvas->getTotalMatrix());
Stan Iliev021693b2016-10-17 16:26:15 -040096 SkRegion clipRegion;
Stan Iliev98d251b2017-01-19 18:08:38 -050097 canvas->temporary_internal_getRgnClip(&clipRegion);
Derek Sollenberger02456f02018-05-30 18:08:57 -040098
99 sk_sp<SkSurface> tmpSurface;
100 // we are in a state where there is an unclipped saveLayer
101 if (fboID != 0 && !surfaceBounds.contains(clipBounds)) {
Derek Sollenberger02456f02018-05-30 18:08:57 -0400102 // create an offscreen layer and clear it
John Reck283bb462018-12-13 16:40:14 -0800103 SkImageInfo surfaceInfo =
104 canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height());
105 tmpSurface =
106 SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes, surfaceInfo);
Derek Sollenberger02456f02018-05-30 18:08:57 -0400107 tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
108
109 GrGLFramebufferInfo fboInfo;
110 if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess)
John Reck283bb462018-12-13 16:40:14 -0800111 .getGLFramebufferInfo(&fboInfo)) {
Derek Sollenberger02456f02018-05-30 18:08:57 -0400112 ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor");
113 return;
114 }
115
116 fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height());
117 fboID = fboInfo.fFBOID;
118
119 // update the matrix and clip that we pass to the WebView to match the coordinates of
120 // the offscreen layer
121 mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop, 0);
122 clipBounds.offsetTo(0, 0);
123 clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop);
124
125 } else if (fboID != 0) {
126 // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix
127 // from device coordinates to the layer's coordinates
128 clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop);
129 mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop, 0);
130 }
131
132 DrawGlInfo info;
133 info.clipLeft = clipBounds.fLeft;
134 info.clipTop = clipBounds.fTop;
135 info.clipRight = clipBounds.fRight;
136 info.clipBottom = clipBounds.fBottom;
137 info.isLayer = fboID != 0;
138 info.width = fboSize.width();
139 info.height = fboSize.height();
140 mat4.asColMajorf(&info.transform[0]);
Bo Liub6da7f62019-01-23 20:59:00 -0800141 info.color_space_ptr = canvas->imageInfo().colorSpace();
Derek Sollenberger02456f02018-05-30 18:08:57 -0400142
143 // ensure that the framebuffer that the webview will render into is bound before we clear
144 // the stencil and/or draw the functor.
Stan Iliev357c63d2018-05-23 15:29:09 -0400145 canvas->flush();
Stan Iliev357c63d2018-05-23 15:29:09 -0400146 glViewport(0, 0, info.width, info.height);
Derek Sollenberger02456f02018-05-30 18:08:57 -0400147 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
148
149 // apply a simple clip with a scissor or a complex clip with a stencil
150 bool clearStencilAfterFunctor = false;
Stan Iliev021693b2016-10-17 16:26:15 -0400151 if (CC_UNLIKELY(clipRegion.isComplex())) {
Derek Sollenberger02456f02018-05-30 18:08:57 -0400152 // clear the stencil
John Reck283bb462018-12-13 16:40:14 -0800153 // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
Stan Iliev021693b2016-10-17 16:26:15 -0400154 glDisable(GL_SCISSOR_TEST);
Stan Iliev6a3b05532017-08-01 18:51:37 -0400155 glStencilMask(0x1);
Stan Iliev021693b2016-10-17 16:26:15 -0400156 glClearStencil(0);
157 glClear(GL_STENCIL_BUFFER_BIT);
Derek Sollenberger02456f02018-05-30 18:08:57 -0400158
159 // notify Skia that we just updated the FBO and stencil
160 const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState;
161 canvas->getGrContext()->resetContext(grState);
162
163 SkCanvas* tmpCanvas = canvas;
164 if (tmpSurface) {
165 tmpCanvas = tmpSurface->getCanvas();
166 // set the clip on the new canvas
167 tmpCanvas->clipRegion(clipRegion);
168 }
169
Stan Iliev357c63d2018-05-23 15:29:09 -0400170 // GL ops get inserted here if previous flush is missing, which could dirty the stencil
Derek Sollenberger02456f02018-05-30 18:08:57 -0400171 bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas);
John Reck283bb462018-12-13 16:40:14 -0800172 tmpCanvas->flush(); // need this flush for the single op that draws into the stencil
Derek Sollenberger02456f02018-05-30 18:08:57 -0400173
174 // ensure that the framebuffer that the webview will render into is bound before after we
175 // draw into the stencil
Stan Iliev357c63d2018-05-23 15:29:09 -0400176 glViewport(0, 0, info.width, info.height);
Derek Sollenberger02456f02018-05-30 18:08:57 -0400177 glBindFramebuffer(GL_FRAMEBUFFER, fboID);
178
Stan Iliev6a3b05532017-08-01 18:51:37 -0400179 if (stencilWritten) {
180 glStencilMask(0x1);
181 glStencilFunc(GL_EQUAL, 0x1, 0x1);
182 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
183 clearStencilAfterFunctor = true;
184 glEnable(GL_STENCIL_TEST);
185 } else {
186 glDisable(GL_STENCIL_TEST);
Stan Iliev021693b2016-10-17 16:26:15 -0400187 }
Stan Iliev021693b2016-10-17 16:26:15 -0400188 } else if (clipRegion.isEmpty()) {
189 glDisable(GL_STENCIL_TEST);
190 glDisable(GL_SCISSOR_TEST);
191 } else {
192 glDisable(GL_STENCIL_TEST);
193 glEnable(GL_SCISSOR_TEST);
194 setScissor(info.height, clipRegion.getBounds());
195 }
196
John Reck283bb462018-12-13 16:40:14 -0800197 if (mAnyFunctor.index() == 0) {
198 std::get<0>(mAnyFunctor).handle->drawGl(info);
199 } else {
200 (*(std::get<1>(mAnyFunctor).functor))(DrawGlInfo::kModeDraw, &info);
201 }
Stan Iliev021693b2016-10-17 16:26:15 -0400202
Stan Iliev6a3b05532017-08-01 18:51:37 -0400203 if (clearStencilAfterFunctor) {
John Reck1bcacfd2017-11-03 10:12:19 -0700204 // clear stencil buffer as it may be used by Skia
Stan Iliev6a3b05532017-08-01 18:51:37 -0400205 glDisable(GL_SCISSOR_TEST);
206 glDisable(GL_STENCIL_TEST);
207 glStencilMask(0x1);
208 glClearStencil(0);
209 glClear(GL_STENCIL_BUFFER_BIT);
210 }
211
Stan Iliev021693b2016-10-17 16:26:15 -0400212 canvas->getGrContext()->resetContext();
Derek Sollenberger02456f02018-05-30 18:08:57 -0400213
214 // if there were unclipped save layers involved we draw our offscreen surface to the canvas
215 if (tmpSurface) {
216 SkAutoCanvasRestore acr(canvas, true);
217 SkMatrix invertedMatrix;
218 if (!canvas->getTotalMatrix().invert(&invertedMatrix)) {
219 ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw");
220 return;
221 }
222 canvas->concat(invertedMatrix);
223
224 const SkIRect deviceBounds = canvas->getDeviceClipBounds();
225 tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop, nullptr);
226 }
John Reck1bcacfd2017-11-03 10:12:19 -0700227}
Stan Iliev021693b2016-10-17 16:26:15 -0400228
Chris Blume7b8a8082018-11-30 15:51:58 -0800229} // namespace skiapipeline
230} // namespace uirenderer
231} // namespace android