• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "GlopBuilder.h"
17 
18 #include "Caches.h"
19 #include "GlLayer.h"
20 #include "Glop.h"
21 #include "Layer.h"
22 #include "Matrix.h"
23 #include "Patch.h"
24 #include "PathCache.h"
25 #include "renderstate/MeshState.h"
26 #include "renderstate/RenderState.h"
27 #include "SkiaShader.h"
28 #include "Texture.h"
29 #include "utils/PaintUtils.h"
30 #include "VertexBuffer.h"
31 
32 #include <GLES2/gl2.h>
33 #include <SkPaint.h>
34 
35 #define DEBUG_GLOP_BUILDER 0
36 
37 #if DEBUG_GLOP_BUILDER
38 
39 #define TRIGGER_STAGE(stageFlag) \
40     LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \
41     mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
42 
43 #define REQUIRE_STAGES(requiredFlags) \
44     LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
45             "not prepared for current stage")
46 
47 #else
48 
49 #define TRIGGER_STAGE(stageFlag) ((void)0)
50 #define REQUIRE_STAGES(requiredFlags) ((void)0)
51 
52 #endif
53 
54 namespace android {
55 namespace uirenderer {
56 
setUnitQuadTextureCoords(Rect uvs,TextureVertex * quadVertex)57 static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
58     quadVertex[0] = {0, 0, uvs.left, uvs.top};
59     quadVertex[1] = {1, 0, uvs.right, uvs.top};
60     quadVertex[2] = {0, 1, uvs.left, uvs.bottom};
61     quadVertex[3] = {1, 1, uvs.right, uvs.bottom};
62 }
63 
GlopBuilder(RenderState & renderState,Caches & caches,Glop * outGlop)64 GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
65         : mRenderState(renderState)
66         , mCaches(caches)
67         , mShader(nullptr)
68         , mOutGlop(outGlop) {
69     mStageFlags = kInitialStage;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 // Mesh
74 ////////////////////////////////////////////////////////////////////////////////
75 
setMeshTexturedIndexedVbo(GLuint vbo,GLsizei elementCount)76 GlopBuilder& GlopBuilder::setMeshTexturedIndexedVbo(GLuint vbo, GLsizei elementCount) {
77     TRIGGER_STAGE(kMeshStage);
78 
79     mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
80     mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
81     mOutGlop->mesh.vertices = {
82             vbo,
83             VertexAttribFlags::TextureCoord,
84             nullptr, (const void*) kMeshTextureOffset, nullptr,
85             kTextureVertexStride };
86     mOutGlop->mesh.elementCount = elementCount;
87     return *this;
88 }
89 
setMeshUnitQuad()90 GlopBuilder& GlopBuilder::setMeshUnitQuad() {
91     TRIGGER_STAGE(kMeshStage);
92 
93     mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
94     mOutGlop->mesh.indices = { 0, nullptr };
95     mOutGlop->mesh.vertices = {
96             mRenderState.meshState().getUnitQuadVBO(),
97             VertexAttribFlags::None,
98             nullptr, nullptr, nullptr,
99             kTextureVertexStride };
100     mOutGlop->mesh.elementCount = 4;
101     return *this;
102 }
103 
setMeshTexturedUnitQuad(const UvMapper * uvMapper)104 GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) {
105     if (uvMapper) {
106         // can't use unit quad VBO, so build UV vertices manually
107         return setMeshTexturedUvQuad(uvMapper, Rect(1, 1));
108     }
109 
110     TRIGGER_STAGE(kMeshStage);
111 
112     mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
113     mOutGlop->mesh.indices = { 0, nullptr };
114     mOutGlop->mesh.vertices = {
115             mRenderState.meshState().getUnitQuadVBO(),
116             VertexAttribFlags::TextureCoord,
117             nullptr, (const void*) kMeshTextureOffset, nullptr,
118             kTextureVertexStride };
119     mOutGlop->mesh.elementCount = 4;
120     return *this;
121 }
122 
setMeshTexturedUvQuad(const UvMapper * uvMapper,Rect uvs)123 GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
124     TRIGGER_STAGE(kMeshStage);
125 
126     if (CC_UNLIKELY(uvMapper)) {
127         uvMapper->map(uvs);
128     }
129     setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
130 
131     const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices;
132     mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
133     mOutGlop->mesh.indices = { 0, nullptr };
134     mOutGlop->mesh.vertices = {
135             0,
136             VertexAttribFlags::TextureCoord,
137             &textureVertex[0].x, &textureVertex[0].u, nullptr,
138             kTextureVertexStride };
139     mOutGlop->mesh.elementCount = 4;
140     return *this;
141 }
142 
setMeshIndexedQuads(Vertex * vertexData,int quadCount)143 GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) {
144     TRIGGER_STAGE(kMeshStage);
145 
146     mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
147     mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
148     mOutGlop->mesh.vertices = {
149             0,
150             VertexAttribFlags::None,
151             vertexData, nullptr, nullptr,
152             kVertexStride };
153     mOutGlop->mesh.elementCount = 6 * quadCount;
154     return *this;
155 }
156 
setMeshTexturedIndexedQuads(TextureVertex * vertexData,int elementCount)157 GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
158     TRIGGER_STAGE(kMeshStage);
159 
160     mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
161     mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
162     mOutGlop->mesh.vertices = {
163             0,
164             VertexAttribFlags::TextureCoord,
165             &vertexData[0].x, &vertexData[0].u, nullptr,
166             kTextureVertexStride };
167     mOutGlop->mesh.elementCount = elementCount;
168     return *this;
169 }
170 
setMeshColoredTexturedMesh(ColorTextureVertex * vertexData,int elementCount)171 GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount) {
172     TRIGGER_STAGE(kMeshStage);
173 
174     mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
175     mOutGlop->mesh.indices = { 0, nullptr };
176     mOutGlop->mesh.vertices = {
177             0,
178             VertexAttribFlags::TextureCoord | VertexAttribFlags::Color,
179             &vertexData[0].x, &vertexData[0].u, &vertexData[0].r,
180             kColorTextureVertexStride };
181     mOutGlop->mesh.elementCount = elementCount;
182     return *this;
183 }
184 
setMeshVertexBuffer(const VertexBuffer & vertexBuffer)185 GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer) {
186     TRIGGER_STAGE(kMeshStage);
187 
188     const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
189 
190     bool alphaVertex = flags & VertexBuffer::kAlpha;
191     bool indices = flags & VertexBuffer::kIndices;
192 
193     mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
194     mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() };
195     mOutGlop->mesh.vertices = {
196             0,
197             alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None,
198             vertexBuffer.getBuffer(), nullptr, nullptr,
199             alphaVertex ? kAlphaVertexStride : kVertexStride };
200     mOutGlop->mesh.elementCount = indices
201                 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
202     mOutGlop->mesh.vertexCount = vertexBuffer.getVertexCount(); // used for glDrawRangeElements()
203     return *this;
204 }
205 
setMeshPatchQuads(const Patch & patch)206 GlopBuilder& GlopBuilder::setMeshPatchQuads(const Patch& patch) {
207     TRIGGER_STAGE(kMeshStage);
208 
209     mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
210     mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
211     mOutGlop->mesh.vertices = {
212             mCaches.patchCache.getMeshBuffer(),
213             VertexAttribFlags::TextureCoord,
214             (void*)patch.positionOffset, (void*)patch.textureOffset, nullptr,
215             kTextureVertexStride };
216     mOutGlop->mesh.elementCount = patch.indexCount;
217     return *this;
218 }
219 
220 ////////////////////////////////////////////////////////////////////////////////
221 // Fill
222 ////////////////////////////////////////////////////////////////////////////////
223 
setFill(int color,float alphaScale,SkBlendMode mode,Blend::ModeOrderSwap modeUsage,const SkShader * shader,const SkColorFilter * colorFilter)224 void GlopBuilder::setFill(int color, float alphaScale,
225         SkBlendMode mode, Blend::ModeOrderSwap modeUsage,
226         const SkShader* shader, const SkColorFilter* colorFilter) {
227     if (mode != SkBlendMode::kClear) {
228         if (!shader) {
229             FloatColor c;
230             c.set(color);
231             c.r *= alphaScale;
232             c.g *= alphaScale;
233             c.b *= alphaScale;
234             c.a *= alphaScale;
235             mOutGlop->fill.color = c;
236         } else {
237             float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
238             mOutGlop->fill.color = { 1, 1, 1, alpha };
239         }
240     } else {
241         mOutGlop->fill.color = { 0, 0, 0, 1 };
242     }
243 
244     mOutGlop->blend = { GL_ZERO, GL_ZERO };
245     if (mOutGlop->fill.color.a < 1.0f
246             || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
247             || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend)
248             || mOutGlop->roundRectClipState
249             || PaintUtils::isBlendedShader(shader)
250             || PaintUtils::isBlendedColorFilter(colorFilter)
251             || mode != SkBlendMode::kSrcOver) {
252         if (CC_LIKELY(mode <= SkBlendMode::kScreen)) {
253             Blend::getFactors(mode, modeUsage,
254                     &mOutGlop->blend.src, &mOutGlop->blend.dst);
255         } else {
256             // These blend modes are not supported by OpenGL directly and have
257             // to be implemented using shaders. Since the shader will perform
258             // the blending, don't enable GL blending off here
259             // If the blend mode cannot be implemented using shaders, fall
260             // back to the default SrcOver blend mode instead
261             if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
262                 mDescription.framebufferMode = mode;
263                 mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap);
264                 // blending in shader, don't enable
265             } else {
266                 // unsupported
267                 Blend::getFactors(SkBlendMode::kSrcOver, modeUsage,
268                         &mOutGlop->blend.src, &mOutGlop->blend.dst);
269             }
270         }
271     }
272     mShader = shader; // shader resolved in ::build()
273 
274     if (colorFilter) {
275         SkColor color;
276         SkBlendMode bmode;
277         SkScalar srcColorMatrix[20];
278         if (colorFilter->asColorMode(&color, &bmode)) {
279             mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::ColorFilterMode::Blend;
280             mDescription.colorMode = bmode;
281             mOutGlop->fill.filter.color.set(color);
282         } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
283             mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::ColorFilterMode::Matrix;
284 
285             float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
286             memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
287             memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
288             memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
289             memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
290 
291             // Skia uses the range [0..255] for the addition vector, but we need
292             // the [0..1] range to apply the vector in GLSL
293             float* colorVector = mOutGlop->fill.filter.matrix.vector;
294             colorVector[0] = EOCF(srcColorMatrix[4]  / 255.0f);
295             colorVector[1] = EOCF(srcColorMatrix[9]  / 255.0f);
296             colorVector[2] = EOCF(srcColorMatrix[14] / 255.0f);
297             colorVector[3] =      srcColorMatrix[19] / 255.0f;  // alpha is linear
298         } else {
299             LOG_ALWAYS_FATAL("unsupported ColorFilter");
300         }
301     } else {
302         mOutGlop->fill.filterMode = ProgramDescription::ColorFilterMode::None;
303     }
304 }
305 
setFillTexturePaint(Texture & texture,const int textureFillFlags,const SkPaint * paint,float alphaScale)306 GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture,
307         const int textureFillFlags, const SkPaint* paint, float alphaScale) {
308     TRIGGER_STAGE(kFillStage);
309     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
310 
311     GLenum filter = (textureFillFlags & TextureFillFlags::ForceFilter)
312             ? GL_LINEAR : PaintUtils::getFilter(paint);
313     mOutGlop->fill.texture = { &texture, filter, GL_CLAMP_TO_EDGE, nullptr };
314 
315     if (paint) {
316         int color = paint->getColor();
317         SkShader* shader = paint->getShader();
318 
319         if (!(textureFillFlags & TextureFillFlags::IsAlphaMaskTexture)) {
320             // Texture defines color, so disable shaders, and reset all non-alpha color channels
321             color |= 0x00FFFFFF;
322             shader = nullptr;
323         }
324         setFill(color, alphaScale,
325                 paint->getBlendMode(), Blend::ModeOrderSwap::NoSwap,
326                 shader, paint->getColorFilter());
327     } else {
328         mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
329 
330         if (alphaScale < 1.0f
331                 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
332                 || texture.blend
333                 || mOutGlop->roundRectClipState) {
334             Blend::getFactors(SkBlendMode::kSrcOver, Blend::ModeOrderSwap::NoSwap,
335                     &mOutGlop->blend.src, &mOutGlop->blend.dst);
336         } else {
337             mOutGlop->blend = { GL_ZERO, GL_ZERO };
338         }
339     }
340 
341     if (textureFillFlags & TextureFillFlags::IsAlphaMaskTexture) {
342         mDescription.modulate = mOutGlop->fill.color.isNotBlack();
343         mDescription.hasAlpha8Texture = true;
344     } else {
345         mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
346     }
347     return *this;
348 }
349 
setFillPaint(const SkPaint & paint,float alphaScale,bool shadowInterp)350 GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale, bool shadowInterp) {
351     TRIGGER_STAGE(kFillStage);
352     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
353 
354     if (CC_LIKELY(!shadowInterp)) {
355         mOutGlop->fill.texture = {
356                 nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
357     } else {
358         mOutGlop->fill.texture = {
359                 mCaches.textureState().getShadowLutTexture(),
360                 GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
361     }
362 
363     setFill(paint.getColor(), alphaScale,
364             paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
365             paint.getShader(), paint.getColorFilter());
366     mDescription.useShadowAlphaInterp = shadowInterp;
367     mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
368     return *this;
369 }
370 
setFillPathTexturePaint(PathTexture & texture,const SkPaint & paint,float alphaScale)371 GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
372         const SkPaint& paint, float alphaScale) {
373     TRIGGER_STAGE(kFillStage);
374     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
375 
376     //specify invalid filter/clamp, since these are always static for PathTextures
377     mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
378 
379     setFill(paint.getColor(), alphaScale,
380             paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
381             paint.getShader(), paint.getColorFilter());
382 
383     mDescription.hasAlpha8Texture = true;
384     mDescription.modulate = mOutGlop->fill.color.isNotBlack();
385     return *this;
386 }
387 
setFillShadowTexturePaint(ShadowTexture & texture,int shadowColor,const SkPaint & paint,float alphaScale)388 GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
389         const SkPaint& paint, float alphaScale) {
390     TRIGGER_STAGE(kFillStage);
391     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
392 
393     //specify invalid filter/clamp, since these are always static for ShadowTextures
394     mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
395 
396     const int ALPHA_BITMASK = SK_ColorBLACK;
397     const int COLOR_BITMASK = ~ALPHA_BITMASK;
398     if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
399         // shadow color is fully opaque: override its alpha with that of paint
400         shadowColor &= paint.getColor() | COLOR_BITMASK;
401     }
402 
403     setFill(shadowColor, alphaScale,
404             paint.getBlendMode(), Blend::ModeOrderSwap::NoSwap,
405             paint.getShader(), paint.getColorFilter());
406 
407     mDescription.hasAlpha8Texture = true;
408     mDescription.modulate = mOutGlop->fill.color.isNotBlack();
409     return *this;
410 }
411 
setFillBlack()412 GlopBuilder& GlopBuilder::setFillBlack() {
413     TRIGGER_STAGE(kFillStage);
414     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
415 
416     mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
417     setFill(SK_ColorBLACK, 1.0f, SkBlendMode::kSrcOver, Blend::ModeOrderSwap::NoSwap,
418             nullptr, nullptr);
419     return *this;
420 }
421 
setFillClear()422 GlopBuilder& GlopBuilder::setFillClear() {
423     TRIGGER_STAGE(kFillStage);
424     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
425 
426     mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
427     setFill(SK_ColorBLACK, 1.0f, SkBlendMode::kClear, Blend::ModeOrderSwap::NoSwap,
428             nullptr, nullptr);
429     return *this;
430 }
431 
setFillLayer(Texture & texture,const SkColorFilter * colorFilter,float alpha,SkBlendMode mode,Blend::ModeOrderSwap modeUsage)432 GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
433         float alpha, SkBlendMode mode, Blend::ModeOrderSwap modeUsage) {
434     TRIGGER_STAGE(kFillStage);
435     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
436 
437     mOutGlop->fill.texture = { &texture, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr };
438 
439     setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter);
440 
441     mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
442     return *this;
443 }
444 
setFillTextureLayer(GlLayer & layer,float alpha)445 GlopBuilder& GlopBuilder::setFillTextureLayer(GlLayer& layer, float alpha) {
446     TRIGGER_STAGE(kFillStage);
447     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
448 
449     mOutGlop->fill.texture = { &(layer.getTexture()),
450             GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() };
451 
452     setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap,
453             nullptr, layer.getColorFilter());
454 
455     mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
456     mDescription.hasTextureTransform = true;
457     return *this;
458 }
459 
setFillExternalTexture(Texture & texture,Matrix4 & textureTransform,bool requiresFilter)460 GlopBuilder& GlopBuilder::setFillExternalTexture(Texture& texture, Matrix4& textureTransform,
461         bool requiresFilter) {
462     TRIGGER_STAGE(kFillStage);
463     REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
464 
465     GLenum filter = requiresFilter ? GL_LINEAR : GL_NEAREST;
466     mOutGlop->fill.texture = { &texture, filter, GL_CLAMP_TO_EDGE, &textureTransform };
467 
468     setFill(SK_ColorWHITE, 1.0f, SkBlendMode::kSrc, Blend::ModeOrderSwap::NoSwap,
469             nullptr, nullptr);
470 
471     mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
472     mDescription.hasTextureTransform = true;
473     return *this;
474 }
475 
setGammaCorrection(bool enabled)476 GlopBuilder& GlopBuilder::setGammaCorrection(bool enabled) {
477     REQUIRE_STAGES(kFillStage);
478 
479     mDescription.hasGammaCorrection = enabled;
480     return *this;
481 }
482 
483 ////////////////////////////////////////////////////////////////////////////////
484 // Transform
485 ////////////////////////////////////////////////////////////////////////////////
486 
setTransform(const Matrix4 & canvas,const int transformFlags)487 GlopBuilder& GlopBuilder::setTransform(const Matrix4& canvas, const int transformFlags) {
488     TRIGGER_STAGE(kTransformStage);
489 
490     mOutGlop->transform.canvas = canvas;
491     mOutGlop->transform.transformFlags = transformFlags;
492     return *this;
493 }
494 
495 ////////////////////////////////////////////////////////////////////////////////
496 // ModelView
497 ////////////////////////////////////////////////////////////////////////////////
498 
setModelViewMapUnitToRect(const Rect destination)499 GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
500     TRIGGER_STAGE(kModelViewStage);
501 
502     mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
503     mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
504     return *this;
505 }
506 
setModelViewMapUnitToRectSnap(const Rect destination)507 GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
508     TRIGGER_STAGE(kModelViewStage);
509     REQUIRE_STAGES(kTransformStage | kFillStage);
510 
511     float left = destination.left;
512     float top = destination.top;
513 
514     const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
515     if (CC_LIKELY(meshTransform.isPureTranslate())) {
516         // snap by adjusting the model view matrix
517         const float translateX = meshTransform.getTranslateX();
518         const float translateY = meshTransform.getTranslateY();
519 
520         left = (int) floorf(left + translateX + 0.5f) - translateX;
521         top = (int) floorf(top + translateY + 0.5f) - translateY;
522         mOutGlop->fill.texture.filter = GL_NEAREST;
523     }
524 
525     mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
526     mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
527     return *this;
528 }
529 
setModelViewOffsetRect(float offsetX,float offsetY,const Rect source)530 GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
531     TRIGGER_STAGE(kModelViewStage);
532 
533     mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
534     return *this;
535 }
536 
setModelViewOffsetRectSnap(float offsetX,float offsetY,const Rect source)537 GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
538     TRIGGER_STAGE(kModelViewStage);
539     REQUIRE_STAGES(kTransformStage | kFillStage);
540 
541     const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
542     if (CC_LIKELY(meshTransform.isPureTranslate())) {
543         // snap by adjusting the model view matrix
544         const float translateX = meshTransform.getTranslateX();
545         const float translateY = meshTransform.getTranslateY();
546 
547         offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
548         offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
549         mOutGlop->fill.texture.filter = GL_NEAREST;
550     }
551 
552     mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
553     return *this;
554 }
555 
556 ////////////////////////////////////////////////////////////////////////////////
557 // RoundRectClip
558 ////////////////////////////////////////////////////////////////////////////////
559 
setRoundRectClipState(const RoundRectClipState * roundRectClipState)560 GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
561     TRIGGER_STAGE(kRoundRectClipStage);
562 
563     mOutGlop->roundRectClipState = roundRectClipState;
564     mDescription.hasRoundRectClip = roundRectClipState != nullptr;
565     return *this;
566 }
567 
568 ////////////////////////////////////////////////////////////////////////////////
569 // Build
570 ////////////////////////////////////////////////////////////////////////////////
571 
verify(const ProgramDescription & description,const Glop & glop)572 void verify(const ProgramDescription& description, const Glop& glop) {
573     if (glop.fill.texture.texture != nullptr) {
574         LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture)
575                         || (!description.hasTexture
576                                 && !description.hasExternalTexture
577                                 && !description.useShadowAlphaInterp)
578                         || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0
579                                 && !description.useShadowAlphaInterp)),
580                 "Texture %p, hT%d, hET %d, attribFlags %x",
581                 glop.fill.texture.texture,
582                 description.hasTexture, description.hasExternalTexture,
583                 glop.mesh.vertices.attribFlags);
584     } else {
585         LOG_ALWAYS_FATAL_IF((description.hasTexture
586                         || description.hasExternalTexture
587                         || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)),
588                 "No texture, hT%d, hET %d, attribFlags %x",
589                 description.hasTexture, description.hasExternalTexture,
590                 glop.mesh.vertices.attribFlags);
591     }
592 
593     if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
594             && glop.mesh.vertices.bufferObject) {
595         LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
596     }
597 
598     if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) {
599         LOG_ALWAYS_FATAL("Texture transform incorrectly specified");
600     }
601 }
602 
build()603 void GlopBuilder::build() {
604     REQUIRE_STAGES(kAllStages);
605     if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) {
606         Texture* texture = mOutGlop->fill.texture.texture;
607         if (texture->target() == GL_TEXTURE_2D) {
608             mDescription.hasTexture = true;
609         } else {
610             mDescription.hasExternalTexture = true;
611         }
612         mDescription.hasLinearTexture = texture->isLinear();
613         mDescription.hasColorSpaceConversion = texture->hasColorSpaceConversion();
614         mDescription.transferFunction = texture->getTransferFunctionType();
615         mDescription.hasTranslucentConversion = texture->blend;
616     }
617 
618     mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color;
619     mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha;
620 
621     // Enable debug highlight when what we're about to draw is tested against
622     // the stencil buffer and if stencil highlight debugging is on
623     mDescription.hasDebugHighlight = !Properties::debugOverdraw
624             && Properties::debugStencilClip == StencilClipDebug::ShowHighlight
625             && mRenderState.stencil().isTestEnabled();
626 
627     // serialize shader info into ShaderData
628     GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
629 
630     if (CC_LIKELY(!mShader)) {
631         mOutGlop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType;
632     } else {
633         Matrix4 shaderMatrix;
634         if (mOutGlop->transform.transformFlags & TransformFlags::MeshIgnoresCanvasTransform) {
635             // canvas level transform was built into the modelView and geometry,
636             // so the shader matrix must reverse this
637             shaderMatrix.loadInverse(mOutGlop->transform.canvas);
638             shaderMatrix.multiply(mOutGlop->transform.modelView);
639         } else {
640             shaderMatrix = mOutGlop->transform.modelView;
641         }
642         SkiaShader::store(mCaches, *mShader, shaderMatrix,
643                 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
644     }
645 
646     // duplicates ProgramCache's definition of color uniform presence
647     const bool singleColor = !mDescription.hasTexture
648             && !mDescription.hasExternalTexture
649             && !mDescription.hasGradient
650             && !mDescription.hasBitmap;
651     mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
652 
653     verify(mDescription, *mOutGlop);
654 
655     // Final step: populate program and map bounds into render target space
656     mOutGlop->fill.program = mCaches.programCache.get(mDescription);
657 }
658 
dump(const Glop & glop)659 void GlopBuilder::dump(const Glop& glop) {
660     ALOGD("Glop Mesh");
661     const Glop::Mesh& mesh = glop.mesh;
662     ALOGD("    primitive mode: %d", mesh.primitiveMode);
663     ALOGD("    indices: buffer obj %x, indices %p", mesh.indices.bufferObject, mesh.indices.indices);
664 
665     const Glop::Mesh::Vertices& vertices = glop.mesh.vertices;
666     ALOGD("    vertices: buffer obj %x, flags %x, pos %p, tex %p, clr %p, stride %d",
667             vertices.bufferObject, vertices.attribFlags,
668             vertices.position, vertices.texCoord, vertices.color, vertices.stride);
669     ALOGD("    element count: %d", mesh.elementCount);
670 
671     ALOGD("Glop Fill");
672     const Glop::Fill& fill = glop.fill;
673     ALOGD("    program %p", fill.program);
674     if (fill.texture.texture) {
675         ALOGD("    texture %p, target %d, filter %d, clamp %d",
676                 fill.texture.texture, fill.texture.texture->target(),
677                 fill.texture.filter, fill.texture.clamp);
678         if (fill.texture.textureTransform) {
679             fill.texture.textureTransform->dump("texture transform");
680         }
681     }
682     ALOGD_IF(fill.colorEnabled, "    color (argb) %.2f %.2f %.2f %.2f",
683             fill.color.a, fill.color.r, fill.color.g, fill.color.b);
684     ALOGD_IF(fill.filterMode != ProgramDescription::ColorFilterMode::None,
685             "    filterMode %d", (int)fill.filterMode);
686     ALOGD_IF(fill.skiaShaderData.skiaShaderType, "    shader type %d",
687             fill.skiaShaderData.skiaShaderType);
688 
689     ALOGD("Glop transform");
690     glop.transform.modelView.dump("  model view");
691     glop.transform.canvas.dump("  canvas");
692     ALOGD_IF(glop.transform.transformFlags, "  transformFlags 0x%x", glop.transform.transformFlags);
693 
694     ALOGD_IF(glop.roundRectClipState, "Glop RRCS %p", glop.roundRectClipState);
695 
696     ALOGD("Glop blend %d %d", glop.blend.src, glop.blend.dst);
697 }
698 
699 } /* namespace uirenderer */
700 } /* namespace android */
701