blob: 3108a8dd42c309584dc759ebce5093ffa3272c04 [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 Craik26bf3422015-02-26 16:28:17 -0800277 mOutGlop->fill.texture = { &texture,
278 GL_TEXTURE_2D, PaintUtils::getFilter(paint), GL_CLAMP_TO_EDGE, nullptr };
Chris Craik0519c8102015-02-11 13:17:06 -0800279
280 if (paint) {
281 int color = paint->getColor();
282 SkShader* shader = paint->getShader();
283
284 if (!isAlphaMaskTexture) {
285 // Texture defines color, so disable shaders, and reset all non-alpha color channels
286 color |= 0x00FFFFFF;
287 shader = nullptr;
288 }
289 setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()),
290 shader, paint->getColorFilter());
291 } else {
292 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
293
294 const bool SWAP_SRC_DST = false;
295 if (alphaScale < 1.0f
Chris Craikef250742015-02-25 17:16:16 -0800296 || (mOutGlop->mesh.vertices.flags & VertexAttribFlags::kAlpha)
Chris Craik0519c8102015-02-11 13:17:06 -0800297 || texture.blend
298 || mOutGlop->roundRectClipState) {
299 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
300 &mOutGlop->blend.src, &mOutGlop->blend.dst);
301 } else {
302 mOutGlop->blend = { GL_ZERO, GL_ZERO };
303 }
304 }
305
Chris Craikf27133d2015-02-19 09:51:53 -0800306 mDescription.hasAlpha8Texture = isAlphaMaskTexture;
Chris Craik0519c8102015-02-11 13:17:06 -0800307 if (isAlphaMaskTexture) {
Chris Craik2bb8f562015-02-17 16:42:02 -0800308 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik0519c8102015-02-11 13:17:06 -0800309 } else {
310 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
311 }
Chris Craik03188872015-02-02 18:39:33 -0800312 return *this;
313}
314
Chris Craik0519c8102015-02-11 13:17:06 -0800315GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
316 TRIGGER_STAGE(kFillStage);
317 REQUIRE_STAGES(kMeshStage);
318
Chris Craik26bf3422015-02-26 16:28:17 -0800319 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik0519c8102015-02-11 13:17:06 -0800320
321 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
322 paint.getShader(), paint.getColorFilter());
323 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
324 return *this;
325}
326
Chris Craik2bb8f562015-02-17 16:42:02 -0800327GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
Chris Craik30036092015-02-12 10:41:39 -0800328 const SkPaint& paint, float alphaScale) {
329 TRIGGER_STAGE(kFillStage);
330 REQUIRE_STAGES(kMeshStage);
331
Chris Craikf27133d2015-02-19 09:51:53 -0800332 //specify invalid filter/clamp, since these are always static for PathTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800333 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik30036092015-02-12 10:41:39 -0800334
335 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
336 paint.getShader(), paint.getColorFilter());
337
Chris Craikf27133d2015-02-19 09:51:53 -0800338 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800339 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik30036092015-02-12 10:41:39 -0800340 return *this;
341}
342
Chris Craik2bb8f562015-02-17 16:42:02 -0800343GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
344 const SkPaint& paint, float alphaScale) {
345 TRIGGER_STAGE(kFillStage);
346 REQUIRE_STAGES(kMeshStage);
347
Chris Craikf27133d2015-02-19 09:51:53 -0800348 //specify invalid filter/clamp, since these are always static for ShadowTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800349 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik2bb8f562015-02-17 16:42:02 -0800350
351 const int ALPHA_BITMASK = SK_ColorBLACK;
352 const int COLOR_BITMASK = ~ALPHA_BITMASK;
353 if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
354 // shadow color is fully opaque: override its alpha with that of paint
355 shadowColor &= paint.getColor() | COLOR_BITMASK;
356 }
357
358 setFill(shadowColor, alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
359 paint.getShader(), paint.getColorFilter());
360
Chris Craikf27133d2015-02-19 09:51:53 -0800361 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800362 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
363 return *this;
364}
365
366GlopBuilder& GlopBuilder::setFillBlack() {
367 TRIGGER_STAGE(kFillStage);
368 REQUIRE_STAGES(kMeshStage);
369
Chris Craik26bf3422015-02-26 16:28:17 -0800370 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik2bb8f562015-02-17 16:42:02 -0800371 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800372 return *this;
373}
374
375GlopBuilder& GlopBuilder::setFillClear() {
376 TRIGGER_STAGE(kFillStage);
377 REQUIRE_STAGES(kMeshStage);
378
Chris Craik26bf3422015-02-26 16:28:17 -0800379 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik2bb8f562015-02-17 16:42:02 -0800380 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800381 return *this;
382}
Chris Craikf27133d2015-02-19 09:51:53 -0800383
384GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
385 float alpha, SkXfermode::Mode mode) {
386 TRIGGER_STAGE(kFillStage);
387 REQUIRE_STAGES(kMeshStage);
388
Chris Craik26bf3422015-02-26 16:28:17 -0800389 mOutGlop->fill.texture = { &texture,
390 GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr };
Chris Craikf27133d2015-02-19 09:51:53 -0800391 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
392
393 setFill(SK_ColorWHITE, alpha, mode, nullptr, colorFilter);
394
395 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
396 return *this;
397}
398
Chris Craik26bf3422015-02-26 16:28:17 -0800399GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) {
400 TRIGGER_STAGE(kFillStage);
401 REQUIRE_STAGES(kMeshStage);
402
403 mOutGlop->fill.texture = { &(layer.getTexture()),
404 layer.getRenderTarget(), GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() };
405 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
406
407 setFill(SK_ColorWHITE, alpha, layer.getMode(), nullptr, layer.getColorFilter());
408
409 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
410 mDescription.hasTextureTransform = true;
411 return *this;
412}
413
Chris Craik0519c8102015-02-11 13:17:06 -0800414////////////////////////////////////////////////////////////////////////////////
415// Transform
416////////////////////////////////////////////////////////////////////////////////
417
Chris Craikf27133d2015-02-19 09:51:53 -0800418GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
Chris Craik0519c8102015-02-11 13:17:06 -0800419 const Matrix4& transform, bool fudgingOffset) {
420 TRIGGER_STAGE(kTransformStage);
421
422 mOutGlop->transform.ortho.load(ortho);
423 mOutGlop->transform.canvas.load(transform);
424 mOutGlop->transform.fudgingOffset = fudgingOffset;
425 return *this;
426}
427
Chris Craik0519c8102015-02-11 13:17:06 -0800428////////////////////////////////////////////////////////////////////////////////
429// ModelView
430////////////////////////////////////////////////////////////////////////////////
431
432GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
433 TRIGGER_STAGE(kModelViewStage);
434
435 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
436 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
437 mOutGlop->bounds = destination;
438 return *this;
439}
440
441GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
442 TRIGGER_STAGE(kModelViewStage);
443 REQUIRE_STAGES(kTransformStage | kFillStage);
444
445 float left = destination.left;
446 float top = destination.top;
447
448 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
449 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800450 // snap by adjusting the model view matrix
Chris Craik0519c8102015-02-11 13:17:06 -0800451 const float translateX = canvasTransform.getTranslateX();
452 const float translateY = canvasTransform.getTranslateY();
453
454 left = (int) floorf(left + translateX + 0.5f) - translateX;
455 top = (int) floorf(top + translateY + 0.5f) - translateY;
Chris Craikf27133d2015-02-19 09:51:53 -0800456 mOutGlop->fill.texture.filter = GL_NEAREST;
Chris Craik0519c8102015-02-11 13:17:06 -0800457 }
458
459 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
460 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
461 mOutGlop->bounds = destination;
462 return *this;
463}
464
465GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
466 TRIGGER_STAGE(kModelViewStage);
467
468 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
469 mOutGlop->bounds = source;
470 mOutGlop->bounds.translate(offsetX, offsetY);
471 return *this;
472}
473
Chris Craikf27133d2015-02-19 09:51:53 -0800474GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
475 TRIGGER_STAGE(kModelViewStage);
476 REQUIRE_STAGES(kTransformStage | kFillStage);
477
478 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
479 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
480 // snap by adjusting the model view matrix
481 const float translateX = canvasTransform.getTranslateX();
482 const float translateY = canvasTransform.getTranslateY();
483
484 offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
485 offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
486 mOutGlop->fill.texture.filter = GL_NEAREST;
487 }
488
489 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
490 mOutGlop->bounds.translate(offsetX, offsetY);
491 return *this;
492}
493
494////////////////////////////////////////////////////////////////////////////////
495// RoundRectClip
496////////////////////////////////////////////////////////////////////////////////
497
Chris Craik0519c8102015-02-11 13:17:06 -0800498GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
499 TRIGGER_STAGE(kRoundRectClipStage);
500
501 mOutGlop->roundRectClipState = roundRectClipState;
502 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
503 return *this;
504}
505
506////////////////////////////////////////////////////////////////////////////////
507// Build
508////////////////////////////////////////////////////////////////////////////////
509
Chris Craikf27133d2015-02-19 09:51:53 -0800510void verify(const ProgramDescription& description, const Glop& glop) {
511 bool hasTexture = glop.fill.texture.texture != nullptr;
Chris Craik26bf3422015-02-26 16:28:17 -0800512 LOG_ALWAYS_FATAL_IF(description.hasTexture && description.hasExternalTexture);
513 LOG_ALWAYS_FATAL_IF((description.hasTexture || description.hasExternalTexture )!= hasTexture);
Chris Craikef250742015-02-25 17:16:16 -0800514 LOG_ALWAYS_FATAL_IF((glop.mesh.vertices.flags & VertexAttribFlags::kTextureCoord) != hasTexture);
515
516 if ((glop.mesh.vertices.flags & VertexAttribFlags::kAlpha) && glop.mesh.vertices.bufferObject) {
517 LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
518 }
Chris Craik26bf3422015-02-26 16:28:17 -0800519
520 if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) {
521 LOG_ALWAYS_FATAL("Texture transform incorrectly specified");
522 }
Chris Craikf27133d2015-02-19 09:51:53 -0800523}
524
Chris Craik03188872015-02-02 18:39:33 -0800525void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800526 REQUIRE_STAGES(kAllStages);
Chris Craik26bf3422015-02-26 16:28:17 -0800527 if (mOutGlop->mesh.vertices.flags & VertexAttribFlags::kTextureCoord) {
528 mDescription.hasTexture = mOutGlop->fill.texture.target == GL_TEXTURE_2D;
529 mDescription.hasExternalTexture = mOutGlop->fill.texture.target == GL_TEXTURE_EXTERNAL_OES;
530 }
531 mDescription.hasColors = mOutGlop->mesh.vertices.flags & VertexAttribFlags::kColor;
532 mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.flags & VertexAttribFlags::kAlpha;
Chris Craikef250742015-02-25 17:16:16 -0800533
Chris Craik922d3a72015-02-13 17:47:21 -0800534 // serialize shader info into ShaderData
Chris Craikf27133d2015-02-19 09:51:53 -0800535 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
Chris Craik922d3a72015-02-13 17:47:21 -0800536 SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView,
537 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
538
Chris Craik0519c8102015-02-11 13:17:06 -0800539 // duplicates ProgramCache's definition of color uniform presence
540 const bool singleColor = !mDescription.hasTexture
541 && !mDescription.hasExternalTexture
542 && !mDescription.hasGradient
543 && !mDescription.hasBitmap;
544 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craikf27133d2015-02-19 09:51:53 -0800545
546 verify(mDescription, *mOutGlop);
Chris Craikef250742015-02-25 17:16:16 -0800547
548 // Final step: populate program and map bounds into render target space
549 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
550 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800551}
552
553} /* namespace uirenderer */
554} /* namespace android */