GlopBuilder.h revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "Glop.h"
20#include "Program.h"
21#include "renderstate/Blend.h"
22#include "utils/Macros.h"
23
24class SkPaint;
25class SkShader;
26
27namespace android {
28namespace uirenderer {
29
30class Caches;
31class Matrix4;
32class Patch;
33class RenderState;
34class Texture;
35class UvMapper;
36class VertexBuffer;
37struct PathTexture;
38struct ShadowTexture;
39
40namespace TextureFillFlags {
41    enum {
42        None = 0,
43        IsAlphaMaskTexture = 1 << 0,
44        ForceFilter = 1 << 1,
45    };
46}
47
48class GlopBuilder {
49    PREVENT_COPY_AND_ASSIGN(GlopBuilder);
50public:
51    GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop);
52
53    GlopBuilder& setMeshTexturedIndexedVbo(GLuint vbo, GLsizei elementCount);
54    GlopBuilder& setMeshUnitQuad();
55    GlopBuilder& setMeshTexturedUnitQuad(const UvMapper* uvMapper);
56    GlopBuilder& setMeshTexturedUvQuad(const UvMapper* uvMapper, const Rect uvs);
57    GlopBuilder& setMeshVertexBuffer(const VertexBuffer& vertexBuffer);
58    GlopBuilder& setMeshIndexedQuads(Vertex* vertexData, int quadCount);
59    GlopBuilder& setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount); // TODO: use indexed quads
60    GlopBuilder& setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount); // TODO: take quadCount
61    GlopBuilder& setMeshPatchQuads(const Patch& patch);
62
63    GlopBuilder& setFillPaint(const SkPaint& paint, float alphaScale, bool shadowInterp = false); // TODO: avoid boolean with default
64    GlopBuilder& setFillTexturePaint(Texture& texture, const int textureFillFlags,
65            const SkPaint* paint, float alphaScale);
66    GlopBuilder& setFillPathTexturePaint(PathTexture& texture,
67            const SkPaint& paint, float alphaScale);
68    GlopBuilder& setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
69            const SkPaint& paint, float alphaScale);
70    GlopBuilder& setFillBlack();
71    GlopBuilder& setFillClear();
72    GlopBuilder& setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
73            float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage);
74    GlopBuilder& setFillTextureLayer(Layer& layer, float alpha);
75    // TODO: Texture should probably know and own its target.
76    // setFillLayer() forces it to GL_TEXTURE which isn't always correct.
77    // Similarly setFillLayer normally forces its own wrap & filter mode
78    GlopBuilder& setFillExternalTexture(Texture& texture, Matrix4& textureTransform);
79
80    GlopBuilder& setTransform(const Matrix4& canvas, const int transformFlags);
81
82    GlopBuilder& setModelViewMapUnitToRect(const Rect destination);
83    GlopBuilder& setModelViewMapUnitToRectSnap(const Rect destination);
84    GlopBuilder& setModelViewMapUnitToRectOptionalSnap(bool snap, const Rect& destination) {
85        if (snap) {
86            return setModelViewMapUnitToRectSnap(destination);
87        } else {
88            return setModelViewMapUnitToRect(destination);
89        }
90    }
91    GlopBuilder& setModelViewOffsetRect(float offsetX, float offsetY, const Rect source);
92    GlopBuilder& setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source);
93    GlopBuilder& setModelViewOffsetRectOptionalSnap(bool snap,
94            float offsetX, float offsetY, const Rect& source) {
95        if (snap) {
96            return setModelViewOffsetRectSnap(offsetX, offsetY, source);
97        } else {
98            return setModelViewOffsetRect(offsetX, offsetY, source);
99        }
100    }
101    GlopBuilder& setModelViewIdentityEmptyBounds() {
102        // pass empty rect since not needed for damage / snap
103        return setModelViewOffsetRect(0, 0, Rect());
104    }
105
106    GlopBuilder& setRoundRectClipState(const RoundRectClipState* roundRectClipState);
107
108    GlopBuilder& setGammaCorrection(bool enabled);
109
110    void build();
111
112    static void dump(const Glop& glop);
113private:
114    void setFill(int color, float alphaScale,
115            SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage,
116            const SkShader* shader, const SkColorFilter* colorFilter);
117
118    enum StageFlags {
119        kInitialStage = 0,
120        kMeshStage = 1 << 0,
121        kTransformStage = 1 << 1,
122        kModelViewStage = 1 << 2,
123        kFillStage = 1 << 3,
124        kRoundRectClipStage = 1 << 4,
125        kAllStages = kMeshStage | kFillStage | kTransformStage | kModelViewStage | kRoundRectClipStage,
126    } mStageFlags;
127
128    ProgramDescription mDescription;
129    RenderState& mRenderState;
130    Caches& mCaches;
131    const SkShader* mShader;
132    Glop* mOutGlop;
133};
134
135} /* namespace uirenderer */
136} /* namespace android */
137