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" |
| 21 | #include "Texture.h" |
| 22 | #include "renderstate/MeshState.h" |
| 23 | #include "renderstate/RenderState.h" |
| 24 | #include "utils/PaintUtils.h" |
| 25 | |
| 26 | #include <GLES2/gl2.h> |
| 27 | #include <SkPaint.h> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
| 32 | GlopBuilder::GlopBuilder(RenderState& renderState, Caches& caches, Glop* outGlop) |
| 33 | : mRenderState(renderState) |
| 34 | , mCaches(caches) |
| 35 | , mOutGlop(outGlop){ |
| 36 | } |
| 37 | |
| 38 | GlopBuilder& GlopBuilder::setMeshUnitQuad() { |
| 39 | mOutGlop->mesh.vertexFlags = static_cast<VertexAttribFlags>(0); |
| 40 | mOutGlop->mesh.primitiveMode = GL_TRIANGLE_STRIP; |
| 41 | mOutGlop->mesh.vertexBufferObject = mRenderState.meshState().getUnitQuadVBO(); |
| 42 | mOutGlop->mesh.indexBufferObject = 0; |
| 43 | mOutGlop->mesh.vertexCount = 4; |
| 44 | mOutGlop->mesh.stride = kTextureVertexStride; |
| 45 | return *this; |
| 46 | } |
| 47 | |
| 48 | GlopBuilder& GlopBuilder::setTransformAndRect(ModelViewMode mode, |
| 49 | const Matrix4& ortho, const Matrix4& transform, |
| 50 | float left, float top, float right, float bottom, bool offset) { |
| 51 | mOutGlop->transform.ortho.load(ortho); |
| 52 | |
| 53 | mOutGlop->transform.modelView.loadTranslate(left, top, 0.0f); |
| 54 | if (mode == kModelViewMode_TranslateAndScale) { |
| 55 | mOutGlop->transform.modelView.scale(right - left, bottom - top, 1.0f); |
| 56 | } |
| 57 | |
| 58 | mOutGlop->transform.canvas.load(transform); |
| 59 | |
| 60 | mOutGlop->transform.offset = offset; |
| 61 | |
| 62 | mOutGlop->bounds.set(left, top, right, bottom); |
| 63 | mOutGlop->transform.canvas.mapRect(mOutGlop->bounds); |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | GlopBuilder& GlopBuilder::setPaint(const SkPaint* paint, float alphaScale) { |
| 68 | // TODO: support null paint |
| 69 | const SkShader* shader = paint->getShader(); |
| 70 | const SkColorFilter* colorFilter = paint->getColorFilter(); |
| 71 | |
| 72 | SkXfermode::Mode mode = PaintUtils::getXfermode(paint->getXfermode()); |
| 73 | if (mode != SkXfermode::kClear_Mode) { |
| 74 | int color = paint->getColor(); |
| 75 | float alpha = (SkColorGetA(color) / 255.0f) * alphaScale; |
| 76 | if (shader) { |
| 77 | // shader discards color channels |
| 78 | color |= 0x00FFFFFF; |
| 79 | } |
| 80 | mOutGlop->fill.color = { |
| 81 | alpha, |
| 82 | alpha * SkColorGetR(color), |
| 83 | alpha * SkColorGetG(color), |
| 84 | alpha * SkColorGetB(color) |
| 85 | }; |
| 86 | } else { |
| 87 | mOutGlop->fill.color = { 1, 0, 0, 0 }; |
| 88 | } |
| 89 | const bool SWAP_SRC_DST = false; |
| 90 | const bool HAS_FRAMEBUFFER_FETCH = false; //mExtensions.hasFramebufferFetch(); |
| 91 | |
| 92 | mOutGlop->blend = {GL_ZERO, GL_ZERO}; |
| 93 | if (mOutGlop->fill.color.a < 1.0f |
| 94 | || (shader && !shader->isOpaque()) |
| 95 | || PaintUtils::isBlendedColorFilter(colorFilter) |
| 96 | || mode != SkXfermode::kSrcOver_Mode) { |
| 97 | if (CC_LIKELY(mode <= SkXfermode::kScreen_Mode)) { |
| 98 | Blend::getFactors(mode, SWAP_SRC_DST, |
| 99 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 100 | } else { |
| 101 | // These blend modes are not supported by OpenGL directly and have |
| 102 | // to be implemented using shaders. Since the shader will perform |
| 103 | // the blending, don't enable GL blending off here |
| 104 | // If the blend mode cannot be implemented using shaders, fall |
| 105 | // back to the default SrcOver blend mode instead |
| 106 | if (CC_UNLIKELY(HAS_FRAMEBUFFER_FETCH)) { |
| 107 | mDescription.framebufferMode = mode; |
| 108 | mDescription.swapSrcDst = SWAP_SRC_DST; |
| 109 | // blending in shader, don't enable |
| 110 | } else { |
| 111 | // unsupported |
| 112 | Blend::getFactors(SkXfermode::kSrcOver_Mode, SWAP_SRC_DST, |
| 113 | &mOutGlop->blend.src, &mOutGlop->blend.dst); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return *this; |
| 119 | } |
| 120 | |
| 121 | GlopBuilder& GlopBuilder::setTexture(Texture* texture) { |
| 122 | LOG_ALWAYS_FATAL("not yet supported"); |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | void GlopBuilder::build() { |
| 127 | mDescription.modulate = mOutGlop->fill.color.a < 1.0f; |
| 128 | mOutGlop->fill.program = mCaches.programCache.get(mDescription); |
| 129 | } |
| 130 | |
| 131 | } /* namespace uirenderer */ |
| 132 | } /* namespace android */ |
| 133 | |