blob: f50dfc8f65b74d91ce3e960ce45bdc66247345f8 [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 Craik0519c8102015-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 Craik0519c8102015-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 Craik0519c8102015-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 Craik0519c8102015-02-11 13:17:06 -080053 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080054 mStageFlags = kInitialStage;
55}
56
Chris Craik0519c8102015-02-11 13:17:06 -080057////////////////////////////////////////////////////////////////////////////////
58// Mesh
59////////////////////////////////////////////////////////////////////////////////
60
61GlopBuilder& GlopBuilder::setMeshUnitQuad() {
62 TRIGGER_STAGE(kMeshStage);
63
Chris Craik0519c8102015-02-11 13:17:06 -080064 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080065 mOutGlop->mesh.indices = { 0, nullptr };
66 mOutGlop->mesh.vertices = {
67 mRenderState.meshState().getUnitQuadVBO(),
68 VertexAttribFlags::kNone,
69 nullptr, nullptr, nullptr,
70 kTextureVertexStride };
Chris Craik0519c8102015-02-11 13:17:06 -080071 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c8102015-02-11 13:17:06 -080072 return *this;
73}
74
Chris Craikf27133d2015-02-19 09:51:53 -080075GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) {
Chris Craik14100ac2015-02-24 13:46:29 -080076 if (uvMapper) {
77 // can't use unit quad VBO, so build UV vertices manually
78 return setMeshTexturedUvQuad(uvMapper, Rect(0, 0, 1, 1));
79 }
80
81 TRIGGER_STAGE(kMeshStage);
82
Chris Craik14100ac2015-02-24 13:46:29 -080083 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080084 mOutGlop->mesh.indices = { 0, nullptr };
85 mOutGlop->mesh.vertices = {
86 mRenderState.meshState().getUnitQuadVBO(),
87 VertexAttribFlags::kTextureCoord,
88 nullptr, (const void*) kMeshTextureOffset, nullptr,
89 kTextureVertexStride };
Chris Craik14100ac2015-02-24 13:46:29 -080090 mOutGlop->mesh.elementCount = 4;
Chris Craik14100ac2015-02-24 13:46:29 -080091 return *this;
92}
93
94GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
Chris Craik0519c8102015-02-11 13:17:06 -080095 TRIGGER_STAGE(kMeshStage);
96
Chris Craik0519c8102015-02-11 13:17:06 -080097 if (CC_UNLIKELY(uvMapper)) {
Chris Craik0519c8102015-02-11 13:17:06 -080098 uvMapper->map(uvs);
Chris Craik0519c8102015-02-11 13:17:06 -080099 }
Chris Craik14100ac2015-02-24 13:46:29 -0800100 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
101
Chris Craikef250742015-02-25 17:16:16 -0800102 const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices;
103 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
104 mOutGlop->mesh.indices = { 0, nullptr };
105 mOutGlop->mesh.vertices = {
106 0,
107 VertexAttribFlags::kTextureCoord,
108 &textureVertex[0].x, &textureVertex[0].u, nullptr,
109 kTextureVertexStride };
Chris Craik0519c8102015-02-11 13:17:06 -0800110 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c8102015-02-11 13:17:06 -0800111 return *this;
112}
113
Chris Craikef250742015-02-25 17:16:16 -0800114GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) {
Chris Craik0519c8102015-02-11 13:17:06 -0800115 TRIGGER_STAGE(kMeshStage);
116
Chris Craik0519c8102015-02-11 13:17:06 -0800117 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800118 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
119 mOutGlop->mesh.vertices = {
120 0,
121 VertexAttribFlags::kNone,
122 vertexData, nullptr, nullptr,
123 kVertexStride };
Chris Craik0519c8102015-02-11 13:17:06 -0800124 mOutGlop->mesh.elementCount = 6 * quadCount;
Chris Craik0519c8102015-02-11 13:17:06 -0800125 return *this;
126}
127
Chris Craikf27133d2015-02-19 09:51:53 -0800128GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
129 TRIGGER_STAGE(kMeshStage);
130
Chris Craikf27133d2015-02-19 09:51:53 -0800131 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800132 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
133 mOutGlop->mesh.vertices = {
134 0,
135 VertexAttribFlags::kTextureCoord,
136 &vertexData[0].x, &vertexData[0].u, nullptr,
137 kTextureVertexStride };
Chris Craikf27133d2015-02-19 09:51:53 -0800138 mOutGlop->mesh.elementCount = elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800139 return *this;
140}
141
142GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount) {
143 TRIGGER_STAGE(kMeshStage);
144
145 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
146 mOutGlop->mesh.indices = { 0, nullptr };
147 mOutGlop->mesh.vertices = {
148 0,
149 static_cast<VertexAttribFlags>(VertexAttribFlags::kTextureCoord | VertexAttribFlags::kColor),
150 &vertexData[0].x, &vertexData[0].u, &vertexData[0].r,
151 kColorTextureVertexStride };
152 mOutGlop->mesh.elementCount = elementCount;
Chris Craikf27133d2015-02-19 09:51:53 -0800153 return *this;
154}
155
Chris Craik117bdbc2015-02-05 10:12:38 -0800156GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
157 TRIGGER_STAGE(kMeshStage);
158
159 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
160
161 bool alphaVertex = flags & VertexBuffer::kAlpha;
162 bool indices = flags & VertexBuffer::kIndices;
Chris Craik117bdbc2015-02-05 10:12:38 -0800163
Chris Craikef250742015-02-25 17:16:16 -0800164 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
165 mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() };
166 mOutGlop->mesh.vertices = {
167 0,
168 alphaVertex ? VertexAttribFlags::kAlpha : VertexAttribFlags::kNone,
169 vertexBuffer.getBuffer(), nullptr, nullptr,
170 alphaVertex ? kAlphaVertexStride : kVertexStride };
171 mOutGlop->mesh.elementCount = indices
172 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
173
Chris Craik117bdbc2015-02-05 10:12:38 -0800174 mDescription.useShadowAlphaInterp = shadowInterp;
175 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800176}
177
Chris Craik0519c8102015-02-11 13:17:06 -0800178////////////////////////////////////////////////////////////////////////////////
179// Fill
180////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800181
Chris Craik0519c8102015-02-11 13:17:06 -0800182void GlopBuilder::setFill(int color, float alphaScale, SkXfermode::Mode mode,
183 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800184 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800185 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800186 if (!shader) {
187 float colorScale = alpha / 255.0f;
188 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800189 colorScale * SkColorGetR(color),
190 colorScale * SkColorGetG(color),
Chris Craik0519c8102015-02-11 13:17:06 -0800191 colorScale * SkColorGetB(color),
192 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800193 };
194 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800195 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800196 }
Chris Craik03188872015-02-02 18:39:33 -0800197 } else {
Chris Craik0519c8102015-02-11 13:17:06 -0800198 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800199 }
200 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800201
Chris Craik117bdbc2015-02-05 10:12:38 -0800202 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800203 if (mOutGlop->fill.color.a < 1.0f
Chris Craikef250742015-02-25 17:16:16 -0800204 || (mOutGlop->mesh.vertices.flags & VertexAttribFlags::kAlpha)
Chris Craikf27133d2015-02-19 09:51:53 -0800205 || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend)
Chris Craik0519c8102015-02-11 13:17:06 -0800206 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800207 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800208 || PaintUtils::isBlendedColorFilter(colorFilter)
209 || mode != SkXfermode::kSrcOver_Mode) {
210 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
211 Blend::getFactors(mode, SWAP_SRC_DST,
212 &mOutGlop->blend.src, &mOutGlop->blend.dst);
213 } else {
214 // These blend modes are not supported by OpenGL directly and have
215 // to be implemented using shaders. Since the shader will perform
216 // the blending, don't enable GL blending off here
217 // If the blend mode cannot be implemented using shaders, fall
218 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800219 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800220 mDescription.framebufferMode = mode;
221 mDescription.swapSrcDst = SWAP_SRC_DST;
222 // blending in shader, don't enable
223 } else {
224 // unsupported
225 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
226 &mOutGlop->blend.src, &mOutGlop->blend.dst);
227 }
228 }
229 }
Chris Craik922d3a72015-02-13 17:47:21 -0800230 mShader = shader; // shader resolved in ::build()
Chris Craik03188872015-02-02 18:39:33 -0800231
Chris Craik117bdbc2015-02-05 10:12:38 -0800232 if (colorFilter) {
233 SkColor color;
234 SkXfermode::Mode mode;
235 SkScalar srcColorMatrix[20];
236 if (colorFilter->asColorMode(&color, &mode)) {
237 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
238 mDescription.colorMode = mode;
239
240 const float alpha = SkColorGetA(color) / 255.0f;
241 float colorScale = alpha / 255.0f;
242 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800243 colorScale * SkColorGetR(color),
244 colorScale * SkColorGetG(color),
245 colorScale * SkColorGetB(color),
Chris Craik0519c8102015-02-11 13:17:06 -0800246 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800247 };
248 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
249 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
250
251 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
252 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
253 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
254 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
255 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
256
257 // Skia uses the range [0..255] for the addition vector, but we need
258 // the [0..1] range to apply the vector in GLSL
259 float* colorVector = mOutGlop->fill.filter.matrix.vector;
260 colorVector[0] = srcColorMatrix[4] / 255.0f;
261 colorVector[1] = srcColorMatrix[9] / 255.0f;
262 colorVector[2] = srcColorMatrix[14] / 255.0f;
263 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800264 } else {
265 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800266 }
267 } else {
268 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
269 }
Chris Craik0519c8102015-02-11 13:17:06 -0800270}
Chris Craik117bdbc2015-02-05 10:12:38 -0800271
Chris Craik0519c8102015-02-11 13:17:06 -0800272GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, bool isAlphaMaskTexture,
273 const SkPaint* paint, float alphaScale) {
274 TRIGGER_STAGE(kFillStage);
275 REQUIRE_STAGES(kMeshStage);
276
Chris Craikf27133d2015-02-19 09:51:53 -0800277 mOutGlop->fill.texture = { &texture, PaintUtils::getFilter(paint), GL_CLAMP_TO_EDGE };
Chris Craik0519c8102015-02-11 13:17:06 -0800278
279 if (paint) {
280 int color = paint->getColor();
281 SkShader* shader = paint->getShader();
282
283 if (!isAlphaMaskTexture) {
284 // Texture defines color, so disable shaders, and reset all non-alpha color channels
285 color |= 0x00FFFFFF;
286 shader = nullptr;
287 }
288 setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()),
289 shader, paint->getColorFilter());
290 } else {
291 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
292
293 const bool SWAP_SRC_DST = false;
294 if (alphaScale < 1.0f
Chris Craikef250742015-02-25 17:16:16 -0800295 || (mOutGlop->mesh.vertices.flags & VertexAttribFlags::kAlpha)
Chris Craik0519c8102015-02-11 13:17:06 -0800296 || texture.blend
297 || mOutGlop->roundRectClipState) {
298 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
299 &mOutGlop->blend.src, &mOutGlop->blend.dst);
300 } else {
301 mOutGlop->blend = { GL_ZERO, GL_ZERO };
302 }
303 }
304
Chris Craikf27133d2015-02-19 09:51:53 -0800305 mDescription.hasAlpha8Texture = isAlphaMaskTexture;
Chris Craik0519c8102015-02-11 13:17:06 -0800306 if (isAlphaMaskTexture) {
Chris Craik2bb8f562015-02-17 16:42:02 -0800307 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik0519c8102015-02-11 13:17:06 -0800308 } else {
309 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
310 }
Chris Craik03188872015-02-02 18:39:33 -0800311 return *this;
312}
313
Chris Craik0519c8102015-02-11 13:17:06 -0800314GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
315 TRIGGER_STAGE(kFillStage);
316 REQUIRE_STAGES(kMeshStage);
317
Chris Craikf27133d2015-02-19 09:51:53 -0800318 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik0519c8102015-02-11 13:17:06 -0800319
320 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
321 paint.getShader(), paint.getColorFilter());
322 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
323 return *this;
324}
325
Chris Craik2bb8f562015-02-17 16:42:02 -0800326GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
Chris Craik30036092015-02-12 10:41:39 -0800327 const SkPaint& paint, float alphaScale) {
328 TRIGGER_STAGE(kFillStage);
329 REQUIRE_STAGES(kMeshStage);
330
Chris Craikf27133d2015-02-19 09:51:53 -0800331 //specify invalid filter/clamp, since these are always static for PathTextures
332 mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik30036092015-02-12 10:41:39 -0800333
334 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
335 paint.getShader(), paint.getColorFilter());
336
Chris Craikf27133d2015-02-19 09:51:53 -0800337 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800338 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik30036092015-02-12 10:41:39 -0800339 return *this;
340}
341
Chris Craik2bb8f562015-02-17 16:42:02 -0800342GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
343 const SkPaint& paint, float alphaScale) {
344 TRIGGER_STAGE(kFillStage);
345 REQUIRE_STAGES(kMeshStage);
346
Chris Craikf27133d2015-02-19 09:51:53 -0800347 //specify invalid filter/clamp, since these are always static for ShadowTextures
348 mOutGlop->fill.texture = { &texture, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800349
350 const int ALPHA_BITMASK = SK_ColorBLACK;
351 const int COLOR_BITMASK = ~ALPHA_BITMASK;
352 if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
353 // shadow color is fully opaque: override its alpha with that of paint
354 shadowColor &= paint.getColor() | COLOR_BITMASK;
355 }
356
357 setFill(shadowColor, alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
358 paint.getShader(), paint.getColorFilter());
359
Chris Craikf27133d2015-02-19 09:51:53 -0800360 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800361 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
362 return *this;
363}
364
365GlopBuilder& GlopBuilder::setFillBlack() {
366 TRIGGER_STAGE(kFillStage);
367 REQUIRE_STAGES(kMeshStage);
368
Chris Craikf27133d2015-02-19 09:51:53 -0800369 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800370 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800371 return *this;
372}
373
374GlopBuilder& GlopBuilder::setFillClear() {
375 TRIGGER_STAGE(kFillStage);
376 REQUIRE_STAGES(kMeshStage);
377
Chris Craikf27133d2015-02-19 09:51:53 -0800378 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM };
Chris Craik2bb8f562015-02-17 16:42:02 -0800379 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800380 return *this;
381}
Chris Craikf27133d2015-02-19 09:51:53 -0800382
383GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
384 float alpha, SkXfermode::Mode mode) {
385 TRIGGER_STAGE(kFillStage);
386 REQUIRE_STAGES(kMeshStage);
387
388 mOutGlop->fill.texture = { &texture, GL_LINEAR, GL_CLAMP_TO_EDGE };
389 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
390
391 setFill(SK_ColorWHITE, alpha, mode, nullptr, colorFilter);
392
393 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
394 return *this;
395}
396
Chris Craik0519c8102015-02-11 13:17:06 -0800397////////////////////////////////////////////////////////////////////////////////
398// Transform
399////////////////////////////////////////////////////////////////////////////////
400
Chris Craikf27133d2015-02-19 09:51:53 -0800401GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
Chris Craik0519c8102015-02-11 13:17:06 -0800402 const Matrix4& transform, bool fudgingOffset) {
403 TRIGGER_STAGE(kTransformStage);
404
405 mOutGlop->transform.ortho.load(ortho);
406 mOutGlop->transform.canvas.load(transform);
407 mOutGlop->transform.fudgingOffset = fudgingOffset;
408 return *this;
409}
410
Chris Craik0519c8102015-02-11 13:17:06 -0800411////////////////////////////////////////////////////////////////////////////////
412// ModelView
413////////////////////////////////////////////////////////////////////////////////
414
415GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
416 TRIGGER_STAGE(kModelViewStage);
417
418 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
419 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
420 mOutGlop->bounds = destination;
421 return *this;
422}
423
424GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
425 TRIGGER_STAGE(kModelViewStage);
426 REQUIRE_STAGES(kTransformStage | kFillStage);
427
428 float left = destination.left;
429 float top = destination.top;
430
431 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
432 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800433 // snap by adjusting the model view matrix
Chris Craik0519c8102015-02-11 13:17:06 -0800434 const float translateX = canvasTransform.getTranslateX();
435 const float translateY = canvasTransform.getTranslateY();
436
437 left = (int) floorf(left + translateX + 0.5f) - translateX;
438 top = (int) floorf(top + translateY + 0.5f) - translateY;
Chris Craikf27133d2015-02-19 09:51:53 -0800439 mOutGlop->fill.texture.filter = GL_NEAREST;
Chris Craik0519c8102015-02-11 13:17:06 -0800440 }
441
442 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
443 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
444 mOutGlop->bounds = destination;
445 return *this;
446}
447
448GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
449 TRIGGER_STAGE(kModelViewStage);
450
451 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
452 mOutGlop->bounds = source;
453 mOutGlop->bounds.translate(offsetX, offsetY);
454 return *this;
455}
456
Chris Craikf27133d2015-02-19 09:51:53 -0800457GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
458 TRIGGER_STAGE(kModelViewStage);
459 REQUIRE_STAGES(kTransformStage | kFillStage);
460
461 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
462 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
463 // snap by adjusting the model view matrix
464 const float translateX = canvasTransform.getTranslateX();
465 const float translateY = canvasTransform.getTranslateY();
466
467 offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
468 offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
469 mOutGlop->fill.texture.filter = GL_NEAREST;
470 }
471
472 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
473 mOutGlop->bounds.translate(offsetX, offsetY);
474 return *this;
475}
476
477////////////////////////////////////////////////////////////////////////////////
478// RoundRectClip
479////////////////////////////////////////////////////////////////////////////////
480
Chris Craik0519c8102015-02-11 13:17:06 -0800481GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
482 TRIGGER_STAGE(kRoundRectClipStage);
483
484 mOutGlop->roundRectClipState = roundRectClipState;
485 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
486 return *this;
487}
488
489////////////////////////////////////////////////////////////////////////////////
490// Build
491////////////////////////////////////////////////////////////////////////////////
492
Chris Craikf27133d2015-02-19 09:51:53 -0800493void verify(const ProgramDescription& description, const Glop& glop) {
494 bool hasTexture = glop.fill.texture.texture != nullptr;
495 LOG_ALWAYS_FATAL_IF(description.hasTexture != hasTexture);
Chris Craikef250742015-02-25 17:16:16 -0800496 LOG_ALWAYS_FATAL_IF((glop.mesh.vertices.flags & VertexAttribFlags::kTextureCoord) != hasTexture);
497
498 if ((glop.mesh.vertices.flags & VertexAttribFlags::kAlpha) && glop.mesh.vertices.bufferObject) {
499 LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
500 }
Chris Craikf27133d2015-02-19 09:51:53 -0800501}
502
Chris Craik03188872015-02-02 18:39:33 -0800503void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800504 REQUIRE_STAGES(kAllStages);
Chris Craik117bdbc2015-02-05 10:12:38 -0800505
Chris Craikef250742015-02-25 17:16:16 -0800506 mDescription.hasTexture = static_cast<int>(mOutGlop->mesh.vertices.flags & VertexAttribFlags::kTextureCoord);
507 mDescription.hasColors = static_cast<int>(mOutGlop->mesh.vertices.flags & VertexAttribFlags::kColor);
508 mDescription.hasVertexAlpha = static_cast<int>(mOutGlop->mesh.vertices.flags & VertexAttribFlags::kAlpha);
509
Chris Craik922d3a72015-02-13 17:47:21 -0800510 // serialize shader info into ShaderData
Chris Craikf27133d2015-02-19 09:51:53 -0800511 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
Chris Craik922d3a72015-02-13 17:47:21 -0800512 SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView,
513 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
514
Chris Craik0519c8102015-02-11 13:17:06 -0800515 // duplicates ProgramCache's definition of color uniform presence
516 const bool singleColor = !mDescription.hasTexture
517 && !mDescription.hasExternalTexture
518 && !mDescription.hasGradient
519 && !mDescription.hasBitmap;
520 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craikf27133d2015-02-19 09:51:53 -0800521
522 verify(mDescription, *mOutGlop);
Chris Craikef250742015-02-25 17:16:16 -0800523
524 // Final step: populate program and map bounds into render target space
525 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
526 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800527}
528
529} /* namespace uirenderer */
530} /* namespace android */