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"
219e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "Glop.h"
229e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "GlopBuilder.h"
23f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik#include "Patch.h"
24386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#include "PathTessellator.h"
259e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "renderstate/OffscreenBufferPool.h"
269e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "renderstate/RenderState.h"
279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "utils/GLUtils.h"
289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include "VertexBuffer.h"
299e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
309e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include <algorithm>
319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik#include <math.h>
32386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#include <SkPaintDefaults.h>
33678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik#include <SkPathOps.h>
349e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
359e7fcfda28fde747ba4e026772007cea77374e16Chris Craiknamespace android {
369e7fcfda28fde747ba4e026772007cea77374e16Chris Craiknamespace uirenderer {
379e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
3815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikstatic void storeTexturedRect(TextureVertex* vertices, const Rect& bounds, const Rect& texCoord) {
3915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    vertices[0] = { bounds.left, bounds.top, texCoord.left, texCoord.top };
4015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    vertices[1] = { bounds.right, bounds.top, texCoord.right, texCoord.top };
4115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    vertices[2] = { bounds.left, bounds.bottom, texCoord.left, texCoord.bottom };
4215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    vertices[3] = { bounds.right, bounds.bottom, texCoord.right, texCoord.bottom };
4315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
4415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
4515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikvoid BakedOpDispatcher::onMergedBitmapOps(BakedOpRenderer& renderer,
4615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const MergedBakedOpList& opList) {
4715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
4815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const BakedOpState& firstState = *(opList.states[0]);
4915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const SkBitmap* bitmap = (static_cast<const BitmapOp*>(opList.states[0]->op))->bitmap;
5015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
5115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry(bitmap->pixelRef());
5215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Texture* texture = entry ? entry->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    Rect texCoords(0, 0, 1, 1);
5815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (entry) {
5915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        entry->uvMapper.map(texCoords);
6015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
6115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    for (size_t i = 0; i < opList.count; i++) {
6215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const BakedOpState& state = *(opList.states[i]);
6315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        TextureVertex* rectVerts = &vertices[i * 4];
64f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik
65f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        // calculate unclipped bounds, since they'll determine texture coordinates
66f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        Rect opBounds = state.op->unmappedBounds;
67f6c20e4b4232e30901676bcdf1aed64801d50c7eChris Craik        state.computedState.transform.mapRect(opBounds);
6815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        if (CC_LIKELY(state.computedState.transform.isPureTranslate())) {
6915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            // pure translate, so snap (same behavior as onBitmapOp)
7015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            opBounds.snapToPixelBoundaries();
7115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        }
7215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        storeTexturedRect(rectVerts, opBounds, texCoords);
7315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        renderer.dirtyRenderTarget(opBounds);
7415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
7515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
7615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const int textureFillFlags = (bitmap->colorType() == kAlpha_8_SkColorType)
7715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
7815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Glop glop;
7915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
8015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setRoundRectClipState(firstState.roundRectClipState)
8115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setMeshTexturedIndexedQuads(vertices, opList.count * 6)
8215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setFillTexturePaint(*texture, textureFillFlags, firstState.op->paint, firstState.alpha)
8315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
84f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewIdentityEmptyBounds()
85f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
86e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
87e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
88e4db79de127cfe961195f52907af8451026eaa20Chris Craik    renderer.renderGlop(nullptr, clip, glop);
89f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
90f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
91f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onMergedPatchOps(BakedOpRenderer& renderer,
92f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const MergedBakedOpList& opList) {
93f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const PatchOp& firstOp = *(static_cast<const PatchOp*>(opList.states[0]->op));
94f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const BakedOpState& firstState = *(opList.states[0]);
95f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry(
96f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            firstOp.bitmap->pixelRef());
97f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
98f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // Batches will usually contain a small number of items so it's
99f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // worth performing a first iteration to count the exact number
100f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // of vertices we need in the new mesh
101f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    uint32_t totalVertices = 0;
102f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
103f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (size_t i = 0; i < opList.count; i++) {
104f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op));
105f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
106f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // TODO: cache mesh lookups
107f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const Patch* opMesh = renderer.caches().patchCache.get(
108f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                entry, op.bitmap->width(), op.bitmap->height(),
109f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
110f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        totalVertices += opMesh->verticesCount;
111f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
112f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
113f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const bool dirtyRenderTarget = renderer.offscreenRenderTarget();
114f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
115f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    uint32_t indexCount = 0;
116f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
117f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    TextureVertex vertices[totalVertices];
118f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    TextureVertex* vertex = &vertices[0];
119f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // Create a mesh that contains the transformed vertices for all the
120f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9-patch objects that are part of the batch. Note that onDefer()
121f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // enforces ops drawn by this function to have a pure translate or
122f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // identity matrix
123f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (size_t i = 0; i < opList.count; i++) {
124f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const PatchOp& op = *(static_cast<const PatchOp*>(opList.states[i]->op));
125f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const BakedOpState& state = *opList.states[i];
126f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
127f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // TODO: cache mesh lookups
128f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const Patch* opMesh = renderer.caches().patchCache.get(
129f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                entry, op.bitmap->width(), op.bitmap->height(),
130f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
131f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
132f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
133f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        uint32_t vertexCount = opMesh->verticesCount;
134f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        if (vertexCount == 0) continue;
135f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
136f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // We use the bounds to know where to translate our vertices
137f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Using patchOp->state.mBounds wouldn't work because these
138f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // bounds are clipped
139f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const float tx = floorf(state.computedState.transform.getTranslateX()
140f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                + op.unmappedBounds.left + 0.5f);
141f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        const float ty = floorf(state.computedState.transform.getTranslateY()
142f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                + op.unmappedBounds.top + 0.5f);
143f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
144f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Copy & transform all the vertices for the current operation
145f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        TextureVertex* opVertices = opMesh->vertices.get();
146f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        for (uint32_t j = 0; j < vertexCount; j++, opVertices++) {
147f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            TextureVertex::set(vertex++,
148f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    opVertices->x + tx, opVertices->y + ty,
149f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    opVertices->u, opVertices->v);
150f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
151f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
152f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // Dirty the current layer if possible. When the 9-patch does not
153f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // contain empty quads we can take a shortcut and simply set the
154f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        // dirty rect to the object's bounds.
155f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        if (dirtyRenderTarget) {
156f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            if (!opMesh->hasEmptyQuads) {
157f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                renderer.dirtyRenderTarget(Rect(tx, ty,
158f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                        tx + op.unmappedBounds.getWidth(), ty + op.unmappedBounds.getHeight()));
159f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            } else {
160f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                const size_t count = opMesh->quads.size();
161f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                for (size_t i = 0; i < count; i++) {
162f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const Rect& quadBounds = opMesh->quads[i];
163f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const float x = tx + quadBounds.left;
164f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    const float y = ty + quadBounds.top;
165f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                    renderer.dirtyRenderTarget(Rect(x, y,
166f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                            x + quadBounds.getWidth(), y + quadBounds.getHeight()));
167f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik                }
168f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            }
169f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
170f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
171f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        indexCount += opMesh->indexCount;
172f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
173f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
174f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
175f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Texture* texture = entry ? entry->texture : renderer.caches().textureCache.get(firstOp.bitmap);
176f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!texture) return;
177f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const AutoTexture autoCleanup(texture);
178f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
179f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9 patches are built for stretching - always filter
180f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int textureFillFlags = TextureFillFlags::ForceFilter;
181f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (firstOp.bitmap->colorType() == kAlpha_8_SkColorType) {
182f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
183f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
184f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
185f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
186f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(firstState.roundRectClipState)
187f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshTexturedIndexedQuads(vertices, indexCount)
188f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, firstOp.paint, firstState.alpha)
189f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
190f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewIdentityEmptyBounds()
19115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .build();
192e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
193e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
194e4db79de127cfe961195f52907af8451026eaa20Chris Craik    renderer.renderGlop(nullptr, clip, glop);
19515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
19615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
197284b765e3c1647859d4dac772744e8859c033216sergeyvstatic void renderTextShadow(BakedOpRenderer& renderer,
19870969ccde405ea410fc7ccfe869999a03f056686Chris Craik        const TextOp& op, const BakedOpState& textOpState) {
199284b765e3c1647859d4dac772744e8859c033216sergeyv    if (CC_LIKELY(!PaintUtils::hasTextShadow(op.paint))) return;
200284b765e3c1647859d4dac772744e8859c033216sergeyv
201284b765e3c1647859d4dac772744e8859c033216sergeyv    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
202284b765e3c1647859d4dac772744e8859c033216sergeyv    fontRenderer.setFont(op.paint, SkMatrix::I());
20315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    renderer.caches().textureState().activateTexture(0);
20415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
20515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    PaintUtils::TextShadow textShadow;
20615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!PaintUtils::getTextShadow(op.paint, &textShadow)) {
20715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        LOG_ALWAYS_FATAL("failed to query shadow attributes");
20815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
20915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
21015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    renderer.caches().dropShadowCache.setFontRenderer(fontRenderer);
21115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    ShadowTexture* texture = renderer.caches().dropShadowCache.get(
212e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik            op.paint, op.glyphs, op.glyphCount, textShadow.radius, op.positions);
21315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // If the drop shadow exceeds the max texture size or couldn't be
21415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    // allocated, skip drawing
21515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (!texture) return;
21615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const AutoTexture autoCleanup(texture);
21715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
21815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const float sx = op.x - texture->left + textShadow.dx;
21915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const float sy = op.y - texture->top + textShadow.dy;
22015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
22115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Glop glop;
22215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
22370969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setRoundRectClipState(textOpState.roundRectClipState)
22415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .setMeshTexturedUnitQuad(nullptr)
22570969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setFillShadowTexturePaint(*texture, textShadow.color, *op.paint, textOpState.alpha)
22670969ccde405ea410fc7ccfe869999a03f056686Chris Craik            .setTransform(textOpState.computedState.transform, TransformFlags::None)
22738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            .setModelViewMapUnitToRect(Rect(sx, sy, sx + texture->width(), sy + texture->height()))
22815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            .build();
22970969ccde405ea410fc7ccfe869999a03f056686Chris Craik
23070969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // Compute damage bounds and clip (since may differ from those in textOpState).
23170969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // Bounds should be same as text op, but with dx/dy offset and radius outset
23270969ccde405ea410fc7ccfe869999a03f056686Chris Craik    // applied in local space.
23370969ccde405ea410fc7ccfe869999a03f056686Chris Craik    auto& transform = textOpState.computedState.transform;
23470969ccde405ea410fc7ccfe869999a03f056686Chris Craik    Rect shadowBounds = op.unmappedBounds; // STROKE
23570969ccde405ea410fc7ccfe869999a03f056686Chris Craik    const bool expandForStroke = op.paint->getStyle() != SkPaint::kFill_Style;
23670969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (expandForStroke) {
23770969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.outset(op.paint->getStrokeWidth() * 0.5f);
23870969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
23970969ccde405ea410fc7ccfe869999a03f056686Chris Craik    shadowBounds.translate(textShadow.dx, textShadow.dy);
24070969ccde405ea410fc7ccfe869999a03f056686Chris Craik    shadowBounds.outset(textShadow.radius, textShadow.radius);
24170969ccde405ea410fc7ccfe869999a03f056686Chris Craik    transform.mapRect(shadowBounds);
24270969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (CC_UNLIKELY(expandForStroke &&
24370969ccde405ea410fc7ccfe869999a03f056686Chris Craik            (!transform.isPureTranslate() || op.paint->getStrokeWidth() < 1.0f))) {
24470969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.outset(0.5f);
24570969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
24670969ccde405ea410fc7ccfe869999a03f056686Chris Craik
24770969ccde405ea410fc7ccfe869999a03f056686Chris Craik    auto clipState = textOpState.computedState.clipState;
24870969ccde405ea410fc7ccfe869999a03f056686Chris Craik    if (clipState->mode != ClipMode::Rectangle
24970969ccde405ea410fc7ccfe869999a03f056686Chris Craik            || !clipState->rect.contains(shadowBounds)) {
25070969ccde405ea410fc7ccfe869999a03f056686Chris Craik        // need clip, so pass it and clip bounds
25170969ccde405ea410fc7ccfe869999a03f056686Chris Craik        shadowBounds.doIntersect(clipState->rect);
25270969ccde405ea410fc7ccfe869999a03f056686Chris Craik    } else {
25370969ccde405ea410fc7ccfe869999a03f056686Chris Craik        // don't need clip, ignore
25470969ccde405ea410fc7ccfe869999a03f056686Chris Craik        clipState = nullptr;
25570969ccde405ea410fc7ccfe869999a03f056686Chris Craik    }
25670969ccde405ea410fc7ccfe869999a03f056686Chris Craik
25770969ccde405ea410fc7ccfe869999a03f056686Chris Craik    renderer.renderGlop(&shadowBounds, clipState, glop);
25815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
25915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
26015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikenum class TextRenderType {
26115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Defer,
26215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Flush
26315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik};
26415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
265284b765e3c1647859d4dac772744e8859c033216sergeyvstatic void renderText(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state,
266e4db79de127cfe961195f52907af8451026eaa20Chris Craik        const ClipBase* renderClip, TextRenderType renderType) {
26715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
26815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    float x = op.x;
26915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    float y = op.y;
27015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const Matrix4& transform = state.computedState.transform;
27115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    const bool pureTranslate = transform.isPureTranslate();
27215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (CC_LIKELY(pureTranslate)) {
27315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        x = floorf(x + transform.getTranslateX() + 0.5f);
27415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        y = floorf(y + transform.getTranslateY() + 0.5f);
27515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::I());
27615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(false);
27715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else if (CC_UNLIKELY(transform.isPerspective())) {
27815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::I());
27915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(true);
28015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    } else {
28115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // We only pass a partial transform to the font renderer. That partial
28215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // matrix defines how glyphs are rasterized. Typically we want glyphs
28315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // to be rasterized at their final size on screen, which means the partial
28415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // matrix needs to take the scale factor into account.
28515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // When a partial matrix is used to transform glyphs during rasterization,
28615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // the mesh is generated with the inverse transform (in the case of scale,
28715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // the mesh is generated at 1.0 / scale for instance.) This allows us to
28815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // apply the full transform matrix at draw time in the vertex shader.
28915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // Applying the full matrix in the shader is the easiest way to handle
29015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // rotation and perspective and allows us to always generated quads in the
29115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        // font renderer which greatly simplifies the code, clipping in particular.
29215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        float sx, sy;
29315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        transform.decomposeScale(sx, sy);
29415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setFont(op.paint, SkMatrix::MakeScale(
29515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                roundf(std::max(1.0f, sx)),
29615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                roundf(std::max(1.0f, sy))));
29715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        fontRenderer.setTextureFiltering(true);
29815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
29915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
30015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
30115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha;
30215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(op.paint);
30315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    TextDrawFunctor functor(&renderer, &state, renderClip,
30415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            x, y, pureTranslate, alpha, mode, op.paint);
30515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
30615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    bool forceFinish = (renderType == TextRenderType::Flush);
30715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    bool mustDirtyRenderTarget = renderer.offscreenRenderTarget();
308e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const Rect* localOpClip = pureTranslate ? &state.computedState.clipRect() : nullptr;
309e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik    fontRenderer.renderPosText(op.paint, localOpClip, op.glyphs, op.glyphCount, x, y,
31015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            op.positions, mustDirtyRenderTarget ? &layerBounds : nullptr, &functor, forceFinish);
31115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
31215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    if (mustDirtyRenderTarget) {
31315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        if (!pureTranslate) {
31415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik            transform.mapRect(layerBounds);
31515c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        }
31615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        renderer.dirtyRenderTarget(layerBounds);
31715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
31815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
31915c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
32015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craikvoid BakedOpDispatcher::onMergedTextOps(BakedOpRenderer& renderer,
32115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const MergedBakedOpList& opList) {
322284b765e3c1647859d4dac772744e8859c033216sergeyv    for (size_t i = 0; i < opList.count; i++) {
323284b765e3c1647859d4dac772744e8859c033216sergeyv        const BakedOpState& state = *(opList.states[i]);
324284b765e3c1647859d4dac772744e8859c033216sergeyv        const TextOp& op = *(static_cast<const TextOp*>(state.op));
325284b765e3c1647859d4dac772744e8859c033216sergeyv        renderTextShadow(renderer, op, state);
326284b765e3c1647859d4dac772744e8859c033216sergeyv    }
327284b765e3c1647859d4dac772744e8859c033216sergeyv
328e4db79de127cfe961195f52907af8451026eaa20Chris Craik    ClipRect renderTargetClip(opList.clip);
329e4db79de127cfe961195f52907af8451026eaa20Chris Craik    const ClipBase* clip = opList.clipSideFlags ? &renderTargetClip : nullptr;
33015c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    for (size_t i = 0; i < opList.count; i++) {
33115c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const BakedOpState& state = *(opList.states[i]);
33215c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        const TextOp& op = *(static_cast<const TextOp*>(state.op));
33315c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik        TextRenderType renderType = (i + 1 == opList.count)
33415c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik                ? TextRenderType::Flush : TextRenderType::Defer;
335284b765e3c1647859d4dac772744e8859c033216sergeyv        renderText(renderer, op, state, clip, renderType);
33615c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik    }
33715c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik}
33815c3f19a445b8df575911a16e8a6dba755a084b5Chris Craik
339386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craiknamespace VertexBufferRenderFlags {
340386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    enum {
341386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        Offset = 0x1,
342386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        ShadowInterp = 0x2,
343386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    };
344386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
345386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
346386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderVertexBuffer(BakedOpRenderer& renderer, const BakedOpState& state,
347386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const VertexBuffer& vertexBuffer, float translateX, float translateY,
348386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const SkPaint& paint, int vertexBufferRenderFlags) {
349386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (CC_LIKELY(vertexBuffer.getVertexCount())) {
350386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        bool shadowInterp = vertexBufferRenderFlags & VertexBufferRenderFlags::ShadowInterp;
35192a5d4b99a5554adca0e94627d44d2bd7f4f0bc0sergeyv        const int transformFlags = vertexBufferRenderFlags & VertexBufferRenderFlags::Offset
35292a5d4b99a5554adca0e94627d44d2bd7f4f0bc0sergeyv                ? TransformFlags::OffsetByFudgeFactor : 0;
353138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik
354386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        Glop glop;
355386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
356386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setRoundRectClipState(state.roundRectClipState)
357138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik                .setMeshVertexBuffer(vertexBuffer)
358138c21fbec12bead3c7ca1f181c3fd35542ccb00Chris Craik                .setFillPaint(paint, state.alpha, shadowInterp)
359386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setTransform(state.computedState.transform, transformFlags)
360386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .setModelViewOffsetRect(translateX, translateY, vertexBuffer.getBounds())
361386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                .build();
362386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderer.renderGlop(state, glop);
363386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
364386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
365386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
366386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderConvexPath(BakedOpRenderer& renderer, const BakedOpState& state,
367386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const SkPath& path, const SkPaint& paint) {
368386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer vertexBuffer;
369386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    // TODO: try clipping large paths to viewport
370386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellatePath(path, &paint, state.computedState.transform, vertexBuffer);
371386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, vertexBuffer, 0.0f, 0.0f, paint, 0);
372386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
373386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
374386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikstatic void renderPathTexture(BakedOpRenderer& renderer, const BakedOpState& state,
375e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        float xOffset, float yOffset, PathTexture& texture, const SkPaint& paint) {
37638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    Rect dest(texture.width(), texture.height());
377e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik    dest.translate(xOffset + texture.left - texture.offset,
378e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            yOffset + texture.top - texture.offset);
379386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    Glop glop;
380386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
381386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setRoundRectClipState(state.roundRectClipState)
382386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setMeshTexturedUnitQuad(nullptr)
383e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            .setFillPathTexturePaint(texture, paint, state.alpha)
384386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setTransform(state.computedState.transform,  TransformFlags::None)
385386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .setModelViewMapUnitToRect(dest)
386386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            .build();
387386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderer.renderGlop(state, glop);
388386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
389386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
390386aa031793bb037ec43b6cdbd8908c343cc86cbChris CraikSkRect getBoundsOfFill(const RecordedOp& op) {
391386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    SkRect bounds = op.unmappedBounds.toSkRect();
392386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() == SkPaint::kStrokeAndFill_Style) {
393386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        float outsetDistance = op.paint->getStrokeWidth() / 2;
394386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        bounds.outset(outsetDistance, outsetDistance);
395386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
396386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    return bounds;
397386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
398386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
399386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onArcOp(BakedOpRenderer& renderer, const ArcOp& op, const BakedOpState& state) {
400386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
401386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() != SkPaint::kStroke_Style
402386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            || op.paint->getPathEffect() != nullptr
403386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            || op.useCenter) {
404386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getArc(
405386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(),
406386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.startAngle, op.sweepAngle, op.useCenter, op.paint);
407386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
408386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
4090066a01d7ceb9db89fe5edb97e06f62db1c52a74Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
410e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
411386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
412386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
413386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkRect rect = getBoundsOfFill(op);
414386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkPath path;
415386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.useCenter) {
416386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.moveTo(rect.centerX(), rect.centerY());
417386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
418386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        path.arcTo(rect, op.startAngle, op.sweepAngle, !op.useCenter);
419386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.useCenter) {
420386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.close();
421386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
422386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderConvexPath(renderer, state, path, *(op.paint));
423386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
424386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
425386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
4269e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onBitmapOp(BakedOpRenderer& renderer, const BitmapOp& op, const BakedOpState& state) {
4279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Texture* texture = renderer.getTexture(op.bitmap);
4289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (!texture) return;
4299e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    const AutoTexture autoCleanup(texture);
4309e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
4319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType)
4329e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
4339e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Glop glop;
4349e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
4359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setRoundRectClipState(state.roundRectClipState)
4369e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setMeshTexturedUnitQuad(texture->uvMapper)
4379e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
4389e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
43938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            .setModelViewMapUnitToRectSnap(Rect(texture->width(), texture->height()))
4409e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .build();
4419e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderer.renderGlop(state, glop);
4429e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
4439e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
444f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onBitmapMeshOp(BakedOpRenderer& renderer, const BitmapMeshOp& op, const BakedOpState& state) {
445f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const static UvMapper defaultUvMapper;
446f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const uint32_t elementCount = op.meshWidth * op.meshHeight * 6;
447f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
448f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[elementCount]);
449f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    ColorTextureVertex* vertex = &mesh[0];
450f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
451f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int* colors = op.colors;
452f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    std::unique_ptr<int[]> tempColors;
453f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!colors) {
454f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        uint32_t colorsCount = (op.meshWidth + 1) * (op.meshHeight + 1);
455f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        tempColors.reset(new int[colorsCount]);
456f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
457f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        colors = tempColors.get();
458f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
459f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
460f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Texture* texture = renderer.renderState().assetAtlas().getEntryTexture(op.bitmap->pixelRef());
461f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const UvMapper& mapper(texture && texture->uvMapper ? *texture->uvMapper : defaultUvMapper);
462f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
463f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    for (int32_t y = 0; y < op.meshHeight; y++) {
464f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        for (int32_t x = 0; x < op.meshWidth; x++) {
465f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            uint32_t i = (y * (op.meshWidth + 1) + x) * 2;
466f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
467f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float u1 = float(x) / op.meshWidth;
468f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float u2 = float(x + 1) / op.meshWidth;
469f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float v1 = float(y) / op.meshHeight;
470f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            float v2 = float(y + 1) / op.meshHeight;
471f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
472f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            mapper.map(u1, v1, u2, v2);
473f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
474f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int ax = i + (op.meshWidth + 1) * 2;
475f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int ay = ax + 1;
476f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int bx = i;
477f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int by = bx + 1;
478f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int cx = i + 2;
479f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int cy = cx + 1;
480f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int dx = i + (op.meshWidth + 1) * 2 + 2;
481f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            int dy = dx + 1;
482f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
483f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            const float* vertices = op.vertices;
484f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
485f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
486f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
487f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
488f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
489f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
490f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
491f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
492f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
493f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
494f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!texture) {
495f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        texture = renderer.caches().textureCache.get(op.bitmap);
496f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        if (!texture) {
497f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            return;
498f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        }
499f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
500f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const AutoTexture autoCleanup(texture);
501f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
502f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    /*
503f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     * TODO: handle alpha_8 textures correctly by applying paint color, but *not*
504f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     * shader in that case to mimic the behavior in SkiaCanvas::drawBitmapMesh.
505f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik     */
506f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int textureFillFlags = TextureFillFlags::None;
507f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
508f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
509f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(state.roundRectClipState)
510f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshColoredTexturedMesh(mesh.get(), elementCount)
511f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
512f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(state.computedState.transform,  TransformFlags::None)
513f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewOffsetRect(0, 0, op.unmappedBounds)
514f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
515f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    renderer.renderGlop(state, glop);
516f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
517f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
518f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onBitmapRectOp(BakedOpRenderer& renderer, const BitmapRectOp& op, const BakedOpState& state) {
519f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Texture* texture = renderer.getTexture(op.bitmap);
520f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (!texture) return;
521f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const AutoTexture autoCleanup(texture);
522f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
52338e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    Rect uv(std::max(0.0f, op.src.left / texture->width()),
52438e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::max(0.0f, op.src.top / texture->height()),
52538e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::min(1.0f, op.src.right / texture->width()),
52638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck            std::min(1.0f, op.src.bottom / texture->height()));
527f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
528f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const int textureFillFlags = (op.bitmap->colorType() == kAlpha_8_SkColorType)
529f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            ? TextureFillFlags::IsAlphaMaskTexture : TextureFillFlags::None;
530f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const bool tryToSnap = MathUtils::areEqual(op.src.getWidth(), op.unmappedBounds.getWidth())
531f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            && MathUtils::areEqual(op.src.getHeight(), op.unmappedBounds.getHeight());
532f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Glop glop;
533f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
534f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setRoundRectClipState(state.roundRectClipState)
535f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setMeshTexturedUvQuad(texture->uvMapper, uv)
536f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
537f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
538f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .setModelViewMapUnitToRectOptionalSnap(tryToSnap, op.unmappedBounds)
539f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            .build();
540f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    renderer.renderGlop(state, glop);
541f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
542f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
543a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craikvoid BakedOpDispatcher::onColorOp(BakedOpRenderer& renderer, const ColorOp& op, const BakedOpState& state) {
544a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    SkPaint paint;
545a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    paint.setColor(op.color);
546a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    paint.setXfermodeMode(op.mode);
547a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik
548a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    Glop glop;
549a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
550a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setRoundRectClipState(state.roundRectClipState)
551a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setMeshUnitQuad()
552a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setFillPaint(paint, state.alpha)
553a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setTransform(Matrix4::identity(), TransformFlags::None)
554a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .setModelViewMapUnitToRect(state.computedState.clipState->rect)
555a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik            .build();
556a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik    renderer.renderGlop(state, glop);
557a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik}
558a204848b1dc63877a12e2d24108e9d8e1e691e28Chris Craik
559e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craikvoid BakedOpDispatcher::onFunctorOp(BakedOpRenderer& renderer, const FunctorOp& op, const BakedOpState& state) {
560e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik    renderer.renderFunctor(op, state);
561e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik}
562e29ce6f51d681af7649c0a7cddee97c471e43eb5Chris Craik
5639e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onLinesOp(BakedOpRenderer& renderer, const LinesOp& op, const BakedOpState& state) {
564386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer buffer;
565386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellateLines(op.points, op.floatCount, op.paint,
566386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            state.computedState.transform, buffer);
567386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset;
568386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags);
5699e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
5709e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
571386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onOvalOp(BakedOpRenderer& renderer, const OvalOp& op, const BakedOpState& state) {
572386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getPathEffect() != nullptr) {
573386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getOval(
574386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint);
575386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
576386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
577e98a046d3110eb2ff11a17f4ff27764b803a1379Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
578e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
579386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
580386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
581386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkPath path;
582386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        SkRect rect = getBoundsOfFill(op);
583386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        path.addOval(rect);
584678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik
585678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        if (state.computedState.localProjectionPathMask != nullptr) {
586678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            // Mask the ripple path by the local space projection mask in local space.
587678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            // Note that this can create CCW paths.
588678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik            Op(path, *state.computedState.localProjectionPathMask, kIntersect_SkPathOp, &path);
589678ff81105753656aa4822f4f675ef96dc9d2b83Chris Craik        }
590386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderConvexPath(renderer, state, path, *(op.paint));
591386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
5929e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
5939e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
594f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craikvoid BakedOpDispatcher::onPatchOp(BakedOpRenderer& renderer, const PatchOp& op, const BakedOpState& state) {
595f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // 9 patches are built for stretching - always filter
596f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    int textureFillFlags = TextureFillFlags::ForceFilter;
597f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    if (op.bitmap->colorType() == kAlpha_8_SkColorType) {
598f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik        textureFillFlags |= TextureFillFlags::IsAlphaMaskTexture;
599f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    }
600f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
601f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    // TODO: avoid redoing the below work each frame:
602f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    AssetAtlas::Entry* entry = renderer.renderState().assetAtlas().getEntry(op.bitmap->pixelRef());
603f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    const Patch* mesh = renderer.caches().patchCache.get(
604f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            entry, op.bitmap->width(), op.bitmap->height(),
605f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik            op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.patch);
606f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
607f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik    Texture* texture = entry ? entry->texture : renderer.caches().textureCache.get(op.bitmap);
60889ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik    if (CC_LIKELY(texture)) {
60989ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        const AutoTexture autoCleanup(texture);
61089ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        Glop glop;
61189ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
61289ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setRoundRectClipState(state.roundRectClipState)
61389ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setMeshPatchQuads(*mesh)
61489ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setFillTexturePaint(*texture, textureFillFlags, op.paint, state.alpha)
61589ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setTransform(state.computedState.transform, TransformFlags::None)
61689ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .setModelViewOffsetRectSnap(op.unmappedBounds.left, op.unmappedBounds.top,
61789ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                        Rect(op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight()))
61889ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik                .build();
61989ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik        renderer.renderGlop(state, glop);
62089ceb5c97745c6901620c20eb0828a5c2f768f4fChris Craik    }
621f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik}
622f09ff5aa57bff01cb17595fb7ca8e48d238a6acdChris Craik
623386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onPathOp(BakedOpRenderer& renderer, const PathOp& op, const BakedOpState& state) {
624386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTexture* texture = renderer.caches().pathCache.get(op.path, op.paint);
625386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    const AutoTexture holder(texture);
626386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (CC_LIKELY(holder.texture)) {
627e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        // Unlike other callers to renderPathTexture, no offsets are used because PathOp doesn't
628e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        // have any translate built in, other than what's in the SkPath itself
629e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik        renderPathTexture(renderer, state, 0, 0, *texture, *(op.paint));
630386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
6319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
6329e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
633386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onPointsOp(BakedOpRenderer& renderer, const PointsOp& op, const BakedOpState& state) {
634386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    VertexBuffer buffer;
635386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    PathTessellator::tessellatePoints(op.points, op.floatCount, op.paint,
636386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            state.computedState.transform, buffer);
637386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    int displayFlags = op.paint->isAntiAlias() ? 0 : VertexBufferRenderFlags::Offset;
638386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    renderVertexBuffer(renderer, state, buffer, 0, 0, *(op.paint), displayFlags);
639386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
640386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
641386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik// See SkPaintDefaults.h
642386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
643386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
644386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onRectOp(BakedOpRenderer& renderer, const RectOp& op, const BakedOpState& state) {
645386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getStyle() != SkPaint::kFill_Style) {
646386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        // only fill + default miter is supported by drawConvexPath, since others must handle joins
647386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        static_assert(SkPaintDefaults_MiterLimit == 4.0f, "Miter limit has changed");
648386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_UNLIKELY(op.paint->getPathEffect() != nullptr
649386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                || op.paint->getStrokeJoin() != SkPaint::kMiter_Join
650386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                || op.paint->getStrokeMiter() != SkPaintDefaults_MiterLimit)) {
651386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             PathTexture* texture = renderer.caches().pathCache.getRect(
652386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                     op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.paint);
653386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             const AutoTexture holder(texture);
654386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             if (CC_LIKELY(holder.texture)) {
655e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                 renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
656e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                         *texture, *(op.paint));
657386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik             }
658386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        } else {
659386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            SkPath path;
660386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.addRect(getBoundsOfFill(op));
661386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderConvexPath(renderer, state, path, *(op.paint));
662386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
663386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
664386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (op.paint->isAntiAlias() && !state.computedState.transform.isSimple()) {
665386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            SkPath path;
666386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            path.addRect(op.unmappedBounds.toSkRect());
667386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderConvexPath(renderer, state, path, *(op.paint));
668386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        } else {
669386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            // render simple unit quad, no tessellation required
670386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            Glop glop;
671386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
672386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setRoundRectClipState(state.roundRectClipState)
673386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setMeshUnitQuad()
674386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setFillPaint(*op.paint, state.alpha)
675386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setTransform(state.computedState.transform, TransformFlags::None)
676386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .setModelViewMapUnitToRect(op.unmappedBounds)
677386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                    .build();
678386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik            renderer.renderGlop(state, glop);
679386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
680386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    }
681386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik}
682386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik
683386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craikvoid BakedOpDispatcher::onRoundRectOp(BakedOpRenderer& renderer, const RoundRectOp& op, const BakedOpState& state) {
684386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    if (op.paint->getPathEffect() != nullptr) {
685386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        PathTexture* texture = renderer.caches().pathCache.getRoundRect(
686386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(),
687386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.rx, op.ry, op.paint);
688386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const AutoTexture holder(texture);
689386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        if (CC_LIKELY(holder.texture)) {
690e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik            renderPathTexture(renderer, state, op.unmappedBounds.left, op.unmappedBounds.top,
691e2822e4c799815e6e5633af0f75fe5f221897d87Chris Craik                    *texture, *(op.paint));
692386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        }
693386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik    } else {
694386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        const VertexBuffer* buffer = renderer.caches().tessellationCache.getRoundRect(
695386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                state.computedState.transform, *(op.paint),
696386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
697386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik        renderVertexBuffer(renderer, state, *buffer,
698386aa031793bb037ec43b6cdbd8908c343cc86cbChris Craik                op.unmappedBounds.left, op.unmappedBounds.top, *(op.paint), 0);
6999e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7009e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7019e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7029e7fcfda28fde747ba4e026772007cea77374e16Chris Craikstatic void renderShadow(BakedOpRenderer& renderer, const BakedOpState& state, float casterAlpha,
7039e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
7049e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    SkPaint paint;
7059e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    paint.setAntiAlias(true); // want to use AlphaVertex
7069e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7079e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    // The caller has made sure casterAlpha > 0.
7089e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    uint8_t ambientShadowAlpha = renderer.getLightInfo().ambientShadowAlpha;
7099e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
7109e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        ambientShadowAlpha = Properties::overrideAmbientShadowStrength;
7119e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7129e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
7139e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        paint.setAlpha((uint8_t)(casterAlpha * ambientShadowAlpha));
7149e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        renderVertexBuffer(renderer, state, *ambientShadowVertexBuffer, 0, 0,
7159e7fcfda28fde747ba4e026772007cea77374e16Chris Craik                paint, VertexBufferRenderFlags::ShadowInterp);
7169e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7179e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7189e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    uint8_t spotShadowAlpha = renderer.getLightInfo().spotShadowAlpha;
7199e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
7209e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        spotShadowAlpha = Properties::overrideSpotShadowStrength;
7219e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7229e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
7239e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        paint.setAlpha((uint8_t)(casterAlpha * spotShadowAlpha));
7249e7fcfda28fde747ba4e026772007cea77374e16Chris Craik        renderVertexBuffer(renderer, state, *spotShadowVertexBuffer, 0, 0,
7259e7fcfda28fde747ba4e026772007cea77374e16Chris Craik                paint, VertexBufferRenderFlags::ShadowInterp);
7269e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    }
7279e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7289e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7299e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onShadowOp(BakedOpRenderer& renderer, const ShadowOp& op, const BakedOpState& state) {
730d8165e863b37d82c937ad960dc970f2d2cf72c2aChris Craik    TessellationCache::vertexBuffer_pair_t buffers = op.shadowTask->getResult();
7319e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderShadow(renderer, state, op.casterAlpha, buffers.first, buffers.second);
7329e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7339e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7349e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onSimpleRectsOp(BakedOpRenderer& renderer, const SimpleRectsOp& op, const BakedOpState& state) {
7359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    Glop glop;
7369e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
7379e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setRoundRectClipState(state.roundRectClipState)
7389e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setMeshIndexedQuads(&op.vertices[0], op.vertexCount / 4)
7399e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setFillPaint(*op.paint, state.alpha)
7409e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
7419e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .setModelViewOffsetRect(0, 0, op.unmappedBounds)
7429e7fcfda28fde747ba4e026772007cea77374e16Chris Craik            .build();
7439e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    renderer.renderGlop(state, glop);
7449e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7459e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
7469e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onTextOp(BakedOpRenderer& renderer, const TextOp& op, const BakedOpState& state) {
747284b765e3c1647859d4dac772744e8859c033216sergeyv    renderTextShadow(renderer, op, state);
748284b765e3c1647859d4dac772744e8859c033216sergeyv    renderText(renderer, op, state, state.computedState.getClipIfNeeded(), TextRenderType::Flush);
7499e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
7509e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
751d7448e65e243754f31890baef29dff187dc2e5e5Chris Craikvoid BakedOpDispatcher::onTextOnPathOp(BakedOpRenderer& renderer, const TextOnPathOp& op, const BakedOpState& state) {
752d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    // Note: can't trust clipSideFlags since we record with unmappedBounds == clip.
753d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    // TODO: respect clipSideFlags, once we record with bounds
754e4db79de127cfe961195f52907af8451026eaa20Chris Craik    auto renderTargetClip = state.computedState.clipState;
755d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
756d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    FontRenderer& fontRenderer = renderer.caches().fontRenderer.getFontRenderer();
757d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    fontRenderer.setFont(op.paint, SkMatrix::I());
758d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    fontRenderer.setTextureFiltering(true);
759d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
760d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
761d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
762d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    int alpha = PaintUtils::getAlphaDirect(op.paint) * state.alpha;
763d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    SkXfermode::Mode mode = PaintUtils::getXfermodeDirect(op.paint);
764d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    TextDrawFunctor functor(&renderer, &state, renderTargetClip,
765d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            0.0f, 0.0f, false, alpha, mode, op.paint);
766d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
767d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    bool mustDirtyRenderTarget = renderer.offscreenRenderTarget();
768d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    const Rect localSpaceClip = state.computedState.computeLocalSpaceClip();
769e8c3c813b0e3ac98304b17a751ce6e436e252bd9Chris Craik    if (fontRenderer.renderTextOnPath(op.paint, &localSpaceClip, op.glyphs, op.glyphCount,
770d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            op.path, op.hOffset, op.vOffset,
771d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            mustDirtyRenderTarget ? &layerBounds : nullptr, &functor)) {
772d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik        if (mustDirtyRenderTarget) {
773d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            // manually dirty render target, since TextDrawFunctor won't
774d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            state.computedState.transform.mapRect(layerBounds);
775d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik            renderer.dirtyRenderTarget(layerBounds);
776d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik        }
777d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik    }
778d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik}
779d7448e65e243754f31890baef29dff187dc2e5e5Chris Craik
780d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craikvoid BakedOpDispatcher::onTextureLayerOp(BakedOpRenderer& renderer, const TextureLayerOp& op, const BakedOpState& state) {
781d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    const bool tryToSnap = !op.layer->getForceFilter();
782d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    float alpha = (op.layer->getAlpha() / 255.0f) * state.alpha;
783d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    Glop glop;
784d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
785d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .setRoundRectClipState(state.roundRectClipState)
786d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .setMeshTexturedUvQuad(nullptr, Rect(0, 1, 1, 0)) // TODO: simplify with VBO
787d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .setFillTextureLayer(*(op.layer), alpha)
788d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .setTransform(state.computedState.transform, TransformFlags::None)
789d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .setModelViewMapUnitToRectOptionalSnap(tryToSnap, Rect(op.layer->getWidth(), op.layer->getHeight()))
790d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik            .build();
791d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik    renderer.renderGlop(state, glop);
792d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik}
793d2dfd8f128b632ed99418ab2b32949c939a9a369Chris Craik
79437413289478a965336239c731ebfea37ac4dde28Chris Craikvoid renderRectForLayer(BakedOpRenderer& renderer, const LayerOp& op, const BakedOpState& state,
79537413289478a965336239c731ebfea37ac4dde28Chris Craik        int color, SkXfermode::Mode mode, SkColorFilter* colorFilter) {
79637413289478a965336239c731ebfea37ac4dde28Chris Craik    SkPaint paint;
79737413289478a965336239c731ebfea37ac4dde28Chris Craik    paint.setColor(color);
79837413289478a965336239c731ebfea37ac4dde28Chris Craik    paint.setXfermodeMode(mode);
79937413289478a965336239c731ebfea37ac4dde28Chris Craik    paint.setColorFilter(colorFilter);
80037413289478a965336239c731ebfea37ac4dde28Chris Craik    RectOp rectOp(op.unmappedBounds, op.localMatrix, op.localClip, &paint);
80137413289478a965336239c731ebfea37ac4dde28Chris Craik    BakedOpDispatcher::onRectOp(renderer, rectOp, state);
80237413289478a965336239c731ebfea37ac4dde28Chris Craik}
80337413289478a965336239c731ebfea37ac4dde28Chris Craik
8049e7fcfda28fde747ba4e026772007cea77374e16Chris Craikvoid BakedOpDispatcher::onLayerOp(BakedOpRenderer& renderer, const LayerOp& op, const BakedOpState& state) {
805c0f3f2f8c0c2fa97099e5f3c5e3860653ce0f261Chris Craik    // Note that we don't use op->paint in this function - it's never set on a LayerOp
8069e7fcfda28fde747ba4e026772007cea77374e16Chris Craik    OffscreenBuffer* buffer = *op.layerHandle;
8079e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
808aebbbef5658fea13b06586d39adca351135d9edbsergeyv    if (CC_UNLIKELY(!buffer)) return;
809aebbbef5658fea13b06586d39adca351135d9edbsergeyv
810aebbbef5658fea13b06586d39adca351135d9edbsergeyv    float layerAlpha = op.alpha * state.alpha;
811aebbbef5658fea13b06586d39adca351135d9edbsergeyv    Glop glop;
812aebbbef5658fea13b06586d39adca351135d9edbsergeyv    GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
813aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .setRoundRectClipState(state.roundRectClipState)
814aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .setMeshTexturedIndexedVbo(buffer->vbo, buffer->elementCount)
815aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .setFillLayer(buffer->texture, op.colorFilter, layerAlpha, op.mode, Blend::ModeOrderSwap::NoSwap)
816aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .setTransform(state.computedState.transform, TransformFlags::None)
817aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .setModelViewOffsetRectSnap(op.unmappedBounds.left, op.unmappedBounds.top,
818aebbbef5658fea13b06586d39adca351135d9edbsergeyv                    Rect(op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight()))
819aebbbef5658fea13b06586d39adca351135d9edbsergeyv            .build();
820aebbbef5658fea13b06586d39adca351135d9edbsergeyv    renderer.renderGlop(state, glop);
82137413289478a965336239c731ebfea37ac4dde28Chris Craik
822aebbbef5658fea13b06586d39adca351135d9edbsergeyv    if (!buffer->hasRenderedSinceRepaint) {
82337413289478a965336239c731ebfea37ac4dde28Chris Craik        buffer->hasRenderedSinceRepaint = true;
82437413289478a965336239c731ebfea37ac4dde28Chris Craik        if (CC_UNLIKELY(Properties::debugLayersUpdates)) {
82537413289478a965336239c731ebfea37ac4dde28Chris Craik            // render debug layer highlight
82637413289478a965336239c731ebfea37ac4dde28Chris Craik            renderRectForLayer(renderer, op, state,
82737413289478a965336239c731ebfea37ac4dde28Chris Craik                    0x7f00ff00, SkXfermode::Mode::kSrcOver_Mode, nullptr);
82837413289478a965336239c731ebfea37ac4dde28Chris Craik        } else if (CC_UNLIKELY(Properties::debugOverdraw)) {
82937413289478a965336239c731ebfea37ac4dde28Chris Craik            // render transparent to increment overdraw for repaint area
83037413289478a965336239c731ebfea37ac4dde28Chris Craik            renderRectForLayer(renderer, op, state,
83137413289478a965336239c731ebfea37ac4dde28Chris Craik                    SK_ColorTRANSPARENT, SkXfermode::Mode::kSrcOver_Mode, nullptr);
83237413289478a965336239c731ebfea37ac4dde28Chris Craik        }
83337413289478a965336239c731ebfea37ac4dde28Chris Craik    }
8349e7fcfda28fde747ba4e026772007cea77374e16Chris Craik}
8359e7fcfda28fde747ba4e026772007cea77374e16Chris Craik
836b87eadda1818034ce03d85f30388384d1ac65916Chris Craikvoid BakedOpDispatcher::onCopyToLayerOp(BakedOpRenderer& renderer, const CopyToLayerOp& op, const BakedOpState& state) {
8377435eb148e72382126e9073183e881357bb38a8bChris Craik    LOG_ALWAYS_FATAL_IF(*(op.layerHandle) != nullptr, "layer already exists!");
8387435eb148e72382126e9073183e881357bb38a8bChris Craik    *(op.layerHandle) = renderer.copyToLayer(state.computedState.clippedBounds);
8397435eb148e72382126e9073183e881357bb38a8bChris Craik    LOG_ALWAYS_FATAL_IF(*op.layerHandle == nullptr, "layer copy failed");
840b87eadda1818034ce03d85f30388384d1ac65916Chris Craik}
841b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
842b87eadda1818034ce03d85f30388384d1ac65916Chris Craikvoid BakedOpDispatcher::onCopyFromLayerOp(BakedOpRenderer& renderer, const CopyFromLayerOp& op, const BakedOpState& state) {
8437435eb148e72382126e9073183e881357bb38a8bChris Craik    LOG_ALWAYS_FATAL_IF(*op.layerHandle == nullptr, "no layer to draw underneath!");
8447435eb148e72382126e9073183e881357bb38a8bChris Craik    if (!state.computedState.clippedBounds.isEmpty()) {
8457435eb148e72382126e9073183e881357bb38a8bChris Craik        if (op.paint && op.paint->getAlpha() < 255) {
8467435eb148e72382126e9073183e881357bb38a8bChris Craik            SkPaint layerPaint;
8477435eb148e72382126e9073183e881357bb38a8bChris Craik            layerPaint.setAlpha(op.paint->getAlpha());
8487435eb148e72382126e9073183e881357bb38a8bChris Craik            layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
8497435eb148e72382126e9073183e881357bb38a8bChris Craik            layerPaint.setColorFilter(op.paint->getColorFilter());
8507435eb148e72382126e9073183e881357bb38a8bChris Craik            RectOp rectOp(state.computedState.clippedBounds, Matrix4::identity(), nullptr, &layerPaint);
8517435eb148e72382126e9073183e881357bb38a8bChris Craik            BakedOpDispatcher::onRectOp(renderer, rectOp, state);
8527435eb148e72382126e9073183e881357bb38a8bChris Craik        }
8537435eb148e72382126e9073183e881357bb38a8bChris Craik
8547435eb148e72382126e9073183e881357bb38a8bChris Craik        OffscreenBuffer& layer = **(op.layerHandle);
8557435eb148e72382126e9073183e881357bb38a8bChris Craik        auto mode = PaintUtils::getXfermodeDirect(op.paint);
8567435eb148e72382126e9073183e881357bb38a8bChris Craik        Glop glop;
8577435eb148e72382126e9073183e881357bb38a8bChris Craik        GlopBuilder(renderer.renderState(), renderer.caches(), &glop)
8587435eb148e72382126e9073183e881357bb38a8bChris Craik                .setRoundRectClipState(state.roundRectClipState)
8597435eb148e72382126e9073183e881357bb38a8bChris Craik                .setMeshTexturedUvQuad(nullptr, layer.getTextureCoordinates())
8607435eb148e72382126e9073183e881357bb38a8bChris Craik                .setFillLayer(layer.texture, nullptr, 1.0f, mode, Blend::ModeOrderSwap::Swap)
8617435eb148e72382126e9073183e881357bb38a8bChris Craik                .setTransform(state.computedState.transform, TransformFlags::None)
8627435eb148e72382126e9073183e881357bb38a8bChris Craik                .setModelViewMapUnitToRect(state.computedState.clippedBounds)
8637435eb148e72382126e9073183e881357bb38a8bChris Craik                .build();
8647435eb148e72382126e9073183e881357bb38a8bChris Craik        renderer.renderGlop(state, glop);
8657435eb148e72382126e9073183e881357bb38a8bChris Craik    }
8667db5ffb7dbd30202468459e2ef4426e91d4fcbb3John Reck    renderer.renderState().layerPool().putOrDelete(*op.layerHandle);
867b87eadda1818034ce03d85f30388384d1ac65916Chris Craik}
868b87eadda1818034ce03d85f30388384d1ac65916Chris Craik
8699e7fcfda28fde747ba4e026772007cea77374e16Chris Craik} // namespace uirenderer
8709e7fcfda28fde747ba4e026772007cea77374e16Chris Craik} // namespace android
871