Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 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" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 18 | #include <GrContext.h> |
| 19 | #include <private/hwui/DrawGlInfo.h> |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 20 | #include "FunctorDrawable.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 21 | #include "GlFunctorLifecycleListener.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 22 | #include "GrBackendSurface.h" |
| 23 | #include "GrRenderTarget.h" |
| 24 | #include "GrRenderTargetContext.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 25 | #include "RenderNode.h" |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 26 | #include "SkAndroidFrameworkUtils.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 27 | #include "SkClipStack.h" |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 28 | #include "SkRect.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace skiapipeline { |
| 33 | |
| 34 | GLFunctorDrawable::~GLFunctorDrawable() { |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 35 | if (auto lp = std::get_if<LegacyFunctor>(&mAnyFunctor)) { |
| 36 | if (lp->listener) { |
| 37 | lp->listener->onGlFunctorReleased(lp->functor); |
| 38 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 42 | static 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 Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 50 | static bool GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) { |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 51 | GrRenderTargetContext* renderTargetContext = |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 52 | 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 Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 58 | GrRenderTarget* renderTarget = renderTargetContext->accessRenderTarget(); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 59 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 75 | void GLFunctorDrawable::onDraw(SkCanvas* canvas) { |
| 76 | if (canvas->getGrContext() == nullptr) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 77 | // 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 87 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 95 | SkMatrix44 mat4(canvas->getTotalMatrix()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 96 | SkRegion clipRegion; |
Stan Iliev | 98d251b | 2017-01-19 18:08:38 -0500 | [diff] [blame] | 97 | canvas->temporary_internal_getRgnClip(&clipRegion); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 98 | |
| 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 Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 102 | // create an offscreen layer and clear it |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 103 | SkImageInfo surfaceInfo = |
| 104 | canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height()); |
| 105 | tmpSurface = |
| 106 | SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes, surfaceInfo); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 107 | tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 108 | |
| 109 | GrGLFramebufferInfo fboInfo; |
| 110 | if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess) |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 111 | .getGLFramebufferInfo(&fboInfo)) { |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 112 | 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 Liu | b6da7f6 | 2019-01-23 20:59:00 -0800 | [diff] [blame] | 141 | info.color_space_ptr = canvas->imageInfo().colorSpace(); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 142 | |
| 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 Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 145 | canvas->flush(); |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 146 | glViewport(0, 0, info.width, info.height); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 147 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 151 | if (CC_UNLIKELY(clipRegion.isComplex())) { |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 152 | // clear the stencil |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 153 | // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 154 | glDisable(GL_SCISSOR_TEST); |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 155 | glStencilMask(0x1); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 156 | glClearStencil(0); |
| 157 | glClear(GL_STENCIL_BUFFER_BIT); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 158 | |
| 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 Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 170 | // GL ops get inserted here if previous flush is missing, which could dirty the stencil |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 171 | bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 172 | tmpCanvas->flush(); // need this flush for the single op that draws into the stencil |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 173 | |
| 174 | // ensure that the framebuffer that the webview will render into is bound before after we |
| 175 | // draw into the stencil |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 176 | glViewport(0, 0, info.width, info.height); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 177 | glBindFramebuffer(GL_FRAMEBUFFER, fboID); |
| 178 | |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 179 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 187 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 188 | } 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 Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 197 | 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 Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 202 | |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 203 | if (clearStencilAfterFunctor) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 204 | // clear stencil buffer as it may be used by Skia |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 205 | glDisable(GL_SCISSOR_TEST); |
| 206 | glDisable(GL_STENCIL_TEST); |
| 207 | glStencilMask(0x1); |
| 208 | glClearStencil(0); |
| 209 | glClear(GL_STENCIL_BUFFER_BIT); |
| 210 | } |
| 211 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 212 | canvas->getGrContext()->resetContext(); |
Derek Sollenberger | 02456f0 | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 213 | |
| 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 227 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 228 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 229 | } // namespace skiapipeline |
| 230 | } // namespace uirenderer |
| 231 | } // namespace android |