blob: 9ca6bc635338084707e0b312a08570bf3328a85e [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 Craik0556d902015-03-03 09:54:14 -080021#include "Patch.h"
Chris Craik03188872015-02-02 18:39:33 -080022#include "renderstate/MeshState.h"
23#include "renderstate/RenderState.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080024#include "SkiaShader.h"
25#include "Texture.h"
Chris Craik03188872015-02-02 18:39:33 -080026#include "utils/PaintUtils.h"
Chris Craik117bdbc2015-02-05 10:12:38 -080027#include "VertexBuffer.h"
Chris Craik03188872015-02-02 18:39:33 -080028
29#include <GLES2/gl2.h>
30#include <SkPaint.h>
31
32namespace android {
33namespace uirenderer {
34
Chris Craik117bdbc2015-02-05 10:12:38 -080035#define TRIGGER_STAGE(stageFlag) \
Chris Craik14100ac2015-02-24 13:46:29 -080036 LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \
Chris Craik0519c812015-02-11 13:17:06 -080037 mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
Chris Craik117bdbc2015-02-05 10:12:38 -080038
Chris Craik08fa43f2015-02-09 18:58:32 -080039#define REQUIRE_STAGES(requiredFlags) \
Chris Craik0519c812015-02-11 13:17:06 -080040 LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
Chris Craik08fa43f2015-02-09 18:58:32 -080041 "not prepared for current stage")
42
Chris Craik922d3a72015-02-13 17:47:21 -080043static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
Chris Craik14100ac2015-02-24 13:46:29 -080044 quadVertex[0] = {0, 0, uvs.left, uvs.top};
45 quadVertex[1] = {1, 0, uvs.right, uvs.top};
46 quadVertex[2] = {0, 1, uvs.left, uvs.bottom};
47 quadVertex[3] = {1, 1, uvs.right, uvs.bottom};
Chris Craik0519c812015-02-11 13:17:06 -080048}
49
Chris Craik03188872015-02-02 18:39:33 -080050GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
51 : mRenderState(renderState)
52 , mCaches(caches)
Chris Craik922d3a72015-02-13 17:47:21 -080053 , mShader(nullptr)
Chris Craik0519c812015-02-11 13:17:06 -080054 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080055 mStageFlags = kInitialStage;
56}
57
Chris Craik0519c812015-02-11 13:17:06 -080058////////////////////////////////////////////////////////////////////////////////
59// Mesh
60////////////////////////////////////////////////////////////////////////////////
61
62GlopBuilder& GlopBuilder::setMeshUnitQuad() {
63 TRIGGER_STAGE(kMeshStage);
64
Chris Craik0519c812015-02-11 13:17:06 -080065 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080066 mOutGlop->mesh.indices = { 0, nullptr };
67 mOutGlop->mesh.vertices = {
68 mRenderState.meshState().getUnitQuadVBO(),
Chris Craikeb911c22015-03-06 17:30:11 -080069 static_cast<int>(VertexAttribFlags::kNone),
Chris Craikef250742015-02-25 17:16:16 -080070 nullptr, nullptr, nullptr,
71 kTextureVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -080072 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c812015-02-11 13:17:06 -080073 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
Chris Craik14100ac2015-02-24 13:46:29 -080084 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080085 mOutGlop->mesh.indices = { 0, nullptr };
86 mOutGlop->mesh.vertices = {
87 mRenderState.meshState().getUnitQuadVBO(),
Chris Craikeb911c22015-03-06 17:30:11 -080088 static_cast<int>(VertexAttribFlags::kTextureCoord),
Chris Craikef250742015-02-25 17:16:16 -080089 nullptr, (const void*) kMeshTextureOffset, nullptr,
90 kTextureVertexStride };
Chris Craik14100ac2015-02-24 13:46:29 -080091 mOutGlop->mesh.elementCount = 4;
Chris Craik14100ac2015-02-24 13:46:29 -080092 return *this;
93}
94
95GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
Chris Craik0519c812015-02-11 13:17:06 -080096 TRIGGER_STAGE(kMeshStage);
97
Chris Craik0519c812015-02-11 13:17:06 -080098 if (CC_UNLIKELY(uvMapper)) {
Chris Craik0519c812015-02-11 13:17:06 -080099 uvMapper->map(uvs);
Chris Craik0519c812015-02-11 13:17:06 -0800100 }
Chris Craik14100ac2015-02-24 13:46:29 -0800101 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
102
Chris Craikef250742015-02-25 17:16:16 -0800103 const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices;
104 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
105 mOutGlop->mesh.indices = { 0, nullptr };
106 mOutGlop->mesh.vertices = {
107 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800108 static_cast<int>(VertexAttribFlags::kTextureCoord),
Chris Craikef250742015-02-25 17:16:16 -0800109 &textureVertex[0].x, &textureVertex[0].u, nullptr,
110 kTextureVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -0800111 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c812015-02-11 13:17:06 -0800112 return *this;
113}
114
Chris Craikef250742015-02-25 17:16:16 -0800115GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) {
Chris Craik0519c812015-02-11 13:17:06 -0800116 TRIGGER_STAGE(kMeshStage);
117
Chris Craik0519c812015-02-11 13:17:06 -0800118 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800119 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
120 mOutGlop->mesh.vertices = {
121 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800122 static_cast<int>(VertexAttribFlags::kNone),
Chris Craikef250742015-02-25 17:16:16 -0800123 vertexData, nullptr, nullptr,
124 kVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -0800125 mOutGlop->mesh.elementCount = 6 * quadCount;
Chris Craik0519c812015-02-11 13:17:06 -0800126 return *this;
127}
128
Chris Craikf27133d2015-02-19 09:51:53 -0800129GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
130 TRIGGER_STAGE(kMeshStage);
131
Chris Craikf27133d2015-02-19 09:51:53 -0800132 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800133 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
134 mOutGlop->mesh.vertices = {
135 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800136 static_cast<int>(VertexAttribFlags::kTextureCoord),
Chris Craikef250742015-02-25 17:16:16 -0800137 &vertexData[0].x, &vertexData[0].u, nullptr,
138 kTextureVertexStride };
Chris Craikf27133d2015-02-19 09:51:53 -0800139 mOutGlop->mesh.elementCount = elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800140 return *this;
141}
142
Chris Craika6b52192015-02-27 17:43:02 -0800143GlopBuilder& GlopBuilder::setMeshTexturedMesh(TextureVertex* vertexData, int elementCount) {
144 TRIGGER_STAGE(kMeshStage);
145
146 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
147 mOutGlop->mesh.indices = { 0, nullptr };
148 mOutGlop->mesh.vertices = {
149 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800150 static_cast<int>(VertexAttribFlags::kTextureCoord),
Chris Craika6b52192015-02-27 17:43:02 -0800151 &vertexData[0].x, &vertexData[0].u, nullptr,
152 kTextureVertexStride };
153 mOutGlop->mesh.elementCount = elementCount;
154 return *this;
155}
156
Chris Craikef250742015-02-25 17:16:16 -0800157GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount) {
158 TRIGGER_STAGE(kMeshStage);
159
160 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
161 mOutGlop->mesh.indices = { 0, nullptr };
162 mOutGlop->mesh.vertices = {
163 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800164 VertexAttribFlags::kTextureCoord | VertexAttribFlags::kColor,
Chris Craikef250742015-02-25 17:16:16 -0800165 &vertexData[0].x, &vertexData[0].u, &vertexData[0].r,
166 kColorTextureVertexStride };
167 mOutGlop->mesh.elementCount = elementCount;
Chris Craikf27133d2015-02-19 09:51:53 -0800168 return *this;
169}
170
Chris Craik117bdbc2015-02-05 10:12:38 -0800171GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
172 TRIGGER_STAGE(kMeshStage);
173
174 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
175
176 bool alphaVertex = flags & VertexBuffer::kAlpha;
177 bool indices = flags & VertexBuffer::kIndices;
Chris Craik117bdbc2015-02-05 10:12:38 -0800178
Chris Craikef250742015-02-25 17:16:16 -0800179 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
180 mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() };
181 mOutGlop->mesh.vertices = {
182 0,
Chris Craikeb911c22015-03-06 17:30:11 -0800183 static_cast<int>(alphaVertex ? VertexAttribFlags::kAlpha : VertexAttribFlags::kNone),
Chris Craikef250742015-02-25 17:16:16 -0800184 vertexBuffer.getBuffer(), nullptr, nullptr,
185 alphaVertex ? kAlphaVertexStride : kVertexStride };
186 mOutGlop->mesh.elementCount = indices
187 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
188
Chris Craik117bdbc2015-02-05 10:12:38 -0800189 mDescription.useShadowAlphaInterp = shadowInterp;
190 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800191}
192
Chris Craik0556d902015-03-03 09:54:14 -0800193GlopBuilder& GlopBuilder::setMeshPatchQuads(const Patch& patch) {
194 TRIGGER_STAGE(kMeshStage);
195
196 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
197 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
198 mOutGlop->mesh.vertices = {
199 mCaches.patchCache.getMeshBuffer(),
Chris Craikeb911c22015-03-06 17:30:11 -0800200 static_cast<int>(VertexAttribFlags::kTextureCoord),
Chris Craik8820fd12015-03-03 14:20:47 -0800201 (void*)patch.positionOffset, (void*)patch.textureOffset, nullptr,
Chris Craik0556d902015-03-03 09:54:14 -0800202 kTextureVertexStride };
203 mOutGlop->mesh.elementCount = patch.indexCount;
204 return *this;
205}
206
Chris Craik0519c812015-02-11 13:17:06 -0800207////////////////////////////////////////////////////////////////////////////////
208// Fill
209////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800210
Chris Craik182952f2015-03-09 14:17:29 -0700211void GlopBuilder::setFill(int color, float alphaScale,
212 SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage,
Chris Craik0519c812015-02-11 13:17:06 -0800213 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800214 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800215 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800216 if (!shader) {
217 float colorScale = alpha / 255.0f;
218 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800219 colorScale * SkColorGetR(color),
220 colorScale * SkColorGetG(color),
Chris Craik0519c812015-02-11 13:17:06 -0800221 colorScale * SkColorGetB(color),
222 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800223 };
224 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800225 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800226 }
Chris Craik03188872015-02-02 18:39:33 -0800227 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800228 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800229 }
Chris Craik03188872015-02-02 18:39:33 -0800230
Chris Craik117bdbc2015-02-05 10:12:38 -0800231 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800232 if (mOutGlop->fill.color.a < 1.0f
Chris Craikeb911c22015-03-06 17:30:11 -0800233 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::kAlpha)
Chris Craikf27133d2015-02-19 09:51:53 -0800234 || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend)
Chris Craik0519c812015-02-11 13:17:06 -0800235 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800236 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800237 || PaintUtils::isBlendedColorFilter(colorFilter)
238 || mode != SkXfermode::kSrcOver_Mode) {
239 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
Chris Craik182952f2015-03-09 14:17:29 -0700240 Blend::getFactors(mode, modeUsage,
Chris Craik03188872015-02-02 18:39:33 -0800241 &mOutGlop->blend.src, &mOutGlop->blend.dst);
242 } else {
243 // These blend modes are not supported by OpenGL directly and have
244 // to be implemented using shaders. Since the shader will perform
245 // the blending, don't enable GL blending off here
246 // If the blend mode cannot be implemented using shaders, fall
247 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800248 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800249 mDescription.framebufferMode = mode;
Chris Craik182952f2015-03-09 14:17:29 -0700250 mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap);
Chris Craik03188872015-02-02 18:39:33 -0800251 // blending in shader, don't enable
252 } else {
253 // unsupported
Chris Craik182952f2015-03-09 14:17:29 -0700254 Blend::getFactors(SkXfermode::kSrcOver_Mode, modeUsage,
Chris Craik03188872015-02-02 18:39:33 -0800255 &mOutGlop->blend.src, &mOutGlop->blend.dst);
256 }
257 }
258 }
Chris Craik922d3a72015-02-13 17:47:21 -0800259 mShader = shader; // shader resolved in ::build()
Chris Craik03188872015-02-02 18:39:33 -0800260
Chris Craik117bdbc2015-02-05 10:12:38 -0800261 if (colorFilter) {
262 SkColor color;
263 SkXfermode::Mode mode;
264 SkScalar srcColorMatrix[20];
265 if (colorFilter->asColorMode(&color, &mode)) {
266 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
267 mDescription.colorMode = mode;
268
269 const float alpha = SkColorGetA(color) / 255.0f;
270 float colorScale = alpha / 255.0f;
271 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800272 colorScale * SkColorGetR(color),
273 colorScale * SkColorGetG(color),
274 colorScale * SkColorGetB(color),
Chris Craik0519c812015-02-11 13:17:06 -0800275 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800276 };
277 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
278 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
279
280 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
281 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
282 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
283 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
284 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
285
286 // Skia uses the range [0..255] for the addition vector, but we need
287 // the [0..1] range to apply the vector in GLSL
288 float* colorVector = mOutGlop->fill.filter.matrix.vector;
289 colorVector[0] = srcColorMatrix[4] / 255.0f;
290 colorVector[1] = srcColorMatrix[9] / 255.0f;
291 colorVector[2] = srcColorMatrix[14] / 255.0f;
292 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800293 } else {
294 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800295 }
296 } else {
297 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
298 }
Chris Craik0519c812015-02-11 13:17:06 -0800299}
Chris Craik117bdbc2015-02-05 10:12:38 -0800300
Chris Craika6b52192015-02-27 17:43:02 -0800301GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, int textureFillFlags,
Chris Craik0519c812015-02-11 13:17:06 -0800302 const SkPaint* paint, float alphaScale) {
303 TRIGGER_STAGE(kFillStage);
304 REQUIRE_STAGES(kMeshStage);
305
Chris Craika6b52192015-02-27 17:43:02 -0800306 GLenum filter = (textureFillFlags & TextureFillFlags::kForceFilter)
307 ? GL_LINEAR : PaintUtils::getFilter(paint);
Chris Craik26bf3422015-02-26 16:28:17 -0800308 mOutGlop->fill.texture = { &texture,
Chris Craika6b52192015-02-27 17:43:02 -0800309 GL_TEXTURE_2D, filter, GL_CLAMP_TO_EDGE, nullptr };
Chris Craik0519c812015-02-11 13:17:06 -0800310
311 if (paint) {
312 int color = paint->getColor();
313 SkShader* shader = paint->getShader();
314
Chris Craika6b52192015-02-27 17:43:02 -0800315 if (!(textureFillFlags & TextureFillFlags::kIsAlphaMaskTexture)) {
Chris Craik0519c812015-02-11 13:17:06 -0800316 // Texture defines color, so disable shaders, and reset all non-alpha color channels
317 color |= 0x00FFFFFF;
318 shader = nullptr;
319 }
Chris Craik182952f2015-03-09 14:17:29 -0700320 setFill(color, alphaScale,
321 PaintUtils::getXfermode(paint->getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800322 shader, paint->getColorFilter());
323 } else {
324 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
325
Chris Craik0519c812015-02-11 13:17:06 -0800326 if (alphaScale < 1.0f
Chris Craikeb911c22015-03-06 17:30:11 -0800327 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::kAlpha)
Chris Craik0519c812015-02-11 13:17:06 -0800328 || texture.blend
329 || mOutGlop->roundRectClipState) {
Chris Craik182952f2015-03-09 14:17:29 -0700330 Blend::getFactors(SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800331 &mOutGlop->blend.src, &mOutGlop->blend.dst);
332 } else {
333 mOutGlop->blend = { GL_ZERO, GL_ZERO };
334 }
335 }
336
Chris Craika6b52192015-02-27 17:43:02 -0800337 if (textureFillFlags & TextureFillFlags::kIsAlphaMaskTexture) {
Chris Craik2bb8f562015-02-17 16:42:02 -0800338 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craika6b52192015-02-27 17:43:02 -0800339 mDescription.hasAlpha8Texture = true;
Chris Craik0519c812015-02-11 13:17:06 -0800340 } else {
341 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
342 }
Chris Craik03188872015-02-02 18:39:33 -0800343 return *this;
344}
345
Chris Craik0519c812015-02-11 13:17:06 -0800346GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
347 TRIGGER_STAGE(kFillStage);
348 REQUIRE_STAGES(kMeshStage);
349
Chris Craik26bf3422015-02-26 16:28:17 -0800350 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik0519c812015-02-11 13:17:06 -0800351
Chris Craik182952f2015-03-09 14:17:29 -0700352 setFill(paint.getColor(), alphaScale,
353 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800354 paint.getShader(), paint.getColorFilter());
355 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
356 return *this;
357}
358
Chris Craik2bb8f562015-02-17 16:42:02 -0800359GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
Chris Craik30036092015-02-12 10:41:39 -0800360 const SkPaint& paint, float alphaScale) {
361 TRIGGER_STAGE(kFillStage);
362 REQUIRE_STAGES(kMeshStage);
363
Chris Craikf27133d2015-02-19 09:51:53 -0800364 //specify invalid filter/clamp, since these are always static for PathTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800365 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik30036092015-02-12 10:41:39 -0800366
Chris Craik182952f2015-03-09 14:17:29 -0700367 setFill(paint.getColor(), alphaScale,
368 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik30036092015-02-12 10:41:39 -0800369 paint.getShader(), paint.getColorFilter());
370
Chris Craikf27133d2015-02-19 09:51:53 -0800371 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800372 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik30036092015-02-12 10:41:39 -0800373 return *this;
374}
375
Chris Craik2bb8f562015-02-17 16:42:02 -0800376GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
377 const SkPaint& paint, float alphaScale) {
378 TRIGGER_STAGE(kFillStage);
379 REQUIRE_STAGES(kMeshStage);
380
Chris Craikf27133d2015-02-19 09:51:53 -0800381 //specify invalid filter/clamp, since these are always static for ShadowTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800382 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik2bb8f562015-02-17 16:42:02 -0800383
384 const int ALPHA_BITMASK = SK_ColorBLACK;
385 const int COLOR_BITMASK = ~ALPHA_BITMASK;
386 if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
387 // shadow color is fully opaque: override its alpha with that of paint
388 shadowColor &= paint.getColor() | COLOR_BITMASK;
389 }
390
Chris Craik182952f2015-03-09 14:17:29 -0700391 setFill(shadowColor, alphaScale,
392 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik2bb8f562015-02-17 16:42:02 -0800393 paint.getShader(), paint.getColorFilter());
394
Chris Craikf27133d2015-02-19 09:51:53 -0800395 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800396 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
397 return *this;
398}
399
400GlopBuilder& GlopBuilder::setFillBlack() {
401 TRIGGER_STAGE(kFillStage);
402 REQUIRE_STAGES(kMeshStage);
403
Chris Craik26bf3422015-02-26 16:28:17 -0800404 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik182952f2015-03-09 14:17:29 -0700405 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap,
406 nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800407 return *this;
408}
409
410GlopBuilder& GlopBuilder::setFillClear() {
411 TRIGGER_STAGE(kFillStage);
412 REQUIRE_STAGES(kMeshStage);
413
Chris Craik26bf3422015-02-26 16:28:17 -0800414 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik182952f2015-03-09 14:17:29 -0700415 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, Blend::ModeOrderSwap::NoSwap,
416 nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800417 return *this;
418}
Chris Craikf27133d2015-02-19 09:51:53 -0800419
420GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
Chris Craik182952f2015-03-09 14:17:29 -0700421 float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage) {
Chris Craikf27133d2015-02-19 09:51:53 -0800422 TRIGGER_STAGE(kFillStage);
423 REQUIRE_STAGES(kMeshStage);
424
Chris Craik26bf3422015-02-26 16:28:17 -0800425 mOutGlop->fill.texture = { &texture,
426 GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr };
Chris Craikf27133d2015-02-19 09:51:53 -0800427 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
428
Chris Craik182952f2015-03-09 14:17:29 -0700429 setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter);
Chris Craikf27133d2015-02-19 09:51:53 -0800430
431 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
432 return *this;
433}
434
Chris Craik26bf3422015-02-26 16:28:17 -0800435GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) {
436 TRIGGER_STAGE(kFillStage);
437 REQUIRE_STAGES(kMeshStage);
438
439 mOutGlop->fill.texture = { &(layer.getTexture()),
440 layer.getRenderTarget(), GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() };
441 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
442
Chris Craik182952f2015-03-09 14:17:29 -0700443 setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap,
444 nullptr, layer.getColorFilter());
Chris Craik26bf3422015-02-26 16:28:17 -0800445
446 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
447 mDescription.hasTextureTransform = true;
448 return *this;
449}
450
Chris Craik0519c812015-02-11 13:17:06 -0800451////////////////////////////////////////////////////////////////////////////////
452// Transform
453////////////////////////////////////////////////////////////////////////////////
454
Chris Craikf27133d2015-02-19 09:51:53 -0800455GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
Chris Craik0519c812015-02-11 13:17:06 -0800456 const Matrix4& transform, bool fudgingOffset) {
457 TRIGGER_STAGE(kTransformStage);
458
459 mOutGlop->transform.ortho.load(ortho);
460 mOutGlop->transform.canvas.load(transform);
461 mOutGlop->transform.fudgingOffset = fudgingOffset;
462 return *this;
463}
464
Chris Craik0519c812015-02-11 13:17:06 -0800465////////////////////////////////////////////////////////////////////////////////
466// ModelView
467////////////////////////////////////////////////////////////////////////////////
468
469GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
470 TRIGGER_STAGE(kModelViewStage);
471
472 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
473 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
474 mOutGlop->bounds = destination;
475 return *this;
476}
477
478GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
479 TRIGGER_STAGE(kModelViewStage);
480 REQUIRE_STAGES(kTransformStage | kFillStage);
481
482 float left = destination.left;
483 float top = destination.top;
484
485 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
486 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800487 // snap by adjusting the model view matrix
Chris Craik0519c812015-02-11 13:17:06 -0800488 const float translateX = canvasTransform.getTranslateX();
489 const float translateY = canvasTransform.getTranslateY();
490
491 left = (int) floorf(left + translateX + 0.5f) - translateX;
492 top = (int) floorf(top + translateY + 0.5f) - translateY;
Chris Craikf27133d2015-02-19 09:51:53 -0800493 mOutGlop->fill.texture.filter = GL_NEAREST;
Chris Craik0519c812015-02-11 13:17:06 -0800494 }
495
496 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
497 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
498 mOutGlop->bounds = destination;
499 return *this;
500}
501
502GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
503 TRIGGER_STAGE(kModelViewStage);
504
505 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
506 mOutGlop->bounds = source;
507 mOutGlop->bounds.translate(offsetX, offsetY);
508 return *this;
509}
510
Chris Craikf27133d2015-02-19 09:51:53 -0800511GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
512 TRIGGER_STAGE(kModelViewStage);
513 REQUIRE_STAGES(kTransformStage | kFillStage);
514
515 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
516 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
517 // snap by adjusting the model view matrix
518 const float translateX = canvasTransform.getTranslateX();
519 const float translateY = canvasTransform.getTranslateY();
520
521 offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
522 offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
523 mOutGlop->fill.texture.filter = GL_NEAREST;
524 }
525
526 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
Chris Craik8820fd12015-03-03 14:20:47 -0800527 mOutGlop->bounds = source;
Chris Craikf27133d2015-02-19 09:51:53 -0800528 mOutGlop->bounds.translate(offsetX, offsetY);
529 return *this;
530}
531
532////////////////////////////////////////////////////////////////////////////////
533// RoundRectClip
534////////////////////////////////////////////////////////////////////////////////
535
Chris Craik0519c812015-02-11 13:17:06 -0800536GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
537 TRIGGER_STAGE(kRoundRectClipStage);
538
539 mOutGlop->roundRectClipState = roundRectClipState;
540 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
541 return *this;
542}
543
544////////////////////////////////////////////////////////////////////////////////
545// Build
546////////////////////////////////////////////////////////////////////////////////
547
Chris Craikf27133d2015-02-19 09:51:53 -0800548void verify(const ProgramDescription& description, const Glop& glop) {
Chris Craikeb911c22015-03-06 17:30:11 -0800549 if (glop.fill.texture.texture != nullptr) {
550 LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture)
551 || (!description.hasTexture && !description.hasExternalTexture)
552 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::kTextureCoord) == 0)),
553 "Texture %p, hT%d, hET %d, attribFlags %x",
554 glop.fill.texture.texture,
555 description.hasTexture, description.hasExternalTexture,
556 glop.mesh.vertices.attribFlags);
557 } else {
558 LOG_ALWAYS_FATAL_IF((description.hasTexture
559 || description.hasExternalTexture
560 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::kTextureCoord) != 0)),
561 "No texture, hT%d, hET %d, attribFlags %x",
562 description.hasTexture, description.hasExternalTexture,
563 glop.mesh.vertices.attribFlags);
564 }
Chris Craikef250742015-02-25 17:16:16 -0800565
Chris Craikeb911c22015-03-06 17:30:11 -0800566 if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::kAlpha)
567 && glop.mesh.vertices.bufferObject) {
Chris Craikef250742015-02-25 17:16:16 -0800568 LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
569 }
Chris Craik26bf3422015-02-26 16:28:17 -0800570
571 if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) {
572 LOG_ALWAYS_FATAL("Texture transform incorrectly specified");
573 }
Chris Craikf27133d2015-02-19 09:51:53 -0800574}
575
Chris Craik03188872015-02-02 18:39:33 -0800576void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800577 REQUIRE_STAGES(kAllStages);
Chris Craikeb911c22015-03-06 17:30:11 -0800578 if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::kTextureCoord) {
Chris Craik48f650c2015-03-10 11:03:39 -0700579 if (mOutGlop->fill.texture.target == GL_TEXTURE_2D) {
580 mDescription.hasTexture = true;
581 } else {
582 mDescription.hasExternalTexture = true;
583 }
584
Chris Craik26bf3422015-02-26 16:28:17 -0800585 }
Chris Craikeb911c22015-03-06 17:30:11 -0800586 mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::kColor;
587 mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::kAlpha;
Chris Craikef250742015-02-25 17:16:16 -0800588
Chris Craik82840732015-04-03 09:37:49 -0700589 // Enable debug highlight when what we're about to draw is tested against
590 // the stencil buffer and if stencil highlight debugging is on
591 mDescription.hasDebugHighlight = !mCaches.debugOverdraw
592 && mCaches.debugStencilClip == Caches::kStencilShowHighlight
593 && mRenderState.stencil().isTestEnabled();
594
Chris Craik922d3a72015-02-13 17:47:21 -0800595 // serialize shader info into ShaderData
Chris Craikf27133d2015-02-19 09:51:53 -0800596 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
Chris Craik922d3a72015-02-13 17:47:21 -0800597 SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView,
598 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
599
Chris Craik0519c812015-02-11 13:17:06 -0800600 // duplicates ProgramCache's definition of color uniform presence
601 const bool singleColor = !mDescription.hasTexture
602 && !mDescription.hasExternalTexture
603 && !mDescription.hasGradient
604 && !mDescription.hasBitmap;
605 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craikf27133d2015-02-19 09:51:53 -0800606
607 verify(mDescription, *mOutGlop);
Chris Craikef250742015-02-25 17:16:16 -0800608
609 // Final step: populate program and map bounds into render target space
610 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
611 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800612}
613
614} /* namespace uirenderer */
615} /* namespace android */