blob: bdc5c5c2755247e907b28fcfda131dae6ba814cf [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) \
35 LOG_ALWAYS_FATAL_IF(stageFlag & mStageFlags, "Stage %d cannot be run twice"); \
Chris Craik0519c812015-02-11 13:17:06 -080036 mStageFlags = static_cast<StageFlags>(mStageFlags | (stageFlag))
Chris Craik117bdbc2015-02-05 10:12:38 -080037
Chris Craik08fa43f2015-02-09 18:58:32 -080038#define REQUIRE_STAGES(requiredFlags) \
Chris Craik0519c812015-02-11 13:17:06 -080039 LOG_ALWAYS_FATAL_IF((mStageFlags & (requiredFlags)) != (requiredFlags), \
Chris Craik08fa43f2015-02-09 18:58:32 -080040 "not prepared for current stage")
41
Chris Craik922d3a72015-02-13 17:47:21 -080042static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {
Chris Craik0519c812015-02-11 13:17:06 -080043 TextureVertex::setUV(quadVertex++, uvs.left, uvs.top);
44 TextureVertex::setUV(quadVertex++, uvs.right, uvs.top);
45 TextureVertex::setUV(quadVertex++, uvs.left, uvs.bottom);
46 TextureVertex::setUV(quadVertex++, uvs.right, uvs.bottom);
47}
48
Chris Craik03188872015-02-02 18:39:33 -080049GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
50 : mRenderState(renderState)
51 , mCaches(caches)
Chris Craik922d3a72015-02-13 17:47:21 -080052 , mShader(nullptr)
Chris Craik0519c812015-02-11 13:17:06 -080053 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080054 mStageFlags = kInitialStage;
55}
56
Chris Craik0519c812015-02-11 13:17:06 -080057////////////////////////////////////////////////////////////////////////////////
58// Mesh
59////////////////////////////////////////////////////////////////////////////////
60
61GlopBuilder& GlopBuilder::setMeshUnitQuad() {
62 TRIGGER_STAGE(kMeshStage);
63
64 mOutGlop->mesh.vertexFlags = kNone_Attrib;
65 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
66 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
67 mOutGlop->mesh.vertices = nullptr;
68 mOutGlop->mesh.indexBufferObject = 0;
69 mOutGlop->mesh.indices = nullptr;
70 mOutGlop->mesh.elementCount = 4;
71 mOutGlop->mesh.stride = kTextureVertexStride;
72 mOutGlop->mesh.texCoordOffset = nullptr;
73 return *this;
74}
75
76GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper,
77 bool isAlphaMaskTexture) {
78 TRIGGER_STAGE(kMeshStage);
79
80 mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib;
81 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
82
83 if (CC_UNLIKELY(uvMapper)) {
84 Rect uvs(0, 0, 1, 1);
85 uvMapper->map(uvs);
86 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
87
88 mOutGlop->mesh.vertexBufferObject = 0;
89 mOutGlop->mesh.vertices = &mOutGlop->mesh.mappedVertices[0];
90 } else {
91 // standard UV coordinates, use regular unit quad VBO
92 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
93 mOutGlop->mesh.vertices = nullptr;
94 }
95 mOutGlop->mesh.indexBufferObject = 0;
96 mOutGlop->mesh.indices = nullptr;
97 mOutGlop->mesh.elementCount = 4;
98 mOutGlop->mesh.stride = kTextureVertexStride;
99 mOutGlop->mesh.texCoordOffset = (GLvoid*) kMeshTextureOffset;
100
101 mDescription.hasTexture = true;
102 mDescription.hasAlpha8Texture = isAlphaMaskTexture;
103 return *this;
104}
105
106GlopBuilder& GlopBuilder::setMeshIndexedQuads(void* vertexData, int quadCount) {
107 TRIGGER_STAGE(kMeshStage);
108
109 mOutGlop->mesh.vertexFlags = kNone_Attrib;
110 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
111 mOutGlop->mesh.vertexBufferObject = 0;
112 mOutGlop->mesh.vertices = vertexData;
113 mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO();
114 mOutGlop->mesh.indices = nullptr;
115 mOutGlop->mesh.elementCount = 6 * quadCount;
116 mOutGlop->mesh.stride = kVertexStride;
117 mOutGlop->mesh.texCoordOffset = nullptr;
118
119 return *this;
120}
121
Chris Craik117bdbc2015-02-05 10:12:38 -0800122GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
123 TRIGGER_STAGE(kMeshStage);
124
125 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
126
127 bool alphaVertex = flags & VertexBuffer::kAlpha;
128 bool indices = flags & VertexBuffer::kIndices;
129 mOutGlop->mesh.vertexFlags = alphaVertex ? kAlpha_Attrib : kNone_Attrib;
130 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
131 mOutGlop->mesh.vertexBufferObject = 0;
132 mOutGlop->mesh.vertices = vertexBuffer.getBuffer();
133 mOutGlop->mesh.indexBufferObject = 0;
134 mOutGlop->mesh.indices = vertexBuffer.getIndices();
Chris Craik0519c812015-02-11 13:17:06 -0800135 mOutGlop->mesh.elementCount = indices
Chris Craik117bdbc2015-02-05 10:12:38 -0800136 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
137 mOutGlop->mesh.stride = alphaVertex ? kAlphaVertexStride : kVertexStride;
138
139 mDescription.hasVertexAlpha = alphaVertex;
140 mDescription.useShadowAlphaInterp = shadowInterp;
141 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800142}
143
Chris Craik0519c812015-02-11 13:17:06 -0800144////////////////////////////////////////////////////////////////////////////////
145// Fill
146////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800147
Chris Craik0519c812015-02-11 13:17:06 -0800148void GlopBuilder::setFill(int color, float alphaScale, SkXfermode::Mode mode,
149 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800150 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800151 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800152 if (!shader) {
153 float colorScale = alpha / 255.0f;
154 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800155 colorScale * SkColorGetR(color),
156 colorScale * SkColorGetG(color),
Chris Craik0519c812015-02-11 13:17:06 -0800157 colorScale * SkColorGetB(color),
158 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800159 };
160 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800161 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800162 }
Chris Craik03188872015-02-02 18:39:33 -0800163 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800164 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800165 }
166 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800167
Chris Craik117bdbc2015-02-05 10:12:38 -0800168 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800169 if (mOutGlop->fill.color.a < 1.0f
Chris Craik08fa43f2015-02-09 18:58:32 -0800170 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
Chris Craik0519c812015-02-11 13:17:06 -0800171 || (mOutGlop->fill.texture && mOutGlop->fill.texture->blend)
172 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800173 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800174 || PaintUtils::isBlendedColorFilter(colorFilter)
175 || mode != SkXfermode::kSrcOver_Mode) {
176 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
177 Blend::getFactors(mode, SWAP_SRC_DST,
178 &mOutGlop->blend.src, &mOutGlop->blend.dst);
179 } else {
180 // These blend modes are not supported by OpenGL directly and have
181 // to be implemented using shaders. Since the shader will perform
182 // the blending, don't enable GL blending off here
183 // If the blend mode cannot be implemented using shaders, fall
184 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800185 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800186 mDescription.framebufferMode = mode;
187 mDescription.swapSrcDst = SWAP_SRC_DST;
188 // blending in shader, don't enable
189 } else {
190 // unsupported
191 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
192 &mOutGlop->blend.src, &mOutGlop->blend.dst);
193 }
194 }
195 }
Chris Craik922d3a72015-02-13 17:47:21 -0800196 mShader = shader; // shader resolved in ::build()
Chris Craik03188872015-02-02 18:39:33 -0800197
Chris Craik117bdbc2015-02-05 10:12:38 -0800198 if (colorFilter) {
199 SkColor color;
200 SkXfermode::Mode mode;
201 SkScalar srcColorMatrix[20];
202 if (colorFilter->asColorMode(&color, &mode)) {
203 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
204 mDescription.colorMode = mode;
205
206 const float alpha = SkColorGetA(color) / 255.0f;
207 float colorScale = alpha / 255.0f;
208 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800209 colorScale * SkColorGetR(color),
210 colorScale * SkColorGetG(color),
211 colorScale * SkColorGetB(color),
Chris Craik0519c812015-02-11 13:17:06 -0800212 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800213 };
214 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
215 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
216
217 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
218 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
219 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
220 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
221 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
222
223 // Skia uses the range [0..255] for the addition vector, but we need
224 // the [0..1] range to apply the vector in GLSL
225 float* colorVector = mOutGlop->fill.filter.matrix.vector;
226 colorVector[0] = srcColorMatrix[4] / 255.0f;
227 colorVector[1] = srcColorMatrix[9] / 255.0f;
228 colorVector[2] = srcColorMatrix[14] / 255.0f;
229 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800230 } else {
231 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800232 }
233 } else {
234 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
235 }
Chris Craik0519c812015-02-11 13:17:06 -0800236}
Chris Craik117bdbc2015-02-05 10:12:38 -0800237
Chris Craik0519c812015-02-11 13:17:06 -0800238GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, bool isAlphaMaskTexture,
239 const SkPaint* paint, float alphaScale) {
240 TRIGGER_STAGE(kFillStage);
241 REQUIRE_STAGES(kMeshStage);
242
243 mOutGlop->fill.texture = &texture;
244 mOutGlop->fill.textureFilter = PaintUtils::getFilter(paint);
Chris Craik30036092015-02-12 10:41:39 -0800245 mOutGlop->fill.textureClamp = GL_CLAMP_TO_EDGE;
Chris Craik0519c812015-02-11 13:17:06 -0800246
247 if (paint) {
248 int color = paint->getColor();
249 SkShader* shader = paint->getShader();
250
251 if (!isAlphaMaskTexture) {
252 // Texture defines color, so disable shaders, and reset all non-alpha color channels
253 color |= 0x00FFFFFF;
254 shader = nullptr;
255 }
256 setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()),
257 shader, paint->getColorFilter());
258 } else {
259 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
260
261 const bool SWAP_SRC_DST = false;
262 if (alphaScale < 1.0f
263 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
264 || texture.blend
265 || mOutGlop->roundRectClipState) {
266 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
267 &mOutGlop->blend.src, &mOutGlop->blend.dst);
268 } else {
269 mOutGlop->blend = { GL_ZERO, GL_ZERO };
270 }
271 }
272
273 if (isAlphaMaskTexture) {
274 mDescription.modulate = mOutGlop->fill.color.a < 1.0f
275 || mOutGlop->fill.color.r > 0.0f
276 || mOutGlop->fill.color.g > 0.0f
277 || mOutGlop->fill.color.b > 0.0f;
278 } else {
279 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
280 }
Chris Craik03188872015-02-02 18:39:33 -0800281 return *this;
282}
283
Chris Craik0519c812015-02-11 13:17:06 -0800284GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
285 TRIGGER_STAGE(kFillStage);
286 REQUIRE_STAGES(kMeshStage);
287
288 mOutGlop->fill.texture = nullptr;
Chris Craik30036092015-02-12 10:41:39 -0800289 mOutGlop->fill.textureFilter = GL_INVALID_ENUM;
290 mOutGlop->fill.textureClamp = GL_INVALID_ENUM;
Chris Craik0519c812015-02-11 13:17:06 -0800291
292 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
293 paint.getShader(), paint.getColorFilter());
294 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
295 return *this;
296}
297
Chris Craik30036092015-02-12 10:41:39 -0800298GlopBuilder& GlopBuilder::setFillPathTexturePaint(Texture& texture,
299 const SkPaint& paint, float alphaScale) {
300 TRIGGER_STAGE(kFillStage);
301 REQUIRE_STAGES(kMeshStage);
302
303 mOutGlop->fill.texture = &texture;
304
305 //specify invalid, since these are always static for path textures
306 mOutGlop->fill.textureFilter = GL_INVALID_ENUM;
307 mOutGlop->fill.textureClamp = GL_INVALID_ENUM;
308
309 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
310 paint.getShader(), paint.getColorFilter());
311
312 mDescription.modulate = mOutGlop->fill.color.a < 1.0f
313 || mOutGlop->fill.color.r > 0.0f
314 || mOutGlop->fill.color.g > 0.0f
315 || mOutGlop->fill.color.b > 0.0f;
316 return *this;
317}
318
Chris Craik0519c812015-02-11 13:17:06 -0800319////////////////////////////////////////////////////////////////////////////////
320// Transform
321////////////////////////////////////////////////////////////////////////////////
322
323GlopBuilder& GlopBuilder::setTransformClip(const Matrix4& ortho,
324 const Matrix4& transform, bool fudgingOffset) {
325 TRIGGER_STAGE(kTransformStage);
326
327 mOutGlop->transform.ortho.load(ortho);
328 mOutGlop->transform.canvas.load(transform);
329 mOutGlop->transform.fudgingOffset = fudgingOffset;
330 return *this;
331}
332
Chris Craik0519c812015-02-11 13:17:06 -0800333////////////////////////////////////////////////////////////////////////////////
334// ModelView
335////////////////////////////////////////////////////////////////////////////////
336
337GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
338 TRIGGER_STAGE(kModelViewStage);
339
340 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
341 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
342 mOutGlop->bounds = destination;
343 return *this;
344}
345
346GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
347 TRIGGER_STAGE(kModelViewStage);
348 REQUIRE_STAGES(kTransformStage | kFillStage);
349
350 float left = destination.left;
351 float top = destination.top;
352
353 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
354 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
355 const float translateX = canvasTransform.getTranslateX();
356 const float translateY = canvasTransform.getTranslateY();
357
358 left = (int) floorf(left + translateX + 0.5f) - translateX;
359 top = (int) floorf(top + translateY + 0.5f) - translateY;
360 mOutGlop->fill.textureFilter = GL_NEAREST;
361 }
362
363 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
364 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
365 mOutGlop->bounds = destination;
366 return *this;
367}
368
369GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
370 TRIGGER_STAGE(kModelViewStage);
371
372 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
373 mOutGlop->bounds = source;
374 mOutGlop->bounds.translate(offsetX, offsetY);
375 return *this;
376}
377
378GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
379 TRIGGER_STAGE(kRoundRectClipStage);
380
381 mOutGlop->roundRectClipState = roundRectClipState;
382 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
383 return *this;
384}
385
386////////////////////////////////////////////////////////////////////////////////
387// Build
388////////////////////////////////////////////////////////////////////////////////
389
Chris Craik03188872015-02-02 18:39:33 -0800390void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800391 REQUIRE_STAGES(kAllStages);
Chris Craik117bdbc2015-02-05 10:12:38 -0800392
Chris Craik922d3a72015-02-13 17:47:21 -0800393 // serialize shader info into ShaderData
394 GLuint textureUnit = mOutGlop->fill.texture ? 1 : 0;
395 SkiaShader::store(mCaches, mShader, mOutGlop->transform.modelView,
396 &textureUnit, &mDescription, &(mOutGlop->fill.skiaShaderData));
397
Chris Craik03188872015-02-02 18:39:33 -0800398 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik117bdbc2015-02-05 10:12:38 -0800399 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik0519c812015-02-11 13:17:06 -0800400
401 // duplicates ProgramCache's definition of color uniform presence
402 const bool singleColor = !mDescription.hasTexture
403 && !mDescription.hasExternalTexture
404 && !mDescription.hasGradient
405 && !mDescription.hasBitmap;
406 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craik03188872015-02-02 18:39:33 -0800407}
408
409} /* namespace uirenderer */
410} /* namespace android */