19e7fcfda28fde747ba4e026772007cea77374e16Chris Craik/*
29e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * Copyright (C) 2015 The Android Open Source Project
39e7fcfda28fde747ba4e026772007cea77374e16Chris Craik *
49e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
59e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * you may not use this file except in compliance with the License.
69e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * You may obtain a copy of the License at
79e7fcfda28fde747ba4e026772007cea77374e16Chris Craik *
89e7fcfda28fde747ba4e026772007cea77374e16Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
99e7fcfda28fde747ba4e026772007cea77374e16Chris Craik *
109e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * Unless required by applicable law or agreed to in writing, software
119e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
129e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * See the License for the specific language governing permissions and
149e7fcfda28fde747ba4e026772007cea77374e16Chris Craik * limitations under the License.
159e7fcfda28fde747ba4e026772007cea77374e16Chris Craik */
169e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
179e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "BakedOpDispatcher.h"
189e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
199e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "BakedOpRenderer.h"
209e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "Caches.h"
213e9999bd866fac71c72e6b484a9836c87c328a08sergeyv#include "DeferredLayerUpdater.h"
229e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "Glop.h"
239e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "GlopBuilder.h"
24f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik#include "Patch.h"
25386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#include "PathTessellator.h"
269e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "renderstate/OffscreenBufferPool.h"
279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "renderstate/RenderState.h"
289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "utils/GLUtils.h"
299e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "VertexBuffer.h"
309e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include <algorithm>
329e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include <math.h>
33386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#include <SkPaintDefaults.h>
34678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik#include <SkPathOps.h>
359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
369e7fcfda28fde747ba4e026772007cea77374e16Chris Craiknamespace android {
379e7fcfda28fde747ba4e026772007cea77374e16Chris Craiknamespace uirenderer {
389e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
39253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guystatic void storeTexturedRect(TextureVertex* vertices, const Rect& bounds) {
40253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    vertices[0] = { bounds.left,  bounds.top,    0, 0 };
41253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    vertices[1] = { bounds.right, bounds.top,    1, 0 };
42253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    vertices[2] = { bounds.left,  bounds.bottom, 0, 1 };
43253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    vertices[3] = { bounds.right, bounds.bottom, 1, 1 };
4415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
4515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
4615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikvoid BakedOpDispatcher::onMergedBitmapOps(BakedOpRenderer& renderer,
4715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const MergedBakedOpList& opList) {
4815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
4915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const BakedOpState& firstState = *(opList.states[0]);
50ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    Bitmap* bitmap = (static_cast<const BitmapOp*>(opList.states[0]->op))->bitmap;
5115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
52253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    Texture* texture = renderer.caches().textureCache.get(bitmap);
5315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!texture) return;
5415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const AutoTexture autoCleanup(texture);
5515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
5615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    TextureVertex vertices[opList.count * 4];
5715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    for (size_t i = 0; i < opList.count; i++) {
5815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const BakedOpState& state = *(opList.states[i]);
5915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        TextureVertex* rectVerts = &vertices[i * 4];
60f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik
61f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        // calculate unclipped bounds, since they'll determine texture coordinates
62f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        Rect opBounds = state.op->unmappedBounds;
63f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        state.computedState.transform.mapRect(opBounds);
6415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        if (CC_LIKELY(state.computedState.transform.isPureTranslate())) {
6515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            // pure translate, so snap (same behavior as onBitmapOp)
6615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            opBounds.snapToPixelBoundaries();
6715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        }
68253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        storeTexturedRect(rectVerts, opBounds);
6915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        renderer.dirtyRenderTarget(opBounds);
7015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
7115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
7215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
7315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
7415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Glop glop;
7515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
7615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setRoundRectClipState(firstState.roundRectClipState)
7715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setMeshTexturedIndexedQuads(vertices, opList.count * 6)
7815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setFillTexturePaint(*texture, textureFillFlags, firstState.op->paint, firstState.alpha)
7915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
80f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewIdentityEmptyBounds()
81f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
82e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
83e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
84e4db79de127cfe961195f52907af8451026eaa20Chris Craik    renderer.renderGlop(nullptr, clip, glop);
85f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
86f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
87f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onMergedPatchOps(BakedOpRenderer& renderer,
88f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const MergedBakedOpList& opList) {
89f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const PatchOp& firstOp = *(static_cast<const PatchOp*>(opList.states[0]->op));
90f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const BakedOpState& firstState = *(opList.states[0]);
91f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
92f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // Batches will usually contain a small number of items so it's
93f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // worth performing a first iteration to count the exact number
94f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // of vertices we need in the new mesh
95f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    uint32_t totalVertices = 0;
96f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
97f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (size_t i = 0; i < opList.count; i++) {
98f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op));
99f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
100f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // TODO: cache mesh lookups
101f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const Patch* opMesh = renderer.caches().patchCache.get(
102253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy                op.bitmap->width(), op.bitmap->height(),
103f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
104f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        totalVertices += opMesh->verticesCount;
105f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
106f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
107f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const bool dirtyRenderTarget = renderer.offscreenRenderTarget();
108f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
109f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    uint32_t indexCount = 0;
110f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
111f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    TextureVertex vertices[totalVertices];
112f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    TextureVertex* vertex = &vertices[0];
113f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // Create a mesh that contains the transformed vertices for all the
114f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9-patch objects that are part of the batch. Note that onDefer()
115f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // enforces ops drawn by this function to have a pure translate or
116f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // identity matrix
117f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (size_t i = 0; i < opList.count; i++) {
118f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op));
119f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const BakedOpState& state = *opList.states[i];
120f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
121f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // TODO: cache mesh lookups
122f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const Patch* opMesh = renderer.caches().patchCache.get(
123253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy                op.bitmap->width(), op.bitmap->height(),
124f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
125f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
126f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
127f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        uint32_t vertexCount = opMesh->verticesCount;
128f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        if (vertexCount == 0) continue;
129f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
130f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // We use the bounds to know where to translate our vertices
131f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Using patchOp->state.mBounds wouldn't work because these
132f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // bounds are clipped
133f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const float tx = floorf(state.computedState.transform.getTranslateX()
134f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                + op.unmappedBounds.left + 0.5f);
135f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const float ty = floorf(state.computedState.transform.getTranslateY()
136f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                + op.unmappedBounds.top + 0.5f);
137f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
138f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Copy & transform all the vertices for the current operation
139f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        TextureVertex* opVertices = opMesh->vertices.get();
140f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        for (uint32_t j = 0; j < vertexCount; j++, opVertices++) {
141f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            TextureVertex::set(vertex++,
142f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    opVertices->x + tx, opVertices->y + ty,
143f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    opVertices->u, opVertices->v);
144f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
145f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
146f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Dirty the current layer if possible. When the 9-patch does not
147f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // contain empty quads we can take a shortcut and simply set the
148f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // dirty rect to the object's bounds.
149f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        if (dirtyRenderTarget) {
150f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            if (!opMesh->hasEmptyQuads) {
151f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                renderer.dirtyRenderTarget(Rect(tx, ty,
152f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                        tx + op.unmappedBounds.getWidth(), ty + op.unmappedBounds.getHeight()));
153f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            } else {
154f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                const size_t count = opMesh->quads.size();
155f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                for (size_t i = 0; i < count; i++) {
156f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const Rect& quadBounds = opMesh->quads[i];
157f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const float x = tx + quadBounds.left;
158f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const float y = ty + quadBounds.top;
159f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    renderer.dirtyRenderTarget(Rect(x, y,
160f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                            x + quadBounds.getWidth(), y + quadBounds.getHeight()));
161f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                }
162f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            }
163f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
164f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
165f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        indexCount += opMesh->indexCount;
166f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
167f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
168f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
169253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    Texture* texture = renderer.caches().textureCache.get(firstOp.bitmap);
170f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!texture) return;
171f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const AutoTexture autoCleanup(texture);
172f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
173f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9 patches are built for stretching - always filter
174f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int textureFillFlags = TextureFillFlags::ForceFilter;
175f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (firstOp.bitmap->colorType() == kAlpha_8_SkColorType) {
176f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
177f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
178f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
179f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
180f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(firstState.roundRectClipState)
181f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshTexturedIndexedQuads(vertices, indexCount)
182f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, firstOp.paint, firstState.alpha)
183f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
184f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewIdentityEmptyBounds()
18515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .build();
186e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
187e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
188e4db79de127cfe961195f52907af8451026eaa20Chris Craik    renderer.renderGlop(nullptr, clip, glop);
18915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
19015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
191284b765e3c1647859d4dac772744e8859c033216sergeyvstatic void renderTextShadow(BakedOpRenderer& renderer,
19270969ccde405ea410fc7ccfe869999a03f056686Chris Craik        const TextOp& op, const BakedOpState& textOpState) {
193284b765e3c1647859d4dac772744e8859c033216sergeyv    if (CC_LIKELY(!PaintUtils::hasTextShadow(op.paint))) return;
194284b765e3c1647859d4dac772744e8859c033216sergeyv
195284b765e3c1647859d4dac772744e8859c033216sergeyv    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
196284b765e3c1647859d4dac772744e8859c033216sergeyv    fontRenderer.setFont(op.paint, SkMatrix::I());
19715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    renderer.caches().textureState().activateTexture(0);
19815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
19915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    PaintUtils::TextShadow textShadow;
20015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!PaintUtils::getTextShadow(op.paint, &textShadow)) {
20115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        LOG_ALWAYS_FATAL("failed to query shadow attributes");
20215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
20315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
20415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    renderer.caches().dropShadowCache.setFontRenderer(fontRenderer);
20515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    ShadowTexture* texture = renderer.caches().dropShadowCache.get(
206e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik            op.paint, op.glyphs, op.glyphCount, textShadow.radius, op.positions);
20715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // If the drop shadow exceeds the max texture size or couldn't be
20815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // allocated, skip drawing
20915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!texture) return;
21015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const AutoTexture autoCleanup(texture);
21115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
21215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const float sx = op.x - texture->left + textShadow.dx;
21315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const float sy = op.y - texture->top + textShadow.dy;
21415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
21515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Glop glop;
21615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
21770969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setRoundRectClipState(textOpState.roundRectClipState)
21815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setMeshTexturedUnitQuad(nullptr)
21970969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setFillShadowTexturePaint(*texture, textShadow.color, *op.paint, textOpState.alpha)
22070969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setTransform(textOpState.computedState.transform, TransformFlags::None)
22138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width(), sy + texture->height()))
22215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .build();
22370969ccde405ea410fc7ccfe869999a03f056686Chris Craik
22470969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // Compute damage bounds and clip (since may differ from those in textOpState).
22570969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // Bounds should be same as text op, but with dx/dy offset and radius outset
22670969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // applied in local space.
22770969ccde405ea410fc7ccfe869999a03f056686Chris Craik    auto& transform = textOpState.computedState.transform;
22870969ccde405ea410fc7ccfe869999a03f056686Chris Craik    Rect shadowBounds = op.unmappedBounds; // STROKE
22970969ccde405ea410fc7ccfe869999a03f056686Chris Craik    const bool expandForStroke = op.paint->getStyle() != SkPaint::kFill_Style;
23070969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (expandForStroke) {
23170969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.outset(op.paint->getStrokeWidth() * 0.5f);
23270969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
23370969ccde405ea410fc7ccfe869999a03f056686Chris Craik    shadowBounds.translate(textShadow.dx, textShadow.dy);
23470969ccde405ea410fc7ccfe869999a03f056686Chris Craik    shadowBounds.outset(textShadow.radius, textShadow.radius);
23570969ccde405ea410fc7ccfe869999a03f056686Chris Craik    transform.mapRect(shadowBounds);
23670969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (CC_UNLIKELY(expandForStroke &&
23770969ccde405ea410fc7ccfe869999a03f056686Chris Craik            (!transform.isPureTranslate() || op.paint->getStrokeWidth() < 1.0f))) {
23870969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.outset(0.5f);
23970969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
24070969ccde405ea410fc7ccfe869999a03f056686Chris Craik
24170969ccde405ea410fc7ccfe869999a03f056686Chris Craik    auto clipState = textOpState.computedState.clipState;
24270969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (clipState->mode != ClipMode::Rectangle
24370969ccde405ea410fc7ccfe869999a03f056686Chris Craik            || !clipState->rect.contains(shadowBounds)) {
24470969ccde405ea410fc7ccfe869999a03f056686Chris Craik        // need clip, so pass it and clip bounds
24570969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.doIntersect(clipState->rect);
24670969ccde405ea410fc7ccfe869999a03f056686Chris Craik    } else {
24770969ccde405ea410fc7ccfe869999a03f056686Chris Craik        // don't need clip, ignore
24870969ccde405ea410fc7ccfe869999a03f056686Chris Craik        clipState = nullptr;
24970969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
25070969ccde405ea410fc7ccfe869999a03f056686Chris Craik
25170969ccde405ea410fc7ccfe869999a03f056686Chris Craik    renderer.renderGlop(&shadowBounds, clipState, glop);
25215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
25315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
25415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikenum class TextRenderType {
25515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Defer,
25615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Flush
25715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik};
25815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
259284b765e3c1647859d4dac772744e8859c033216sergeyvstatic void renderText(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state,
260e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const ClipBase* renderClip, TextRenderType renderType) {
26115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
26215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    float x = op.x;
26315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    float y = op.y;
26415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const Matrix4& transform = state.computedState.transform;
26515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const bool pureTranslate = transform.isPureTranslate();
26615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (CC_LIKELY(pureTranslate)) {
26715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        x = floorf(x + transform.getTranslateX() + 0.5f);
26815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        y = floorf(y + transform.getTranslateY() + 0.5f);
26915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::I());
27015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(false);
27115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else if (CC_UNLIKELY(transform.isPerspective())) {
27215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::I());
27315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(true);
27415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else {
27515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // We only pass a partial transform to the font renderer. That partial
27615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // matrix defines how glyphs are rasterized. Typically we want glyphs
27715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // to be rasterized at their final size on screen, which means the partial
27815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // matrix needs to take the scale factor into account.
27915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // When a partial matrix is used to transform glyphs during rasterization,
28015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // the mesh is generated with the inverse transform (in the case of scale,
28115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // the mesh is generated at 1.0 / scale for instance.) This allows us to
28215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // apply the full transform matrix at draw time in the vertex shader.
28315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // Applying the full matrix in the shader is the easiest way to handle
28415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // rotation and perspective and allows us to always generated quads in the
28515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // font renderer which greatly simplifies the code, clipping in particular.
28615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        float sx, sy;
28715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        transform.decomposeScale(sx, sy);
28815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::MakeScale(
28915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                roundf(std::max(1.0f, sx)),
29015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                roundf(std::max(1.0f, sy))));
29115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(true);
29215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
29315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
29415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
29515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha;
296260ab726486317496bc12a57d599ea96dcde3284Mike Reed    SkBlendMode mode = PaintUtils::getBlendModeDirect(op.paint);
29715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    TextDrawFunctor functor(&renderer, &state, renderClip,
29815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            x, y, pureTranslate, alpha, mode, op.paint);
29915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
30015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    bool forceFinish = (renderType == TextRenderType::Flush);
30115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    bool mustDirtyRenderTarget = renderer.offscreenRenderTarget();
302e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const Rect* localOpClip = pureTranslate ? &state.computedState.clipRect() : nullptr;
303e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik    fontRenderer.renderPosText(op.paint, localOpClip, op.glyphs, op.glyphCount, x, y,
30415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            op.positions, mustDirtyRenderTarget ? &layerBounds : nullptr, &functor, forceFinish);
30515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
30615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (mustDirtyRenderTarget) {
30715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        if (!pureTranslate) {
30815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            transform.mapRect(layerBounds);
30915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        }
31015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        renderer.dirtyRenderTarget(layerBounds);
31115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
31215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
31315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
31415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikvoid BakedOpDispatcher::onMergedTextOps(BakedOpRenderer& renderer,
31515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const MergedBakedOpList& opList) {
316284b765e3c1647859d4dac772744e8859c033216sergeyv    for (size_t i = 0; i < opList.count; i++) {
317284b765e3c1647859d4dac772744e8859c033216sergeyv        const BakedOpState& state = *(opList.states[i]);
318284b765e3c1647859d4dac772744e8859c033216sergeyv        const TextOp& op = *(static_cast<const TextOp*>(state.op));
319284b765e3c1647859d4dac772744e8859c033216sergeyv        renderTextShadow(renderer, op, state);
320284b765e3c1647859d4dac772744e8859c033216sergeyv    }
321284b765e3c1647859d4dac772744e8859c033216sergeyv
322e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
323e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
32415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    for (size_t i = 0; i < opList.count; i++) {
32515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const BakedOpState& state = *(opList.states[i]);
32615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const TextOp& op = *(static_cast<const TextOp*>(state.op));
32715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        TextRenderType renderType = (i + 1 == opList.count)
32815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                ? TextRenderType::Flush : TextRenderType::Defer;
329284b765e3c1647859d4dac772744e8859c033216sergeyv        renderText(renderer, op, state, clip, renderType);
33015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
33115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
33215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
333386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craiknamespace VertexBufferRenderFlags {
334386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    enum {
335386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        Offset = 0x1,
336386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        ShadowInterp = 0x2,
337386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    };
338386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
339386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
340386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderVertexBuffer(BakedOpRenderer& renderer, const BakedOpState& state,
341386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const VertexBuffer& vertexBuffer, float translateX, float translateY,
342386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const SkPaint& paint, int vertexBufferRenderFlags) {
343386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (CC_LIKELY(vertexBuffer.getVertexCount())) {
344386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        bool shadowInterp = vertexBufferRenderFlags & VertexBufferRenderFlags::ShadowInterp;
34592a5d4b99a5554adca0e94627d44d2bd7f4f0bc0sergeyv        const int transformFlags = vertexBufferRenderFlags & VertexBufferRenderFlags::Offset
34692a5d4b99a5554adca0e94627d44d2bd7f4f0bc0sergeyv                ? TransformFlags::OffsetByFudgeFactor : 0;
347138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik
348386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        Glop glop;
349386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
350386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setRoundRectClipState(state.roundRectClipState)
351138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik                .setMeshVertexBuffer(vertexBuffer)
352138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik                .setFillPaint(paint, state.alpha, shadowInterp)
353386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setTransform(state.computedState.transform, transformFlags)
354386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
355386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .build();
356386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderer.renderGlop(state, glop);
357386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
358386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
359386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
360386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderConvexPath(BakedOpRenderer& renderer, const BakedOpState& state,
361386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const SkPath& path, const SkPaint& paint) {
362386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer vertexBuffer;
363386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    // TODO: try clipping large paths to viewport
364386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellatePath(path, &paint, state.computedState.transform, vertexBuffer);
365386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, vertexBuffer, 0.0f, 0.0f, paint, 0);
366386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
367386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
368386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderPathTexture(BakedOpRenderer& renderer, const BakedOpState& state,
369e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        float xOffset, float yOffset, PathTexture& texture, const SkPaint& paint) {
37038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    Rect dest(texture.width(), texture.height());
371e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik    dest.translate(xOffset + texture.left - texture.offset,
372e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            yOffset + texture.top - texture.offset);
373386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    Glop glop;
374386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
375386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setRoundRectClipState(state.roundRectClipState)
376386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setMeshTexturedUnitQuad(nullptr)
377e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            .setFillPathTexturePaint(texture, paint, state.alpha)
378386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setTransform(state.computedState.transform,  TransformFlags::None)
379386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setModelViewMapUnitToRect(dest)
380386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .build();
381386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderer.renderGlop(state, glop);
382386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
383386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
384386aa031793bb037ec43b6cdbd8908c343cc86cbChris CraikSkRect getBoundsOfFill(const RecordedOp& op) {
385386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    SkRect bounds = op.unmappedBounds.toSkRect();
386386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() == SkPaint::kStrokeAndFill_Style) {
387386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        float outsetDistance = op.paint->getStrokeWidth() / 2;
388386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        bounds.outset(outsetDistance, outsetDistance);
389386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
390386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    return bounds;
391386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
392386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
393386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onArcOp(BakedOpRenderer& renderer, const ArcOp& op, const BakedOpState& state) {
394386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
395386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() != SkPaint::kStroke_Style
396386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            || op.paint->getPathEffect() != nullptr
397386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            || op.useCenter) {
398386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getArc(
399386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(),
400386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.startAngle, op.sweepAngle, op.useCenter, op.paint);
401386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
402386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
4030066a01d7ceb9db89fe5edb97e06f62db1c52a74Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
404e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
405386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
406386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
407386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkRect rect = getBoundsOfFill(op);
408386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkPath path;
409386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.useCenter) {
410386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.moveTo(rect.centerX(), rect.centerY());
411386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
412386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        path.arcTo(rect, op.startAngle, op.sweepAngle, !op.useCenter);
413386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.useCenter) {
414386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.close();
415386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
416386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderConvexPath(renderer, state, path, *(op.paint));
417386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
418386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
419386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
4209e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onBitmapOp(BakedOpRenderer& renderer, const BitmapOp& op, const BakedOpState& state) {
4219e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Texture* texture = renderer.getTexture(op.bitmap);
4229e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (!texture) return;
4239e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    const AutoTexture autoCleanup(texture);
4249e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
4259e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType)
4269e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
4279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Glop glop;
4289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
4299e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setRoundRectClipState(state.roundRectClipState)
4309e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setMeshTexturedUnitQuad(texture->uvMapper)
4319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
4329e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
43338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            .setModelViewMapUnitToRectSnap(Rect(texture->width(), texture->height()))
4349e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .build();
4359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderer.renderGlop(state, glop);
4369e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
4379e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
438f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onBitmapMeshOp(BakedOpRenderer& renderer, const BitmapMeshOp& op, const BakedOpState& state) {
439253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    Texture* texture = renderer.caches().textureCache.get(op.bitmap);
440253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    if (!texture) {
441253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        return;
442253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    }
443253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    const AutoTexture autoCleanup(texture);
444253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy
445f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const uint32_t elementCount = op.meshWidth * op.meshHeight * 6;
446f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
447f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
448f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    ColorTextureVertex* vertex = &mesh[0];
449f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
450f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int* colors = op.colors;
451f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    std::unique_ptr<int[]> tempColors;
452f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!colors) {
453f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        uint32_t colorsCount = (op.meshWidth + 1) * (op.meshHeight + 1);
454f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        tempColors.reset(new int[colorsCount]);
455f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
456f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        colors = tempColors.get();
457f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
458f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
459f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (int32_t y = 0; y < op.meshHeight; y++) {
460f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        for (int32_t x = 0; x < op.meshWidth; x++) {
461f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            uint32_t i = (y * (op.meshWidth + 1) + x) * 2;
462f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
463f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float u1 = float(x) / op.meshWidth;
464f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float u2 = float(x + 1) / op.meshWidth;
465f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float v1 = float(y) / op.meshHeight;
466f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float v2 = float(y + 1) / op.meshHeight;
467f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
468f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int ax = i + (op.meshWidth + 1) * 2;
469f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int ay = ax + 1;
470f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int bx = i;
471f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int by = bx + 1;
472f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int cx = i + 2;
473f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int cy = cx + 1;
474f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int dx = i + (op.meshWidth + 1) * 2 + 2;
475f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int dy = dx + 1;
476f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
477f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            const float* vertices = op.vertices;
478f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
479f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
480f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
481f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
482f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
483f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
484f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
485f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
486f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
487f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
488f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    /*
489f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
490f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
491f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     */
492f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int textureFillFlags = TextureFillFlags::None;
493f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
494f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
495f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(state.roundRectClipState)
496f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshColoredTexturedMesh(mesh.get(), elementCount)
497f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
498f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(state.computedState.transform,  TransformFlags::None)
499f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewOffsetRect(0, 0, op.unmappedBounds)
500f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
501f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    renderer.renderGlop(state, glop);
502f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
503f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
504f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onBitmapRectOp(BakedOpRenderer& renderer, const BitmapRectOp& op, const BakedOpState& state) {
505f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Texture* texture = renderer.getTexture(op.bitmap);
506f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!texture) return;
507f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const AutoTexture autoCleanup(texture);
508f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
50938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    Rect uv(std::max(0.0f, op.src.left / texture->width()),
51038e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::max(0.0f, op.src.top / texture->height()),
51138e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::min(1.0f, op.src.right / texture->width()),
51238e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::min(1.0f, op.src.bottom / texture->height()));
513f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
514f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType)
515f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
516f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const bool tryToSnap = MathUtils::areEqual(op.src.getWidth(), op.unmappedBounds.getWidth())
517f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            && MathUtils::areEqual(op.src.getHeight(), op.unmappedBounds.getHeight());
518f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
519f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
520f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(state.roundRectClipState)
521f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshTexturedUvQuad(texture->uvMapper, uv)
522f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
523f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
524f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewMapUnitToRectOptionalSnap(tryToSnap, op.unmappedBounds)
525f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
526f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    renderer.renderGlop(state, glop);
527f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
528f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
529a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craikvoid BakedOpDispatcher::onColorOp(BakedOpRenderer& renderer, const ColorOp& op, const BakedOpState& state) {
530a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    SkPaint paint;
531a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    paint.setColor(op.color);
532260ab726486317496bc12a57d599ea96dcde3284Mike Reed    paint.setBlendMode(op.mode);
533a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik
534a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    Glop glop;
535a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
536a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setRoundRectClipState(state.roundRectClipState)
537a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setMeshUnitQuad()
538a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setFillPaint(paint, state.alpha)
539a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
540a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setModelViewMapUnitToRect(state.computedState.clipState->rect)
541a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .build();
542a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    renderer.renderGlop(state, glop);
543a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik}
544a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik
545e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craikvoid BakedOpDispatcher::onFunctorOp(BakedOpRenderer& renderer, const FunctorOp& op, const BakedOpState& state) {
546e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    renderer.renderFunctor(op, state);
547e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik}
548e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
5499e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onLinesOp(BakedOpRenderer& renderer, const LinesOp& op, const BakedOpState& state) {
550386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer buffer;
551386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellateLines(op.points, op.floatCount, op.paint,
552386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            state.computedState.transform, buffer);
553386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset;
554386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags);
5559e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
5569e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
557386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onOvalOp(BakedOpRenderer& renderer, const OvalOp& op, const BakedOpState& state) {
558386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getPathEffect() != nullptr) {
559386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getOval(
560386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint);
561386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
562386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
563e98a046d3110eb2ff11a17f4ff27764b803a1379Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
564e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
565386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
566386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
567386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkPath path;
568386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkRect rect = getBoundsOfFill(op);
569386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        path.addOval(rect);
570678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik
571678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        if (state.computedState.localProjectionPathMask != nullptr) {
572678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            // Mask the ripple path by the local space projection mask in local space.
573678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            // Note that this can create CCW paths.
574678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            Op(path, *state.computedState.localProjectionPathMask, kIntersect_SkPathOp, &path);
575678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        }
576386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderConvexPath(renderer, state, path, *(op.paint));
577386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
5789e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
5799e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
580f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onPatchOp(BakedOpRenderer& renderer, const PatchOp& op, const BakedOpState& state) {
581f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9 patches are built for stretching - always filter
582f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int textureFillFlags = TextureFillFlags::ForceFilter;
583f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (op.bitmap->colorType() == kAlpha_8_SkColorType) {
584f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
585f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
586f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
587f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // TODO: avoid redoing the below work each frame:
588f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const Patch* mesh = renderer.caches().patchCache.get(
589253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy            op.bitmap->width(), op.bitmap->height(),
590f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
591f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
592253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    Texture* texture = renderer.caches().textureCache.get(op.bitmap);
59389ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik    if (CC_LIKELY(texture)) {
59489ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        const AutoTexture autoCleanup(texture);
59589ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        Glop glop;
59689ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
59789ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setRoundRectClipState(state.roundRectClipState)
59889ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setMeshPatchQuads(*mesh)
59989ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
60089ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setTransform(state.computedState.transform, TransformFlags::None)
60189ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setModelViewOffsetRectSnap(op.unmappedBounds.left, op.unmappedBounds.top,
60289ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                        Rect(op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight()))
60389ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .build();
60489ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        renderer.renderGlop(state, glop);
60589ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik    }
606f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
607f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
608386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onPathOp(BakedOpRenderer& renderer, const PathOp& op, const BakedOpState& state) {
609386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTexture* texture = renderer.caches().pathCache.get(op.path, op.paint);
610386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    const AutoTexture holder(texture);
611386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (CC_LIKELY(holder.texture)) {
612e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        // Unlike other callers to renderPathTexture, no offsets are used because PathOp doesn't
613e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        // have any translate built in, other than what's in the SkPath itself
614e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        renderPathTexture(renderer, state, 0, 0, *texture, *(op.paint));
615386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
6169e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
6179e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
618386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onPointsOp(BakedOpRenderer& renderer, const PointsOp& op, const BakedOpState& state) {
619386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer buffer;
620386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellatePoints(op.points, op.floatCount, op.paint,
621386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            state.computedState.transform, buffer);
622386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset;
623386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags);
624386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
625386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
626386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik// See SkPaintDefaults.h
627386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
628386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
629386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onRectOp(BakedOpRenderer& renderer, const RectOp& op, const BakedOpState& state) {
630386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() != SkPaint::kFill_Style) {
631386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        // only fill + default miter is supported by drawConvexPath, since others must handle joins
632386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed");
633386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_UNLIKELY(op.paint->getPathEffect() != nullptr
634386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                || op.paint->getStrokeJoin() != SkPaint::kMiter_Join
635386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                || op.paint->getStrokeMiter() != SkPaintDefaults_MiterLimit)) {
636386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             PathTexture* texture = renderer.caches().pathCache.getRect(
637386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                     op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint);
638386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             const AutoTexture holder(texture);
639386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             if (CC_LIKELY(holder.texture)) {
640e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
641e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                         *texture, *(op.paint));
642386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             }
643386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        } else {
644386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            SkPath path;
645386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.addRect(getBoundsOfFill(op));
646386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderConvexPath(renderer, state, path, *(op.paint));
647386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
648386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
649386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.paint->isAntiAlias() && !state.computedState.transform.isSimple()) {
650386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            SkPath path;
651386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.addRect(op.unmappedBounds.toSkRect());
652386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderConvexPath(renderer, state, path, *(op.paint));
653386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        } else {
654386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            // render simple unit quad, no tessellation required
655386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Glop glop;
656386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
657386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setRoundRectClipState(state.roundRectClipState)
658386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setMeshUnitQuad()
659386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setFillPaint(*op.paint, state.alpha)
660386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setTransform(state.computedState.transform, TransformFlags::None)
661386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setModelViewMapUnitToRect(op.unmappedBounds)
662386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .build();
663386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderer.renderGlop(state, glop);
664386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
665386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
666386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
667386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
668386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onRoundRectOp(BakedOpRenderer& renderer, const RoundRectOp& op, const BakedOpState& state) {
669386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getPathEffect() != nullptr) {
670386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getRoundRect(
671386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(),
672386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.rx, op.ry, op.paint);
673386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
674386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
675e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
676e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
677386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
678386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
679386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const VertexBuffer* buffer = renderer.caches().tessellationCache.getRoundRect(
680386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                state.computedState.transform, *(op.paint),
681386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
682386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderVertexBuffer(renderer, state, *buffer,
683386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.left, op.unmappedBounds.top, *(op.paint), 0);
6849e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
6859e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
6869e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
6879e7fcfda28fde747ba4e026772007cea77374e16Chris Craikstatic void renderShadow(BakedOpRenderer& renderer, const BakedOpState& state, float casterAlpha,
6889e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
6899e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    SkPaint paint;
6909e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    paint.setAntiAlias(true); // want to use AlphaVertex
6919e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
6929e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    // The caller has made sure casterAlpha > 0.
6939e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    uint8_t ambientShadowAlpha = renderer.getLightInfo().ambientShadowAlpha;
6949e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
6959e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
6969e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
6979e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
6989e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        paint.setAlpha((uint8_t)(casterAlpha * ambientShadowAlpha));
6999e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        renderVertexBuffer(renderer, state, *ambientShadowVertexBuffer, 0, 0,
7009e7fcfda28fde747ba4e026772007cea77374e16Chris Craik                paint, VertexBufferRenderFlags::ShadowInterp);
7019e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7029e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7039e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    uint8_t spotShadowAlpha = renderer.getLightInfo().spotShadowAlpha;
7049e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
7059e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        spotShadowAlpha = Properties::overrideSpotShadowStrength;
7069e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7079e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
7089e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        paint.setAlpha((uint8_t)(casterAlpha * spotShadowAlpha));
7099e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        renderVertexBuffer(renderer, state, *spotShadowVertexBuffer, 0, 0,
7109e7fcfda28fde747ba4e026772007cea77374e16Chris Craik                paint, VertexBufferRenderFlags::ShadowInterp);
7119e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7129e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7139e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7149e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onShadowOp(BakedOpRenderer& renderer, const ShadowOp& op, const BakedOpState& state) {
715d8165e863b37d82c937ad960dc970f2d2cf72c2aChris Craik    TessellationCache::vertexBuffer_pair_t buffers = op.shadowTask->getResult();
7169e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderShadow(renderer, state, op.casterAlpha, buffers.first, buffers.second);
7179e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7189e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7199e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onSimpleRectsOp(BakedOpRenderer& renderer, const SimpleRectsOp& op, const BakedOpState& state) {
7209e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Glop glop;
7219e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
7229e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setRoundRectClipState(state.roundRectClipState)
7239e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setMeshIndexedQuads(&op.vertices[0], op.vertexCount / 4)
7249e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setFillPaint(*op.paint, state.alpha)
7259e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
7269e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setModelViewOffsetRect(0, 0, op.unmappedBounds)
7279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .build();
7289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderer.renderGlop(state, glop);
7299e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7309e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7319e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onTextOp(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state) {
732284b765e3c1647859d4dac772744e8859c033216sergeyv    renderTextShadow(renderer, op, state);
733284b765e3c1647859d4dac772744e8859c033216sergeyv    renderText(renderer, op, state, state.computedState.getClipIfNeeded(), TextRenderType::Flush);
7349e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
736d7448e65e243754f31890baef29dff187dc2e5e5Chris Craikvoid BakedOpDispatcher::onTextOnPathOp(BakedOpRenderer& renderer, const TextOnPathOp& op, const BakedOpState& state) {
737d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    // Note: can't trust clipSideFlags since we record with unmappedBounds == clip.
738d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    // TODO: respect clipSideFlags, once we record with bounds
739e4db79de127cfe961195f52907af8451026eaa20Chris Craik    auto renderTargetClip = state.computedState.clipState;
740d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
741d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
742d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    fontRenderer.setFont(op.paint, SkMatrix::I());
743d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    fontRenderer.setTextureFiltering(true);
744d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
745d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
746d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
747d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha;
748260ab726486317496bc12a57d599ea96dcde3284Mike Reed    SkBlendMode mode = PaintUtils::getBlendModeDirect(op.paint);
749d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    TextDrawFunctor functor(&renderer, &state, renderTargetClip,
750d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            0.0f, 0.0f, false, alpha, mode, op.paint);
751d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
752d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    bool mustDirtyRenderTarget = renderer.offscreenRenderTarget();
753d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    const Rect localSpaceClip = state.computedState.computeLocalSpaceClip();
754e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik    if (fontRenderer.renderTextOnPath(op.paint, &localSpaceClip, op.glyphs, op.glyphCount,
755