blob: 1342c52485e25534a7adf98e871e833c8bb51517 [file] [log] [blame]
Chris Craik03188872015-02-02 18:39:33 -08001/*
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 "Glop.h"
20#include "Matrix.h"
Chris Craik03188872015-02-02 18:39:33 -080021#include "renderstate/MeshState.h"
22#include "renderstate/RenderState.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080023#include "SkiaShader.h"
24#include "Texture.h"
Chris Craik03188872015-02-02 18:39:33 -080025#include "utils/PaintUtils.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080026#include "VertexBuffer.h"
Chris Craik03188872015-02-02 18:39:33 -080027
28#include <GLES2/gl2.h>
29#include <SkPaint.h>
30
31namespace android {
32namespace uirenderer {
33
Chris Craik117bdbc2015-02-05 10:12:38 -080034#define TRIGGER_STAGE(stageFlag) \
Chris Craik14100ac2015-02-24 13:46:29 -080035 LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \
Chris Craik0519c812015-02-11 13:17:06 -080036 mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
Chris Craik117bdbc2015-02-05 10:12:38 -080037
Chris Craik08fa43f2015-02-09 18:58:32 -080038#define REQUIRE_STAGES(requiredFlags) \
Chris Craik0519c812015-02-11 13:17:06 -080039 LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
Chris Craik08fa43f2015-02-09 18:58:32 -080040 "not prepared for current stage")
41
Chris Craik922d3a72015-02-13 17:47:21 -080042static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
Chris Craik14100ac2015-02-24 13:46:29 -080043 quadVertex[0] = {0, 0, uvs.left, uvs.top};
44 quadVertex[1] = {1, 0, uvs.right, uvs.top};
45 quadVertex[2] = {0, 1, uvs.left, uvs.bottom};
46 quadVertex[3] = {1, 1, uvs.right, uvs.bottom};
Chris Craik0519c812015-02-11 13:17:06 -080047}
48
Chris Craik03188872015-02-02 18:39:33 -080049GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
50 : mRenderState(renderState)
51 , mCaches(caches)
Chris Craik922d3a72015-02-13 17:47:21 -080052 , mShader(nullptr)
Chris Craik0519c812015-02-11 13:17:06 -080053 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080054 mStageFlags = kInitialStage;
55}
56
Chris Craik0519c812015-02-11 13:17:06 -080057////////////////////////////////////////////////////////////////////////////////
58// Mesh
59////////////////////////////////////////////////////////////////////////////////
60
61GlopBuilder& GlopBuilder::setMeshUnitQuad() {
62 TRIGGER_STAGE(kMeshStage);
63
64 mOutGlop->mesh.vertexFlags = kNone_Attrib;
65 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
66 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
67 mOutGlop->mesh.vertices = nullptr;
68 mOutGlop->mesh.indexBufferObject = 0;
69 mOutGlop->mesh.indices = nullptr;
70 mOutGlop->mesh.elementCount = 4;
71 mOutGlop->mesh.stride = kTextureVertexStride;
72 mOutGlop->mesh.texCoordOffset = nullptr;
73 return *this;
74}
75
Chris Craikf27133d2015-02-19 09:51:53 -080076GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) {
Chris Craik14100ac2015-02-24 13:46:29 -080077 if (uvMapper) {
78 // can't use unit quad VBO, so build UV vertices manually
79 return setMeshTexturedUvQuad(uvMapper, Rect(0, 0, 1, 1));
80 }
81
82 TRIGGER_STAGE(kMeshStage);
83
84 mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib;
85 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
86 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
87 mOutGlop->mesh.vertices = nullptr;
88 mOutGlop->mesh.texCoordOffset = (GLvoid*) kMeshTextureOffset;
89 mOutGlop->mesh.indexBufferObject = 0;
90 mOutGlop->mesh.indices = nullptr;
91 mOutGlop->mesh.elementCount = 4;
92 mOutGlop->mesh.stride = kTextureVertexStride;
93 mDescription.hasTexture = true;
94 return *this;
95}
96
97GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
Chris Craik0519c812015-02-11 13:17:06 -080098 TRIGGER_STAGE(kMeshStage);
99
100 mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib;
101 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
102
103 if (CC_UNLIKELY(uvMapper)) {
Chris Craik0519c812015-02-11 13:17:06 -0800104 uvMapper->map(uvs);
Chris Craik0519c812015-02-11 13:17:06 -0800105 }
Chris Craik14100ac2015-02-24 13:46:29 -0800106 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
107
108 mOutGlop->mesh.vertexBufferObject = 0;
109 mOutGlop->mesh.vertices = &mOutGlop->mesh.mappedVertices[0].x;
110 mOutGlop->mesh.texCoordOffset = &mOutGlop->mesh.mappedVertices[0].u;
Chris Craik0519c812015-02-11 13:17:06 -0800111 mOutGlop->mesh.indexBufferObject = 0;
112 mOutGlop->mesh.indices = nullptr;
113 mOutGlop->mesh.elementCount = 4;
114 mOutGlop->mesh.stride = kTextureVertexStride;
Chris Craik0519c812015-02-11 13:17:06 -0800115 mDescription.hasTexture = true;
Chris Craik0519c812015-02-11 13:17:06 -0800116 return *this;
117}
118
119GlopBuilder& GlopBuilder::setMeshIndexedQuads(void* vertexData, int quadCount) {
120 TRIGGER_STAGE(kMeshStage);
121
122 mOutGlop->mesh.vertexFlags = kNone_Attrib;
123 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
124 mOutGlop->mesh.vertexBufferObject = 0;
125 mOutGlop->mesh.vertices = vertexData;
126 mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO();
127 mOutGlop->mesh.indices = nullptr;
Chris Craikf27133d2015-02-19 09:51:53 -0800128 mOutGlop->mesh.texCoordOffset = nullptr;
Chris Craik0519c812015-02-11 13:17:06 -0800129 mOutGlop->mesh.elementCount = 6 * quadCount;
130 mOutGlop->mesh.stride = kVertexStride;
Chris Craik0519c812015-02-11 13:17:06 -0800131
132 return *this;
133}
134
Chris Craikf27133d2015-02-19 09:51:53 -0800135GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
136 TRIGGER_STAGE(kMeshStage);
137
138 mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib;
139 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
140 mOutGlop->mesh.vertexBufferObject = 0;
141 mOutGlop->mesh.vertices = &vertexData[0].x;
142 mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO();
143 mOutGlop->mesh.indices = nullptr;
144 mOutGlop->mesh.texCoordOffset = &vertexData[0].u;
145 mOutGlop->mesh.elementCount = elementCount;
146 mOutGlop->mesh.stride = kTextureVertexStride;
147 mDescription.hasTexture = true;
148 return *this;
149}
150
Chris Craik117bdbc2015-02-05 10:12:38 -0800151GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
152 TRIGGER_STAGE(kMeshStage);
153
154 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
155
156 bool alphaVertex = flags & VertexBuffer::kAlpha;
157 bool indices = flags & VertexBuffer::kIndices;
158 mOutGlop->mesh.vertexFlags = alphaVertex ? kAlpha_Attrib : kNone_Attrib;
159 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
160 mOutGlop->mesh.vertexBufferObject = 0;
161 mOutGlop->mesh.vertices = vertexBuffer.getBuffer();
162 mOutGlop->mesh.indexBufferObject = 0;
163 mOutGlop->mesh.indices = vertexBuffer.getIndices();
Chris Craikf27133d2015-02-19 09:51:53 -0800164 mOutGlop->mesh.texCoordOffset = nullptr;
Chris Craik0519c812015-02-11 13:17:06 -0800165 mOutGlop->mesh.elementCount = indices
Chris Craik117bdbc2015-02-05 10:12:38 -0800166 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
167 mOutGlop->mesh.stride = alphaVertex ? kAlphaVertexStride : kVertexStride;
168
169 mDescription.hasVertexAlpha = alphaVertex;
170 mDescription.useShadowAlphaInterp = shadowInterp;
171 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800172}
173
Chris Craik0519c812015-02-11 13:17:06 -0800174////////////////////////////////////////////////////////////////////////////////
175// Fill
176////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800177
Chris Craik0519c812015-02-11 13:17:06 -0800178void GlopBuilder::setFill(int color, float alphaScale, SkXfermode::Mode mode,
179 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800180 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800181 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800182 if (!shader) {
183 float colorScale = alpha / 255.0f;
184 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800185 colorScale * SkColorGetR(color),
186 colorScale * SkColorGetG(color),
Chris Craik0519c812015-02-11 13:17:06 -0800187 colorScale * SkColorGetB(color),
188 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800189 };
190 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800191 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800192 }
Chris Craik03188872015-02-02 18:39:33 -0800193 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800194 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800195 }
196 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800197
Chris Craik117bdbc2015-02-05 10:12:38 -0800198 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800199 if (mOutGlop->fill.color.a < 1.0f
Chris Craik08fa43f2015-02-09 18:58:32 -0800200 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
Chris Craikf27133d2015-02-19 09:51:53 -0800201 || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend)
Chris Craik0519c812015-02-11 13:17:06 -0800202 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800203 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800204 || PaintUtils::isBlendedColorFilter(colorFilter)
205 || mode != SkXfermode::kSrcOver_Mode) {
206 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
207 Blend::getFactors(mode, SWAP_SRC_DST,
208 &mOutGlop->blend.src, &mOutGlop->blend.dst);
209 } else {
210 // These blend modes are not supported by OpenGL directly and have
211 // to be implemented using shaders. Since the shader will perform
212 // the blending, don't enable GL blending off here
213 // If the blend mode cannot be implemented using shaders, fall
214 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800215 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800216 mDescription.framebufferMode = mode;
217 mDescription.swapSrcDst = SWAP_SRC_DST;
218 // blending in shader, don't enable
219 } else {
220 // unsupported
221 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
222 &mOutGlop->blend.src, &mOutGlop->blend.dst);
223 }
224 }
225 }
Chris Craik922d3a72015-02-13 17:47:21 -0800226 mShader = shader; // shader resolved in ::build()
Chris Craik03188872015-02-02 18:39:33 -0800227
Chris Craik117bdbc2015-02-05 10:12:38 -0800228 if (colorFilter) {
229 SkColor color;
230 SkXfermode::Mode mode;
231 SkScalar srcColorMatrix[20];
232 if (colorFilter->asColorMode(&color, &mode)) {
233 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
234 mDescription.colorMode = mode;
235
236 const float alpha = SkColorGetA(color) / 255.0f;
237 float colorScale = alpha / 255.0f;
238 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800239 colorScale * SkColorGetR(color),
240 colorScale * SkColorGetG(color),
241 colorScale * SkColorGetB(color),
Chris Craik0519c812015-02-11 13:17:06 -0800242 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800243 };
244 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
245 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
246
247 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
248 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
249 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
250 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
251 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
252
253 // Skia uses the range [0..255] for the addition vector, but we need
254 // the [0..1] range to apply the vector in GLSL
255 float* colorVector = mOutGlop->fill.filter.matrix.vector;
256 colorVector[0] = srcColorMatrix[4] / 255.0f;
257 colorVector[1] = srcColorMatrix[9] / 255.0f;
258 colorVector[2] = srcColorMatrix[14] / 255.0f;
259 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800260 } else {
261 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800262 }
263 } else {
264 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
265 }
Chris Craik0519c812015-02-11 13:17:06 -0800266}
Chris Craik117bdbc2015-02-05 10:12:38 -0800267
Chris Craik0519c812015-02-11 13:17:06 -0800268GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, bool isAlphaMaskTexture,
269 const SkPaint* paint, float alphaScale) {
270 TRIGGER_STAGE(kFillStage);
271 REQUIRE_STAGES(kMeshStage);
272
Chris Craikf27133d2015-02-19 09:51:53 -0800273 mOutGlop->fill.texture = { &texture, PaintUtils::getFilter(paint), GL_CLAMP_TO_EDGE };
Chris Craik0519c812015-02-11 13:17:06 -0800274
275 if (paint) {
276 int color = paint->getColor();
277 SkShader* shader = paint->getShader();
278
279 if (!isAlphaMaskTexture) {
280 // Texture defines color, so disable shaders, and reset all non-alpha color channels
281 color |= 0x00FFFFFF;
282 shader = nullptr;
283 }
284 setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()),
285 shader, paint->getColorFilter());
286 } else {
287 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
288
289 const bool SWAP_SRC_DST = false;
290 if (alphaScale < 1.0f
291 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
292 || texture.blend
293 || mOutGlop->roundRectClipState) {
294 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
295 &mOutGlop->blend.src, &mOutGlop->blend.dst);
296 } else {
297 mOutGlop->blend = { GL_ZERO, GL_ZERO };
298 }
299 }
300
Chris Craikf27133d2015-02-19 09:51:53 -0800301 mDescription.hasAlpha8Texture = isAlphaMaskTexture;
Chris Craik0519c812015-02-11 13:17:06 -0800302 if (isAlphaMaskTexture) {
Chris Craik2bb8f562015-02-17 16:42:02 -0800303 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik0519c812015-02-11 13:17:06 -0800304 } else {
305 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
306 }
Chris Craik03188872015-02-02 18:39:33 -0800307 return *this;
308}
309
Chris Craik0519c812015-02-11 13:17:06 -0800310GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
311 TRIGGER_STAGE(kFillStage);
312 REQUIRE_STAGES(kMeshStage);
313
Chris Craikf27133d2015-02-19 09:51:53 -0800314 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik0519c812015-02-11 13:17:06 -0800315
316 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
317 paint.getShader(), paint.getColorFilter());
318 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
319 return *this;
320}
321
Chris Craik2bb8f562015-02-17 16:42:02 -0800322GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
Chris Craik30036092015-02-12 10:41:39 -0800323 const SkPaint& paint, float alphaScale) {
324 TRIGGER_STAGE(kFillStage);
325 REQUIRE_STAGES(kMeshStage);
326
Chris Craikf27133d2015-02-19 09:51:53 -0800327 //specify invalid filter/clamp, since these are always static for PathTextures
328 mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik30036092015-02-12 10:41:39 -0800329
330 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
331 paint.getShader(), paint.getColorFilter());
332
Chris Craikf27133d2015-02-19 09:51:53 -0800333 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800334 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik30036092015-02-12 10:41:39 -0800335 return *this;
336}
337
Chris Craik2bb8f562015-02-17 16:42:02 -0800338GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
339 const SkPaint& paint, float alphaScale) {
340 TRIGGER_STAGE(kFillStage);
341 REQUIRE_STAGES(kMeshStage);
342
Chris Craikf27133d2015-02-19 09:51:53 -0800343 //specify invalid filter/clamp, since these are always static for ShadowTextures
344 mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800345
346 const int ALPHA_BITMASK = SK_ColorBLACK;
347 const int COLOR_BITMASK = ~ALPHA_BITMASK;
348 if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
349 // shadow color is fully opaque: override its alpha with that of paint
350 shadowColor &= paint.getColor() | COLOR_BITMASK;
351 }
352
353 setFill(shadowColor, alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
354 paint.getShader(), paint.getColorFilter());
355
Chris Craikf27133d2015-02-19 09:51:53 -0800356 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800357 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
358 return *this;
359}
360
361GlopBuilder& GlopBuilder::setFillBlack() {
362 TRIGGER_STAGE(kFillStage);
363 REQUIRE_STAGES(kMeshStage);
364
Chris Craikf27133d2015-02-19 09:51:53 -0800365 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800366 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800367 return *this;
368}
369
370GlopBuilder& GlopBuilder::setFillClear() {
371 TRIGGER_STAGE(kFillStage);
372 REQUIRE_STAGES(kMeshStage);
373
Chris Craikf27133d2015-02-19 09:51:53 -0800374 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800375 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800376 return *this;
377}
Chris Craikf27133d2015-02-19 09:51:53 -0800378
379GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
380 float alpha, SkXfermode::Mode mode) {
381 TRIGGER_STAGE(kFillStage);
382 REQUIRE_STAGES(kMeshStage);
383
384 mOutGlop->fill.texture = { &texture, GL_LINEAR, GL_CLAMP_TO_EDGE };
385 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
386
387 setFill(SK_ColorWHITE, alpha, mode, nullptr, colorFilter);
388
389 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
390 return *this;
391}
392
Chris Craik0519c812015-02-11 13:17:06 -0800393////////////////////////////////////////////////////////////////////////////////
394// Transform
395////////////////////////////////////////////////////////////////////////////////
396
Chris Craikf27133d2015-02-19 09:51:53 -0800397GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
Chris Craik0519c812015-02-11 13:17:06 -0800398 const Matrix4& transform, bool fudgingOffset) {
399 TRIGGER_STAGE(kTransformStage);
400
401 mOutGlop->transform.ortho.load(ortho);
402 mOutGlop->transform.canvas.load(transform);
403 mOutGlop->transform.fudgingOffset = fudgingOffset;
404 return *this;
405}
406
Chris Craik0519c812015-02-11 13:17:06 -0800407////////////////////////////////////////////////////////////////////////////////
408// ModelView
409////////////////////////////////////////////////////////////////////////////////
410
411GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
412 TRIGGER_STAGE(kModelViewStage);
413
414 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
415 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
416 mOutGlop->bounds = destination;
417 return *this;
418}
419
420GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
421 TRIGGER_STAGE(kModelViewStage);
422 REQUIRE_STAGES(kTransformStage | kFillStage);
423
424 float left = destination.left;
425 float top = destination.top;
426
427 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
428 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800429 // snap by adjusting the model view matrix
Chris Craik0519c812015-02-11 13:17:06 -0800430 const float translateX = canvasTransform.getTranslateX();
431 const float translateY = canvasTransform.getTranslateY();
432
433 left = (int) floorf(left + translateX + 0.5f) - translateX;
434 top = (int) floorf(top + translateY + 0.5f) - translateY;
Chris Craikf27133d2015-02-19 09:51:53 -0800435 mOutGlop->fill.texture.filter = GL_NEAREST;
Chris Craik0519c812015-02-11 13:17:06 -0800436 }
437
438 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
439 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
440 mOutGlop->bounds = destination;
441 return *this;
442}
443
444GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
445 TRIGGER_STAGE(kModelViewStage);
446
447 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
448 mOutGlop->bounds = source;
449 mOutGlop->bounds.translate(offsetX, offsetY);
450 return *this;
451}
452
Chris Craikf27133d2015-02-19 09:51:53 -0800453GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
454 TRIGGER_STAGE(kModelViewStage);
455 REQUIRE_STAGES(kTransformStage | kFillStage);
456
457 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
458 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
459 // snap by adjusting the model view matrix
460 const float translateX = canvasTransform.getTranslateX();
461 const float translateY = canvasTransform.getTranslateY();
462
463 offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
464 offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
465 mOutGlop->fill.texture.filter = GL_NEAREST;
466 }
467
468 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
469 mOutGlop->bounds.translate(offsetX, offsetY);
470 return *this;
471}
472
473////////////////////////////////////////////////////////////////////////////////
474// RoundRectClip
475////////////////////////////////////////////////////////////////////////////////
476
Chris Craik0519c812015-02-11 13:17:06 -0800477GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
478 TRIGGER_STAGE(kRoundRectClipStage);
479
480 mOutGlop->roundRectClipState = roundRectClipState;
481 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
482 return *this;
483}
484
485////////////////////////////////////////////////////////////////////////////////
486// Build
487////////////////////////////////////////////////////////////////////////////////
488
Chris Craikf27133d2015-02-19 09:51:53 -0800489void verify(const ProgramDescription& description, const Glop& glop) {
490 bool hasTexture = glop.fill.texture.texture != nullptr;
491 LOG_ALWAYS_FATAL_IF(description.hasTexture != hasTexture);
492 LOG_ALWAYS_FATAL_IF((glop.mesh.vertexFlags & kTextureCoord_Attrib) != hasTexture);
493}
494
Chris Craik03188872015-02-02 18:39:33 -0800495void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800496 REQUIRE_STAGES(kAllStages);
Chris Craik117bdbc2015-02-05 10:12:38 -0800497
Chris Craik922d3a72015-02-13 17:47:21 -0800498 // serialize shader info into ShaderData
Chris Craikf27133d2015-02-19 09:51:53 -0800499 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
Chris Craik922d3a72015-02-13 17:47:21 -0800500 SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView,
501 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
502
Chris Craik03188872015-02-02 18:39:33 -0800503 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik117bdbc2015-02-05 10:12:38 -0800504 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik0519c812015-02-11 13:17:06 -0800505
506 // duplicates ProgramCache's definition of color uniform presence
507 const bool singleColor = !mDescription.hasTexture
508 && !mDescription.hasExternalTexture
509 && !mDescription.hasGradient
510 && !mDescription.hasBitmap;
511 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craikf27133d2015-02-19 09:51:53 -0800512
513 verify(mDescription, *mOutGlop);
Chris Craik03188872015-02-02 18:39:33 -0800514}
515
516} /* namespace uirenderer */
517} /* namespace android */