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> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 20 | #include "GlFunctorLifecycleListener.h" |
| 21 | #include "RenderNode.h" |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 22 | #include "SkAndroidFrameworkUtils.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 23 | #include "SkClipStack.h" |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 24 | #include "SkRect.h" |
| 25 | #include "GrBackendSurface.h" |
| 26 | #include "GrRenderTarget.h" |
| 27 | #include "GrRenderTargetContext.h" |
| 28 | #include "GrGLTypes.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 | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | if (mListener.get() != nullptr) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 36 | mListener->onGlFunctorReleased(mFunctor); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void GLFunctorDrawable::syncFunctor() const { |
| 41 | (*mFunctor)(DrawGlInfo::kModeSync, nullptr); |
| 42 | } |
| 43 | |
| 44 | static void setScissor(int viewportHeight, const SkIRect& clip) { |
| 45 | SkASSERT(!clip.isEmpty()); |
| 46 | // transform to Y-flipped GL space, and prevent negatives |
| 47 | GLint y = viewportHeight - clip.fBottom; |
| 48 | GLint height = (viewportHeight - clip.fTop) - y; |
| 49 | glScissor(clip.fLeft, y, clip.width(), height); |
| 50 | } |
| 51 | |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 52 | static bool GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) { |
| 53 | GrRenderTargetContext *renderTargetContext = |
| 54 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 55 | if (!renderTargetContext) { |
| 56 | ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw"); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | GrRenderTarget *renderTarget = renderTargetContext->accessRenderTarget(); |
| 61 | if (!renderTarget) { |
| 62 | ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw"); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | GrBackendRenderTarget backendTarget = renderTarget->getBackendRenderTarget(); |
| 67 | const GrGLFramebufferInfo* fboInfo = backendTarget.getGLFramebufferInfo(); |
| 68 | |
| 69 | if (!fboInfo) { |
| 70 | ALOGW("Unable to extract renderTarget info from canvas; aborting GLFunctor draw"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | *outFboID = fboInfo->fFBOID; |
| 75 | *outFboSize = SkISize::Make(renderTargetContext->width(), renderTargetContext->height()); |
| 76 | return true; |
| 77 | } |
| 78 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 79 | void GLFunctorDrawable::onDraw(SkCanvas* canvas) { |
| 80 | if (canvas->getGrContext() == nullptr) { |
| 81 | SkDEBUGF(("Attempting to draw GLFunctor into an unsupported surface")); |
| 82 | return; |
| 83 | } |
| 84 | |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 85 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) { |
| 86 | canvas->clear(SK_ColorRED); |
| 87 | return; |
| 88 | } |
| 89 | |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 90 | GLuint fboID = 0; |
| 91 | SkISize fboSize; |
| 92 | if (!GetFboDetails(canvas, &fboID, &fboSize)) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | SkIRect surfaceBounds = canvas->internal_private_getTopLayerBounds(); |
| 97 | SkIRect clipBounds = canvas->getDeviceClipBounds(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 98 | SkMatrix44 mat4(canvas->getTotalMatrix()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 99 | SkRegion clipRegion; |
Stan Iliev | 98d251b | 2017-01-19 18:08:38 -0500 | [diff] [blame] | 100 | canvas->temporary_internal_getRgnClip(&clipRegion); |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 101 | |
| 102 | sk_sp<SkSurface> tmpSurface; |
| 103 | // we are in a state where there is an unclipped saveLayer |
| 104 | if (fboID != 0 && !surfaceBounds.contains(clipBounds)) { |
| 105 | |
| 106 | // create an offscreen layer and clear it |
| 107 | SkImageInfo surfaceInfo = canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height()); |
| 108 | tmpSurface = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes, |
| 109 | surfaceInfo); |
| 110 | tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 111 | |
| 112 | GrBackendObject backendObject; |
| 113 | if (!tmpSurface->getRenderTargetHandle(&backendObject, SkSurface::kFlushWrite_BackendHandleAccess)) { |
| 114 | ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height()); |
| 119 | fboID = (GLuint)backendObject; |
| 120 | |
| 121 | // update the matrix and clip that we pass to the WebView to match the coordinates of |
| 122 | // the offscreen layer |
| 123 | mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop, 0); |
| 124 | clipBounds.offsetTo(0, 0); |
| 125 | clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop); |
| 126 | |
| 127 | } else if (fboID != 0) { |
| 128 | // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix |
| 129 | // from device coordinates to the layer's coordinates |
| 130 | clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop); |
| 131 | mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop, 0); |
| 132 | } |
| 133 | |
| 134 | DrawGlInfo info; |
| 135 | info.clipLeft = clipBounds.fLeft; |
| 136 | info.clipTop = clipBounds.fTop; |
| 137 | info.clipRight = clipBounds.fRight; |
| 138 | info.clipBottom = clipBounds.fBottom; |
| 139 | info.isLayer = fboID != 0; |
| 140 | info.width = fboSize.width(); |
| 141 | info.height = fboSize.height(); |
| 142 | mat4.asColMajorf(&info.transform[0]); |
| 143 | |
| 144 | // ensure that the framebuffer that the webview will render into is bound before we clear |
| 145 | // the stencil and/or draw the functor. |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 146 | canvas->flush(); |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 147 | glViewport(0, 0, info.width, info.height); |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 148 | glBindFramebuffer(GL_FRAMEBUFFER, fboID); |
| 149 | |
| 150 | // apply a simple clip with a scissor or a complex clip with a stencil |
| 151 | bool clearStencilAfterFunctor = false; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 152 | if (CC_UNLIKELY(clipRegion.isComplex())) { |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 153 | // clear the stencil |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 154 | //TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 155 | glDisable(GL_SCISSOR_TEST); |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 156 | glStencilMask(0x1); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 157 | glClearStencil(0); |
| 158 | glClear(GL_STENCIL_BUFFER_BIT); |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 159 | |
| 160 | // notify Skia that we just updated the FBO and stencil |
| 161 | const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState; |
| 162 | canvas->getGrContext()->resetContext(grState); |
| 163 | |
| 164 | SkCanvas* tmpCanvas = canvas; |
| 165 | if (tmpSurface) { |
| 166 | tmpCanvas = tmpSurface->getCanvas(); |
| 167 | // set the clip on the new canvas |
| 168 | tmpCanvas->clipRegion(clipRegion); |
| 169 | } |
| 170 | |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 171 | // GL ops get inserted here if previous flush is missing, which could dirty the stencil |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 172 | bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas); |
| 173 | tmpCanvas->flush(); //need this flush for the single op that draws into the stencil |
| 174 | |
| 175 | // ensure that the framebuffer that the webview will render into is bound before after we |
| 176 | // draw into the stencil |
Stan Iliev | 357c63d | 2018-05-23 15:29:09 -0400 | [diff] [blame] | 177 | glViewport(0, 0, info.width, info.height); |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 178 | glBindFramebuffer(GL_FRAMEBUFFER, fboID); |
| 179 | |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 180 | if (stencilWritten) { |
| 181 | glStencilMask(0x1); |
| 182 | glStencilFunc(GL_EQUAL, 0x1, 0x1); |
| 183 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 184 | clearStencilAfterFunctor = true; |
| 185 | glEnable(GL_STENCIL_TEST); |
| 186 | } else { |
| 187 | glDisable(GL_STENCIL_TEST); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 188 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 189 | } else if (clipRegion.isEmpty()) { |
| 190 | glDisable(GL_STENCIL_TEST); |
| 191 | glDisable(GL_SCISSOR_TEST); |
| 192 | } else { |
| 193 | glDisable(GL_STENCIL_TEST); |
| 194 | glEnable(GL_SCISSOR_TEST); |
| 195 | setScissor(info.height, clipRegion.getBounds()); |
| 196 | } |
| 197 | |
| 198 | (*mFunctor)(DrawGlInfo::kModeDraw, &info); |
| 199 | |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 200 | if (clearStencilAfterFunctor) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 201 | // clear stencil buffer as it may be used by Skia |
Stan Iliev | 6a3b0553 | 2017-08-01 18:51:37 -0400 | [diff] [blame] | 202 | glDisable(GL_SCISSOR_TEST); |
| 203 | glDisable(GL_STENCIL_TEST); |
| 204 | glStencilMask(0x1); |
| 205 | glClearStencil(0); |
| 206 | glClear(GL_STENCIL_BUFFER_BIT); |
| 207 | } |
| 208 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 209 | canvas->getGrContext()->resetContext(); |
Derek Sollenberger | 0fba15b | 2018-05-30 18:08:57 -0400 | [diff] [blame] | 210 | |
| 211 | // if there were unclipped save layers involved we draw our offscreen surface to the canvas |
| 212 | if (tmpSurface) { |
| 213 | SkAutoCanvasRestore acr(canvas, true); |
| 214 | SkMatrix invertedMatrix; |
| 215 | if (!canvas->getTotalMatrix().invert(&invertedMatrix)) { |
| 216 | ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw"); |
| 217 | return; |
| 218 | } |
| 219 | canvas->concat(invertedMatrix); |
| 220 | |
| 221 | const SkIRect deviceBounds = canvas->getDeviceClipBounds(); |
| 222 | tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop, nullptr); |
| 223 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 224 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 225 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 226 | }; // namespace skiapipeline |
| 227 | }; // namespace uirenderer |
| 228 | }; // namespace android |