blob: 288fed360162530b201c2cb5dd2063315c13b533 [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
Chris Craikb1f990d2015-06-12 11:28:52 -070032#define DEBUG_GLOP_BUILDER 0
33
34#if DEBUG_GLOP_BUILDER
Chris Craik03188872015-02-02 18:39:33 -080035
Chris Craik117bdbc2015-02-05 10:12:38 -080036#define TRIGGER_STAGE(stageFlag) \
Chris Craik14100ac2015-02-24 13:46:29 -080037 LOG_ALWAYS_FATAL_IF((stageFlag) & mStageFlags, "Stage %d cannot be run twice", (stageFlag)); \
Chris Craik0519c812015-02-11 13:17:06 -080038 mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
Chris Craik117bdbc2015-02-05 10:12:38 -080039
Chris Craik08fa43f2015-02-09 18:58:32 -080040#define REQUIRE_STAGES(requiredFlags) \
Chris Craik0519c812015-02-11 13:17:06 -080041 LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
Chris Craik08fa43f2015-02-09 18:58:32 -080042 "not prepared for current stage")
43
Chris Craikb1f990d2015-06-12 11:28:52 -070044#else
45
46#define TRIGGER_STAGE(stageFlag) ((void)0)
47#define REQUIRE_STAGES(requiredFlags) ((void)0)
48
49#endif
50
51namespace android {
52namespace uirenderer {
53
Chris Craik922d3a72015-02-13 17:47:21 -080054static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
Chris Craik14100ac2015-02-24 13:46:29 -080055 quadVertex[0] = {0, 0, uvs.left, uvs.top};
56 quadVertex[1] = {1, 0, uvs.right, uvs.top};
57 quadVertex[2] = {0, 1, uvs.left, uvs.bottom};
58 quadVertex[3] = {1, 1, uvs.right, uvs.bottom};
Chris Craik0519c812015-02-11 13:17:06 -080059}
60
Chris Craik03188872015-02-02 18:39:33 -080061GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
62 : mRenderState(renderState)
63 , mCaches(caches)
Chris Craik922d3a72015-02-13 17:47:21 -080064 , mShader(nullptr)
Chris Craik0519c812015-02-11 13:17:06 -080065 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080066 mStageFlags = kInitialStage;
67}
68
Chris Craik0519c812015-02-11 13:17:06 -080069////////////////////////////////////////////////////////////////////////////////
70// Mesh
71////////////////////////////////////////////////////////////////////////////////
72
73GlopBuilder& GlopBuilder::setMeshUnitQuad() {
74 TRIGGER_STAGE(kMeshStage);
75
Chris Craik0519c812015-02-11 13:17:06 -080076 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080077 mOutGlop->mesh.indices = { 0, nullptr };
78 mOutGlop->mesh.vertices = {
79 mRenderState.meshState().getUnitQuadVBO(),
Chris Craik53e51e42015-06-01 10:35:35 -070080 VertexAttribFlags::None,
Chris Craikef250742015-02-25 17:16:16 -080081 nullptr, nullptr, nullptr,
82 kTextureVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -080083 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c812015-02-11 13:17:06 -080084 return *this;
85}
86
Chris Craikf27133d2015-02-19 09:51:53 -080087GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper) {
Chris Craik14100ac2015-02-24 13:46:29 -080088 if (uvMapper) {
89 // can't use unit quad VBO, so build UV vertices manually
90 return setMeshTexturedUvQuad(uvMapper, Rect(0, 0, 1, 1));
91 }
92
93 TRIGGER_STAGE(kMeshStage);
94
Chris Craik14100ac2015-02-24 13:46:29 -080095 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
Chris Craikef250742015-02-25 17:16:16 -080096 mOutGlop->mesh.indices = { 0, nullptr };
97 mOutGlop->mesh.vertices = {
98 mRenderState.meshState().getUnitQuadVBO(),
Chris Craik53e51e42015-06-01 10:35:35 -070099 VertexAttribFlags::TextureCoord,
Chris Craikef250742015-02-25 17:16:16 -0800100 nullptr, (const void*) kMeshTextureOffset, nullptr,
101 kTextureVertexStride };
Chris Craik14100ac2015-02-24 13:46:29 -0800102 mOutGlop->mesh.elementCount = 4;
Chris Craik14100ac2015-02-24 13:46:29 -0800103 return *this;
104}
105
106GlopBuilder& GlopBuilder::setMeshTexturedUvQuad(const UvMapper* uvMapper, Rect uvs) {
Chris Craik0519c812015-02-11 13:17:06 -0800107 TRIGGER_STAGE(kMeshStage);
108
Chris Craik0519c812015-02-11 13:17:06 -0800109 if (CC_UNLIKELY(uvMapper)) {
Chris Craik0519c812015-02-11 13:17:06 -0800110 uvMapper->map(uvs);
Chris Craik0519c812015-02-11 13:17:06 -0800111 }
Chris Craik14100ac2015-02-24 13:46:29 -0800112 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
113
Chris Craikef250742015-02-25 17:16:16 -0800114 const TextureVertex* textureVertex = mOutGlop->mesh.mappedVertices;
115 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
116 mOutGlop->mesh.indices = { 0, nullptr };
117 mOutGlop->mesh.vertices = {
118 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700119 VertexAttribFlags::TextureCoord,
Chris Craikef250742015-02-25 17:16:16 -0800120 &textureVertex[0].x, &textureVertex[0].u, nullptr,
121 kTextureVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -0800122 mOutGlop->mesh.elementCount = 4;
Chris Craik0519c812015-02-11 13:17:06 -0800123 return *this;
124}
125
Chris Craikef250742015-02-25 17:16:16 -0800126GlopBuilder& GlopBuilder::setMeshIndexedQuads(Vertex* vertexData, int quadCount) {
Chris Craik0519c812015-02-11 13:17:06 -0800127 TRIGGER_STAGE(kMeshStage);
128
Chris Craik0519c812015-02-11 13:17:06 -0800129 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800130 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
131 mOutGlop->mesh.vertices = {
132 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700133 VertexAttribFlags::None,
Chris Craikef250742015-02-25 17:16:16 -0800134 vertexData, nullptr, nullptr,
135 kVertexStride };
Chris Craik0519c812015-02-11 13:17:06 -0800136 mOutGlop->mesh.elementCount = 6 * quadCount;
Chris Craik0519c812015-02-11 13:17:06 -0800137 return *this;
138}
139
Chris Craikf27133d2015-02-19 09:51:53 -0800140GlopBuilder& GlopBuilder::setMeshTexturedIndexedQuads(TextureVertex* vertexData, int elementCount) {
141 TRIGGER_STAGE(kMeshStage);
142
Chris Craikf27133d2015-02-19 09:51:53 -0800143 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
Chris Craikef250742015-02-25 17:16:16 -0800144 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
145 mOutGlop->mesh.vertices = {
146 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700147 VertexAttribFlags::TextureCoord,
Chris Craikef250742015-02-25 17:16:16 -0800148 &vertexData[0].x, &vertexData[0].u, nullptr,
149 kTextureVertexStride };
Chris Craikf27133d2015-02-19 09:51:53 -0800150 mOutGlop->mesh.elementCount = elementCount;
Chris Craikef250742015-02-25 17:16:16 -0800151 return *this;
152}
153
Chris Craika6b52192015-02-27 17:43:02 -0800154GlopBuilder& GlopBuilder::setMeshTexturedMesh(TextureVertex* vertexData, int elementCount) {
155 TRIGGER_STAGE(kMeshStage);
156
157 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
158 mOutGlop->mesh.indices = { 0, nullptr };
159 mOutGlop->mesh.vertices = {
160 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700161 VertexAttribFlags::TextureCoord,
Chris Craika6b52192015-02-27 17:43:02 -0800162 &vertexData[0].x, &vertexData[0].u, nullptr,
163 kTextureVertexStride };
164 mOutGlop->mesh.elementCount = elementCount;
165 return *this;
166}
167
Chris Craikef250742015-02-25 17:16:16 -0800168GlopBuilder& GlopBuilder::setMeshColoredTexturedMesh(ColorTextureVertex* vertexData, int elementCount) {
169 TRIGGER_STAGE(kMeshStage);
170
171 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
172 mOutGlop->mesh.indices = { 0, nullptr };
173 mOutGlop->mesh.vertices = {
174 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700175 VertexAttribFlags::TextureCoord | VertexAttribFlags::Color,
Chris Craikef250742015-02-25 17:16:16 -0800176 &vertexData[0].x, &vertexData[0].u, &vertexData[0].r,
177 kColorTextureVertexStride };
178 mOutGlop->mesh.elementCount = elementCount;
Chris Craikf27133d2015-02-19 09:51:53 -0800179 return *this;
180}
181
Chris Craik117bdbc2015-02-05 10:12:38 -0800182GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
183 TRIGGER_STAGE(kMeshStage);
184
185 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
186
187 bool alphaVertex = flags & VertexBuffer::kAlpha;
188 bool indices = flags & VertexBuffer::kIndices;
Chris Craik117bdbc2015-02-05 10:12:38 -0800189
Chris Craikef250742015-02-25 17:16:16 -0800190 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
191 mOutGlop->mesh.indices = { 0, vertexBuffer.getIndices() };
192 mOutGlop->mesh.vertices = {
193 0,
Chris Craik53e51e42015-06-01 10:35:35 -0700194 alphaVertex ? VertexAttribFlags::Alpha : VertexAttribFlags::None,
Chris Craikef250742015-02-25 17:16:16 -0800195 vertexBuffer.getBuffer(), nullptr, nullptr,
196 alphaVertex ? kAlphaVertexStride : kVertexStride };
197 mOutGlop->mesh.elementCount = indices
198 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
199
Chris Craik117bdbc2015-02-05 10:12:38 -0800200 mDescription.useShadowAlphaInterp = shadowInterp;
201 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800202}
203
Chris Craik0556d902015-03-03 09:54:14 -0800204GlopBuilder& GlopBuilder::setMeshPatchQuads(const Patch& patch) {
205 TRIGGER_STAGE(kMeshStage);
206
207 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
208 mOutGlop->mesh.indices = { mRenderState.meshState().getQuadListIBO(), nullptr };
209 mOutGlop->mesh.vertices = {
210 mCaches.patchCache.getMeshBuffer(),
Chris Craik53e51e42015-06-01 10:35:35 -0700211 VertexAttribFlags::TextureCoord,
Chris Craik8820fd12015-03-03 14:20:47 -0800212 (void*)patch.positionOffset, (void*)patch.textureOffset, nullptr,
Chris Craik0556d902015-03-03 09:54:14 -0800213 kTextureVertexStride };
214 mOutGlop->mesh.elementCount = patch.indexCount;
215 return *this;
216}
217
Chris Craik0519c812015-02-11 13:17:06 -0800218////////////////////////////////////////////////////////////////////////////////
219// Fill
220////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800221
Chris Craik182952f2015-03-09 14:17:29 -0700222void GlopBuilder::setFill(int color, float alphaScale,
223 SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage,
Chris Craik0519c812015-02-11 13:17:06 -0800224 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800225 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800226 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800227 if (!shader) {
228 float colorScale = alpha / 255.0f;
229 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800230 colorScale * SkColorGetR(color),
231 colorScale * SkColorGetG(color),
Chris Craik0519c812015-02-11 13:17:06 -0800232 colorScale * SkColorGetB(color),
233 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800234 };
235 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800236 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800237 }
Chris Craik03188872015-02-02 18:39:33 -0800238 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800239 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800240 }
Chris Craik03188872015-02-02 18:39:33 -0800241
Chris Craik117bdbc2015-02-05 10:12:38 -0800242 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800243 if (mOutGlop->fill.color.a < 1.0f
Chris Craik53e51e42015-06-01 10:35:35 -0700244 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
Chris Craikf27133d2015-02-19 09:51:53 -0800245 || (mOutGlop->fill.texture.texture && mOutGlop->fill.texture.texture->blend)
Chris Craik0519c812015-02-11 13:17:06 -0800246 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800247 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800248 || PaintUtils::isBlendedColorFilter(colorFilter)
249 || mode != SkXfermode::kSrcOver_Mode) {
250 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
Chris Craik182952f2015-03-09 14:17:29 -0700251 Blend::getFactors(mode, modeUsage,
Chris Craik03188872015-02-02 18:39:33 -0800252 &mOutGlop->blend.src, &mOutGlop->blend.dst);
253 } else {
254 // These blend modes are not supported by OpenGL directly and have
255 // to be implemented using shaders. Since the shader will perform
256 // the blending, don't enable GL blending off here
257 // If the blend mode cannot be implemented using shaders, fall
258 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800259 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800260 mDescription.framebufferMode = mode;
Chris Craik182952f2015-03-09 14:17:29 -0700261 mDescription.swapSrcDst = (modeUsage == Blend::ModeOrderSwap::Swap);
Chris Craik03188872015-02-02 18:39:33 -0800262 // blending in shader, don't enable
263 } else {
264 // unsupported
Chris Craik182952f2015-03-09 14:17:29 -0700265 Blend::getFactors(SkXfermode::kSrcOver_Mode, modeUsage,
Chris Craik03188872015-02-02 18:39:33 -0800266 &mOutGlop->blend.src, &mOutGlop->blend.dst);
267 }
268 }
269 }
Chris Craik922d3a72015-02-13 17:47:21 -0800270 mShader = shader; // shader resolved in ::build()
Chris Craik03188872015-02-02 18:39:33 -0800271
Chris Craik117bdbc2015-02-05 10:12:38 -0800272 if (colorFilter) {
273 SkColor color;
274 SkXfermode::Mode mode;
275 SkScalar srcColorMatrix[20];
276 if (colorFilter->asColorMode(&color, &mode)) {
277 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
278 mDescription.colorMode = mode;
279
280 const float alpha = SkColorGetA(color) / 255.0f;
281 float colorScale = alpha / 255.0f;
282 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800283 colorScale * SkColorGetR(color),
284 colorScale * SkColorGetG(color),
285 colorScale * SkColorGetB(color),
Chris Craik0519c812015-02-11 13:17:06 -0800286 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800287 };
288 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
289 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
290
291 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
292 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
293 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
294 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
295 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
296
297 // Skia uses the range [0..255] for the addition vector, but we need
298 // the [0..1] range to apply the vector in GLSL
299 float* colorVector = mOutGlop->fill.filter.matrix.vector;
300 colorVector[0] = srcColorMatrix[4] / 255.0f;
301 colorVector[1] = srcColorMatrix[9] / 255.0f;
302 colorVector[2] = srcColorMatrix[14] / 255.0f;
303 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800304 } else {
305 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800306 }
307 } else {
308 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
309 }
Chris Craik0519c812015-02-11 13:17:06 -0800310}
Chris Craik117bdbc2015-02-05 10:12:38 -0800311
Chris Craik53e51e42015-06-01 10:35:35 -0700312GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture,
313 const int textureFillFlags, const SkPaint* paint, float alphaScale) {
Chris Craik0519c812015-02-11 13:17:06 -0800314 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700315 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik0519c812015-02-11 13:17:06 -0800316
Chris Craik53e51e42015-06-01 10:35:35 -0700317 GLenum filter = (textureFillFlags & TextureFillFlags::ForceFilter)
Chris Craika6b52192015-02-27 17:43:02 -0800318 ? GL_LINEAR : PaintUtils::getFilter(paint);
Chris Craik26bf3422015-02-26 16:28:17 -0800319 mOutGlop->fill.texture = { &texture,
Chris Craika6b52192015-02-27 17:43:02 -0800320 GL_TEXTURE_2D, filter, GL_CLAMP_TO_EDGE, nullptr };
Chris Craik0519c812015-02-11 13:17:06 -0800321
322 if (paint) {
323 int color = paint->getColor();
324 SkShader* shader = paint->getShader();
325
Chris Craik53e51e42015-06-01 10:35:35 -0700326 if (!(textureFillFlags & TextureFillFlags::IsAlphaMaskTexture)) {
Chris Craik0519c812015-02-11 13:17:06 -0800327 // Texture defines color, so disable shaders, and reset all non-alpha color channels
328 color |= 0x00FFFFFF;
329 shader = nullptr;
330 }
Chris Craik182952f2015-03-09 14:17:29 -0700331 setFill(color, alphaScale,
332 PaintUtils::getXfermode(paint->getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800333 shader, paint->getColorFilter());
334 } else {
335 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
336
Chris Craik0519c812015-02-11 13:17:06 -0800337 if (alphaScale < 1.0f
Chris Craik53e51e42015-06-01 10:35:35 -0700338 || (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
Chris Craik0519c812015-02-11 13:17:06 -0800339 || texture.blend
340 || mOutGlop->roundRectClipState) {
Chris Craik182952f2015-03-09 14:17:29 -0700341 Blend::getFactors(SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800342 &mOutGlop->blend.src, &mOutGlop->blend.dst);
343 } else {
344 mOutGlop->blend = { GL_ZERO, GL_ZERO };
345 }
346 }
347
Chris Craik53e51e42015-06-01 10:35:35 -0700348 if (textureFillFlags & TextureFillFlags::IsAlphaMaskTexture) {
Chris Craik2bb8f562015-02-17 16:42:02 -0800349 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craika6b52192015-02-27 17:43:02 -0800350 mDescription.hasAlpha8Texture = true;
Chris Craik0519c812015-02-11 13:17:06 -0800351 } else {
352 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
353 }
Chris Craik03188872015-02-02 18:39:33 -0800354 return *this;
355}
356
Chris Craik0519c812015-02-11 13:17:06 -0800357GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
358 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700359 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik0519c812015-02-11 13:17:06 -0800360
Chris Craik26bf3422015-02-26 16:28:17 -0800361 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik0519c812015-02-11 13:17:06 -0800362
Chris Craik182952f2015-03-09 14:17:29 -0700363 setFill(paint.getColor(), alphaScale,
364 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik0519c812015-02-11 13:17:06 -0800365 paint.getShader(), paint.getColorFilter());
366 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
367 return *this;
368}
369
Chris Craik2bb8f562015-02-17 16:42:02 -0800370GlopBuilder& GlopBuilder::setFillPathTexturePaint(PathTexture& texture,
Chris Craik30036092015-02-12 10:41:39 -0800371 const SkPaint& paint, float alphaScale) {
372 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700373 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik30036092015-02-12 10:41:39 -0800374
Chris Craikf27133d2015-02-19 09:51:53 -0800375 //specify invalid filter/clamp, since these are always static for PathTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800376 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik30036092015-02-12 10:41:39 -0800377
Chris Craik182952f2015-03-09 14:17:29 -0700378 setFill(paint.getColor(), alphaScale,
379 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik30036092015-02-12 10:41:39 -0800380 paint.getShader(), paint.getColorFilter());
381
Chris Craikf27133d2015-02-19 09:51:53 -0800382 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800383 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
Chris Craik30036092015-02-12 10:41:39 -0800384 return *this;
385}
386
Chris Craik2bb8f562015-02-17 16:42:02 -0800387GlopBuilder& GlopBuilder::setFillShadowTexturePaint(ShadowTexture& texture, int shadowColor,
388 const SkPaint& paint, float alphaScale) {
389 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700390 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik2bb8f562015-02-17 16:42:02 -0800391
Chris Craikf27133d2015-02-19 09:51:53 -0800392 //specify invalid filter/clamp, since these are always static for ShadowTextures
Chris Craik26bf3422015-02-26 16:28:17 -0800393 mOutGlop->fill.texture = { &texture, GL_TEXTURE_2D, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik2bb8f562015-02-17 16:42:02 -0800394
395 const int ALPHA_BITMASK = SK_ColorBLACK;
396 const int COLOR_BITMASK = ~ALPHA_BITMASK;
397 if ((shadowColor & ALPHA_BITMASK) == ALPHA_BITMASK) {
398 // shadow color is fully opaque: override its alpha with that of paint
399 shadowColor &= paint.getColor() | COLOR_BITMASK;
400 }
401
Chris Craik182952f2015-03-09 14:17:29 -0700402 setFill(shadowColor, alphaScale,
403 PaintUtils::getXfermode(paint.getXfermode()), Blend::ModeOrderSwap::NoSwap,
Chris Craik2bb8f562015-02-17 16:42:02 -0800404 paint.getShader(), paint.getColorFilter());
405
Chris Craikf27133d2015-02-19 09:51:53 -0800406 mDescription.hasAlpha8Texture = true;
Chris Craik2bb8f562015-02-17 16:42:02 -0800407 mDescription.modulate = mOutGlop->fill.color.isNotBlack();
408 return *this;
409}
410
411GlopBuilder& GlopBuilder::setFillBlack() {
412 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700413 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik2bb8f562015-02-17 16:42:02 -0800414
Chris Craik26bf3422015-02-26 16:28:17 -0800415 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik182952f2015-03-09 14:17:29 -0700416 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kSrcOver_Mode, Blend::ModeOrderSwap::NoSwap,
417 nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800418 return *this;
419}
420
421GlopBuilder& GlopBuilder::setFillClear() {
422 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700423 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik2bb8f562015-02-17 16:42:02 -0800424
Chris Craik26bf3422015-02-26 16:28:17 -0800425 mOutGlop->fill.texture = { nullptr, GL_INVALID_ENUM, GL_INVALID_ENUM, GL_INVALID_ENUM, nullptr };
Chris Craik182952f2015-03-09 14:17:29 -0700426 setFill(SK_ColorBLACK, 1.0f, SkXfermode::kClear_Mode, Blend::ModeOrderSwap::NoSwap,
427 nullptr, nullptr);
Chris Craik2bb8f562015-02-17 16:42:02 -0800428 return *this;
429}
Chris Craikf27133d2015-02-19 09:51:53 -0800430
431GlopBuilder& GlopBuilder::setFillLayer(Texture& texture, const SkColorFilter* colorFilter,
Chris Craik182952f2015-03-09 14:17:29 -0700432 float alpha, SkXfermode::Mode mode, Blend::ModeOrderSwap modeUsage) {
Chris Craikf27133d2015-02-19 09:51:53 -0800433 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700434 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craikf27133d2015-02-19 09:51:53 -0800435
Chris Craik26bf3422015-02-26 16:28:17 -0800436 mOutGlop->fill.texture = { &texture,
437 GL_TEXTURE_2D, GL_LINEAR, GL_CLAMP_TO_EDGE, nullptr };
Chris Craikf27133d2015-02-19 09:51:53 -0800438 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
439
Chris Craik182952f2015-03-09 14:17:29 -0700440 setFill(SK_ColorWHITE, alpha, mode, modeUsage, nullptr, colorFilter);
Chris Craikf27133d2015-02-19 09:51:53 -0800441
442 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
443 return *this;
444}
445
Chris Craik26bf3422015-02-26 16:28:17 -0800446GlopBuilder& GlopBuilder::setFillTextureLayer(Layer& layer, float alpha) {
447 TRIGGER_STAGE(kFillStage);
Chris Craikb1f990d2015-06-12 11:28:52 -0700448 REQUIRE_STAGES(kMeshStage | kRoundRectClipStage);
Chris Craik26bf3422015-02-26 16:28:17 -0800449
450 mOutGlop->fill.texture = { &(layer.getTexture()),
451 layer.getRenderTarget(), GL_LINEAR, GL_CLAMP_TO_EDGE, &layer.getTexTransform() };
452 mOutGlop->fill.color = { alpha, alpha, alpha, alpha };
453
Chris Craik182952f2015-03-09 14:17:29 -0700454 setFill(SK_ColorWHITE, alpha, layer.getMode(), Blend::ModeOrderSwap::NoSwap,
455 nullptr, layer.getColorFilter());
Chris Craik26bf3422015-02-26 16:28:17 -0800456
457 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
458 mDescription.hasTextureTransform = true;
459 return *this;
460}
461
Chris Craik0519c812015-02-11 13:17:06 -0800462////////////////////////////////////////////////////////////////////////////////
463// Transform
464////////////////////////////////////////////////////////////////////////////////
465
Chris Craik53e51e42015-06-01 10:35:35 -0700466void GlopBuilder::setTransform(const Matrix4& ortho, const Matrix4& canvas,
467 const int transformFlags) {
Chris Craik0519c812015-02-11 13:17:06 -0800468 TRIGGER_STAGE(kTransformStage);
469
470 mOutGlop->transform.ortho.load(ortho);
Chris Craik53e51e42015-06-01 10:35:35 -0700471 mOutGlop->transform.canvas.load(canvas);
472 mOutGlop->transform.transformFlags = transformFlags;
Chris Craik0519c812015-02-11 13:17:06 -0800473}
474
Chris Craik0519c812015-02-11 13:17:06 -0800475////////////////////////////////////////////////////////////////////////////////
476// ModelView
477////////////////////////////////////////////////////////////////////////////////
478
479GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
480 TRIGGER_STAGE(kModelViewStage);
481
482 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
483 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
484 mOutGlop->bounds = destination;
485 return *this;
486}
487
488GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
489 TRIGGER_STAGE(kModelViewStage);
490 REQUIRE_STAGES(kTransformStage | kFillStage);
491
492 float left = destination.left;
493 float top = destination.top;
494
Chris Craik53e51e42015-06-01 10:35:35 -0700495 const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
496 if (CC_LIKELY(meshTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800497 // snap by adjusting the model view matrix
Chris Craik53e51e42015-06-01 10:35:35 -0700498 const float translateX = meshTransform.getTranslateX();
499 const float translateY = meshTransform.getTranslateY();
Chris Craik0519c812015-02-11 13:17:06 -0800500
501 left = (int) floorf(left + translateX + 0.5f) - translateX;
502 top = (int) floorf(top + translateY + 0.5f) - translateY;
Chris Craikf27133d2015-02-19 09:51:53 -0800503 mOutGlop->fill.texture.filter = GL_NEAREST;
Chris Craik0519c812015-02-11 13:17:06 -0800504 }
505
506 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
507 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
508 mOutGlop->bounds = destination;
509 return *this;
510}
511
512GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
513 TRIGGER_STAGE(kModelViewStage);
514
515 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
516 mOutGlop->bounds = source;
517 mOutGlop->bounds.translate(offsetX, offsetY);
518 return *this;
519}
520
Chris Craikf27133d2015-02-19 09:51:53 -0800521GlopBuilder& GlopBuilder::setModelViewOffsetRectSnap(float offsetX, float offsetY, const Rect source) {
522 TRIGGER_STAGE(kModelViewStage);
523 REQUIRE_STAGES(kTransformStage | kFillStage);
524
Chris Craik53e51e42015-06-01 10:35:35 -0700525 const Matrix4& meshTransform = mOutGlop->transform.meshTransform();
526 if (CC_LIKELY(meshTransform.isPureTranslate())) {
Chris Craikf27133d2015-02-19 09:51:53 -0800527 // snap by adjusting the model view matrix
Chris Craik53e51e42015-06-01 10:35:35 -0700528 const float translateX = meshTransform.getTranslateX();
529 const float translateY = meshTransform.getTranslateY();
Chris Craikf27133d2015-02-19 09:51:53 -0800530
531 offsetX = (int) floorf(offsetX + translateX + source.left + 0.5f) - translateX - source.left;
532 offsetY = (int) floorf(offsetY + translateY + source.top + 0.5f) - translateY - source.top;
533 mOutGlop->fill.texture.filter = GL_NEAREST;
534 }
535
536 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
Chris Craik8820fd12015-03-03 14:20:47 -0800537 mOutGlop->bounds = source;
Chris Craikf27133d2015-02-19 09:51:53 -0800538 mOutGlop->bounds.translate(offsetX, offsetY);
539 return *this;
540}
541
542////////////////////////////////////////////////////////////////////////////////
543// RoundRectClip
544////////////////////////////////////////////////////////////////////////////////
545
Chris Craik0519c812015-02-11 13:17:06 -0800546GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
547 TRIGGER_STAGE(kRoundRectClipStage);
548
549 mOutGlop->roundRectClipState = roundRectClipState;
550 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
551 return *this;
552}
553
554////////////////////////////////////////////////////////////////////////////////
555// Build
556////////////////////////////////////////////////////////////////////////////////
557
Chris Craikf27133d2015-02-19 09:51:53 -0800558void verify(const ProgramDescription& description, const Glop& glop) {
Chris Craikeb911c22015-03-06 17:30:11 -0800559 if (glop.fill.texture.texture != nullptr) {
560 LOG_ALWAYS_FATAL_IF(((description.hasTexture && description.hasExternalTexture)
561 || (!description.hasTexture && !description.hasExternalTexture)
Chris Craik53e51e42015-06-01 10:35:35 -0700562 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) == 0)),
Chris Craikeb911c22015-03-06 17:30:11 -0800563 "Texture %p, hT%d, hET %d, attribFlags %x",
564 glop.fill.texture.texture,
565 description.hasTexture, description.hasExternalTexture,
566 glop.mesh.vertices.attribFlags);
567 } else {
568 LOG_ALWAYS_FATAL_IF((description.hasTexture
569 || description.hasExternalTexture
Chris Craik53e51e42015-06-01 10:35:35 -0700570 || ((glop.mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) != 0)),
Chris Craikeb911c22015-03-06 17:30:11 -0800571 "No texture, hT%d, hET %d, attribFlags %x",
572 description.hasTexture, description.hasExternalTexture,
573 glop.mesh.vertices.attribFlags);
574 }
Chris Craikef250742015-02-25 17:16:16 -0800575
Chris Craik53e51e42015-06-01 10:35:35 -0700576 if ((glop.mesh.vertices.attribFlags & VertexAttribFlags::Alpha)
Chris Craikeb911c22015-03-06 17:30:11 -0800577 && glop.mesh.vertices.bufferObject) {
Chris Craikef250742015-02-25 17:16:16 -0800578 LOG_ALWAYS_FATAL("VBO and alpha attributes are not currently compatible");
579 }
Chris Craik26bf3422015-02-26 16:28:17 -0800580
581 if (description.hasTextureTransform != (glop.fill.texture.textureTransform != nullptr)) {
582 LOG_ALWAYS_FATAL("Texture transform incorrectly specified");
583 }
Chris Craikf27133d2015-02-19 09:51:53 -0800584}
585
Chris Craik03188872015-02-02 18:39:33 -0800586void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800587 REQUIRE_STAGES(kAllStages);
Chris Craik53e51e42015-06-01 10:35:35 -0700588 if (mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::TextureCoord) {
Chris Craik48f650c2015-03-10 11:03:39 -0700589 if (mOutGlop->fill.texture.target == GL_TEXTURE_2D) {
590 mDescription.hasTexture = true;
591 } else {
592 mDescription.hasExternalTexture = true;
593 }
Chris Craik26bf3422015-02-26 16:28:17 -0800594 }
Chris Craik53e51e42015-06-01 10:35:35 -0700595
596 mDescription.hasColors = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Color;
597 mDescription.hasVertexAlpha = mOutGlop->mesh.vertices.attribFlags & VertexAttribFlags::Alpha;
Chris Craikef250742015-02-25 17:16:16 -0800598
Chris Craik82840732015-04-03 09:37:49 -0700599 // Enable debug highlight when what we're about to draw is tested against
600 // the stencil buffer and if stencil highlight debugging is on
Chris Craik2507c342015-05-04 14:36:49 -0700601 mDescription.hasDebugHighlight = !Properties::debugOverdraw
602 && Properties::debugStencilClip == StencilClipDebug::ShowHighlight
Chris Craik82840732015-04-03 09:37:49 -0700603 && mRenderState.stencil().isTestEnabled();
604
Chris Craik922d3a72015-02-13 17:47:21 -0800605 // serialize shader info into ShaderData
Chris Craikf27133d2015-02-19 09:51:53 -0800606 GLuint textureUnit = mOutGlop->fill.texture.texture ? 1 : 0;
Chris Craik53e51e42015-06-01 10:35:35 -0700607
608 if (CC_LIKELY(!mShader)) {
609 mOutGlop->fill.skiaShaderData.skiaShaderType = kNone_SkiaShaderType;
610 } else {
611 Matrix4 shaderMatrix;
612 if (mOutGlop->transform.transformFlags & TransformFlags::MeshIgnoresCanvasTransform) {
613 // canvas level transform was built into the modelView and geometry,
614 // so the shader matrix must reverse this
615 shaderMatrix.loadInverse(mOutGlop->transform.canvas);
616 shaderMatrix.multiply(mOutGlop->transform.modelView);
617 } else {
618 shaderMatrix.load(mOutGlop->transform.modelView);
619 }
620 SkiaShader::store(mCaches, *mShader, shaderMatrix,
621 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
622 }
Chris Craik922d3a72015-02-13 17:47:21 -0800623
Chris Craik0519c812015-02-11 13:17:06 -0800624 // duplicates ProgramCache's definition of color uniform presence
625 const bool singleColor = !mDescription.hasTexture
626 && !mDescription.hasExternalTexture
627 && !mDescription.hasGradient
628 && !mDescription.hasBitmap;
629 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craikf27133d2015-02-19 09:51:53 -0800630
631 verify(mDescription, *mOutGlop);
Chris Craikef250742015-02-25 17:16:16 -0800632
633 // Final step: populate program and map bounds into render target space
634 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik53e51e42015-06-01 10:35:35 -0700635 mOutGlop->transform.meshTransform().mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800636}
637
638} /* namespace uirenderer */
639} /* namespace android */