File: GLFunctorDrawable.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (217 lines) | stat: -rw-r--r-- 8,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "GLFunctorDrawable.h"
#include <GrDirectContext.h>
#include <private/hwui/DrawGlInfo.h>
#include "FunctorDrawable.h"
#include "GrBackendSurface.h"
#include "RenderNode.h"
#include "SkAndroidFrameworkUtils.h"
#include "SkClipStack.h"
#include "SkRect.h"
#include "SkM44.h"
#include "utils/GLUtils.h"

namespace android {
namespace uirenderer {
namespace skiapipeline {

static void setScissor(int viewportHeight, const SkIRect& clip) {
    SkASSERT(!clip.isEmpty());
    // transform to Y-flipped GL space, and prevent negatives
    GLint y = viewportHeight - clip.fBottom;
    GLint height = (viewportHeight - clip.fTop) - y;
    glScissor(clip.fLeft, y, clip.width(), height);
}

static void GetFboDetails(SkCanvas* canvas, GLuint* outFboID, SkISize* outFboSize) {
    GrBackendRenderTarget renderTarget = canvas->topLayerBackendRenderTarget();
    GrGLFramebufferInfo fboInfo;
    LOG_ALWAYS_FATAL_IF(!renderTarget.getGLFramebufferInfo(&fboInfo),
        "getGLFrameBufferInfo failed");

    *outFboID = fboInfo.fFBOID;
    *outFboSize = renderTarget.dimensions();
}

void GLFunctorDrawable::onDraw(SkCanvas* canvas) {
    GrDirectContext* directContext = GrAsDirectContext(canvas->recordingContext());
    if (directContext == nullptr) {
        // We're dumping a picture, render a light-blue rectangle instead
        // TODO: Draw the WebView text on top? Seemingly complicated as SkPaint doesn't
        // seem to have a default typeface that works. We only ever use drawGlyphs, which
        // requires going through minikin & hwui's canvas which we don't have here.
        SkPaint paint;
        paint.setColor(0xFF81D4FA);
        canvas->drawRect(mBounds, paint);
        return;
    }

    // canvas may be an AlphaFilterCanvas, which is intended to draw with a
    // modified alpha. We do not have a way to do this without drawing into an
    // extra layer, which would have a performance cost. Draw directly into the
    // underlying gpu canvas. This matches prior behavior and the behavior in
    // Vulkan.
    {
        auto* gpuCanvas = SkAndroidFrameworkUtils::getBaseWrappedCanvas(canvas);
        LOG_ALWAYS_FATAL_IF(!gpuCanvas, "GLFunctorDrawable::onDraw is using an invalid canvas!");
        canvas = gpuCanvas;
    }

    // flush will create a GrRenderTarget if not already present.
    canvas->flush();

    GLuint fboID = 0;
    SkISize fboSize;
    GetFboDetails(canvas, &fboID, &fboSize);

    SkIRect surfaceBounds = canvas->topLayerBounds();
    SkIRect clipBounds = canvas->getDeviceClipBounds();
    SkM44 mat4(canvas->getLocalToDevice());
    SkRegion clipRegion;
    canvas->temporary_internal_getRgnClip(&clipRegion);

    sk_sp<SkSurface> tmpSurface;
    // we are in a state where there is an unclipped saveLayer
    if (fboID != 0 && !surfaceBounds.contains(clipBounds)) {
        // create an offscreen layer and clear it
        SkImageInfo surfaceInfo =
                canvas->imageInfo().makeWH(clipBounds.width(), clipBounds.height());
        tmpSurface =
                SkSurface::MakeRenderTarget(directContext, SkBudgeted::kYes, surfaceInfo);
        tmpSurface->getCanvas()->clear(SK_ColorTRANSPARENT);

        GrGLFramebufferInfo fboInfo;
        if (!tmpSurface->getBackendRenderTarget(SkSurface::kFlushWrite_BackendHandleAccess)
                     .getGLFramebufferInfo(&fboInfo)) {
            ALOGW("Unable to extract renderTarget info from offscreen canvas; aborting GLFunctor");
            return;
        }

        fboSize = SkISize::Make(surfaceInfo.width(), surfaceInfo.height());
        fboID = fboInfo.fFBOID;

        // update the matrix and clip that we pass to the WebView to match the coordinates of
        // the offscreen layer
        mat4.preTranslate(-clipBounds.fLeft, -clipBounds.fTop);
        clipBounds.offsetTo(0, 0);
        clipRegion.translate(-surfaceBounds.fLeft, -surfaceBounds.fTop);

    } else if (fboID != 0) {
        // we are drawing into a (clipped) offscreen layer so we must update the clip and matrix
        // from device coordinates to the layer's coordinates
        clipBounds.offset(-surfaceBounds.fLeft, -surfaceBounds.fTop);
        mat4.preTranslate(-surfaceBounds.fLeft, -surfaceBounds.fTop);
    }

    DrawGlInfo info;
    info.clipLeft = clipBounds.fLeft;
    info.clipTop = clipBounds.fTop;
    info.clipRight = clipBounds.fRight;
    info.clipBottom = clipBounds.fBottom;
    info.isLayer = fboID != 0;
    info.width = fboSize.width();
    info.height = fboSize.height();
    mat4.getColMajor(&info.transform[0]);
    info.color_space_ptr = canvas->imageInfo().colorSpace();

    // ensure that the framebuffer that the webview will render into is bound before we clear
    // the stencil and/or draw the functor.
    glViewport(0, 0, info.width, info.height);
    glBindFramebuffer(GL_FRAMEBUFFER, fboID);

    // apply a simple clip with a scissor or a complex clip with a stencil
    bool clearStencilAfterFunctor = false;
    if (CC_UNLIKELY(clipRegion.isComplex())) {
        // clear the stencil
        // TODO: move stencil clear and canvas flush to SkAndroidFrameworkUtils::clipWithStencil
        glDisable(GL_SCISSOR_TEST);
        glStencilMask(0x1);
        glClearStencil(0);
        glClear(GL_STENCIL_BUFFER_BIT);

        // notify Skia that we just updated the FBO and stencil
        const uint32_t grState = kStencil_GrGLBackendState | kRenderTarget_GrGLBackendState;
        directContext->resetContext(grState);

        SkCanvas* tmpCanvas = canvas;
        if (tmpSurface) {
            tmpCanvas = tmpSurface->getCanvas();
            // set the clip on the new canvas
            tmpCanvas->clipRegion(clipRegion);
        }

        // GL ops get inserted here if previous flush is missing, which could dirty the stencil
        bool stencilWritten = SkAndroidFrameworkUtils::clipWithStencil(tmpCanvas);
        tmpCanvas->flush();  // need this flush for the single op that draws into the stencil

        // ensure that the framebuffer that the webview will render into is bound before after we
        // draw into the stencil
        glViewport(0, 0, info.width, info.height);
        glBindFramebuffer(GL_FRAMEBUFFER, fboID);

        if (stencilWritten) {
            glStencilMask(0x1);
            glStencilFunc(GL_EQUAL, 0x1, 0x1);
            glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
            clearStencilAfterFunctor = true;
            glEnable(GL_STENCIL_TEST);
        } else {
            glDisable(GL_STENCIL_TEST);
        }
    } else if (clipRegion.isEmpty()) {
        glDisable(GL_STENCIL_TEST);
        glDisable(GL_SCISSOR_TEST);
    } else {
        glDisable(GL_STENCIL_TEST);
        glEnable(GL_SCISSOR_TEST);
        setScissor(info.height, clipRegion.getBounds());
    }

    // WebView may swallow GL errors, so catch them here
    GL_CHECKPOINT(LOW);
    mWebViewHandle->drawGl(info);

    if (clearStencilAfterFunctor) {
        // clear stencil buffer as it may be used by Skia
        glDisable(GL_SCISSOR_TEST);
        glDisable(GL_STENCIL_TEST);
        glStencilMask(0x1);
        glClearStencil(0);
        glClear(GL_STENCIL_BUFFER_BIT);
    }

    directContext->resetContext();

    // if there were unclipped save layers involved we draw our offscreen surface to the canvas
    if (tmpSurface) {
        SkAutoCanvasRestore acr(canvas, true);
        SkMatrix invertedMatrix;
        if (!canvas->getTotalMatrix().invert(&invertedMatrix)) {
            ALOGW("Unable to extract invert canvas matrix; aborting GLFunctor draw");
            return;
        }
        canvas->concat(invertedMatrix);

        const SkIRect deviceBounds = canvas->getDeviceClipBounds();
        tmpSurface->draw(canvas, deviceBounds.fLeft, deviceBounds.fTop);
    }
}

}  // namespace skiapipeline
}  // namespace uirenderer
}  // namespace android