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 | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 81 | GlopBuilder& GlopBuilder::setTransform(const Matrix4& ortho, |
| 82 | const Matrix4& transform, bool fudgingOffset) { |
| 83 | TRIGGER_STAGE(kTransformStage); |
| 84 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 85 | mOutGlop->transform.ortho.load(ortho); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 86 | mOutGlop->transform.canvas.load(transform); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 87 | mOutGlop->transform.fudgingOffset = fudgingOffset; |
| 88 | return *this; |
| 89 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 90 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 91 | GlopBuilder& 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 98 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 99 | GlopBuilder& 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | GlopBuilder& GlopBuilder::setPaint(const SkPaint* paint, float alphaScale) { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 108 | TRIGGER_STAGE(kFillStage); |
| 109 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 110 | // 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 118 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 128 | } |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 129 | } else { |
| 130 | mOutGlop->fill.color = { 1, 0, 0, 0 }; |
| 131 | } |
| 132 | const bool SWAP_SRC_DST = false; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 133 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 134 | mOutGlop->blend = { GL_ZERO, GL_ZERO }; |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 135 | if (mOutGlop->fill.color.a < 1.0f |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 136 | || PaintUtils::isBlendedShader(shader) |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 137 | || 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 148 | if (CC_UNLIKELY(mCaches.extensions().hasFramebufferFetch())) { |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 149 | 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 Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 160 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 165 | |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 166 | 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 Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 203 | return *this; |
| 204 | } |
| 205 | |
| 206 | void GlopBuilder::build() { |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 207 | LOG_ALWAYS_FATAL_IF(mStageFlags != kAllStages, "glop not fully prepared!"); |
| 208 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 209 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 210 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 211 | mOutGlop->transform.canvas.mapRect(mOutGlop->bounds); |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } /* namespace uirenderer */ |
| 215 | } /* namespace android */ |
| 216 | |