Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 1 | /* |
| 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 21 | #include "renderstate/MeshState.h" |
| 22 | #include "renderstate/RenderState.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 23 | #include "SkiaShader.h" |
| 24 | #include "Texture.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 25 | #include "utils/PaintUtils.h" |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 26 | #include "VertexBuffer.h" |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 27 | |
| 28 | #include <GLES2/gl2.h> |
| 29 | #include <SkPaint.h> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 34 | #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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 38 | GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop) |
| 39 | : mRenderState(renderState) |
| 40 | , mCaches(caches) |
| 41 | , mOutGlop(outGlop){ |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 42 | mStageFlags = kInitialStage; |
| 43 | } |
| 44 | |
| 45 | GlopBuilder& 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | GlopBuilder& GlopBuilder::setMeshUnitQuad() { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 68 | TRIGGER_STAGE(kMeshStage); |
| 69 | |
| 70 | mOutGlop->mesh.vertexFlags = kNone_Attrib; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 71 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 72 | mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO(); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 73 | mOutGlop->mesh.vertices = nullptr; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 74 | mOutGlop->mesh.indexBufferObject = 0; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 75 | mOutGlop->mesh.indices = nullptr; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 76 | mOutGlop->mesh.vertexCount = 4; |
| 77 | mOutGlop->mesh.stride = kTextureVertexStride; |
| 78 | return *this; |
| 79 | } |
| 80 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 81 | GlopBuilder& 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 96 | GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho, |
| 97 | const Matrix4& transform, bool fudgingOffset) { |
| 98 | TRIGGER_STAGE(kTransformStage); |
| 99 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 100 | mOutGlop->transform.ortho.load(ortho); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 101 | mOutGlop->transform.canvas.load(transform); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 102 | mOutGlop->transform.fudgingOffset = fudgingOffset; |
| 103 | return *this; |
| 104 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 105 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 106 | GlopBuilder& GlopBuilder::setModelViewMapUnitToRect(const Rect destination) { |
| 107 | TRIGGER_STAGE(kModelViewStage); |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 108 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 109 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 114 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 115 | GlopBuilder& GlopBuilder::setModelViewOffsetRect(float offsetX, float offsetY, const Rect source) { |
| 116 | TRIGGER_STAGE(kModelViewStage); |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 117 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 118 | mOutGlop->transform.modelView.loadTranslate(offsetX, offsetY, 0.0f); |
| 119 | mOutGlop->bounds = source; |
| 120 | mOutGlop->bounds.translate(offsetX, offsetY); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 121 | return *this; |
| 122 | } |
| 123 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 124 | GlopBuilder& GlopBuilder::setOptionalPaint(const SkPaint* paint, float alphaScale) { |
| 125 | if (paint) { |
| 126 | return setPaint(*paint, alphaScale); |
| 127 | } |
| 128 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 129 | TRIGGER_STAGE(kFillStage); |
| 130 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 131 | mOutGlop->fill.color = { alphaScale, alphaScale, alphaScale, alphaScale }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 132 | |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 133 | 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 | } |
| 144 | GlopBuilder& 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 151 | if (mode != SkXfermode::kClear_Mode) { |
Chris Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 152 | int color = paint.getColor(); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 153 | float alpha = (SkColorGetA(color) / 255.0f) * alphaScale; |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 154 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 164 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 165 | } else { |
| 166 | mOutGlop->fill.color = { 1, 0, 0, 0 }; |
| 167 | } |
| 168 | const bool SWAP_SRC_DST = false; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 169 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 170 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 171 | if (mOutGlop->fill.color.a < 1.0f |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 172 | || PaintUtils::isBlendedShader(shader) |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 173 | || 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 184 | if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 185 | 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 196 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 201 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 202 | 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 Craik | 2ab95d7 | 2015-02-06 15:25:51 -0800 | [diff] [blame^] | 234 | } else { |
| 235 | LOG_ALWAYS_FATAL("unsupported ColorFilter"); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 236 | } |
| 237 | } else { |
| 238 | mOutGlop->fill.filterMode = ProgramDescription::kColorNone; |
| 239 | } |
| 240 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | void GlopBuilder::build() { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 245 | LOG_ALWAYS_FATAL_IF(mStageFlags != kAllStages, "glop not fully prepared!"); |
| 246 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 247 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 248 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 249 | mOutGlop->transform.canvas.mapRect(mOutGlop->bounds); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | } /* namespace uirenderer */ |
| 253 | } /* namespace android */ |
| 254 | |