blob: e0f2ec436274ba05bd092647dd619b9579d8b644 [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"); \
36 mStageFlags = static_cast<StageFlags>(mStageFlags | stageFlag)
37
Chris Craik03188872015-02-02 18:39:33 -080038GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop)
39 : mRenderState(renderState)
40 , mCaches(caches)
41 , mOutGlop(outGlop){
Chris Craik117bdbc2015-02-05 10:12:38 -080042 mStageFlags = kInitialStage;
43}
44
45GlopBuilder& GlopBuilder::setMeshVertexBuffer(const VertexBuffer& vertexBuffer, bool shadowInterp) {
46 TRIGGER_STAGE(kMeshStage);
47
48 const VertexBuffer::MeshFeatureFlags flags = vertexBuffer.getMeshFeatureFlags();
49
50 bool alphaVertex = flags & VertexBuffer::kAlpha;
51 bool indices = flags & VertexBuffer::kIndices;
52 mOutGlop->mesh.vertexFlags = alphaVertex ? kAlpha_Attrib : kNone_Attrib;
53 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
54 mOutGlop->mesh.vertexBufferObject = 0;
55 mOutGlop->mesh.vertices = vertexBuffer.getBuffer();
56 mOutGlop->mesh.indexBufferObject = 0;
57 mOutGlop->mesh.indices = vertexBuffer.getIndices();
58 mOutGlop->mesh.vertexCount = indices
59 ? vertexBuffer.getIndexCount() : vertexBuffer.getVertexCount();
60 mOutGlop->mesh.stride = alphaVertex ? kAlphaVertexStride : kVertexStride;
61
62 mDescription.hasVertexAlpha = alphaVertex;
63 mDescription.useShadowAlphaInterp = shadowInterp;
64 return *this;
Chris Craik03188872015-02-02 18:39:33 -080065}
66
67GlopBuilder& GlopBuilder::setMeshUnitQuad() {
Chris Craik117bdbc2015-02-05 10:12:38 -080068 TRIGGER_STAGE(kMeshStage);
69
70 mOutGlop->mesh.vertexFlags = kNone_Attrib;
Chris Craik03188872015-02-02 18:39:33 -080071 mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP;
72 mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO();
Chris Craik117bdbc2015-02-05 10:12:38 -080073 mOutGlop->mesh.vertices = nullptr;
Chris Craik03188872015-02-02 18:39:33 -080074 mOutGlop->mesh.indexBufferObject = 0;
Chris Craik117bdbc2015-02-05 10:12:38 -080075 mOutGlop->mesh.indices = nullptr;
Chris Craik03188872015-02-02 18:39:33 -080076 mOutGlop->mesh.vertexCount = 4;
77 mOutGlop->mesh.stride = kTextureVertexStride;
78 return *this;
79}
80
Chris Craik2ab95d72015-02-06 15:25:51 -080081GlopBuilder& GlopBuilder::setMeshIndexedQuads(void* vertexData, int quadCount) {
82 TRIGGER_STAGE(kMeshStage);
83
84 mOutGlop->mesh.vertexFlags = kNone_Attrib;
85 mOutGlop->mesh.primitiveMode = GL_TRIANGLES;
86 mOutGlop->mesh.vertexBufferObject = 0;
87 mOutGlop->mesh.vertices = vertexData;
88 mOutGlop->mesh.indexBufferObject = mRenderState.meshState().getQuadListIBO();
89 mOutGlop->mesh.indices = nullptr;
90 mOutGlop->mesh.vertexCount = 6 * quadCount;
91 mOutGlop->mesh.stride = kVertexStride;
92
93 return *this;
94}
95
Chris Craik117bdbc2015-02-05 10:12:38 -080096GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
97 const Matrix4& transform, bool fudgingOffset) {
98 TRIGGER_STAGE(kTransformStage);
99
Chris Craik03188872015-02-02 18:39:33 -0800100 mOutGlop->transform.ortho.load(ortho);
Chris Craik03188872015-02-02 18:39:33 -0800101 mOutGlop->transform.canvas.load(transform);
Chris Craik117bdbc2015-02-05 10:12:38 -0800102 mOutGlop->transform.fudgingOffset = fudgingOffset;
103 return *this;
104}
Chris Craik03188872015-02-02 18:39:33 -0800105
Chris Craik117bdbc2015-02-05 10:12:38 -0800106GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
107 TRIGGER_STAGE(kModelViewStage);
Chris Craik2ab95d72015-02-06 15:25:51 -0800108
Chris Craik117bdbc2015-02-05 10:12:38 -0800109 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
110 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
111 mOutGlop->bounds = destination;
112 return *this;
113}
Chris Craik03188872015-02-02 18:39:33 -0800114
Chris Craik117bdbc2015-02-05 10:12:38 -0800115GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
116 TRIGGER_STAGE(kModelViewStage);
Chris Craik2ab95d72015-02-06 15:25:51 -0800117
Chris Craik117bdbc2015-02-05 10:12:38 -0800118 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
119 mOutGlop->bounds = source;
120 mOutGlop->bounds.translate(offsetX, offsetY);
Chris Craik03188872015-02-02 18:39:33 -0800121 return *this;
122}
123
Chris Craik2ab95d72015-02-06 15:25:51 -0800124GlopBuilder& GlopBuilder::setOptionalPaint(const SkPaint* paint, float alphaScale) {
125 if (paint) {
126 return setPaint(*paint, alphaScale);
127 }
128
Chris Craik117bdbc2015-02-05 10:12:38 -0800129 TRIGGER_STAGE(kFillStage);
130
Chris Craik2ab95d72015-02-06 15:25:51 -0800131 mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale };
Chris Craik03188872015-02-02 18:39:33 -0800132
Chris Craik2ab95d72015-02-06 15:25:51 -0800133 const bool SWAP_SRC_DST = false;
134 // TODO: account for texture blend
135 if (alphaScale < 1.0f) {
136 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
137 &mOutGlop->blend.src, &mOutGlop->blend.dst);
138 } else {
139 mOutGlop->blend = { GL_ZERO, GL_ZERO };
140 }
141
142 return *this;
143}
144GlopBuilder& GlopBuilder::setPaint(const SkPaint& paint, float alphaScale) {
145 TRIGGER_STAGE(kFillStage);
146
147 const SkShader* shader = paint.getShader();
148 const SkColorFilter* colorFilter = paint.getColorFilter();
149
150 SkXfermode::Mode mode = PaintUtils::getXfermode(paint.getXfermode());
Chris Craik03188872015-02-02 18:39:33 -0800151 if (mode != SkXfermode::kClear_Mode) {
Chris Craik2ab95d72015-02-06 15:25:51 -0800152 int color = paint.getColor();
Chris Craik03188872015-02-02 18:39:33 -0800153 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800154 if (!shader) {
155 float colorScale = alpha / 255.0f;
156 mOutGlop->fill.color = {
157 alpha,
158 colorScale * SkColorGetR(color),
159 colorScale * SkColorGetG(color),
160 colorScale * SkColorGetB(color)
161 };
162 } else {
163 mOutGlop->fill.color = { alpha, 1, 1, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800164 }
Chris Craik03188872015-02-02 18:39:33 -0800165 } else {
166 mOutGlop->fill.color = { 1, 0, 0, 0 };
167 }
168 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800169
Chris Craik117bdbc2015-02-05 10:12:38 -0800170 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800171 if (mOutGlop->fill.color.a < 1.0f
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 = {
213 alpha,
214 colorScale * SkColorGetR(color),
215 colorScale * SkColorGetG(color),
216 colorScale * SkColorGetB(color),
217 };
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 }
240
Chris Craik03188872015-02-02 18:39:33 -0800241 return *this;
242}
243
244void GlopBuilder::build() {
Chris Craik117bdbc2015-02-05 10:12:38 -0800245 LOG_ALWAYS_FATAL_IF(mStageFlags != kAllStages, "glop not fully prepared!");
246
Chris Craik03188872015-02-02 18:39:33 -0800247 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
248 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik117bdbc2015-02-05 10:12:38 -0800249 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800250}
251
252} /* namespace uirenderer */
253} /* namespace android */
254