blob: e56988c8dc740147dfed3394d35547bd65a92be6 [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 Craik0519c812015-02-11 13:17:06 -080042static void setUnitQuadTextureCoords(Rect uvs, TextureVertex* quadVertex) {;
43 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 Craik0519c812015-02-11 13:17:06 -080052 , mOutGlop(outGlop) {
Chris Craik117bdbc2015-02-05 10:12:38 -080053 mStageFlags = kInitialStage;
54}
55
Chris Craik0519c812015-02-11 13:17:06 -080056////////////////////////////////////////////////////////////////////////////////
57// Mesh
58////////////////////////////////////////////////////////////////////////////////
59
60GlopBuilder& GlopBuilder::setMeshUnitQuad() {
61 TRIGGER_STAGE(kMeshStage);
62
63 mOutGlop->mesh.vertexFlags = kNone_Attrib;
64 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
65 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
66 mOutGlop->mesh.vertices = nullptr;
67 mOutGlop->mesh.indexBufferObject = 0;
68 mOutGlop->mesh.indices = nullptr;
69 mOutGlop->mesh.elementCount = 4;
70 mOutGlop->mesh.stride = kTextureVertexStride;
71 mOutGlop->mesh.texCoordOffset = nullptr;
72 return *this;
73}
74
75GlopBuilder& GlopBuilder::setMeshTexturedUnitQuad(const UvMapper* uvMapper,
76 bool isAlphaMaskTexture) {
77 TRIGGER_STAGE(kMeshStage);
78
79 mOutGlop->mesh.vertexFlags = kTextureCoord_Attrib;
80 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
81
82 if (CC_UNLIKELY(uvMapper)) {
83 Rect uvs(0, 0, 1, 1);
84 uvMapper->map(uvs);
85 setUnitQuadTextureCoords(uvs, &mOutGlop->mesh.mappedVertices[0]);
86
87 mOutGlop->mesh.vertexBufferObject = 0;
88 mOutGlop->mesh.vertices = &mOutGlop->mesh.mappedVertices[0];
89 } else {
90 // standard UV coordinates, use regular unit quad VBO
91 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
92 mOutGlop->mesh.vertices = nullptr;
93 }
94 mOutGlop->mesh.indexBufferObject = 0;
95 mOutGlop->mesh.indices = nullptr;
96 mOutGlop->mesh.elementCount = 4;
97 mOutGlop->mesh.stride = kTextureVertexStride;
98 mOutGlop->mesh.texCoordOffset = (GLvoid*) kMeshTextureOffset;
99
100 mDescription.hasTexture = true;
101 mDescription.hasAlpha8Texture = isAlphaMaskTexture;
102 return *this;
103}
104
105GlopBuilder& GlopBuilder::setMeshIndexedQuads(void* vertexData, int quadCount) {
106 TRIGGER_STAGE(kMeshStage);
107
108 mOutGlop->mesh.vertexFlags = kNone_Attrib;
109 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
110 mOutGlop->mesh.vertexBufferObject = 0;
111 mOutGlop->mesh.vertices = vertexData;
112 mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO();
113 mOutGlop->mesh.indices = nullptr;
114 mOutGlop->mesh.elementCount = 6 * quadCount;
115 mOutGlop->mesh.stride = kVertexStride;
116 mOutGlop->mesh.texCoordOffset = nullptr;
117
118 return *this;
119}
120
Chris Craik117bdbc2015-02-05 10:12:38 -0800121GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
122 TRIGGER_STAGE(kMeshStage);
123
124 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
125
126 bool alphaVertex = flags & VertexBuffer::kAlpha;
127 bool indices = flags & VertexBuffer::kIndices;
128 mOutGlop->mesh.vertexFlags = alphaVertex ? kAlpha_Attrib : kNone_Attrib;
129 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
130 mOutGlop->mesh.vertexBufferObject = 0;
131 mOutGlop->mesh.vertices = vertexBuffer.getBuffer();
132 mOutGlop->mesh.indexBufferObject = 0;
133 mOutGlop->mesh.indices = vertexBuffer.getIndices();
Chris Craik0519c812015-02-11 13:17:06 -0800134 mOutGlop->mesh.elementCount = indices
Chris Craik117bdbc2015-02-05 10:12:38 -0800135 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
136 mOutGlop->mesh.stride = alphaVertex ? kAlphaVertexStride : kVertexStride;
137
138 mDescription.hasVertexAlpha = alphaVertex;
139 mDescription.useShadowAlphaInterp = shadowInterp;
140 return *this;
Chris Craik03188872015-02-02 18:39:33 -0800141}
142
Chris Craik0519c812015-02-11 13:17:06 -0800143////////////////////////////////////////////////////////////////////////////////
144// Fill
145////////////////////////////////////////////////////////////////////////////////
Chris Craik117bdbc2015-02-05 10:12:38 -0800146
Chris Craik0519c812015-02-11 13:17:06 -0800147void GlopBuilder::setFill(int color, float alphaScale, SkXfermode::Mode mode,
148 const SkShader* shader, const SkColorFilter* colorFilter) {
Chris Craik03188872015-02-02 18:39:33 -0800149 if (mode != SkXfermode::kClear_Mode) {
Chris Craik03188872015-02-02 18:39:33 -0800150 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800151 if (!shader) {
152 float colorScale = alpha / 255.0f;
153 mOutGlop->fill.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800154 colorScale * SkColorGetR(color),
155 colorScale * SkColorGetG(color),
Chris Craik0519c812015-02-11 13:17:06 -0800156 colorScale * SkColorGetB(color),
157 alpha
Chris Craik117bdbc2015-02-05 10:12:38 -0800158 };
159 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800160 mOutGlop->fill.color = { 1, 1, 1, alpha };
Chris Craik03188872015-02-02 18:39:33 -0800161 }
Chris Craik03188872015-02-02 18:39:33 -0800162 } else {
Chris Craik0519c812015-02-11 13:17:06 -0800163 mOutGlop->fill.color = { 0, 0, 0, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800164 }
165 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800166
Chris Craik117bdbc2015-02-05 10:12:38 -0800167 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800168 if (mOutGlop->fill.color.a < 1.0f
Chris Craik08fa43f2015-02-09 18:58:32 -0800169 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
Chris Craik0519c812015-02-11 13:17:06 -0800170 || (mOutGlop->fill.texture && mOutGlop->fill.texture->blend)
171 || mOutGlop->roundRectClipState
Chris Craik117bdbc2015-02-05 10:12:38 -0800172 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800173 || PaintUtils::isBlendedColorFilter(colorFilter)
174 || mode != SkXfermode::kSrcOver_Mode) {
175 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
176 Blend::getFactors(mode, SWAP_SRC_DST,
177 &mOutGlop->blend.src, &mOutGlop->blend.dst);
178 } else {
179 // These blend modes are not supported by OpenGL directly and have
180 // to be implemented using shaders. Since the shader will perform
181 // the blending, don't enable GL blending off here
182 // If the blend mode cannot be implemented using shaders, fall
183 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800184 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800185 mDescription.framebufferMode = mode;
186 mDescription.swapSrcDst = SWAP_SRC_DST;
187 // blending in shader, don't enable
188 } else {
189 // unsupported
190 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
191 &mOutGlop->blend.src, &mOutGlop->blend.dst);
192 }
193 }
194 }
195
Chris Craik117bdbc2015-02-05 10:12:38 -0800196 if (shader) {
197 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
198 // TODO: store shader data
199 LOG_ALWAYS_FATAL("shaders not yet supported");
200 }
Chris Craik03188872015-02-02 18:39:33 -0800201
Chris Craik117bdbc2015-02-05 10:12:38 -0800202 if (colorFilter) {
203 SkColor color;
204 SkXfermode::Mode mode;
205 SkScalar srcColorMatrix[20];
206 if (colorFilter->asColorMode(&color, &mode)) {
207 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
208 mDescription.colorMode = mode;
209
210 const float alpha = SkColorGetA(color) / 255.0f;
211 float colorScale = alpha / 255.0f;
212 mOutGlop->fill.filter.color = {
Chris Craik117bdbc2015-02-05 10:12:38 -0800213 colorScale * SkColorGetR(color),
214 colorScale * SkColorGetG(color),
215 colorScale * SkColorGetB(color),
Chris Craik0519c812015-02-11 13:17:06 -0800216 alpha,
Chris Craik117bdbc2015-02-05 10:12:38 -0800217 };
218 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
219 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
220
221 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
222 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
223 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
224 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
225 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
226
227 // Skia uses the range [0..255] for the addition vector, but we need
228 // the [0..1] range to apply the vector in GLSL
229 float* colorVector = mOutGlop->fill.filter.matrix.vector;
230 colorVector[0] = srcColorMatrix[4] / 255.0f;
231 colorVector[1] = srcColorMatrix[9] / 255.0f;
232 colorVector[2] = srcColorMatrix[14] / 255.0f;
233 colorVector[3] = srcColorMatrix[19] / 255.0f;
Chris Craik2ab95d72015-02-06 15:25:51 -0800234 } else {
235 LOG_ALWAYS_FATAL("unsupported ColorFilter");
Chris Craik117bdbc2015-02-05 10:12:38 -0800236 }
237 } else {
238 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
239 }
Chris Craik0519c812015-02-11 13:17:06 -0800240}
Chris Craik117bdbc2015-02-05 10:12:38 -0800241
Chris Craik0519c812015-02-11 13:17:06 -0800242GlopBuilder& GlopBuilder::setFillTexturePaint(Texture& texture, bool isAlphaMaskTexture,
243 const SkPaint* paint, float alphaScale) {
244 TRIGGER_STAGE(kFillStage);
245 REQUIRE_STAGES(kMeshStage);
246
247 mOutGlop->fill.texture = &texture;
248 mOutGlop->fill.textureFilter = PaintUtils::getFilter(paint);
249
250 if (paint) {
251 int color = paint->getColor();
252 SkShader* shader = paint->getShader();
253
254 if (!isAlphaMaskTexture) {
255 // Texture defines color, so disable shaders, and reset all non-alpha color channels
256 color |= 0x00FFFFFF;
257 shader = nullptr;
258 }
259 setFill(color, alphaScale, PaintUtils::getXfermode(paint->getXfermode()),
260 shader, paint->getColorFilter());
261 } else {
262 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
263
264 const bool SWAP_SRC_DST = false;
265 if (alphaScale < 1.0f
266 || (mOutGlop->mesh.vertexFlags & kAlpha_Attrib)
267 || texture.blend
268 || mOutGlop->roundRectClipState) {
269 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
270 &mOutGlop->blend.src, &mOutGlop->blend.dst);
271 } else {
272 mOutGlop->blend = { GL_ZERO, GL_ZERO };
273 }
274 }
275
276 if (isAlphaMaskTexture) {
277 mDescription.modulate = mOutGlop->fill.color.a < 1.0f
278 || mOutGlop->fill.color.r > 0.0f
279 || mOutGlop->fill.color.g > 0.0f
280 || mOutGlop->fill.color.b > 0.0f;
281 } else {
282 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
283 }
Chris Craik03188872015-02-02 18:39:33 -0800284 return *this;
285}
286
Chris Craik0519c812015-02-11 13:17:06 -0800287GlopBuilder& GlopBuilder::setFillPaint(const SkPaint& paint, float alphaScale) {
288 TRIGGER_STAGE(kFillStage);
289 REQUIRE_STAGES(kMeshStage);
290
291 mOutGlop->fill.texture = nullptr;
292 mOutGlop->fill.textureFilter = GL_NEAREST;
293
294 setFill(paint.getColor(), alphaScale, PaintUtils::getXfermode(paint.getXfermode()),
295 paint.getShader(), paint.getColorFilter());
296 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
297 return *this;
298}
299
300////////////////////////////////////////////////////////////////////////////////
301// Transform
302////////////////////////////////////////////////////////////////////////////////
303
304GlopBuilder& GlopBuilder::setTransformClip(const Matrix4& ortho,
305 const Matrix4& transform, bool fudgingOffset) {
306 TRIGGER_STAGE(kTransformStage);
307
308 mOutGlop->transform.ortho.load(ortho);
309 mOutGlop->transform.canvas.load(transform);
310 mOutGlop->transform.fudgingOffset = fudgingOffset;
311 return *this;
312}
313
314
315////////////////////////////////////////////////////////////////////////////////
316// ModelView
317////////////////////////////////////////////////////////////////////////////////
318
319GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
320 TRIGGER_STAGE(kModelViewStage);
321
322 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
323 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
324 mOutGlop->bounds = destination;
325 return *this;
326}
327
328GlopBuilder& GlopBuilder::setModelViewMapUnitToRectSnap(const Rect destination) {
329 TRIGGER_STAGE(kModelViewStage);
330 REQUIRE_STAGES(kTransformStage | kFillStage);
331
332 float left = destination.left;
333 float top = destination.top;
334
335 const Matrix4& canvasTransform = mOutGlop->transform.canvas;
336 if (CC_LIKELY(canvasTransform.isPureTranslate())) {
337 const float translateX = canvasTransform.getTranslateX();
338 const float translateY = canvasTransform.getTranslateY();
339
340 left = (int) floorf(left + translateX + 0.5f) - translateX;
341 top = (int) floorf(top + translateY + 0.5f) - translateY;
342 mOutGlop->fill.textureFilter = GL_NEAREST;
343 }
344
345 mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f);
346 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
347 mOutGlop->bounds = destination;
348 return *this;
349}
350
351GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
352 TRIGGER_STAGE(kModelViewStage);
353
354 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
355 mOutGlop->bounds = source;
356 mOutGlop->bounds.translate(offsetX, offsetY);
357 return *this;
358}
359
360GlopBuilder& GlopBuilder::setRoundRectClipState(const RoundRectClipState* roundRectClipState) {
361 TRIGGER_STAGE(kRoundRectClipStage);
362
363 mOutGlop->roundRectClipState = roundRectClipState;
364 mDescription.hasRoundRectClip = roundRectClipState != nullptr;
365 return *this;
366}
367
368////////////////////////////////////////////////////////////////////////////////
369// Build
370////////////////////////////////////////////////////////////////////////////////
371
Chris Craik03188872015-02-02 18:39:33 -0800372void GlopBuilder::build() {
Chris Craik08fa43f2015-02-09 18:58:32 -0800373 REQUIRE_STAGES(kAllStages);
Chris Craik117bdbc2015-02-05 10:12:38 -0800374
Chris Craik03188872015-02-02 18:39:33 -0800375 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik117bdbc2015-02-05 10:12:38 -0800376 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik0519c812015-02-11 13:17:06 -0800377
378 // duplicates ProgramCache's definition of color uniform presence
379 const bool singleColor = !mDescription.hasTexture
380 && !mDescription.hasExternalTexture
381 && !mDescription.hasGradient
382 && !mDescription.hasBitmap;
383 mOutGlop->fill.colorEnabled = mDescription.modulate || singleColor;
Chris Craik03188872015-02-02 18:39:33 -0800384}
385
386} /* namespace uirenderer */
387} /* namespace android */