blob: df746554f3069d529ef3ba4cfb151102b21d9109 [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
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) {
sergeyv554ffeb2016-11-15 18:01:21 -080060 caches->textureState().bindTexture(texture->target(), 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,
John Reck1bcacfd2017-11-03 10:12:19 -070073 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040074 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///////////////////////////////////////////////////////////////////////////////
Romain Guya0ed6f02016-12-12 18:21:32 -080083// 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,
John Reck1bcacfd2017-11-03 10:12:19 -070098 SkMatrix* matrix) {
Romain Guy14830942010-10-07 15:07:45 -070099 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,
John Reck1bcacfd2017-11-03 10:12:19 -0700121 GLuint* textureUnit, ProgramDescription* description,
122 SkiaShaderData::GradientShaderData* outData) {
Chris Craik922d3a72015-02-13 17:47:21 -0800123 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
John Reck1bcacfd2017-11-03 10:12:19 -0700138 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, gradInfo.fRadius[0],
139 &unitMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800140 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
John Reck1bcacfd2017-11-03 10:12:19 -0700153 computeScreenSpaceMatrix(outData->screenSpace, unitMatrix, shader.getLocalMatrix(),
154 modelViewMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800155
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
Romain Guya0ed6f02016-12-12 18:21:32 -0800163 if (CC_UNLIKELY(!description->isSimpleGradient)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800164 outData->gradientSampler = (*textureUnit)++;
165
166#ifndef SK_SCALAR_IS_FLOAT
John Reck1bcacfd2017-11-03 10:12:19 -0700167#error Need to convert gradInfo.fColorOffsets to float!
Chris Craik922d3a72015-02-13 17:47:21 -0800168#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
Derek Sollenberger669b15a2017-03-31 12:09:24 -0400176 outData->startColor.set(gradInfo.fColors[0]);
177 outData->endColor.set(gradInfo.fColors[1]);
Chris Craik922d3a72015-02-13 17:47:21 -0800178 }
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,
John Reck1bcacfd2017-11-03 10:12:19 -0700184 const GLsizei width, const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -0800185 if (CC_UNLIKELY(data.gradientTexture)) {
186 caches.textureState().activateTexture(data.gradientSampler);
187 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
188 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
189 } else {
190 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
191 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
192 }
193
Romain Guy253f2c22016-09-28 17:34:42 -0700194 glUniform2f(caches.program().getUniform("screenSize"), 1.0f / width, 1.0f / height);
John Reck1bcacfd2017-11-03 10:12:19 -0700195 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1, GL_FALSE,
196 &data.screenSpace.data[0]);
Chris Craik922d3a72015-02-13 17:47:21 -0800197}
198
199bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700200 GLuint* textureUnit, ProgramDescription* description,
201 SkiaShaderData::BitmapShaderData* outData) {
Chris Craik922d3a72015-02-13 17:47:21 -0800202 SkBitmap bitmap;
203 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400204 if (!shader.isABitmap(&bitmap, nullptr, xy)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800205 return false;
206 }
207
sergeyvec4a4b12016-10-20 18:39:04 -0700208 // TODO: create hwui-owned BitmapShader.
209 Bitmap* hwuiBitmap = static_cast<Bitmap*>(bitmap.pixelRef());
210 outData->bitmapTexture = caches.textureCache.get(hwuiBitmap);
Chris Craik922d3a72015-02-13 17:47:21 -0800211 if (!outData->bitmapTexture) return false;
212
213 outData->bitmapSampler = (*textureUnit)++;
214
John Reck38e0c322015-11-10 12:19:17 -0800215 const float width = outData->bitmapTexture->width();
216 const float height = outData->bitmapTexture->height();
Chris Craik922d3a72015-02-13 17:47:21 -0800217
Romain Guycaaaa662017-03-27 00:40:21 -0700218 Texture* texture = outData->bitmapTexture;
219
Chris Craik922d3a72015-02-13 17:47:21 -0800220 description->hasBitmap = true;
Romain Guycaaaa662017-03-27 00:40:21 -0700221 description->hasLinearTexture = texture->isLinear();
222 description->hasColorSpaceConversion = texture->hasColorSpaceConversion();
223 description->transferFunction = texture->getTransferFunctionType();
224 description->hasTranslucentConversion = texture->blend;
sergeyv9c97e482016-12-12 16:14:11 -0800225 description->isShaderBitmapExternal = hwuiBitmap->isHardware();
sergeyv554ffeb2016-11-15 18:01:21 -0800226 // gralloc doesn't support non-clamp modes
John Reck1bcacfd2017-11-03 10:12:19 -0700227 if (hwuiBitmap->isHardware() ||
228 (!caches.extensions().hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) &&
229 (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode))) {
sergeyv554ffeb2016-11-15 18:01:21 -0800230 // need non-clamp mode, but it's not supported for this draw,
231 // so enable custom shader logic to mimic
232 description->useShaderBasedWrap = true;
Chris Craik922d3a72015-02-13 17:47:21 -0800233 description->bitmapWrapS = gTileModes[xy[0]];
234 description->bitmapWrapT = gTileModes[xy[1]];
235
236 outData->wrapS = GL_CLAMP_TO_EDGE;
237 outData->wrapT = GL_CLAMP_TO_EDGE;
238 } else {
239 outData->wrapS = gTileModes[xy[0]];
240 outData->wrapT = gTileModes[xy[1]];
241 }
242
243 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
John Reck1bcacfd2017-11-03 10:12:19 -0700244 modelViewMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800245 outData->textureDimension[0] = 1.0f / width;
246 outData->textureDimension[1] = 1.0f / height;
247
248 return true;
249}
250
251void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
252 caches.textureState().activateTexture(data.bitmapSampler);
253 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
254 data.bitmapTexture->setFilter(GL_LINEAR);
255
256 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
257 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700258 &data.textureTransform.data[0]);
Chris Craik922d3a72015-02-13 17:47:21 -0800259 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
260}
261
262SkiaShaderType getComposeSubType(const SkShader& shader) {
263 // First check for a gradient shader.
264 switch (shader.asAGradient(nullptr)) {
265 case SkShader::kNone_GradientType:
266 // Not a gradient shader. Fall through to check for other types.
267 break;
268 case SkShader::kLinear_GradientType:
269 case SkShader::kRadial_GradientType:
270 case SkShader::kSweep_GradientType:
271 return kGradient_SkiaShaderType;
272 default:
273 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
274 return kNone_SkiaShaderType;
275 }
276
277 // The shader is not a gradient. Check for a bitmap shader.
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400278 if (shader.isABitmap()) {
Chris Craik922d3a72015-02-13 17:47:21 -0800279 return kBitmap_SkiaShaderType;
280 }
281 return kNone_SkiaShaderType;
282}
283
284void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
John Reck1bcacfd2017-11-03 10:12:19 -0700285 const Matrix4& modelViewMatrix, GLuint* textureUnit,
286 ProgramDescription* description, SkiaShaderData* outData) {
287 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix, textureUnit,
288 description, &outData->bitmapData),
289 "failed storing bitmap shader data");
290 LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix, textureUnit,
291 description, &outData->gradientData),
292 "failing storing gradient shader data");
Chris Craik922d3a72015-02-13 17:47:21 -0800293}
294
295bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700296 GLuint* textureUnit, ProgramDescription* description,
297 SkiaShaderData* outData) {
Chris Craik922d3a72015-02-13 17:47:21 -0800298 SkShader::ComposeRec rec;
299 if (!shader.asACompose(&rec)) return false;
300
301 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
302 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
303
304 // check that type enum values are the 2 flags that compose the kCompose value
305 if ((shaderAType & shaderBType) != 0) return false;
306 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
307
308 mat4 transform;
309 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
310 if (shaderAType == kBitmap_SkiaShaderType) {
311 description->isBitmapFirst = true;
John Reck1bcacfd2017-11-03 10:12:19 -0700312 storeCompose(caches, *rec.fShaderA, *rec.fShaderB, transform, textureUnit, description,
313 outData);
Chris Craik922d3a72015-02-13 17:47:21 -0800314 } else {
315 description->isBitmapFirst = false;
John Reck1bcacfd2017-11-03 10:12:19 -0700316 storeCompose(caches, *rec.fShaderB, *rec.fShaderA, transform, textureUnit, description,
317 outData);
Chris Craik922d3a72015-02-13 17:47:21 -0800318 }
Mike Reedc2f31df2016-10-28 17:21:45 -0400319 description->shadersMode = rec.fBlendMode;
Chris Craik922d3a72015-02-13 17:47:21 -0800320 return true;
321}
322
Chris Craik53e51e42015-06-01 10:35:35 -0700323void SkiaShader::store(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
John Reck1bcacfd2017-11-03 10:12:19 -0700324 GLuint* textureUnit, ProgramDescription* description,
325 SkiaShaderData* outData) {
326 if (tryStoreGradient(caches, shader, modelViewMatrix, textureUnit, description,
327 &outData->gradientData)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800328 outData->skiaShaderType = kGradient_SkiaShaderType;
329 return;
330 }
331
John Reck1bcacfd2017-11-03 10:12:19 -0700332 if (tryStoreBitmap(caches, shader, modelViewMatrix, textureUnit, description,
333 &outData->bitmapData)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800334 outData->skiaShaderType = kBitmap_SkiaShaderType;
335 return;
336 }
337
John Reck1bcacfd2017-11-03 10:12:19 -0700338 if (tryStoreCompose(caches, shader, modelViewMatrix, textureUnit, description, outData)) {
Chris Craik922d3a72015-02-13 17:47:21 -0800339 outData->skiaShaderType = kCompose_SkiaShaderType;
340 return;
341 }
342
Chris Craike310f832015-07-13 13:34:07 -0700343 // Unknown/unsupported type, so explicitly ignore shader
344 outData->skiaShaderType = kNone_SkiaShaderType;
Chris Craik922d3a72015-02-13 17:47:21 -0800345}
346
John Reck1bcacfd2017-11-03 10:12:19 -0700347void SkiaShader::apply(Caches& caches, const SkiaShaderData& data, const GLsizei width,
348 const GLsizei height) {
Chris Craik922d3a72015-02-13 17:47:21 -0800349 if (!data.skiaShaderType) return;
350
351 if (data.skiaShaderType & kGradient_SkiaShaderType) {
Romain Guy253f2c22016-09-28 17:34:42 -0700352 applyGradient(caches, data.gradientData, width, height);
Chris Craik922d3a72015-02-13 17:47:21 -0800353 }
354 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
355 applyBitmap(caches, data.bitmapData);
356 }
Chris Craik922d3a72015-02-13 17:47:21 -0800357}
358
John Reck1bcacfd2017-11-03 10:12:19 -0700359}; // namespace uirenderer
360}; // namespace android