blob: e22af40a3c4ca9f87582cda92d20db7792456c49 [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 Craik117bdbc2015-02-05 10:12:38 -080081GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho,
82 const Matrix4& transform, bool fudgingOffset) {
83 TRIGGER_STAGE(kTransformStage);
84
Chris Craik03188872015-02-02 18:39:33 -080085 mOutGlop->transform.ortho.load(ortho);
Chris Craik03188872015-02-02 18:39:33 -080086 mOutGlop->transform.canvas.load(transform);
Chris Craik117bdbc2015-02-05 10:12:38 -080087 mOutGlop->transform.fudgingOffset = fudgingOffset;
88 return *this;
89}
Chris Craik03188872015-02-02 18:39:33 -080090
Chris Craik117bdbc2015-02-05 10:12:38 -080091GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) {
92 TRIGGER_STAGE(kModelViewStage);
93 mOutGlop->transform.modelView.loadTranslate(destination.left, destination.top, 0.0f);
94 mOutGlop->transform.modelView.scale(destination.getWidth(), destination.getHeight(), 1.0f);
95 mOutGlop->bounds = destination;
96 return *this;
97}
Chris Craik03188872015-02-02 18:39:33 -080098
Chris Craik117bdbc2015-02-05 10:12:38 -080099GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) {
100 TRIGGER_STAGE(kModelViewStage);
101 mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f);
102 mOutGlop->bounds = source;
103 mOutGlop->bounds.translate(offsetX, offsetY);
Chris Craik03188872015-02-02 18:39:33 -0800104 return *this;
105}
106
107GlopBuilder& GlopBuilder::setPaint(const SkPaint* paint, float alphaScale) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800108 TRIGGER_STAGE(kFillStage);
109
Chris Craik03188872015-02-02 18:39:33 -0800110 // TODO: support null paint
111 const SkShader* shader = paint->getShader();
112 const SkColorFilter* colorFilter = paint->getColorFilter();
113
114 SkXfermode::Mode mode = PaintUtils::getXfermode(paint->getXfermode());
115 if (mode != SkXfermode::kClear_Mode) {
116 int color = paint->getColor();
117 float alpha = (SkColorGetA(color) / 255.0f) * alphaScale;
Chris Craik117bdbc2015-02-05 10:12:38 -0800118 if (!shader) {
119 float colorScale = alpha / 255.0f;
120 mOutGlop->fill.color = {
121 alpha,
122 colorScale * SkColorGetR(color),
123 colorScale * SkColorGetG(color),
124 colorScale * SkColorGetB(color)
125 };
126 } else {
127 mOutGlop->fill.color = { alpha, 1, 1, 1 };
Chris Craik03188872015-02-02 18:39:33 -0800128 }
Chris Craik03188872015-02-02 18:39:33 -0800129 } else {
130 mOutGlop->fill.color = { 1, 0, 0, 0 };
131 }
132 const bool SWAP_SRC_DST = false;
Chris Craik03188872015-02-02 18:39:33 -0800133
Chris Craik117bdbc2015-02-05 10:12:38 -0800134 mOutGlop->blend = { GL_ZERO, GL_ZERO };
Chris Craik03188872015-02-02 18:39:33 -0800135 if (mOutGlop->fill.color.a < 1.0f
Chris Craik117bdbc2015-02-05 10:12:38 -0800136 || PaintUtils::isBlendedShader(shader)
Chris Craik03188872015-02-02 18:39:33 -0800137 || PaintUtils::isBlendedColorFilter(colorFilter)
138 || mode != SkXfermode::kSrcOver_Mode) {
139 if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) {
140 Blend::getFactors(mode, SWAP_SRC_DST,
141 &mOutGlop->blend.src, &mOutGlop->blend.dst);
142 } else {
143 // These blend modes are not supported by OpenGL directly and have
144 // to be implemented using shaders. Since the shader will perform
145 // the blending, don't enable GL blending off here
146 // If the blend mode cannot be implemented using shaders, fall
147 // back to the default SrcOver blend mode instead
Chris Craik117bdbc2015-02-05 10:12:38 -0800148 if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) {
Chris Craik03188872015-02-02 18:39:33 -0800149 mDescription.framebufferMode = mode;
150 mDescription.swapSrcDst = SWAP_SRC_DST;
151 // blending in shader, don't enable
152 } else {
153 // unsupported
154 Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST,
155 &mOutGlop->blend.src, &mOutGlop->blend.dst);
156 }
157 }
158 }
159
Chris Craik117bdbc2015-02-05 10:12:38 -0800160 if (shader) {
161 SkiaShader::describe(&mCaches, mDescription, mCaches.extensions(), *shader);
162 // TODO: store shader data
163 LOG_ALWAYS_FATAL("shaders not yet supported");
164 }
Chris Craik03188872015-02-02 18:39:33 -0800165
Chris Craik117bdbc2015-02-05 10:12:38 -0800166 if (colorFilter) {
167 SkColor color;
168 SkXfermode::Mode mode;
169 SkScalar srcColorMatrix[20];
170 if (colorFilter->asColorMode(&color, &mode)) {
171 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorBlend;
172 mDescription.colorMode = mode;
173
174 const float alpha = SkColorGetA(color) / 255.0f;
175 float colorScale = alpha / 255.0f;
176 mOutGlop->fill.filter.color = {
177 alpha,
178 colorScale * SkColorGetR(color),
179 colorScale * SkColorGetG(color),
180 colorScale * SkColorGetB(color),
181 };
182 } else if (colorFilter->asColorMatrix(srcColorMatrix)) {
183 mOutGlop->fill.filterMode = mDescription.colorOp = ProgramDescription::kColorMatrix;
184
185 float* colorMatrix = mOutGlop->fill.filter.matrix.matrix;
186 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
187 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
188 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
189 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
190
191 // Skia uses the range [0..255] for the addition vector, but we need
192 // the [0..1] range to apply the vector in GLSL
193 float* colorVector = mOutGlop->fill.filter.matrix.vector;
194 colorVector[0] = srcColorMatrix[4] / 255.0f;
195 colorVector[1] = srcColorMatrix[9] / 255.0f;
196 colorVector[2] = srcColorMatrix[14] / 255.0f;
197 colorVector[3] = srcColorMatrix[19] / 255.0f;
198 }
199 } else {
200 mOutGlop->fill.filterMode = ProgramDescription::kColorNone;
201 }
202
Chris Craik03188872015-02-02 18:39:33 -0800203 return *this;
204}
205
206void GlopBuilder::build() {
Chris Craik117bdbc2015-02-05 10:12:38 -0800207 LOG_ALWAYS_FATAL_IF(mStageFlags != kAllStages, "glop not fully prepared!");
208
Chris Craik03188872015-02-02 18:39:33 -0800209 mDescription.modulate = mOutGlop->fill.color.a < 1.0f;
210 mOutGlop->fill.program = mCaches.programCache.get(mDescription);
Chris Craik117bdbc2015-02-05 10:12:38 -0800211 mOutGlop->transform.canvas.mapRect(mOutGlop->bounds);
Chris Craik03188872015-02-02 18:39:33 -0800212}
213
214} /* namespace uirenderer */
215} /* namespace android */
216