blob: ecf8b6ba82198a9c62c5265ebcb96352a7d4767d [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
Chris Craik922d3a72015-02-13 17:47:21 -080050static inline void bindUniformColor(int slot, FloatColor color) {
51 glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
52}
53
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040054static inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
Chris Craik44eb2c02015-01-29 09:45:09 -080055 caches->textureState().bindTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -080056 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070057}
58
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040059/**
60 * Compute the matrix to transform to screen space.
61 * @param screenSpace Output param for the computed matrix.
62 * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
63 * or identity.
64 * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
65 * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
66 */
67static void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
68 const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
69 mat4 shaderMatrix;
70 // uses implicit construction
71 shaderMatrix.loadInverse(localMatrix);
72 // again, uses implicit construction
73 screenSpace.loadMultiply(unitMatrix, shaderMatrix);
74 screenSpace.multiply(modelViewMatrix);
75}
76
Romain Guy06f96e22010-07-30 19:18:16 -070077///////////////////////////////////////////////////////////////////////////////
Chris Craik82840732015-04-03 09:37:49 -070078// gradient shader matrix helpers
Chris Craik3f0854292014-04-15 16:18:08 -070079///////////////////////////////////////////////////////////////////////////////
80
Chris Craik82840732015-04-03 09:37:49 -070081static void toLinearUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
Romain Guye3095e02010-10-06 16:57:29 -070082 SkVector vec = pts[1] - pts[0];
83 const float mag = vec.length();
84 const float inv = mag ? 1.0f / mag : 0;
85
86 vec.scale(inv);
87 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
88 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
89 matrix->postScale(inv, inv);
90}
91
Romain Guy14830942010-10-07 15:07:45 -070092static void toCircularUnitMatrix(const float x, const float y, const float radius,
93 SkMatrix* matrix) {
94 const float inv = 1.0f / radius;
95 matrix->setTranslate(-x, -y);
96 matrix->postScale(inv, inv);
97}
98
Romain Guy14830942010-10-07 15:07:45 -070099static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
100 matrix->setTranslate(-x, -y);
101}
102
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400103///////////////////////////////////////////////////////////////////////////////
104// Common gradient code
105///////////////////////////////////////////////////////////////////////////////
Romain Guy14830942010-10-07 15:07:45 -0700106
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -0400107static bool isSimpleGradient(const SkShader::GradientInfo& gradInfo) {
108 return gradInfo.fColorCount == 2 && gradInfo.fTileMode == SkShader::kClamp_TileMode;
Romain Guyee916f12010-09-20 17:53:08 -0700109}
110
Chris Craik922d3a72015-02-13 17:47:21 -0800111///////////////////////////////////////////////////////////////////////////////
112// Store / apply
113///////////////////////////////////////////////////////////////////////////////
114
115bool tryStoreGradient(Caches& caches, const SkShader& shader, const Matrix4 modelViewMatrix,
116 GLuint* textureUnit, ProgramDescription* description,
117 SkiaShaderData::GradientShaderData* outData) {
118 SkShader::GradientInfo gradInfo;
119 gradInfo.fColorCount = 0;
120 gradInfo.fColors = nullptr;
121 gradInfo.fColorOffsets = nullptr;
122
123 SkMatrix unitMatrix;
124 switch (shader.asAGradient(&gradInfo)) {
125 case SkShader::kLinear_GradientType:
126 description->gradientType = ProgramDescription::kGradientLinear;
127
Chris Craik82840732015-04-03 09:37:49 -0700128 toLinearUnitMatrix(gradInfo.fPoint, &unitMatrix);
Chris Craik922d3a72015-02-13 17:47:21 -0800129 break;
130 case SkShader::kRadial_GradientType:
131 description->gradientType = ProgramDescription::kGradientCircular;
132
133 toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
134 gradInfo.fRadius[0], &unitMatrix);
135 break;
136 case SkShader::kSweep_GradientType:
137 description->gradientType = ProgramDescription::kGradientSweep;
138
139 toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
140 break;
141 default:
142 // Do nothing. This shader is unsupported.
143 return false;
144 }
145 description->hasGradient = true;
146 description->isSimpleGradient = isSimpleGradient(gradInfo);
147
148 computeScreenSpaceMatrix(outData->screenSpace, unitMatrix,
149 shader.getLocalMatrix(), modelViewMatrix);
150
151 // re-query shader to get full color / offset data
152 std::unique_ptr<SkColor[]> colorStorage(new SkColor[gradInfo.fColorCount]);
153 std::unique_ptr<SkScalar[]> colorOffsets(new SkScalar[gradInfo.fColorCount]);
154 gradInfo.fColors = &colorStorage[0];
155 gradInfo.fColorOffsets = &colorOffsets[0];
156 shader.asAGradient(&gradInfo);
157
158 if (CC_UNLIKELY(!isSimpleGradient(gradInfo))) {
159 outData->gradientSampler = (*textureUnit)++;
160
161#ifndef SK_SCALAR_IS_FLOAT
162 #error Need to convert gradInfo.fColorOffsets to float!
163#endif
164 outData->gradientTexture = caches.gradientCache.get(
165 gradInfo.fColors, gradInfo.fColorOffsets, gradInfo.fColorCount);
166 outData->wrapST = gTileModes[gradInfo.fTileMode];
167 } else {
168 outData->gradientSampler = 0;
169 outData->gradientTexture = nullptr;
170
171 outData->startColor.set(gradInfo.fColors[0]);
172 outData->endColor.set(gradInfo.fColors[1]);
173 }
174
175 outData->ditherSampler = (*textureUnit)++;
176 return true;
177}
178
179void applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data) {
180 if (CC_UNLIKELY(data.gradientTexture)) {
181 caches.textureState().activateTexture(data.gradientSampler);
182 bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
183 glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
184 } else {
185 bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
186 bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
187 }
188
189 // TODO: remove sampler slot incrementing from dither.setupProgram,
190 // since this assignment of slots is done at store, not apply time
191 GLuint ditherSampler = data.ditherSampler;
192 caches.dither.setupProgram(caches.program(), &ditherSampler);
193 glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1,
194 GL_FALSE, &data.screenSpace.data[0]);
195}
196
197bool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
198 GLuint* textureUnit, ProgramDescription* description,
199 SkiaShaderData::BitmapShaderData* outData) {
200 SkBitmap bitmap;
201 SkShader::TileMode xy[2];
202 if (shader.asABitmap(&bitmap, nullptr, xy) != SkShader::kDefault_BitmapType) {
203 return false;
204 }
205
206 outData->bitmapTexture = caches.textureCache.get(&bitmap);
207 if (!outData->bitmapTexture) return false;
208
209 outData->bitmapSampler = (*textureUnit)++;
210
211 const float width = outData->bitmapTexture->width;
212 const float height = outData->bitmapTexture->height;
213
214 description->hasBitmap = true;
215 if (!caches.extensions().hasNPot()
216 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))
217 && (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode)) {
218 description->isBitmapNpot = true;
219 description->bitmapWrapS = gTileModes[xy[0]];
220 description->bitmapWrapT = gTileModes[xy[1]];
221
222 outData->wrapS = GL_CLAMP_TO_EDGE;
223 outData->wrapT = GL_CLAMP_TO_EDGE;
224 } else {
225 outData->wrapS = gTileModes[xy[0]];
226 outData->wrapT = gTileModes[xy[1]];
227 }
228
229 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
230 modelViewMatrix);
231 outData->textureDimension[0] = 1.0f / width;
232 outData->textureDimension[1] = 1.0f / height;
233
234 return true;
235}
236
237void applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
238 caches.textureState().activateTexture(data.bitmapSampler);
239 bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
240 data.bitmapTexture->setFilter(GL_LINEAR);
241
242 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
243 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
244 &data.textureTransform.data[0]);
245 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
246}
247
248SkiaShaderType getComposeSubType(const SkShader& shader) {
249 // First check for a gradient shader.
250 switch (shader.asAGradient(nullptr)) {
251 case SkShader::kNone_GradientType:
252 // Not a gradient shader. Fall through to check for other types.
253 break;
254 case SkShader::kLinear_GradientType:
255 case SkShader::kRadial_GradientType:
256 case SkShader::kSweep_GradientType:
257 return kGradient_SkiaShaderType;
258 default:
259 // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
260 return kNone_SkiaShaderType;
261 }
262
263 // The shader is not a gradient. Check for a bitmap shader.
264 if (shader.asABitmap(nullptr, nullptr, nullptr) == SkShader::kDefault_BitmapType) {
265 return kBitmap_SkiaShaderType;
266 }
267 return kNone_SkiaShaderType;
268}
269
270void storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
271 const Matrix4& modelViewMatrix, GLuint* textureUnit,
272 ProgramDescription* description, SkiaShaderData* outData) {
273 LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix,
274 textureUnit, description, &outData->bitmapData),
275 "failed storing bitmap shader data");
276 LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix,
277 textureUnit, description, &outData->gradientData),
278 "failing storing gradient shader data");
279}
280
281bool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
282 GLuint* textureUnit, ProgramDescription* description,
283 SkiaShaderData* outData) {
284
285 SkShader::ComposeRec rec;
286 if (!shader.asACompose(&rec)) return false;
287
288 const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
289 const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
290
291 // check that type enum values are the 2 flags that compose the kCompose value
292 if ((shaderAType & shaderBType) != 0) return false;
293 if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
294
295 mat4 transform;
296 computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
297 if (shaderAType == kBitmap_SkiaShaderType) {
298 description->isBitmapFirst = true;
299 storeCompose(caches, *rec.fShaderA, *rec.fShaderB,
300 transform, textureUnit, description, outData);
301 } else {
302 description->isBitmapFirst = false;
303 storeCompose(caches, *rec.fShaderB, *rec.fShaderA,
304 transform, textureUnit, description, outData);
305 }
306 if (!SkXfermode::AsMode(rec.fMode, &description->shadersMode)) {
307 // TODO: Support other modes.
308 description->shadersMode = SkXfermode::kSrcOver_Mode;
309 }
310 return true;
311}
312
313bool tryStoreLayer(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
314 GLuint* textureUnit, ProgramDescription* description,
315 SkiaShaderData::LayerShaderData* outData) {
316 Layer* layer;
317 if (!shader.asACustomShader(reinterpret_cast<void**>(&layer))) {
318 return false;
319 }
320
321 description->hasBitmap = true;
Chris Craik36a35e32015-02-18 09:24:33 -0800322 outData->layer = layer;
Chris Craik922d3a72015-02-13 17:47:21 -0800323 outData->bitmapSampler = (*textureUnit)++;
324
325 const float width = layer->getWidth();
326 const float height = layer->getHeight();
327
328 computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
329 modelViewMatrix);
330
331 outData->textureDimension[0] = 1.0f / width;
332 outData->textureDimension[1] = 1.0f / height;
333 return true;
334}
335
336void applyLayer(Caches& caches, const SkiaShaderData::LayerShaderData& data) {
337 caches.textureState().activateTexture(data.bitmapSampler);
338
339 data.layer->bindTexture();
340 data.layer->setWrap(GL_CLAMP_TO_EDGE);
341 data.layer->setFilter(GL_LINEAR);
342
343 glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
344 glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1,
345 GL_FALSE, &data.textureTransform.data[0]);
346 glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
347}
348
349void SkiaShader::store(Caches& caches, const SkShader* shader, const Matrix4& modelViewMatrix,
350 GLuint* textureUnit, ProgramDescription* description,
351 SkiaShaderData* outData) {
352 if (!shader) {
353 outData->skiaShaderType = kNone_SkiaShaderType;
354 return;
355 }
356
357 if (tryStoreGradient(caches, *shader, modelViewMatrix,
358 textureUnit, description, &outData->gradientData)) {
359 outData->skiaShaderType = kGradient_SkiaShaderType;
360 return;
361 }
362
363 if (tryStoreBitmap(caches, *shader, modelViewMatrix,
364 textureUnit, description, &outData->bitmapData)) {
365 outData->skiaShaderType = kBitmap_SkiaShaderType;
366 return;
367 }
368
369 if (tryStoreCompose(caches, *shader, modelViewMatrix,
370 textureUnit, description, outData)) {
371 outData->skiaShaderType = kCompose_SkiaShaderType;
372 return;
373 }
374
375 if (tryStoreLayer(caches, *shader, modelViewMatrix,
376 textureUnit, description, &outData->layerData)) {
377 outData->skiaShaderType = kLayer_SkiaShaderType;
378 }
379}
380
381void SkiaShader::apply(Caches& caches, const SkiaShaderData& data) {
382 if (!data.skiaShaderType) return;
383
384 if (data.skiaShaderType & kGradient_SkiaShaderType) {
385 applyGradient(caches, data.gradientData);
386 }
387 if (data.skiaShaderType & kBitmap_SkiaShaderType) {
388 applyBitmap(caches, data.bitmapData);
389 }
390
391 if (data.skiaShaderType == kLayer_SkiaShaderType) {
392 applyLayer(caches, data.layerData);
393 }
394}
395
Romain Guy06f96e22010-07-30 19:18:16 -0700396}; // namespace uirenderer
397}; // namespace android