blob: 81531e83ef7774d5c6dece99352efcd8a780663f [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
17#define LOG_TAG "OpenGLRenderer"
18
Chris Craik922d3a72015-02-13 17:47:21 -080019#include "SkiaShader.h"
Romain Guy06f96e22010-07-30 19:18:16 -070020
Romain Guya1d3c912011-12-13 14:55:06 -080021#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "Extensions.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040023#include "Layer.h"
24#include "Matrix.h"
Romain Guy06f96e22010-07-30 19:18:16 -070025#include "Texture.h"
Romain Guy06f96e22010-07-30 19:18:16 -070026
Chris Craik922d3a72015-02-13 17:47:21 -080027#include <SkMatrix.h>
28#include <utils/Log.h>
29
Romain Guy06f96e22010-07-30 19:18:16 -070030namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Support
35///////////////////////////////////////////////////////////////////////////////
36
Chris Craik922d3a72015-02-13 17:47:21 -080037static const GLenum gTileModes[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070038 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
39 GL_REPEAT, // == SkShader::kRepeat_Mode
40 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
41};
42
Romain Guy42e1e0d2012-07-30 14:47:51 -070043/**
44 * This function does not work for n == 0.
45 */
46static inline bool isPowerOfTwo(unsigned int n) {
47 return !(n & (n - 1));
48}
49
50static inline void bindUniformColor(int slot, uint32_t color) {
Romain Guyd679b572012-08-29 21:49:00 -070051 const float a = ((color >> 24) & 0xff) / 255.0f;
Romain Guy42e1e0d2012-07-30 14:47:51 -070052 glUniform4f(slot,
Romain Guyd679b572012-08-29 21:49:00 -070053 a * ((color >> 16) & 0xff) / 255.0f,
54 a * ((color >> 8) & 0xff) / 255.0f,
55 a * ((color ) & 0xff) / 255.0f,
56 a);
Romain Guy42e1e0d2012-07-30 14:47:51 -070057}
58
Chris Craik922d3a72015-02-13 17:47:21 -080059static inline void bindUniformColor(int slot, FloatColor color) {
60 glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
61}
62
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040063static inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
Chris Craik44eb2c02015-01-29 09:45:09 -080064 caches->textureState().bindTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -080065 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070066}
67
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040068/**
69 * Compute the matrix to transform to screen space.
70 * @param screenSpace Output param for the computed matrix.
71 * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
72 * or identity.
73 * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
74 * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
75 */
76static void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
77 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
78 mat4 shaderMatrix;
79 // uses implicit construction
80 shaderMatrix.loadInverse(localMatrix);
81 // again, uses implicit construction
82 screenSpace.loadMultiply(unitMatrix, shaderMatrix);
83 screenSpace.multiply(modelViewMatrix);
84}
85
86// Returns true if one is a bitmap and the other is a gradient
87static bool bitmapAndGradient(SkiaShaderType type1, SkiaShaderType type2) {
88 return (type1 == kBitmap_SkiaShaderType && type2 == kGradient_SkiaShaderType)
89 || (type2 == kBitmap_SkiaShaderType && type1 == kGradient_SkiaShaderType);
90}
91
92SkiaShaderType SkiaShader::getType(const SkShader& shader) {
93 // First check for a gradient shader.
Chris Craikd41c4d82015-01-05 15:51:13 -080094 switch (shader.asAGradient(nullptr)) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040095 case SkShader::kNone_GradientType:
96 // Not a gradient shader. Fall through to check for other types.
97 break;
98 case SkShader::kLinear_GradientType:
99 case SkShader::kRadial_GradientType:
100 case SkShader::kSweep_GradientType:
101 return kGradient_SkiaShaderType;
102 default:
103 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
104 return kNone_SkiaShaderType;
105 }
106
107 // The shader is not a gradient. Check for a bitmap shader.
Chris Craikd41c4d82015-01-05 15:51:13 -0800108 if (shader.asABitmap(nullptr, nullptr, nullptr) == SkShader::kDefault_BitmapType) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400109 return kBitmap_SkiaShaderType;
110 }
111
112 // Check for a ComposeShader.
113 SkShader::ComposeRec rec;
114 if (shader.asACompose(&rec)) {
115 const SkiaShaderType shaderAType = getType(*rec.fShaderA);
116 const SkiaShaderType shaderBType = getType(*rec.fShaderB);
117
118 // Compose is only supported if one is a bitmap and the other is a
119 // gradient. Otherwise, return None to skip.
120 if (!bitmapAndGradient(shaderAType, shaderBType)) {
121 return kNone_SkiaShaderType;
122 }
123 return kCompose_SkiaShaderType;
124 }
125
Chris Craikd41c4d82015-01-05 15:51:13 -0800126 if (shader.asACustomShader(nullptr)) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400127 return kLayer_SkiaShaderType;
128 }
129
130 return kNone_SkiaShaderType;
131}
132
133typedef void (*describeProc)(Caches* caches, ProgramDescription& description,
134 const Extensions& extensions, const SkShader& shader);
135
136describeProc gDescribeProc[] = {
137 InvalidSkiaShader::describe,
138 SkiaBitmapShader::describe,
139 SkiaGradientShader::describe,
140 SkiaComposeShader::describe,
141 SkiaLayerShader::describe,
142};
143
144typedef void (*setupProgramProc)(Caches* caches, const mat4& modelViewMatrix,
145 GLuint* textureUnit, const Extensions& extensions, const SkShader& shader);
146
147setupProgramProc gSetupProgramProc[] = {
148 InvalidSkiaShader::setupProgram,
149 SkiaBitmapShader::setupProgram,
150 SkiaGradientShader::setupProgram,
151 SkiaComposeShader::setupProgram,
152 SkiaLayerShader::setupProgram,
153};
154
155void SkiaShader::describe(Caches* caches, ProgramDescription& description,
156 const Extensions& extensions, const SkShader& shader) {
157 gDescribeProc[getType(shader)](caches, description, extensions, shader);
158}
159
160void SkiaShader::setupProgram(Caches* caches, const mat4& modelViewMatrix,
161 GLuint* textureUnit, const Extensions& extensions, const SkShader& shader) {
162
163 gSetupProgramProc[getType(shader)](caches, modelViewMatrix, textureUnit, extensions, shader);
Romain Guy14830942010-10-07 15:07:45 -0700164}
165
Romain Guy06f96e22010-07-30 19:18:16 -0700166///////////////////////////////////////////////////////////////////////////////
Chris Craik3f0854292014-04-15 16:18:08 -0700167// Layer shader
168///////////////////////////////////////////////////////////////////////////////
169
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400170void SkiaLayerShader::describe(Caches*, ProgramDescription& description,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000171 const Extensions&, const SkShader& shader) {
Chris Craik3f0854292014-04-15 16:18:08 -0700172 description.hasBitmap = true;
173}
174
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400175void SkiaLayerShader::setupProgram(Caches* caches, const mat4& modelViewMatrix,
176 GLuint* textureUnit, const Extensions&, const SkShader& shader) {
177 Layer* layer;
178 if (!shader.asACustomShader(reinterpret_cast<void**>(&layer))) {
179 LOG_ALWAYS_FATAL("SkiaLayerShader::setupProgram called on the wrong type of shader!");
180 }
Chris Craik3f0854292014-04-15 16:18:08 -0700181
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400182 GLuint textureSlot = (*textureUnit)++;
Chris Craik44eb2c02015-01-29 09:45:09 -0800183 caches->textureState().activateTexture(textureSlot);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400184
185 const float width = layer->getWidth();
186 const float height = layer->getHeight();
Chris Craik3f0854292014-04-15 16:18:08 -0700187
188 mat4 textureTransform;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400189 computeScreenSpaceMatrix(textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
190 modelViewMatrix);
191
Chris Craik3f0854292014-04-15 16:18:08 -0700192
193 // Uniforms
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400194 layer->bindTexture();
195 layer->setWrap(GL_CLAMP_TO_EDGE);
196 layer->setFilter(GL_LINEAR);
Chris Craik3f0854292014-04-15 16:18:08 -0700197
Chris Craik6c15ffa2015-02-02 13:50:55 -0800198 Program& program = caches->program();
199 glUniform1i(program.getUniform("bitmapSampler"), textureSlot);
200 glUniformMatrix4fv(program.getUniform("textureTransform"), 1,
Chris Craik3f0854292014-04-15 16:18:08 -0700201 GL_FALSE, &textureTransform.data[0]);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800202 glUniform2f(program.getUniform("textureDimension"), 1.0f / width, 1.0f / height);
Chris Craik3f0854292014-04-15 16:18:08 -0700203}
204
205///////////////////////////////////////////////////////////////////////////////
Romain Guy06f96e22010-07-30 19:18:16 -0700206// Bitmap shader
207///////////////////////////////////////////////////////////////////////////////
208
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400209struct BitmapShaderInfo {
210 float width;
211 float height;
212 GLenum wrapS;
213 GLenum wrapT;
214 Texture* texture;
215};
Romain Guy06f96e22010-07-30 19:18:16 -0700216
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400217static bool bitmapShaderHelper(Caches* caches, ProgramDescription* description,
218 BitmapShaderInfo* shaderInfo,
219 const Extensions& extensions,
220 const SkBitmap& bitmap, SkShader::TileMode tileModes[2]) {
221 Texture* texture = caches->textureCache.get(&bitmap);
222 if (!texture) return false;
Romain Guy06f96e22010-07-30 19:18:16 -0700223
224 const float width = texture->width;
225 const float height = texture->height;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400226 GLenum wrapS, wrapT;
Romain Guy06f96e22010-07-30 19:18:16 -0700227
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400228 if (description) {
229 description->hasBitmap = true;
230 }
Romain Guy06f96e22010-07-30 19:18:16 -0700231 // The driver does not support non-power of two mirrored/repeated
232 // textures, so do it ourselves
Romain Guy61c8c9c2010-08-09 20:48:09 -0700233 if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) &&
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400234 (tileModes[0] != SkShader::kClamp_TileMode ||
235 tileModes[1] != SkShader::kClamp_TileMode)) {
236 if (description) {
237 description->isBitmapNpot = true;
238 description->bitmapWrapS = gTileModes[tileModes[0]];
239 description->bitmapWrapT = gTileModes[tileModes[1]];
240 }
241 wrapS = GL_CLAMP_TO_EDGE;
242 wrapT = GL_CLAMP_TO_EDGE;
Romain Guy29d89972010-09-22 16:10:57 -0700243 } else {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400244 wrapS = gTileModes[tileModes[0]];
245 wrapT = gTileModes[tileModes[1]];
Romain Guy06f96e22010-07-30 19:18:16 -0700246 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400247
248 if (shaderInfo) {
249 shaderInfo->width = width;
250 shaderInfo->height = height;
251 shaderInfo->wrapS = wrapS;
252 shaderInfo->wrapT = wrapT;
253 shaderInfo->texture = texture;
254 }
255 return true;
Romain Guy06f96e22010-07-30 19:18:16 -0700256}
257
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400258void SkiaBitmapShader::describe(Caches* caches, ProgramDescription& description,
259 const Extensions& extensions, const SkShader& shader) {
260 SkBitmap bitmap;
261 SkShader::TileMode xy[2];
Chris Craikd41c4d82015-01-05 15:51:13 -0800262 if (shader.asABitmap(&bitmap, nullptr, xy) != SkShader::kDefault_BitmapType) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400263 LOG_ALWAYS_FATAL("SkiaBitmapShader::describe called with a different kind of shader!");
264 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800265 bitmapShaderHelper(caches, &description, nullptr, extensions, bitmap, xy);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400266}
267
268void SkiaBitmapShader::setupProgram(Caches* caches, const mat4& modelViewMatrix,
269 GLuint* textureUnit, const Extensions& extensions, const SkShader& shader) {
270 SkBitmap bitmap;
271 SkShader::TileMode xy[2];
Chris Craikd41c4d82015-01-05 15:51:13 -0800272 if (shader.asABitmap(&bitmap, nullptr, xy) != SkShader::kDefault_BitmapType) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400273 LOG_ALWAYS_FATAL("SkiaBitmapShader::setupProgram called with a different kind of shader!");
274 }
275
Romain Guy06f96e22010-07-30 19:18:16 -0700276 GLuint textureSlot = (*textureUnit)++;
Chris Craik922d3a72015-02-13 17:47:21 -0800277 caches->textureState().activateTexture(textureSlot);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700278
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400279 BitmapShaderInfo shaderInfo;
Chris Craikd41c4d82015-01-05 15:51:13 -0800280 if (!bitmapShaderHelper(caches, nullptr, &shaderInfo, extensions, bitmap, xy)) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400281 return;
282 }
283
Chris Craik6c15ffa2015-02-02 13:50:55 -0800284 Program& program = caches->program();
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400285 Texture* texture = shaderInfo.texture;
286
Romain Guy9cccc2b2010-08-07 23:46:15 -0700287 const AutoTexture autoCleanup(texture);
Romain Guy06f96e22010-07-30 19:18:16 -0700288
Romain Guy06f96e22010-07-30 19:18:16 -0700289 mat4 textureTransform;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400290 computeScreenSpaceMatrix(textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
291 modelViewMatrix);
Romain Guy06f96e22010-07-30 19:18:16 -0700292
293 // Uniforms
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400294 bindTexture(caches, texture, shaderInfo.wrapS, shaderInfo.wrapT);
Romain Guyd21b6e12011-11-30 20:21:23 -0800295 texture->setFilter(GL_LINEAR);
Romain Guye3c26852011-07-25 16:36:01 -0700296
Chris Craik6c15ffa2015-02-02 13:50:55 -0800297 glUniform1i(program.getUniform("bitmapSampler"), textureSlot);
298 glUniformMatrix4fv(program.getUniform("textureTransform"), 1,
Romain Guy06f96e22010-07-30 19:18:16 -0700299 GL_FALSE, &textureTransform.data[0]);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800300 glUniform2f(program.getUniform("textureDimension"), 1.0f / shaderInfo.width,
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400301 1.0f / shaderInfo.height);
Romain Guy06f96e22010-07-30 19:18:16 -0700302}
303
304///////////////////////////////////////////////////////////////////////////////
305// Linear gradient shader
306///////////////////////////////////////////////////////////////////////////////
307
Romain Guye3095e02010-10-06 16:57:29 -0700308static void toUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
309 SkVector vec = pts[1] - pts[0];
310 const float mag = vec.length();
311 const float inv = mag ? 1.0f / mag : 0;
312
313 vec.scale(inv);
314 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
315 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
316 matrix->postScale(inv, inv);
317}
318
Romain Guy06f96e22010-07-30 19:18:16 -0700319///////////////////////////////////////////////////////////////////////////////
Romain Guyddb80be2010-09-20 19:04:33 -0700320// Circular gradient shader
321///////////////////////////////////////////////////////////////////////////////
322
Romain Guy14830942010-10-07 15:07:45 -0700323static void toCircularUnitMatrix(const float x, const float y, const float radius,
324 SkMatrix* matrix) {
325 const float inv = 1.0f / radius;
326 matrix->setTranslate(-x, -y);
327 matrix->postScale(inv, inv);
328}
329
Romain Guyddb80be2010-09-20 19:04:33 -0700330///////////////////////////////////////////////////////////////////////////////
Romain Guyee916f12010-09-20 17:53:08 -0700331// Sweep gradient shader
332///////////////////////////////////////////////////////////////////////////////
333
Romain Guy14830942010-10-07 15:07:45 -0700334static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
335 matrix->setTranslate(-x, -y);
336}
337
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400338///////////////////////////////////////////////////////////////////////////////
339// Common gradient code
340///////////////////////////////////////////////////////////////////////////////
Romain Guy14830942010-10-07 15:07:45 -0700341
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400342static bool isSimpleGradient(const SkShader::GradientInfo& gradInfo) {
343 return gradInfo.fColorCount == 2 && gradInfo.fTileMode == SkShader::kClamp_TileMode;
Romain Guyee916f12010-09-20 17:53:08 -0700344}
345
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400346void SkiaGradientShader::describe(Caches*, ProgramDescription& description,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000347 const Extensions& extensions, const SkShader& shader) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400348 SkShader::GradientInfo gradInfo;
349 gradInfo.fColorCount = 0;
Chris Craikd41c4d82015-01-05 15:51:13 -0800350 gradInfo.fColors = nullptr;
351 gradInfo.fColorOffsets = nullptr;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700352
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400353 switch (shader.asAGradient(&gradInfo)) {
354 case SkShader::kLinear_GradientType:
355 description.gradientType = ProgramDescription::kGradientLinear;
356 break;
357 case SkShader::kRadial_GradientType:
358 description.gradientType = ProgramDescription::kGradientCircular;
359 break;
360 case SkShader::kSweep_GradientType:
361 description.gradientType = ProgramDescription::kGradientSweep;
362 break;
363 default:
364 // Do nothing. This shader is unsupported.
365 return;
366 }
Romain Guyee916f12010-09-20 17:53:08 -0700367 description.hasGradient = true;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400368 description.isSimpleGradient = isSimpleGradient(gradInfo);
Romain Guyee916f12010-09-20 17:53:08 -0700369}
370
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400371void SkiaGradientShader::setupProgram(Caches* caches, const mat4& modelViewMatrix,
372 GLuint* textureUnit, const Extensions&, const SkShader& shader) {
373 // SkShader::GradientInfo.fColorCount is an in/out parameter. As input, it tells asAGradient
374 // how much space has been allocated for fColors and fColorOffsets. 10 was chosen
375 // arbitrarily, but should be >= 2.
376 // As output, it tells the number of actual colors/offsets in the gradient.
377 const int COLOR_COUNT = 10;
378 SkAutoSTMalloc<COLOR_COUNT, SkColor> colorStorage(COLOR_COUNT);
379 SkAutoSTMalloc<COLOR_COUNT, SkScalar> positionStorage(COLOR_COUNT);
Romain Guyee916f12010-09-20 17:53:08 -0700380
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400381 SkShader::GradientInfo gradInfo;
382 gradInfo.fColorCount = COLOR_COUNT;
383 gradInfo.fColors = colorStorage.get();
384 gradInfo.fColorOffsets = positionStorage.get();
385
386 SkShader::GradientType gradType = shader.asAGradient(&gradInfo);
387
Chris Craik6c15ffa2015-02-02 13:50:55 -0800388 Program& program = caches->program();
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400389 if (CC_UNLIKELY(!isSimpleGradient(gradInfo))) {
390 if (gradInfo.fColorCount > COLOR_COUNT) {
391 // There was not enough room in our arrays for all the colors and offsets. Try again,
392 // now that we know the true number of colors.
393 gradInfo.fColors = colorStorage.reset(gradInfo.fColorCount);
394 gradInfo.fColorOffsets = positionStorage.reset(gradInfo.fColorCount);
395
396 shader.asAGradient(&gradInfo);
397 }
398 GLuint textureSlot = (*textureUnit)++;
Chris Craik44eb2c02015-01-29 09:45:09 -0800399 caches->textureState().activateTexture(textureSlot);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400400
401#ifndef SK_SCALAR_IS_FLOAT
402 #error Need to convert gradInfo.fColorOffsets to float!
403#endif
404 Texture* texture = caches->gradientCache.get(gradInfo.fColors, gradInfo.fColorOffsets,
405 gradInfo.fColorCount);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700406
407 // Uniforms
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400408 bindTexture(caches, texture, gTileModes[gradInfo.fTileMode], gTileModes[gradInfo.fTileMode]);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800409 glUniform1i(program.getUniform("gradientSampler"), textureSlot);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700410 } else {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800411 bindUniformColor(program.getUniform("startColor"), gradInfo.fColors[0]);
412 bindUniformColor(program.getUniform("endColor"), gradInfo.fColors[1]);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700413 }
Romain Guyee916f12010-09-20 17:53:08 -0700414
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400415 caches->dither.setupProgram(program, textureUnit);
416
417 SkMatrix unitMatrix;
418 switch (gradType) {
419 case SkShader::kLinear_GradientType:
420 toUnitMatrix(gradInfo.fPoint, &unitMatrix);
421 break;
422 case SkShader::kRadial_GradientType:
423 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
424 gradInfo.fRadius[0], &unitMatrix);
425 break;
426 case SkShader::kSweep_GradientType:
427 toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
428 break;
429 default:
430 LOG_ALWAYS_FATAL("Invalid SkShader gradient type %d", gradType);
431 }
Romain Guy211efea2012-07-31 21:16:07 -0700432
Romain Guy14830942010-10-07 15:07:45 -0700433 mat4 screenSpace;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400434 computeScreenSpaceMatrix(screenSpace, unitMatrix, shader.getLocalMatrix(), modelViewMatrix);
Chris Craik6c15ffa2015-02-02 13:50:55 -0800435 glUniformMatrix4fv(program.getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
Romain Guyee916f12010-09-20 17:53:08 -0700436}
437
Romain Guyee916f12010-09-20 17:53:08 -0700438///////////////////////////////////////////////////////////////////////////////
Romain Guy06f96e22010-07-30 19:18:16 -0700439// Compose shader
440///////////////////////////////////////////////////////////////////////////////
441
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400442void SkiaComposeShader::describe(Caches* caches, ProgramDescription& description,
443 const Extensions& extensions, const SkShader& shader) {
444 SkShader::ComposeRec rec;
445 if (!shader.asACompose(&rec)) {
446 LOG_ALWAYS_FATAL("SkiaComposeShader::describe called on the wrong shader type!");
Romain Guy43ccf462011-01-14 18:51:01 -0800447 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400448 SkiaShader::describe(caches, description, extensions, *rec.fShaderA);
449 SkiaShader::describe(caches, description, extensions, *rec.fShaderB);
450 if (SkiaShader::getType(*rec.fShaderA) == kBitmap_SkiaShaderType) {
Romain Guy06f96e22010-07-30 19:18:16 -0700451 description.isBitmapFirst = true;
452 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400453 if (!SkXfermode::AsMode(rec.fMode, &description.shadersMode)) {
454 // TODO: Support other modes.
455 description.shadersMode = SkXfermode::kSrcOver_Mode;
456 }
Romain Guy06f96e22010-07-30 19:18:16 -0700457}
458
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400459void SkiaComposeShader::setupProgram(Caches* caches, const mat4& modelViewMatrix,
460 GLuint* textureUnit, const Extensions& extensions, const SkShader& shader) {
461 SkShader::ComposeRec rec;
462 if (!shader.asACompose(&rec)) {
463 LOG_ALWAYS_FATAL("SkiaComposeShader::setupProgram called on the wrong shader type!");
464 }
465
Romain Guyd42899222013-03-18 19:30:48 -0700466 // Apply this compose shader's local transform and pass it down to
467 // the child shaders. They will in turn apply their local transform
468 // to this matrix.
469 mat4 transform;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400470 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(),
471 modelViewMatrix);
Romain Guyd42899222013-03-18 19:30:48 -0700472
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400473 SkiaShader::setupProgram(caches, transform, textureUnit, extensions, *rec.fShaderA);
474 SkiaShader::setupProgram(caches, transform, textureUnit, extensions, *rec.fShaderB);
Romain Guy06f96e22010-07-30 19:18:16 -0700475}
476
Chris Craik922d3a72015-02-13 17:47:21 -0800477///////////////////////////////////////////////////////////////////////////////
478// Store / apply
479///////////////////////////////////////////////////////////////////////////////
480
481bool tryStoreGradient(Caches& caches, const SkShader& shader, const Matrix4 modelViewMatrix,
482 GLuint* textureUnit, ProgramDescription* description,
483 SkiaShaderData::GradientShaderData* outData) {
484 SkShader::GradientInfo gradInfo;
485 gradInfo.fColorCount = 0;
486 gradInfo.fColors = nullptr;
487 gradInfo.fColorOffsets = nullptr;
488
489 SkMatrix unitMatrix;
490 switch (shader.asAGradient(&gradInfo)) {
491 case SkShader::kLinear_GradientType:
492 description->gradientType = ProgramDescription::kGradientLinear;
493
494 toUnitMatrix(gradInfo.fPoint, &unitMatrix);
495 break;
496 case SkShader::kRadial_GradientType:
497 description->gradientType = ProgramDescription::kGradientCircular;
498
499 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
500 gradInfo.fRadius[0], &unitMatrix);
501 break;
502 case SkShader::kSweep_GradientType:
503 description->gradientType = ProgramDescription::kGradientSweep;
504
505 toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
506 break;
507 default:
508 // Do nothing. This shader is unsupported.
509 return false;
510 }
511 description->hasGradient = true;
512 description->isSimpleGradient = isSimpleGradient(gradInfo);
513
514 computeScreenSpaceMatrix(outData->screenSpace, unitMatrix,
515 shader.getLocalMatrix(), modelViewMatrix);
516
517 // re-query shader to get full color / offset data
518 std::unique_ptr<SkColor[]> colorStorage(new SkColor[gradInfo.fColorCount]);
519 std::unique_ptr<SkScalar[]> colorOffsets(new SkScalar[gradInfo.fColorCount]);
520 gradInfo.fColors = &colorStorage[0];
521 gradInfo.fColorOffsets = &colorOffsets[0];
522 shader.asAGradient(&gradInfo);
523
524 if (CC_UNLIKELY(!isSimpleGradient(gradInfo))) {
525 outData->gradientSampler = (*textureUnit)++;
526
527#ifndef SK_SCALAR_IS_FLOAT
528 #error Need to convert gradInfo.fColorOffsets to float!
529#endif
530 outData->gradientTexture = caches.gradientCache.get(
531 gradInfo.fColors, gradInfo.fColorOffsets, gradInfo.fColorCount);
532 outData->wrapST = gTileModes[gradInfo.fTileMode];
533 } else {
534 outData->gradientSampler = 0;
535 outData->gradientTexture = nullptr;
536
537 outData->startColor.set(gradInfo.fColors[0]);
538 outData->endColor.set(gradInfo.fColors[1]);
539 }
540
541 outData->ditherSampler = (*textureUnit)++;
542 return true;
543}
544
545void applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data) {
546 if (CC_UNLIKELY(data.gradientTexture)) {
547 caches.textureState().activateTexture(data.gradientSampler);
548 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
549 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
550 } else {
551 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
552 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
553 }
554
555 // TODO: remove sampler slot incrementing from dither.setupProgram,
556 // since this assignment of slots is done at store, not apply time
557 GLuint ditherSampler = data.ditherSampler;
558 caches.dither.setupProgram(caches.program(), &ditherSampler);
559 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1,
560 GL_FALSE, &data.screenSpace.data[0]);
561}
562
563bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
564 GLuint* textureUnit, ProgramDescription* description,
565 SkiaShaderData::BitmapShaderData* outData) {
566 SkBitmap bitmap;
567 SkShader::TileMode xy[2];
568 if (shader.asABitmap(&bitmap, nullptr, xy) != SkShader::kDefault_BitmapType) {
569 return false;
570 }
571
572 outData->bitmapTexture = caches.textureCache.get(&bitmap);
573 if (!outData->bitmapTexture) return false;
574
575 outData->bitmapSampler = (*textureUnit)++;
576
577 const float width = outData->bitmapTexture->width;
578 const float height = outData->bitmapTexture->height;
579
580 description->hasBitmap = true;
581 if (!caches.extensions().hasNPot()
582 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))
583 && (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode)) {
584 description->isBitmapNpot = true;
585 description->bitmapWrapS = gTileModes[xy[0]];
586 description->bitmapWrapT = gTileModes[xy[1]];
587
588 outData->wrapS = GL_CLAMP_TO_EDGE;
589 outData->wrapT = GL_CLAMP_TO_EDGE;
590 } else {
591 outData->wrapS = gTileModes[xy[0]];
592 outData->wrapT = gTileModes[xy[1]];
593 }
594
595 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
596 modelViewMatrix);
597 outData->textureDimension[0] = 1.0f / width;
598 outData->textureDimension[1] = 1.0f / height;
599
600 return true;
601}
602
603void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
604 caches.textureState().activateTexture(data.bitmapSampler);
605 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
606 data.bitmapTexture->setFilter(GL_LINEAR);
607
608 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
609 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
610 &data.textureTransform.data[0]);
611 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
612}
613
614SkiaShaderType getComposeSubType(const SkShader& shader) {
615 // First check for a gradient shader.
616 switch (shader.asAGradient(nullptr)) {
617 case SkShader::kNone_GradientType:
618 // Not a gradient shader. Fall through to check for other types.
619 break;
620 case SkShader::kLinear_GradientType:
621 case SkShader::kRadial_GradientType:
622 case SkShader::kSweep_GradientType:
623 return kGradient_SkiaShaderType;
624 default:
625 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
626 return kNone_SkiaShaderType;
627 }
628
629 // The shader is not a gradient. Check for a bitmap shader.
630 if (shader.asABitmap(nullptr, nullptr, nullptr) == SkShader::kDefault_BitmapType) {
631 return kBitmap_SkiaShaderType;
632 }
633 return kNone_SkiaShaderType;
634}
635
636void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
637 const Matrix4& modelViewMatrix, GLuint* textureUnit,
638 ProgramDescription* description, SkiaShaderData* outData) {
639 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix,
640 textureUnit, description, &outData->bitmapData),
641 "failed storing bitmap shader data");
642 LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix,
643 textureUnit, description, &outData->gradientData),
644 "failing storing gradient shader data");
645}
646
647bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
648 GLuint* textureUnit, ProgramDescription* description,
649 SkiaShaderData* outData) {
650
651 SkShader::ComposeRec rec;
652 if (!shader.asACompose(&rec)) return false;
653
654 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
655 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
656
657 // check that type enum values are the 2 flags that compose the kCompose value
658 if ((shaderAType & shaderBType) != 0) return false;
659 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
660
661 mat4 transform;
662 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
663 if (shaderAType == kBitmap_SkiaShaderType) {
664 description->isBitmapFirst = true;
665 storeCompose(caches, *rec.fShaderA, *rec.fShaderB,
666 transform, textureUnit, description, outData);
667 } else {
668 description->isBitmapFirst = false;
669 storeCompose(caches, *rec.fShaderB, *rec.fShaderA,
670 transform, textureUnit, description, outData);
671 }
672 if (!SkXfermode::AsMode(rec.fMode, &description->shadersMode)) {
673 // TODO: Support other modes.
674 description->shadersMode = SkXfermode::kSrcOver_Mode;
675 }
676 return true;
677}
678
679bool tryStoreLayer(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
680 GLuint* textureUnit, ProgramDescription* description,
681 SkiaShaderData::LayerShaderData* outData) {
682 Layer* layer;
683 if (!shader.asACustomShader(reinterpret_cast<void**>(&layer))) {
684 return false;
685 }
686
687 description->hasBitmap = true;
688
689 outData->bitmapSampler = (*textureUnit)++;
690
691 const float width = layer->getWidth();
692 const float height = layer->getHeight();
693
694 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
695 modelViewMatrix);
696
697 outData->textureDimension[0] = 1.0f / width;
698 outData->textureDimension[1] = 1.0f / height;
699 return true;
700}
701
702void applyLayer(Caches& caches, const SkiaShaderData::LayerShaderData& data) {
703 caches.textureState().activateTexture(data.bitmapSampler);
704
705 data.layer->bindTexture();
706 data.layer->setWrap(GL_CLAMP_TO_EDGE);
707 data.layer->setFilter(GL_LINEAR);
708
709 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
710 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1,
711 GL_FALSE, &data.textureTransform.data[0]);
712 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
713}
714
715void SkiaShader::store(Caches& caches, const SkShader* shader, const Matrix4& modelViewMatrix,
716 GLuint* textureUnit, ProgramDescription* description,
717 SkiaShaderData* outData) {
718 if (!shader) {
719 outData->skiaShaderType = kNone_SkiaShaderType;
720 return;
721 }
722
723 if (tryStoreGradient(caches, *shader, modelViewMatrix,
724 textureUnit, description, &outData->gradientData)) {
725 outData->skiaShaderType = kGradient_SkiaShaderType;
726 return;
727 }
728
729 if (tryStoreBitmap(caches, *shader, modelViewMatrix,
730 textureUnit, description, &outData->bitmapData)) {
731 outData->skiaShaderType = kBitmap_SkiaShaderType;
732 return;
733 }
734
735 if (tryStoreCompose(caches, *shader, modelViewMatrix,
736 textureUnit, description, outData)) {
737 outData->skiaShaderType = kCompose_SkiaShaderType;
738 return;
739 }
740
741 if (tryStoreLayer(caches, *shader, modelViewMatrix,
742 textureUnit, description, &outData->layerData)) {
743 outData->skiaShaderType = kLayer_SkiaShaderType;
744 }
745}
746
747void SkiaShader::apply(Caches& caches, const SkiaShaderData& data) {
748 if (!data.skiaShaderType) return;
749
750 if (data.skiaShaderType & kGradient_SkiaShaderType) {
751 applyGradient(caches, data.gradientData);
752 }
753 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
754 applyBitmap(caches, data.bitmapData);
755 }
756
757 if (data.skiaShaderType == kLayer_SkiaShaderType) {
758 applyLayer(caches, data.layerData);
759 }
760}
761
Romain Guy06f96e22010-07-30 19:18:16 -0700762}; // namespace uirenderer
763}; // namespace android