blob: 6f7bb9b335aaa9287180d62be9401750dd94d50d [file] [log] [blame]
Romain Guy06f96e22010-07-30 19:18:16 -07001/*
2 * Copyright (C) 2010 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
Chris Craik922d3a72015-02-13 17:47:21 -080017#include "SkiaShader.h"
Romain Guy06f96e22010-07-30 19:18:16 -070018
Romain Guya1d3c912011-12-13 14:55:06 -080019#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040020#include "Extensions.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040021#include "Matrix.h"
Romain Guy06f96e22010-07-30 19:18:16 -070022#include "Texture.h"
sergeyvec4a4b12016-10-20 18:39:04 -070023#include "hwui/Bitmap.h"
Romain Guy06f96e22010-07-30 19:18:16 -070024
Chris Craik922d3a72015-02-13 17:47:21 -080025#include <SkMatrix.h>
26#include <utils/Log.h>
27
Romain Guy06f96e22010-07-30 19:18:16 -070028namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Support
33///////////////////////////////////////////////////////////////////////////////
34
Chris Craik216048f2015-08-21 15:31:20 -070035static constexpr GLenum gTileModes[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070036 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
37 GL_REPEAT, // == SkShader::kRepeat_Mode
38 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
39};
40
Chris Craik216048f2015-08-21 15:31:20 -070041static_assert(gTileModes[SkShader::kClamp_TileMode] == GL_CLAMP_TO_EDGE,
John Reck1bcacfd2017-11-03 10:12:19 -070042 "SkShader TileModes have changed");
Chris Craik216048f2015-08-21 15:31:20 -070043static_assert(gTileModes[SkShader::kRepeat_TileMode] == GL_REPEAT,
John Reck1bcacfd2017-11-03 10:12:19 -070044 "SkShader TileModes have changed");
Chris Craik216048f2015-08-21 15:31:20 -070045static_assert(gTileModes[SkShader::kMirror_TileMode] == GL_MIRRORED_REPEAT,
John Reck1bcacfd2017-11-03 10:12:19 -070046 "SkShader TileModes have changed");
Chris Craik216048f2015-08-21 15:31:20 -070047
Chris Craik922d3a72015-02-13 17:47:21 -080048static inline void bindUniformColor(int slot, FloatColor color) {
49 glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
50}
51
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040052static inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
sergeyv554ffeb2016-11-15 18:01:21 -080053 caches->textureState().bindTexture(texture->target(), texture->id());
Romain Guyd21b6e12011-11-30 20:21:23 -080054 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070055}
56
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040057/**
58 * Compute the matrix to transform to screen space.
59 * @param screenSpace Output param for the computed matrix.
60 * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
61 * or identity.
62 * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
63 * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
64 */
65static void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -070066 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040067 mat4 shaderMatrix;
68 // uses implicit construction
69 shaderMatrix.loadInverse(localMatrix);
70 // again, uses implicit construction
71 screenSpace.loadMultiply(unitMatrix, shaderMatrix);
72 screenSpace.multiply(modelViewMatrix);
73}
74
Romain Guy06f96e22010-07-30 19:18:16 -070075///////////////////////////////////////////////////////////////////////////////
Chris Craik922d3a72015-02-13 17:47:21 -080076// Store / apply
77///////////////////////////////////////////////////////////////////////////////
78
Romain Guy253f2c22016-09-28 17:34:42 -070079void applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data,
John Reck1bcacfd2017-11-03 10:12:19 -070080 const GLsizei width, const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -080081 if (CC_UNLIKELY(data.gradientTexture)) {
82 caches.textureState().activateTexture(data.gradientSampler);
83 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
84 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
85 } else {
86 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
87 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
88 }
89
Romain Guy253f2c22016-09-28 17:34:42 -070090 glUniform2f(caches.program().getUniform("screenSize"), 1.0f / width, 1.0f / height);
John Reck1bcacfd2017-11-03 10:12:19 -070091 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1, GL_FALSE,
92 &data.screenSpace.data[0]);
Chris Craik922d3a72015-02-13 17:47:21 -080093}
94
95bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -070096 GLuint* textureUnit, ProgramDescription* description,
97 SkiaShaderData::BitmapShaderData* outData) {
Mike Reed8cafcc62018-05-03 11:32:46 -040098 // DEAD CODE
Chris Craik922d3a72015-02-13 17:47:21 -080099 return true;
100}
101
102void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
103 caches.textureState().activateTexture(data.bitmapSampler);
104 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
105 data.bitmapTexture->setFilter(GL_LINEAR);
106
107 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
108 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700109 &data.textureTransform.data[0]);
Chris Craik922d3a72015-02-13 17:47:21 -0800110 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
111}
112
113SkiaShaderType getComposeSubType(const SkShader& shader) {
114 // First check for a gradient shader.
115 switch (shader.asAGradient(nullptr)) {
116 case SkShader::kNone_GradientType:
117 // Not a gradient shader. Fall through to check for other types.
118 break;
119 case SkShader::kLinear_GradientType:
120 case SkShader::kRadial_GradientType:
121 case SkShader::kSweep_GradientType:
122 return kGradient_SkiaShaderType;
123 default:
124 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
125 return kNone_SkiaShaderType;
126 }
127
128 // The shader is not a gradient. Check for a bitmap shader.
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400129 if (shader.isABitmap()) {
Chris Craik922d3a72015-02-13 17:47:21 -0800130 return kBitmap_SkiaShaderType;
131 }
132 return kNone_SkiaShaderType;
133}
134
135void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
John Reck1bcacfd2017-11-03 10:12:19 -0700136 const Matrix4& modelViewMatrix, GLuint* textureUnit,
137 ProgramDescription* description, SkiaShaderData* outData) {
138 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix, textureUnit,
139 description, &outData->bitmapData),
140 "failed storing bitmap shader data");
Chris Craik922d3a72015-02-13 17:47:21 -0800141}
142
143bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700144 GLuint* textureUnit, ProgramDescription* description,
145 SkiaShaderData* outData) {
Chris Craik922d3a72015-02-13 17:47:21 -0800146 SkShader::ComposeRec rec;
147 if (!shader.asACompose(&rec)) return false;
148
149 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
150 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
151
152 // check that type enum values are the 2 flags that compose the kCompose value
153 if ((shaderAType & shaderBType) != 0) return false;
154 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
155
156 mat4 transform;
157 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
158 if (shaderAType == kBitmap_SkiaShaderType) {
159 description->isBitmapFirst = true;
John Reck1bcacfd2017-11-03 10:12:19 -0700160 storeCompose(caches, *rec.fShaderA, *rec.fShaderB, transform, textureUnit, description,
161 outData);
Chris Craik922d3a72015-02-13 17:47:21 -0800162 } else {
163 description->isBitmapFirst = false;
John Reck1bcacfd2017-11-03 10:12:19 -0700164 storeCompose(caches, *rec.fShaderB, *rec.fShaderA, transform, textureUnit, description,
165 outData);
Chris Craik922d3a72015-02-13 17:47:21 -0800166 }
Mike Reedc2f31df2016-10-28 17:21:45 -0400167 description->shadersMode = rec.fBlendMode;
Chris Craik922d3a72015-02-13 17:47:21 -0800168 return true;
169}
170
Chris Craik53e51e42015-06-01 10:35:35 -0700171void SkiaShader::store(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700172 GLuint* textureUnit, ProgramDescription* description,
173 SkiaShaderData* outData) {
Mike Reed8cafcc62018-05-03 11:32:46 -0400174 // DEAD CODE
John Reck1bcacfd2017-11-03 10:12:19 -0700175 if (tryStoreBitmap(caches, shader, modelViewMatrix, textureUnit, description,
176 &outData->bitmapData)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800177 outData->skiaShaderType = kBitmap_SkiaShaderType;
178 return;
179 }
180
John Reck1bcacfd2017-11-03 10:12:19 -0700181 if (tryStoreCompose(caches, shader, modelViewMatrix, textureUnit, description, outData)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800182 outData->skiaShaderType = kCompose_SkiaShaderType;
183 return;
184 }
185
Chris Craike310f832015-07-13 13:34:07 -0700186 // Unknown/unsupported type, so explicitly ignore shader
187 outData->skiaShaderType = kNone_SkiaShaderType;
Chris Craik922d3a72015-02-13 17:47:21 -0800188}
189
John Reck1bcacfd2017-11-03 10:12:19 -0700190void SkiaShader::apply(Caches& caches, const SkiaShaderData& data, const GLsizei width,
191 const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -0800192 if (!data.skiaShaderType) return;
193
194 if (data.skiaShaderType & kGradient_SkiaShaderType) {
Romain Guy253f2c22016-09-28 17:34:42 -0700195 applyGradient(caches, data.gradientData, width, height);
Chris Craik922d3a72015-02-13 17:47:21 -0800196 }
197 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
198 applyBitmap(caches, data.bitmapData);
199 }
Chris Craik922d3a72015-02-13 17:47:21 -0800200}
201
John Reck1bcacfd2017-11-03 10:12:19 -0700202}; // namespace uirenderer
203}; // namespace android