| 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 | |
| 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 | |
| 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 | |
| 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 | // canvas may be an AlphaFilterCanvas, which is intended to draw with a |
| 65 | // modified alpha. We do not have a way to do this without drawing into an |
| 66 | // extra layer, which would have a performance cost. Draw directly into the |
| 67 | // underlying gpu canvas. This matches prior behavior and the behavior in |
| 68 | // Vulkan. |
| 69 | { |
| 70 | auto* gpuCanvas = SkAndroidFrameworkUtils::getBaseWrappedCanvas(canvas); |
| 71 | LOG_ALWAYS_FATAL_IF(!gpuCanvas, "GLFunctorDrawable::onDraw is using an invalid canvas!"); |
| 72 | canvas = gpuCanvas; |
| 73 | } |
| 74 | |
| 75 | // flush will create a GrRenderTarget if not already present. |
| 76 | canvas->flush(); |
| 77 | |
| 78 | GLuint fboID = 0; |
| 79 | SkISize fboSize; |
| 80 | GetFboDetails(canvas, &fboID, &fboSize); |
| 81 | |
| 82 | SkIRect surfaceBounds = canvas->topLayerBounds(); |
| 83 | SkIRect clipBounds = canvas->getDeviceClipBounds(); |
| 84 | SkM44 mat4(canvas->getLocalToDevice()); |
| 85 | SkRegion clipRegion; |
| 86 | canvas->temporary_internal_getRgnClip(&clipRegion); |
| 87 | |
| 88 | sk_sp<SkSurface> tmpSurface; |
| 89 | // we are in a state where there is an unclipped saveLayer |
| 90 | if (fboID != 0 && !surfaceBounds.contains(clipBounds)) { |
| 91 | // create an offscreen layer and clear it |
| 92 | SkImageInfo surfaceInfo = |
| 93 | canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height()); |
| 94 | tmpSurface = |
| 95 | SkSurface::MakeRenderTarget(directContext, SkBudgeted::kYes, surfaceInfo); |
| 96 | tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 97 | |
| 98 | GrGLFramebufferInfo fboInfo; |
| 99 | if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess) |
| 100 | .getGLFramebufferInfo(&fboInfo)) { |
| 101 | ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height()); |
| 106 | fboID = fboInfo.fFBOID; |
| 107 | |
| 108 | // update the matrix and clip that we pass to the WebView to match the coordinates of |
| 109 | // the offscreen layer |
| 110 | mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop); |
| 111 | clipBounds.offsetTo(0, 0); |
| 112 | clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop); |
| 113 | |
| 114 | } else if (fboID != 0) { |
| 115 | // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix |
| 116 | // from device coordinates to the layer's coordinates |
| 117 | clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop); |
| 118 | mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop); |
| 119 | } |
| 120 | |
| 121 | DrawGlInfo info; |
| 122 | info.clipLeft = clipBounds.fLeft; |
| 123 | info.clipTop = clipBounds.fTop; |
| 124 | info.clipRight = clipBounds.fRight; |
| 125 | info.clipBottom = clipBounds.fBottom; |
| 126 | info.isLayer = fboID != 0; |
| 127 | info.width = fboSize.width(); |
| 128 | info.height = fboSize.height(); |
| 129 | mat4.getColMajor(&info.transform[0]); |
| 130 | info.color_space_ptr = canvas->imageInfo().colorSpace(); |
| 131 | |
| 132 | // ensure that the framebuffer that the webview will render into is bound before we clear |
| 133 | // the stencil and/or draw the functor. |
| 134 | glViewport(0, 0, info.width, info.height); |
| 135 | glBindFramebuffer(GL_FRAMEBUFFER, fboID); |
| 136 | |
| 137 | // apply a simple clip with a scissor or a complex clip with a stencil |
| 138 | bool clearStencilAfterFunctor = false; |
| 139 | if (CC_UNLIKELY(clipRegion.isComplex())) { |
| 140 | // clear the stencil |
| 141 | // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil |
| 142 | glDisable(GL_SCISSOR_TEST); |
| 143 | glStencilMask(0x1); |
| 144 | glClearStencil(0); |
| 145 | glClear(GL_STENCIL_BUFFER_BIT); |
| 146 | |
| 147 | // notify Skia that we just updated the FBO and stencil |
| 148 | const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState; |
| 149 | directContext->resetContext(grState); |
| 150 | |
| 151 | SkCanvas* tmpCanvas = canvas; |
| 152 | if (tmpSurface) { |
| 153 | tmpCanvas = tmpSurface->getCanvas(); |
| 154 | // set the clip on the new canvas |
| 155 | tmpCanvas->clipRegion(clipRegion); |
| 156 | } |
| 157 | |
| 158 | // GL ops get inserted here if previous flush is missing, which could dirty the stencil |
| 159 | bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas); |
| 160 | tmpCanvas->flush(); // need this flush for the single op that draws into the stencil |
| 161 | |
| 162 | // ensure that the framebuffer that the webview will render into is bound before after we |
| 163 | // draw into the stencil |
| 164 | glViewport(0, 0, info.width, info.height); |
| 165 | glBindFramebuffer(GL_FRAMEBUFFER, fboID); |
| 166 | |
| 167 | if (stencilWritten) { |
| 168 | glStencilMask(0x1); |
| 169 | glStencilFunc(GL_EQUAL, 0x1, 0x1); |
| 170 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 171 | clearStencilAfterFunctor = true; |
| 172 | glEnable(GL_STENCIL_TEST); |
| 173 | } else { |
| 174 | glDisable(GL_STENCIL_TEST); |
| 175 | } |
| 176 | } else if (clipRegion.isEmpty()) { |
| 177 | glDisable(GL_STENCIL_TEST); |
| 178 | glDisable(GL_SCISSOR_TEST); |
| 179 | } else { |
| 180 | glDisable(GL_STENCIL_TEST); |
| 181 | glEnable(GL_SCISSOR_TEST); |
| 182 | setScissor(info.height, clipRegion.getBounds()); |
| 183 | } |
| 184 | |
| 185 | // WebView may swallow GL errors, so catch them here |
| 186 | GL_CHECKPOINT(LOW); |
| 187 | mWebViewHandle->drawGl(info); |
| 188 | |
| 189 | if (clearStencilAfterFunctor) { |
| 190 | // clear stencil buffer as it may be used by Skia |
| 191 | glDisable(GL_SCISSOR_TEST); |
| 192 | glDisable(GL_STENCIL_TEST); |
| 193 | glStencilMask(0x1); |
| 194 | glClearStencil(0); |
| 195 | glClear(GL_STENCIL_BUFFER_BIT); |
| 196 | } |
| 197 | |
| 198 | directContext->resetContext(); |
| 199 | |
| 200 | // if there were unclipped save layers involved we draw our offscreen surface to the canvas |
| 201 | if (tmpSurface) { |
| 202 | SkAutoCanvasRestore acr(canvas, true); |
| 203 | SkMatrix invertedMatrix; |
| 204 | if (!canvas->getTotalMatrix().invert(&invertedMatrix)) { |
| 205 | ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw"); |
| 206 | return; |
| 207 | } |
| 208 | canvas->concat(invertedMatrix); |
| 209 | |
| 210 | const SkIRect deviceBounds = canvas->getDeviceClipBounds(); |
| 211 | tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | } // namespace skiapipeline |
| 216 | } // namespace uirenderer |
| 217 | } // namespace android |