blob: 9838df2f6013ea58c1d6a2400f2bb63587ac082d [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 "Layer.h"
22#include "Matrix.h"
Romain Guy06f96e22010-07-30 19:18:16 -070023#include "Texture.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,
42 "SkShader TileModes have changed");
43static_assert(gTileModes[SkShader::kRepeat_TileMode] == GL_REPEAT,
44 "SkShader TileModes have changed");
45static_assert(gTileModes[SkShader::kMirror_TileMode] == GL_MIRRORED_REPEAT,
46 "SkShader TileModes have changed");
47
Romain Guy42e1e0d2012-07-30 14:47:51 -070048/**
49 * This function does not work for n == 0.
50 */
51static inline bool isPowerOfTwo(unsigned int n) {
52 return !(n & (n - 1));
53}
54
Chris Craik922d3a72015-02-13 17:47:21 -080055static inline void bindUniformColor(int slot, FloatColor color) {
56 glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
57}
58
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040059static inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
John Reck38e0c322015-11-10 12:19:17 -080060 caches->textureState().bindTexture(texture->id());
Romain Guyd21b6e12011-11-30 20:21:23 -080061 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070062}
63
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040064/**
65 * Compute the matrix to transform to screen space.
66 * @param screenSpace Output param for the computed matrix.
67 * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
68 * or identity.
69 * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
70 * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
71 */
72static void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
73 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
74 mat4 shaderMatrix;
75 // uses implicit construction
76 shaderMatrix.loadInverse(localMatrix);
77 // again, uses implicit construction
78 screenSpace.loadMultiply(unitMatrix, shaderMatrix);
79 screenSpace.multiply(modelViewMatrix);
80}
81
Romain Guy06f96e22010-07-30 19:18:16 -070082///////////////////////////////////////////////////////////////////////////////
Chris Craik82840732015-04-03 09:37:49 -070083// gradient shader matrix helpers
Chris Craik3f0854292014-04-15 16:18:08 -070084///////////////////////////////////////////////////////////////////////////////
85
Chris Craik82840732015-04-03 09:37:49 -070086static void toLinearUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
Romain Guye3095e02010-10-06 16:57:29 -070087 SkVector vec = pts[1] - pts[0];
88 const float mag = vec.length();
89 const float inv = mag ? 1.0f / mag : 0;
90
91 vec.scale(inv);
92 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
93 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
94 matrix->postScale(inv, inv);
95}
96
Romain Guy14830942010-10-07 15:07:45 -070097static void toCircularUnitMatrix(const float x, const float y, const float radius,
98 SkMatrix* matrix) {
99 const float inv = 1.0f / radius;
100 matrix->setTranslate(-x, -y);
101 matrix->postScale(inv, inv);
102}
103
Romain Guy14830942010-10-07 15:07:45 -0700104static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
105 matrix->setTranslate(-x, -y);
106}
107
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400108///////////////////////////////////////////////////////////////////////////////
109// Common gradient code
110///////////////////////////////////////////////////////////////////////////////
Romain Guy14830942010-10-07 15:07:45 -0700111
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400112static bool isSimpleGradient(const SkShader::GradientInfo& gradInfo) {
113 return gradInfo.fColorCount == 2 && gradInfo.fTileMode == SkShader::kClamp_TileMode;
Romain Guyee916f12010-09-20 17:53:08 -0700114}
115
Chris Craik922d3a72015-02-13 17:47:21 -0800116///////////////////////////////////////////////////////////////////////////////
117// Store / apply
118///////////////////////////////////////////////////////////////////////////////
119
120bool tryStoreGradient(Caches& caches, const SkShader& shader, const Matrix4 modelViewMatrix,
121 GLuint* textureUnit, ProgramDescription* description,
122 SkiaShaderData::GradientShaderData* outData) {
123 SkShader::GradientInfo gradInfo;
124 gradInfo.fColorCount = 0;
125 gradInfo.fColors = nullptr;
126 gradInfo.fColorOffsets = nullptr;
127
128 SkMatrix unitMatrix;
129 switch (shader.asAGradient(&gradInfo)) {
130 case SkShader::kLinear_GradientType:
131 description->gradientType = ProgramDescription::kGradientLinear;
132
Chris Craik82840732015-04-03 09:37:49 -0700133 toLinearUnitMatrix(gradInfo.fPoint, &unitMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800134 break;
135 case SkShader::kRadial_GradientType:
136 description->gradientType = ProgramDescription::kGradientCircular;
137
138 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
139 gradInfo.fRadius[0], &unitMatrix);
140 break;
141 case SkShader::kSweep_GradientType:
142 description->gradientType = ProgramDescription::kGradientSweep;
143
144 toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
145 break;
146 default:
147 // Do nothing. This shader is unsupported.
148 return false;
149 }
150 description->hasGradient = true;
151 description->isSimpleGradient = isSimpleGradient(gradInfo);
152
153 computeScreenSpaceMatrix(outData->screenSpace, unitMatrix,
154 shader.getLocalMatrix(), modelViewMatrix);
155
156 // re-query shader to get full color / offset data
157 std::unique_ptr<SkColor[]> colorStorage(new SkColor[gradInfo.fColorCount]);
158 std::unique_ptr<SkScalar[]> colorOffsets(new SkScalar[gradInfo.fColorCount]);
159 gradInfo.fColors = &colorStorage[0];
160 gradInfo.fColorOffsets = &colorOffsets[0];
161 shader.asAGradient(&gradInfo);
162
163 if (CC_UNLIKELY(!isSimpleGradient(gradInfo))) {
164 outData->gradientSampler = (*textureUnit)++;
165
166#ifndef SK_SCALAR_IS_FLOAT
167 #error Need to convert gradInfo.fColorOffsets to float!
168#endif
169 outData->gradientTexture = caches.gradientCache.get(
170 gradInfo.fColors, gradInfo.fColorOffsets, gradInfo.fColorCount);
171 outData->wrapST = gTileModes[gradInfo.fTileMode];
172 } else {
173 outData->gradientSampler = 0;
174 outData->gradientTexture = nullptr;
175
176 outData->startColor.set(gradInfo.fColors[0]);
177 outData->endColor.set(gradInfo.fColors[1]);
178 }
179
Chris Craik922d3a72015-02-13 17:47:21 -0800180 return true;
181}
182
Romain Guy253f2c22016-09-28 17:34:42 -0700183void applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data,
184 const GLsizei width, const GLsizei height) {
185
Chris Craik922d3a72015-02-13 17:47:21 -0800186 if (CC_UNLIKELY(data.gradientTexture)) {
187 caches.textureState().activateTexture(data.gradientSampler);
188 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
189 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
190 } else {
191 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
192 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
193 }
194
Romain Guy253f2c22016-09-28 17:34:42 -0700195 glUniform2f(caches.program().getUniform("screenSize"), 1.0f / width, 1.0f / height);
Chris Craik922d3a72015-02-13 17:47:21 -0800196 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1,
197 GL_FALSE, &data.screenSpace.data[0]);
198}
199
200bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
201 GLuint* textureUnit, ProgramDescription* description,
202 SkiaShaderData::BitmapShaderData* outData) {
203 SkBitmap bitmap;
204 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400205 if (!shader.isABitmap(&bitmap, nullptr, xy)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800206 return false;
207 }
208
Romain Guy253f2c22016-09-28 17:34:42 -0700209 outData->bitmapTexture = caches.textureCache.get(&bitmap);
Chris Craik922d3a72015-02-13 17:47:21 -0800210 if (!outData->bitmapTexture) return false;
211
212 outData->bitmapSampler = (*textureUnit)++;
213
John Reck38e0c322015-11-10 12:19:17 -0800214 const float width = outData->bitmapTexture->width();
215 const float height = outData->bitmapTexture->height();
Chris Craik922d3a72015-02-13 17:47:21 -0800216
217 description->hasBitmap = true;
218 if (!caches.extensions().hasNPot()
219 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))
220 && (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode)) {
221 description->isBitmapNpot = true;
222 description->bitmapWrapS = gTileModes[xy[0]];
223 description->bitmapWrapT = gTileModes[xy[1]];
224
225 outData->wrapS = GL_CLAMP_TO_EDGE;
226 outData->wrapT = GL_CLAMP_TO_EDGE;
227 } else {
228 outData->wrapS = gTileModes[xy[0]];
229 outData->wrapT = gTileModes[xy[1]];
230 }
231
232 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
233 modelViewMatrix);
234 outData->textureDimension[0] = 1.0f / width;
235 outData->textureDimension[1] = 1.0f / height;
236
237 return true;
238}
239
240void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
241 caches.textureState().activateTexture(data.bitmapSampler);
242 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
243 data.bitmapTexture->setFilter(GL_LINEAR);
244
245 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
246 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
247 &data.textureTransform.data[0]);
248 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
249}
250
251SkiaShaderType getComposeSubType(const SkShader& shader) {
252 // First check for a gradient shader.
253 switch (shader.asAGradient(nullptr)) {
254 case SkShader::kNone_GradientType:
255 // Not a gradient shader. Fall through to check for other types.
256 break;
257 case SkShader::kLinear_GradientType:
258 case SkShader::kRadial_GradientType:
259 case SkShader::kSweep_GradientType:
260 return kGradient_SkiaShaderType;
261 default:
262 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
263 return kNone_SkiaShaderType;
264 }
265
266 // The shader is not a gradient. Check for a bitmap shader.
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400267 if (shader.isABitmap()) {
Chris Craik922d3a72015-02-13 17:47:21 -0800268 return kBitmap_SkiaShaderType;
269 }
270 return kNone_SkiaShaderType;
271}
272
273void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
274 const Matrix4& modelViewMatrix, GLuint* textureUnit,
275 ProgramDescription* description, SkiaShaderData* outData) {
276 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix,
277 textureUnit, description, &outData->bitmapData),
278 "failed storing bitmap shader data");
279 LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix,
280 textureUnit, description, &outData->gradientData),
281 "failing storing gradient shader data");
282}
283
284bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
285 GLuint* textureUnit, ProgramDescription* description,
286 SkiaShaderData* outData) {
287
288 SkShader::ComposeRec rec;
289 if (!shader.asACompose(&rec)) return false;
290
291 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
292 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
293
294 // check that type enum values are the 2 flags that compose the kCompose value
295 if ((shaderAType & shaderBType) != 0) return false;
296 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
297
298 mat4 transform;
299 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
300 if (shaderAType == kBitmap_SkiaShaderType) {
301 description->isBitmapFirst = true;
302 storeCompose(caches, *rec.fShaderA, *rec.fShaderB,
303 transform, textureUnit, description, outData);
304 } else {
305 description->isBitmapFirst = false;
306 storeCompose(caches, *rec.fShaderB, *rec.fShaderA,
307 transform, textureUnit, description, outData);
308 }
309 if (!SkXfermode::AsMode(rec.fMode, &description->shadersMode)) {
310 // TODO: Support other modes.
311 description->shadersMode = SkXfermode::kSrcOver_Mode;
312 }
313 return true;
314}
315
316bool tryStoreLayer(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
317 GLuint* textureUnit, ProgramDescription* description,
318 SkiaShaderData::LayerShaderData* outData) {
319 Layer* layer;
320 if (!shader.asACustomShader(reinterpret_cast<void**>(&layer))) {
321 return false;
322 }
323
324 description->hasBitmap = true;
Chris Craik36a35e32015-02-18 09:24:33 -0800325 outData->layer = layer;
Chris Craik922d3a72015-02-13 17:47:21 -0800326 outData->bitmapSampler = (*textureUnit)++;
327
328 const float width = layer->getWidth();
329 const float height = layer->getHeight();
330
331 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
332 modelViewMatrix);
333
334 outData->textureDimension[0] = 1.0f / width;
335 outData->textureDimension[1] = 1.0f / height;
336 return true;
337}
338
339void applyLayer(Caches& caches, const SkiaShaderData::LayerShaderData& data) {
340 caches.textureState().activateTexture(data.bitmapSampler);
341
342 data.layer->bindTexture();
343 data.layer->setWrap(GL_CLAMP_TO_EDGE);
344 data.layer->setFilter(GL_LINEAR);
345
346 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
347 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1,
348 GL_FALSE, &data.textureTransform.data[0]);
349 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
350}
351
Chris Craik53e51e42015-06-01 10:35:35 -0700352void SkiaShader::store(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800353 GLuint* textureUnit, ProgramDescription* description,
354 SkiaShaderData* outData) {
Chris Craik53e51e42015-06-01 10:35:35 -0700355 if (tryStoreGradient(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800356 textureUnit, description, &outData->gradientData)) {
357 outData->skiaShaderType = kGradient_SkiaShaderType;
358 return;
359 }
360
Chris Craik53e51e42015-06-01 10:35:35 -0700361 if (tryStoreBitmap(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800362 textureUnit, description, &outData->bitmapData)) {
363 outData->skiaShaderType = kBitmap_SkiaShaderType;
364 return;
365 }
366
Chris Craik53e51e42015-06-01 10:35:35 -0700367 if (tryStoreCompose(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800368 textureUnit, description, outData)) {
369 outData->skiaShaderType = kCompose_SkiaShaderType;
370 return;
371 }
372
Chris Craik53e51e42015-06-01 10:35:35 -0700373 if (tryStoreLayer(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800374 textureUnit, description, &outData->layerData)) {
375 outData->skiaShaderType = kLayer_SkiaShaderType;
Chris Craike310f832015-07-13 13:34:07 -0700376 return;
Chris Craik922d3a72015-02-13 17:47:21 -0800377 }
Chris Craike310f832015-07-13 13:34:07 -0700378
379 // Unknown/unsupported type, so explicitly ignore shader
380 outData->skiaShaderType = kNone_SkiaShaderType;
Chris Craik922d3a72015-02-13 17:47:21 -0800381}
382
Romain Guy253f2c22016-09-28 17:34:42 -0700383void SkiaShader::apply(Caches& caches, const SkiaShaderData& data,
384 const GLsizei width, const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -0800385 if (!data.skiaShaderType) return;
386
387 if (data.skiaShaderType & kGradient_SkiaShaderType) {
Romain Guy253f2c22016-09-28 17:34:42 -0700388 applyGradient(caches, data.gradientData, width, height);
Chris Craik922d3a72015-02-13 17:47:21 -0800389 }
390 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
391 applyBitmap(caches, data.bitmapData);
392 }
393
394 if (data.skiaShaderType == kLayer_SkiaShaderType) {
395 applyLayer(caches, data.layerData);
396 }
397}
398
Romain Guy06f96e22010-07-30 19:18:16 -0700399}; // namespace uirenderer
400}; // namespace android