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#ifndef RENDERSTATE_GLOPBUILDER_H
17#define RENDERSTATE_GLOPBUILDER_H
18
19#include "Glop.h"
20#include "OpenGLRenderer.h"
21#include "Program.h"
22#include "renderstate/Blend.h"
23#include "utils/Macros.h"
24
25class SkPaint;
26class SkShader;
27
28namespace android {
29namespace uirenderer {
30
31class Caches;
32class Matrix4;
33class RenderState;
34class Texture;
35class VertexBuffer;
36
37namespace TextureFillFlags {
38    enum {
39        None = 0,
40        IsAlphaMaskTexture = 1 << 0,
41        ForceFilter = 1 << 1,
42    };
43}
44
45class GlopBuilder {
46    PREVENT_COPY_AND_ASSIGN(GlopBuilder);
47public:
48    GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop);
49
50    GlopBuilder& setMeshTexturedIndexedVbo(GLuint vbo, GLsizei elementCount);
51    GlopBuilder& setMeshUnitQuad();
52    GlopBuilder& setMeshTexturedUnitQuad(const UvMapper* uvMapper);
53    GlopBuilder& setMeshTexturedUvQuad(const UvMapper* uvMapper, const Rect uvs);
54    GlopBuilder& setMeshVertexBuffer(const VertexBuffer& vertexBuffer);
55    GlopBuilder& setMeshIndexedQuads(Vertex* vertexData, int quadCount);
56    GlopBuilder& setMeshTexturedMesh(TextureVertex* vertexData, int elementCount); // TODO: delete
57    GlopBuilder& setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount); // TODO: use indexed quads
58    GlopBuilder& setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount); // TODO: take quadCount
59    GlopBuilder& setMeshPatchQuads(const Patch& patch);
60
61    GlopBuilder& setFillPaint(const SkPaint& paint, float alphaScale, bool shadowInterp = false); // TODO: avoid boolean with default
62    GlopBuilder& setFillTexturePaint(Texture& texture, const int textureFillFlags,
63            const SkPaint* paint, float alphaScale);
64    GlopBuilder& setFillPathTexturePaint(PathTexture& texture,
65            const SkPaint& paint, float alphaScale);
66    GlopBuilder& setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
67            const SkPaint& paint, float alphaScale);
68    GlopBuilder& setFillBlack();
69    GlopBuilder& setFillClear();
70    GlopBuilder& setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
71            float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage);
72    GlopBuilder& setFillTextureLayer(Layer& layer, float alpha);
73    // TODO: Texture should probably know and own its target.
74    // setFillLayer() forces it to GL_TEXTURE which isn't always correct.
75    // Similarly setFillLayer normally forces its own wrap & filter mode
76    GlopBuilder& setFillExternalTexture(Texture& texture, Matrix4& textureTransform);
77
78    GlopBuilder& setTransform(const Snapshot& snapshot, const int transformFlags) {
79        return setTransform(*snapshot.transform, transformFlags);
80    }
81    GlopBuilder& setTransform(const Matrix4& canvas, const int transformFlags);
82
83    GlopBuilder& setModelViewMapUnitToRect(const Rect destination);
84    GlopBuilder& setModelViewMapUnitToRectSnap(const Rect destination);
85    GlopBuilder& setModelViewMapUnitToRectOptionalSnap(bool snap, const Rect& destination) {
86        if (snap) {
87            return setModelViewMapUnitToRectSnap(destination);
88        } else {
89            return setModelViewMapUnitToRect(destination);
90        }
91    }
92    GlopBuilder& setModelViewOffsetRect(float offsetX, float offsetY, const Rect source);
93    GlopBuilder& setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source);
94    GlopBuilder& setModelViewOffsetRectOptionalSnap(bool snap,
95            float offsetX, float offsetY, const Rect& source) {
96        if (snap) {
97            return setModelViewOffsetRectSnap(offsetX, offsetY, source);
98        } else {
99            return setModelViewOffsetRect(offsetX, offsetY, source);
100        }
101    }
102    GlopBuilder& setModelViewIdentityEmptyBounds() {
103        // pass empty rect since not needed for damage / snap
104        return setModelViewOffsetRect(0, 0, Rect());
105    }
106
107    GlopBuilder& setRoundRectClipState(const RoundRectClipState* roundRectClipState);
108
109    void build();
110
111    static void dump(const Glop& glop);
112private:
113    void setFill(int color, float alphaScale,
114            SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage,
115            const SkShader* shader, const SkColorFilter* colorFilter);
116
117    enum StageFlags {
118        kInitialStage = 0,
119        kMeshStage = 1 << 0,
120        kTransformStage = 1 << 1,
121        kModelViewStage = 1 << 2,
122        kFillStage = 1 << 3,
123        kRoundRectClipStage = 1 << 4,
124        kAllStages = kMeshStage | kFillStage | kTransformStage | kModelViewStage | kRoundRectClipStage,
125    } mStageFlags;
126
127    ProgramDescription mDescription;
128    RenderState& mRenderState;
129    Caches& mCaches;
130    const SkShader* mShader;
131    Glop* mOutGlop;
132};
133
134} /* namespace uirenderer */
135} /* namespace android */
136
137#endif // RENDERSTATE_GLOPBUILDER_H
138