blob: 489a306c13d1156edc9aa727b6022d769d1fe054 [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"
sergeyvec4a4b12016-10-20 18:39:04 -070024#include "hwui/Bitmap.h"
Romain Guy06f96e22010-07-30 19:18:16 -070025
Chris Craik922d3a72015-02-13 17:47:21 -080026#include <SkMatrix.h>
27#include <utils/Log.h>
28
Romain Guy06f96e22010-07-30 19:18:16 -070029namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Support
34///////////////////////////////////////////////////////////////////////////////
35
Chris Craik216048f2015-08-21 15:31:20 -070036static constexpr GLenum gTileModes[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070037 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
38 GL_REPEAT, // == SkShader::kRepeat_Mode
39 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
40};
41
Chris Craik216048f2015-08-21 15:31:20 -070042static_assert(gTileModes[SkShader::kClamp_TileMode] == GL_CLAMP_TO_EDGE,
43 "SkShader TileModes have changed");
44static_assert(gTileModes[SkShader::kRepeat_TileMode] == GL_REPEAT,
45 "SkShader TileModes have changed");
46static_assert(gTileModes[SkShader::kMirror_TileMode] == GL_MIRRORED_REPEAT,
47 "SkShader TileModes have changed");
48
Romain Guy42e1e0d2012-07-30 14:47:51 -070049/**
50 * This function does not work for n == 0.
51 */
52static inline bool isPowerOfTwo(unsigned int n) {
53 return !(n & (n - 1));
54}
55
Chris Craik922d3a72015-02-13 17:47:21 -080056static inline void bindUniformColor(int slot, FloatColor color) {
57 glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
58}
59
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040060static inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
John Reck38e0c322015-11-10 12:19:17 -080061 caches->textureState().bindTexture(texture->id());
Romain Guyd21b6e12011-11-30 20:21:23 -080062 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070063}
64
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040065/**
66 * Compute the matrix to transform to screen space.
67 * @param screenSpace Output param for the computed matrix.
68 * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
69 * or identity.
70 * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
71 * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
72 */
73static void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
74 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
75 mat4 shaderMatrix;
76 // uses implicit construction
77 shaderMatrix.loadInverse(localMatrix);
78 // again, uses implicit construction
79 screenSpace.loadMultiply(unitMatrix, shaderMatrix);
80 screenSpace.multiply(modelViewMatrix);
81}
82
Romain Guy06f96e22010-07-30 19:18:16 -070083///////////////////////////////////////////////////////////////////////////////
Chris Craik82840732015-04-03 09:37:49 -070084// gradient shader matrix helpers
Chris Craik3f0854292014-04-15 16:18:08 -070085///////////////////////////////////////////////////////////////////////////////
86
Chris Craik82840732015-04-03 09:37:49 -070087static void toLinearUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
Romain Guye3095e02010-10-06 16:57:29 -070088 SkVector vec = pts[1] - pts[0];
89 const float mag = vec.length();
90 const float inv = mag ? 1.0f / mag : 0;
91
92 vec.scale(inv);
93 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
94 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
95 matrix->postScale(inv, inv);
96}
97
Romain Guy14830942010-10-07 15:07:45 -070098static void toCircularUnitMatrix(const float x, const float y, const float radius,
99 SkMatrix* matrix) {
100 const float inv = 1.0f / radius;
101 matrix->setTranslate(-x, -y);
102 matrix->postScale(inv, inv);
103}
104
Romain Guy14830942010-10-07 15:07:45 -0700105static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
106 matrix->setTranslate(-x, -y);
107}
108
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400109///////////////////////////////////////////////////////////////////////////////
110// Common gradient code
111///////////////////////////////////////////////////////////////////////////////
Romain Guy14830942010-10-07 15:07:45 -0700112
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400113static bool isSimpleGradient(const SkShader::GradientInfo& gradInfo) {
114 return gradInfo.fColorCount == 2 && gradInfo.fTileMode == SkShader::kClamp_TileMode;
Romain Guyee916f12010-09-20 17:53:08 -0700115}
116
Chris Craik922d3a72015-02-13 17:47:21 -0800117///////////////////////////////////////////////////////////////////////////////
118// Store / apply
119///////////////////////////////////////////////////////////////////////////////
120
121bool tryStoreGradient(Caches& caches, const SkShader& shader, const Matrix4 modelViewMatrix,
122 GLuint* textureUnit, ProgramDescription* description,
123 SkiaShaderData::GradientShaderData* outData) {
124 SkShader::GradientInfo gradInfo;
125 gradInfo.fColorCount = 0;
126 gradInfo.fColors = nullptr;
127 gradInfo.fColorOffsets = nullptr;
128
129 SkMatrix unitMatrix;
130 switch (shader.asAGradient(&gradInfo)) {
131 case SkShader::kLinear_GradientType:
132 description->gradientType = ProgramDescription::kGradientLinear;
133
Chris Craik82840732015-04-03 09:37:49 -0700134 toLinearUnitMatrix(gradInfo.fPoint, &unitMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800135 break;
136 case SkShader::kRadial_GradientType:
137 description->gradientType = ProgramDescription::kGradientCircular;
138
139 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
140 gradInfo.fRadius[0], &unitMatrix);
141 break;
142 case SkShader::kSweep_GradientType:
143 description->gradientType = ProgramDescription::kGradientSweep;
144
145 toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
146 break;
147 default:
148 // Do nothing. This shader is unsupported.
149 return false;
150 }
151 description->hasGradient = true;
152 description->isSimpleGradient = isSimpleGradient(gradInfo);
153
154 computeScreenSpaceMatrix(outData->screenSpace, unitMatrix,
155 shader.getLocalMatrix(), modelViewMatrix);
156
157 // re-query shader to get full color / offset data
158 std::unique_ptr<SkColor[]> colorStorage(new SkColor[gradInfo.fColorCount]);
159 std::unique_ptr<SkScalar[]> colorOffsets(new SkScalar[gradInfo.fColorCount]);
160 gradInfo.fColors = &colorStorage[0];
161 gradInfo.fColorOffsets = &colorOffsets[0];
162 shader.asAGradient(&gradInfo);
163
164 if (CC_UNLIKELY(!isSimpleGradient(gradInfo))) {
165 outData->gradientSampler = (*textureUnit)++;
166
167#ifndef SK_SCALAR_IS_FLOAT
168 #error Need to convert gradInfo.fColorOffsets to float!
169#endif
170 outData->gradientTexture = caches.gradientCache.get(
171 gradInfo.fColors, gradInfo.fColorOffsets, gradInfo.fColorCount);
172 outData->wrapST = gTileModes[gradInfo.fTileMode];
173 } else {
174 outData->gradientSampler = 0;
175 outData->gradientTexture = nullptr;
176
Romain Guy8762e332016-10-12 12:14:07 -0700177 outData->startColor.setSRGB(gradInfo.fColors[0]);
178 outData->endColor.setSRGB(gradInfo.fColors[1]);
Chris Craik922d3a72015-02-13 17:47:21 -0800179 }
180
Chris Craik922d3a72015-02-13 17:47:21 -0800181 return true;
182}
183
Romain Guy253f2c22016-09-28 17:34:42 -0700184void applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data,
185 const GLsizei width, const GLsizei height) {
186
Chris Craik922d3a72015-02-13 17:47:21 -0800187 if (CC_UNLIKELY(data.gradientTexture)) {
188 caches.textureState().activateTexture(data.gradientSampler);
189 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
190 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
191 } else {
192 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
193 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
194 }
195
Romain Guy253f2c22016-09-28 17:34:42 -0700196 glUniform2f(caches.program().getUniform("screenSize"), 1.0f / width, 1.0f / height);
Chris Craik922d3a72015-02-13 17:47:21 -0800197 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1,
198 GL_FALSE, &data.screenSpace.data[0]);
199}
200
201bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
202 GLuint* textureUnit, ProgramDescription* description,
203 SkiaShaderData::BitmapShaderData* outData) {
204 SkBitmap bitmap;
205 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400206 if (!shader.isABitmap(&bitmap, nullptr, xy)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800207 return false;
208 }
209
sergeyvec4a4b12016-10-20 18:39:04 -0700210 // TODO: create hwui-owned BitmapShader.
211 Bitmap* hwuiBitmap = static_cast<Bitmap*>(bitmap.pixelRef());
212 outData->bitmapTexture = caches.textureCache.get(hwuiBitmap);
Chris Craik922d3a72015-02-13 17:47:21 -0800213 if (!outData->bitmapTexture) return false;
214
215 outData->bitmapSampler = (*textureUnit)++;
216
John Reck38e0c322015-11-10 12:19:17 -0800217 const float width = outData->bitmapTexture->width();
218 const float height = outData->bitmapTexture->height();
Chris Craik922d3a72015-02-13 17:47:21 -0800219
220 description->hasBitmap = true;
221 if (!caches.extensions().hasNPot()
222 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))
223 && (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode)) {
224 description->isBitmapNpot = true;
225 description->bitmapWrapS = gTileModes[xy[0]];
226 description->bitmapWrapT = gTileModes[xy[1]];
227
228 outData->wrapS = GL_CLAMP_TO_EDGE;
229 outData->wrapT = GL_CLAMP_TO_EDGE;
230 } else {
231 outData->wrapS = gTileModes[xy[0]];
232 outData->wrapT = gTileModes[xy[1]];
233 }
234
235 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
236 modelViewMatrix);
237 outData->textureDimension[0] = 1.0f / width;
238 outData->textureDimension[1] = 1.0f / height;
239
240 return true;
241}
242
243void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
244 caches.textureState().activateTexture(data.bitmapSampler);
245 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
246 data.bitmapTexture->setFilter(GL_LINEAR);
247
248 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
249 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
250 &data.textureTransform.data[0]);
251 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
252}
253
254SkiaShaderType getComposeSubType(const SkShader& shader) {
255 // First check for a gradient shader.
256 switch (shader.asAGradient(nullptr)) {
257 case SkShader::kNone_GradientType:
258 // Not a gradient shader. Fall through to check for other types.
259 break;
260 case SkShader::kLinear_GradientType:
261 case SkShader::kRadial_GradientType:
262 case SkShader::kSweep_GradientType:
263 return kGradient_SkiaShaderType;
264 default:
265 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
266 return kNone_SkiaShaderType;
267 }
268
269 // The shader is not a gradient. Check for a bitmap shader.
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400270 if (shader.isABitmap()) {
Chris Craik922d3a72015-02-13 17:47:21 -0800271 return kBitmap_SkiaShaderType;
272 }
273 return kNone_SkiaShaderType;
274}
275
276void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
277 const Matrix4& modelViewMatrix, GLuint* textureUnit,
278 ProgramDescription* description, SkiaShaderData* outData) {
279 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix,
280 textureUnit, description, &outData->bitmapData),
281 "failed storing bitmap shader data");
282 LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix,
283 textureUnit, description, &outData->gradientData),
284 "failing storing gradient shader data");
285}
286
287bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
288 GLuint* textureUnit, ProgramDescription* description,
289 SkiaShaderData* outData) {
290
291 SkShader::ComposeRec rec;
292 if (!shader.asACompose(&rec)) return false;
293
294 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
295 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
296
297 // check that type enum values are the 2 flags that compose the kCompose value
298 if ((shaderAType & shaderBType) != 0) return false;
299 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
300
301 mat4 transform;
302 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
303 if (shaderAType == kBitmap_SkiaShaderType) {
304 description->isBitmapFirst = true;
305 storeCompose(caches, *rec.fShaderA, *rec.fShaderB,
306 transform, textureUnit, description, outData);
307 } else {
308 description->isBitmapFirst = false;
309 storeCompose(caches, *rec.fShaderB, *rec.fShaderA,
310 transform, textureUnit, description, outData);
311 }
312 if (!SkXfermode::AsMode(rec.fMode, &description->shadersMode)) {
313 // TODO: Support other modes.
314 description->shadersMode = SkXfermode::kSrcOver_Mode;
315 }
316 return true;
317}
318
319bool tryStoreLayer(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
320 GLuint* textureUnit, ProgramDescription* description,
321 SkiaShaderData::LayerShaderData* outData) {
322 Layer* layer;
323 if (!shader.asACustomShader(reinterpret_cast<void**>(&layer))) {
324 return false;
325 }
326
327 description->hasBitmap = true;
Chris Craik36a35e32015-02-18 09:24:33 -0800328 outData->layer = layer;
Chris Craik922d3a72015-02-13 17:47:21 -0800329 outData->bitmapSampler = (*textureUnit)++;
330
331 const float width = layer->getWidth();
332 const float height = layer->getHeight();
333
334 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
335 modelViewMatrix);
336
337 outData->textureDimension[0] = 1.0f / width;
338 outData->textureDimension[1] = 1.0f / height;
339 return true;
340}
341
342void applyLayer(Caches& caches, const SkiaShaderData::LayerShaderData& data) {
343 caches.textureState().activateTexture(data.bitmapSampler);
344
345 data.layer->bindTexture();
346 data.layer->setWrap(GL_CLAMP_TO_EDGE);
347 data.layer->setFilter(GL_LINEAR);
348
349 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
350 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1,
351 GL_FALSE, &data.textureTransform.data[0]);
352 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
353}
354
Chris Craik53e51e42015-06-01 10:35:35 -0700355void SkiaShader::store(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800356 GLuint* textureUnit, ProgramDescription* description,
357 SkiaShaderData* outData) {
Chris Craik53e51e42015-06-01 10:35:35 -0700358 if (tryStoreGradient(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800359 textureUnit, description, &outData->gradientData)) {
360 outData->skiaShaderType = kGradient_SkiaShaderType;
361 return;
362 }
363
Chris Craik53e51e42015-06-01 10:35:35 -0700364 if (tryStoreBitmap(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800365 textureUnit, description, &outData->bitmapData)) {
366 outData->skiaShaderType = kBitmap_SkiaShaderType;
367 return;
368 }
369
Chris Craik53e51e42015-06-01 10:35:35 -0700370 if (tryStoreCompose(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800371 textureUnit, description, outData)) {
372 outData->skiaShaderType = kCompose_SkiaShaderType;
373 return;
374 }
375
Chris Craik53e51e42015-06-01 10:35:35 -0700376 if (tryStoreLayer(caches, shader, modelViewMatrix,
Chris Craik922d3a72015-02-13 17:47:21 -0800377 textureUnit, description, &outData->layerData)) {
378 outData->skiaShaderType = kLayer_SkiaShaderType;
Chris Craike310f832015-07-13 13:34:07 -0700379 return;
Chris Craik922d3a72015-02-13 17:47:21 -0800380 }
Chris Craike310f832015-07-13 13:34:07 -0700381
382 // Unknown/unsupported type, so explicitly ignore shader
383 outData->skiaShaderType = kNone_SkiaShaderType;
Chris Craik922d3a72015-02-13 17:47:21 -0800384}
385
Romain Guy253f2c22016-09-28 17:34:42 -0700386void SkiaShader::apply(Caches& caches, const SkiaShaderData& data,
387 const GLsizei width, const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -0800388 if (!data.skiaShaderType) return;
389
390 if (data.skiaShaderType & kGradient_SkiaShaderType) {
Romain Guy253f2c22016-09-28 17:34:42 -0700391 applyGradient(caches, data.gradientData, width, height);
Chris Craik922d3a72015-02-13 17:47:21 -0800392 }
393 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
394 applyBitmap(caches, data.bitmapData);
395 }
396
397 if (data.skiaShaderType == kLayer_SkiaShaderType) {
398 applyLayer(caches, data.layerData);
399 }
400}
401
Romain Guy06f96e22010-07-30 19:18:16 -0700402}; // namespace uirenderer
403}; // namespace android