Home | History | Annotate | Download | only in skia
      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 <GrContext.h>
     19 #include <private/hwui/DrawGlInfo.h>
     20 #include "FunctorDrawable.h"
     21 #include "GlFunctorLifecycleListener.h"
     22 #include "GrBackendSurface.h"
     23 #include "GrRenderTarget.h"
     24 #include "GrRenderTargetContext.h"
     25 #include "RenderNode.h"
     26 #include "SkAndroidFrameworkUtils.h"
     27 #include "SkClipStack.h"
     28 #include "SkRect.h"
     29 
     30 namespace android {
     31 namespace uirenderer {
     32 namespace skiapipeline {
     33 
     34 GLFunctorDrawable::~GLFunctorDrawable() {
     35     if (auto lp = std::get_if<LegacyFunctor>(&mAnyFunctor)) {
     36         if (lp->listener) {
     37             lp->listener->onGlFunctorReleased(lp->functor);
     38         }
     39     }
     40 }
     41 
     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 
     50 static bool GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) {
     51     GrRenderTargetContext* renderTargetContext =
     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 
     58     GrRenderTarget* renderTarget = renderTargetContext->accessRenderTarget();
     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 
     75 void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
     76     if (canvas->getGrContext() == nullptr) {
     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);
     84         return;
     85     }
     86 
     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();
     95     SkMatrix44 mat4(canvas->getTotalMatrix());
     96     SkRegion clipRegion;
     97     canvas->temporary_internal_getRgnClip(&clipRegion);
     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)) {
    102         // create an offscreen layer and clear it
    103         SkImageInfo surfaceInfo =
    104                 canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height());
    105         tmpSurface =
    106                 SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kYes, surfaceInfo);
    107         tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
    108 
    109         GrGLFramebufferInfo fboInfo;
    110         if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess)
    111                      .getGLFramebufferInfo(&fboInfo)) {
    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]);
    141     info.color_space_ptr = canvas->imageInfo().colorSpace();
    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.
    145     canvas->flush();
    146     glViewport(0, 0, info.width, info.height);
    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;
    151     if (CC_UNLIKELY(clipRegion.isComplex())) {
    152         // clear the stencil
    153         // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
    154         glDisable(GL_SCISSOR_TEST);
    155         glStencilMask(0x1);
    156         glClearStencil(0);
    157         glClear(GL_STENCIL_BUFFER_BIT);
    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 
    170         // GL ops get inserted here if previous flush is missing, which could dirty the stencil
    171         bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas);
    172         tmpCanvas->flush();  // need this flush for the single op that draws into the stencil
    173 
    174         // ensure that the framebuffer that the webview will render into is bound before after we
    175         // draw into the stencil
    176         glViewport(0, 0, info.width, info.height);
    177         glBindFramebuffer(GL_FRAMEBUFFER, fboID);
    178 
    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);
    187         }
    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 
    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     }
    202 
    203     if (clearStencilAfterFunctor) {
    204         // clear stencil buffer as it may be used by Skia
    205         glDisable(GL_SCISSOR_TEST);
    206         glDisable(GL_STENCIL_TEST);
    207         glStencilMask(0x1);
    208         glClearStencil(0);
    209         glClear(GL_STENCIL_BUFFER_BIT);
    210     }
    211 
    212     canvas->getGrContext()->resetContext();
    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     }
    227 }
    228 
    229 }  // namespace skiapipeline
    230 }  // namespace uirenderer
    231 }  // namespace android
    232