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 
25 class SkPaint;
26 class SkShader;
27 
28 namespace android {
29 namespace uirenderer {
30 
31 class Caches;
32 class Matrix4;
33 class RenderState;
34 class Texture;
35 class VertexBuffer;
36 
37 namespace TextureFillFlags {
38     enum {
39         None = 0,
40         IsAlphaMaskTexture = 1 << 0,
41         ForceFilter = 1 << 1,
42     };
43 }
44 
45 class GlopBuilder {
46     PREVENT_COPY_AND_ASSIGN(GlopBuilder);
47 public:
48     GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop);
49 
50     GlopBuilder& setMeshUnitQuad();
51     GlopBuilder& setMeshTexturedUnitQuad(const UvMapper* uvMapper);
52     GlopBuilder& setMeshTexturedUvQuad(const UvMapper* uvMapper, const Rect uvs);
53     GlopBuilder& setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp);
54     GlopBuilder& setMeshIndexedQuads(Vertex* vertexData, int quadCount);
55     GlopBuilder& setMeshTexturedMesh(TextureVertex* vertexData, int elementCount); // TODO: use indexed quads
56     GlopBuilder& setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount); // TODO: use indexed quads
57     GlopBuilder& setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount); // TODO: take quadCount
58     GlopBuilder& setMeshPatchQuads(const Patch& patch);
59 
60     GlopBuilder& setFillPaint(const SkPaint& paint, float alphaScale);
61     GlopBuilder& setFillTexturePaint(Texture& texture, const int textureFillFlags,
62             const SkPaint* paint, float alphaScale);
63     GlopBuilder& setFillPathTexturePaint(PathTexture& texture,
64             const SkPaint& paint, float alphaScale);
65     GlopBuilder& setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
66             const SkPaint& paint, float alphaScale);
67     GlopBuilder& setFillBlack();
68     GlopBuilder& setFillClear();
69     GlopBuilder& setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
70             float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage);
71     GlopBuilder& setFillTextureLayer(Layer& layer, float alpha);
72 
setTransform(const Snapshot & snapshot,const int transformFlags)73     GlopBuilder& setTransform(const Snapshot& snapshot, const int transformFlags) {
74         setTransform(snapshot.getOrthoMatrix(), *snapshot.transform, transformFlags);
75         return *this;
76     }
77 
78     GlopBuilder& setModelViewMapUnitToRect(const Rect destination);
79     GlopBuilder& setModelViewMapUnitToRectSnap(const Rect destination);
setModelViewMapUnitToRectOptionalSnap(bool snap,const Rect & destination)80     GlopBuilder& setModelViewMapUnitToRectOptionalSnap(bool snap, const Rect& destination) {
81         if (snap) {
82             return setModelViewMapUnitToRectSnap(destination);
83         } else {
84             return setModelViewMapUnitToRect(destination);
85         }
86     }
87     GlopBuilder& setModelViewOffsetRect(float offsetX, float offsetY, const Rect source);
88     GlopBuilder& setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source);
setModelViewOffsetRectOptionalSnap(bool snap,float offsetX,float offsetY,const Rect & source)89     GlopBuilder& setModelViewOffsetRectOptionalSnap(bool snap,
90             float offsetX, float offsetY, const Rect& source) {
91         if (snap) {
92             return setModelViewOffsetRectSnap(offsetX, offsetY, source);
93         } else {
94             return setModelViewOffsetRect(offsetX, offsetY, source);
95         }
96     }
97 
98     GlopBuilder& setRoundRectClipState(const RoundRectClipState* roundRectClipState);
99 
100     void build();
101 private:
102     void setFill(int color, float alphaScale,
103             SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage,
104             const SkShader* shader, const SkColorFilter* colorFilter);
105     void setTransform(const Matrix4& ortho, const Matrix4& canvas,
106             const int transformFlags);
107 
108     enum StageFlags {
109         kInitialStage = 0,
110         kMeshStage = 1 << 0,
111         kTransformStage = 1 << 1,
112         kModelViewStage = 1 << 2,
113         kFillStage = 1 << 3,
114         kRoundRectClipStage = 1 << 4,
115         kAllStages = kMeshStage | kFillStage | kTransformStage | kModelViewStage | kRoundRectClipStage,
116     } mStageFlags;
117 
118     ProgramDescription mDescription;
119     RenderState& mRenderState;
120     Caches& mCaches;
121     const SkShader* mShader;
122     Glop* mOutGlop;
123 };
124 
125 } /* namespace uirenderer */
126 } /* namespace android */
127 
128 #endif // RENDERSTATE_GLOPBUILDER_H
129