blob: 9013fd5f9fed882e65a0bd2f7ba56b0a0eed569c [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
19#include <utils/Log.h>
20
21#include <SkMatrix.h>
22
Romain Guya1d3c912011-12-13 14:55:06 -080023#include "Caches.h"
Romain Guy06f96e22010-07-30 19:18:16 -070024#include "SkiaShader.h"
25#include "Texture.h"
26#include "Matrix.h"
27
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Support
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guy06f96e22010-07-30 19:18:16 -070035static const GLint gTileModes[] = {
36 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
37 GL_REPEAT, // == SkShader::kRepeat_Mode
38 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
39};
40
Romain Guy42e1e0d2012-07-30 14:47:51 -070041/**
42 * This function does not work for n == 0.
43 */
44static inline bool isPowerOfTwo(unsigned int n) {
45 return !(n & (n - 1));
46}
47
48static inline void bindUniformColor(int slot, uint32_t color) {
Romain Guyd679b572012-08-29 21:49:00 -070049 const float a = ((color >> 24) & 0xff) / 255.0f;
Romain Guy42e1e0d2012-07-30 14:47:51 -070050 glUniform4f(slot,
Romain Guyd679b572012-08-29 21:49:00 -070051 a * ((color >> 16) & 0xff) / 255.0f,
52 a * ((color >> 8) & 0xff) / 255.0f,
53 a * ((color ) & 0xff) / 255.0f,
54 a);
Romain Guy42e1e0d2012-07-30 14:47:51 -070055}
56
Romain Guy06f96e22010-07-30 19:18:16 -070057///////////////////////////////////////////////////////////////////////////////
58// Base shader
59///////////////////////////////////////////////////////////////////////////////
60
Romain Guy24c00212011-01-14 15:31:00 -080061void SkiaShader::copyFrom(const SkiaShader& shader) {
62 mType = shader.mType;
63 mKey = shader.mKey;
64 mTileX = shader.mTileX;
65 mTileY = shader.mTileY;
66 mBlend = shader.mBlend;
67 mUnitMatrix = shader.mUnitMatrix;
68 mShaderMatrix = shader.mShaderMatrix;
69 mGenerationId = shader.mGenerationId;
70}
71
Romain Guy06f96e22010-07-30 19:18:16 -070072SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX,
73 SkShader::TileMode tileY, SkMatrix* matrix, bool blend):
Romain Guy14830942010-10-07 15:07:45 -070074 mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mBlend(blend) {
75 setMatrix(matrix);
Romain Guy24c00212011-01-14 15:31:00 -080076 mGenerationId = 0;
Romain Guy06f96e22010-07-30 19:18:16 -070077}
78
79SkiaShader::~SkiaShader() {
80}
81
82void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) {
83}
84
85void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
86 GLuint* textureUnit) {
87}
88
Romain Guy01d06572010-11-11 12:06:27 -080089void SkiaShader::bindTexture(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -070090 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -080091 texture->setWrapST(wrapS, wrapT);
Romain Guy06f96e22010-07-30 19:18:16 -070092}
93
Romain Guy14830942010-10-07 15:07:45 -070094void SkiaShader::computeScreenSpaceMatrix(mat4& screenSpace, const mat4& modelView) {
95 screenSpace.loadMultiply(mUnitMatrix, mShaderMatrix);
96 screenSpace.multiply(modelView);
97}
98
Romain Guy06f96e22010-07-30 19:18:16 -070099///////////////////////////////////////////////////////////////////////////////
100// Bitmap shader
101///////////////////////////////////////////////////////////////////////////////
102
103SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX,
104 SkShader::TileMode tileY, SkMatrix* matrix, bool blend):
Romain Guy9cccc2b2010-08-07 23:46:15 -0700105 SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) {
Romain Guy14830942010-10-07 15:07:45 -0700106 updateLocalMatrix(matrix);
Romain Guy06f96e22010-07-30 19:18:16 -0700107}
108
Romain Guy24c00212011-01-14 15:31:00 -0800109SkiaShader* SkiaBitmapShader::copy() {
110 SkiaBitmapShader* copy = new SkiaBitmapShader();
111 copy->copyFrom(*this);
112 copy->mBitmap = mBitmap;
113 return copy;
114}
115
Romain Guy06f96e22010-07-30 19:18:16 -0700116void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) {
Romain Guy8164c2d2010-10-25 18:03:28 -0700117 Texture* texture = mTextureCache->get(mBitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700118 if (!texture) return;
119 mTexture = texture;
Romain Guy06f96e22010-07-30 19:18:16 -0700120
121 const float width = texture->width;
122 const float height = texture->height;
123
124 description.hasBitmap = true;
125 // The driver does not support non-power of two mirrored/repeated
126 // textures, so do it ourselves
Romain Guy61c8c9c2010-08-09 20:48:09 -0700127 if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) &&
128 (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) {
Romain Guy06f96e22010-07-30 19:18:16 -0700129 description.isBitmapNpot = true;
130 description.bitmapWrapS = gTileModes[mTileX];
131 description.bitmapWrapT = gTileModes[mTileY];
Romain Guy29d89972010-09-22 16:10:57 -0700132 mWrapS = GL_CLAMP_TO_EDGE;
133 mWrapT = GL_CLAMP_TO_EDGE;
134 } else {
135 mWrapS = gTileModes[mTileX];
136 mWrapT = gTileModes[mTileY];
Romain Guy06f96e22010-07-30 19:18:16 -0700137 }
138}
139
140void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
141 const Snapshot& snapshot, GLuint* textureUnit) {
142 GLuint textureSlot = (*textureUnit)++;
Romain Guya1d3c912011-12-13 14:55:06 -0800143 Caches::getInstance().activeTexture(textureSlot);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700144
Romain Guy8164c2d2010-10-25 18:03:28 -0700145 Texture* texture = mTexture;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700146 mTexture = NULL;
147 if (!texture) return;
148 const AutoTexture autoCleanup(texture);
Romain Guy06f96e22010-07-30 19:18:16 -0700149
150 const float width = texture->width;
151 const float height = texture->height;
152
153 mat4 textureTransform;
Romain Guy14830942010-10-07 15:07:45 -0700154 computeScreenSpaceMatrix(textureTransform, modelView);
Romain Guy06f96e22010-07-30 19:18:16 -0700155
156 // Uniforms
Romain Guy01d06572010-11-11 12:06:27 -0800157 bindTexture(texture, mWrapS, mWrapT);
Romain Guyd21b6e12011-11-30 20:21:23 -0800158 texture->setFilter(GL_LINEAR);
Romain Guye3c26852011-07-25 16:36:01 -0700159
Romain Guy06f96e22010-07-30 19:18:16 -0700160 glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
161 glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
162 GL_FALSE, &textureTransform.data[0]);
163 glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
164}
165
166///////////////////////////////////////////////////////////////////////////////
167// Linear gradient shader
168///////////////////////////////////////////////////////////////////////////////
169
Romain Guye3095e02010-10-06 16:57:29 -0700170static void toUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
171 SkVector vec = pts[1] - pts[0];
172 const float mag = vec.length();
173 const float inv = mag ? 1.0f / mag : 0;
174
175 vec.scale(inv);
176 matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
177 matrix->postTranslate(-pts[0].fX, -pts[0].fY);
178 matrix->postScale(inv, inv);
179}
180
Romain Guy06f96e22010-07-30 19:18:16 -0700181SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors,
182 float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
183 SkMatrix* matrix, bool blend):
184 SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend),
185 mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) {
Romain Guye3095e02010-10-06 16:57:29 -0700186 SkPoint points[2];
187 points[0].set(bounds[0], bounds[1]);
188 points[1].set(bounds[2], bounds[3]);
189
190 SkMatrix unitMatrix;
191 toUnitMatrix(points, &unitMatrix);
192 mUnitMatrix.load(unitMatrix);
193
194 updateLocalMatrix(matrix);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700195
196 mIsSimple = count == 2 && tileMode == SkShader::kClamp_TileMode;
Romain Guy06f96e22010-07-30 19:18:16 -0700197}
198
199SkiaLinearGradientShader::~SkiaLinearGradientShader() {
Romain Guy25ee0372010-08-06 10:57:58 -0700200 delete[] mBounds;
201 delete[] mColors;
202 delete[] mPositions;
Romain Guy06f96e22010-07-30 19:18:16 -0700203}
204
Romain Guy24c00212011-01-14 15:31:00 -0800205SkiaShader* SkiaLinearGradientShader::copy() {
206 SkiaLinearGradientShader* copy = new SkiaLinearGradientShader();
207 copy->copyFrom(*this);
Romain Guy43ccf462011-01-14 18:51:01 -0800208 copy->mBounds = new float[4];
209 memcpy(copy->mBounds, mBounds, sizeof(float) * 4);
210 copy->mColors = new uint32_t[mCount];
211 memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount);
212 copy->mPositions = new float[mCount];
213 memcpy(copy->mPositions, mPositions, sizeof(float) * mCount);
Romain Guy24c00212011-01-14 15:31:00 -0800214 copy->mCount = mCount;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700215 copy->mIsSimple = mIsSimple;
Romain Guy24c00212011-01-14 15:31:00 -0800216 return copy;
217}
218
Romain Guy06f96e22010-07-30 19:18:16 -0700219void SkiaLinearGradientShader::describe(ProgramDescription& description,
220 const Extensions& extensions) {
221 description.hasGradient = true;
Romain Guyee916f12010-09-20 17:53:08 -0700222 description.gradientType = ProgramDescription::kGradientLinear;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700223 description.isSimpleGradient = mIsSimple;
Romain Guy06f96e22010-07-30 19:18:16 -0700224}
225
226void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView,
227 const Snapshot& snapshot, GLuint* textureUnit) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700228 if (CC_UNLIKELY(!mIsSimple)) {
229 GLuint textureSlot = (*textureUnit)++;
230 Caches::getInstance().activeTexture(textureSlot);
Romain Guy06f96e22010-07-30 19:18:16 -0700231
Romain Guy42e1e0d2012-07-30 14:47:51 -0700232 Texture* texture = mGradientCache->get(mColors, mPositions, mCount);
233
234 // Uniforms
235 bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]);
236 glUniform1i(program->getUniform("gradientSampler"), textureSlot);
237 } else {
238 bindUniformColor(program->getUniform("startColor"), mColors[0]);
239 bindUniformColor(program->getUniform("endColor"), mColors[1]);
240 }
Romain Guy06f96e22010-07-30 19:18:16 -0700241
Romain Guy211efea2012-07-31 21:16:07 -0700242 Caches::getInstance().dither.setupProgram(program, textureUnit);
243
Romain Guye3095e02010-10-06 16:57:29 -0700244 mat4 screenSpace;
245 computeScreenSpaceMatrix(screenSpace, modelView);
Romain Guy06f96e22010-07-30 19:18:16 -0700246 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
247}
248
249///////////////////////////////////////////////////////////////////////////////
Romain Guyddb80be2010-09-20 19:04:33 -0700250// Circular gradient shader
251///////////////////////////////////////////////////////////////////////////////
252
Romain Guy14830942010-10-07 15:07:45 -0700253static void toCircularUnitMatrix(const float x, const float y, const float radius,
254 SkMatrix* matrix) {
255 const float inv = 1.0f / radius;
256 matrix->setTranslate(-x, -y);
257 matrix->postScale(inv, inv);
258}
259
Romain Guyddb80be2010-09-20 19:04:33 -0700260SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius,
261 uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
262 SkMatrix* matrix, bool blend):
263 SkiaSweepGradientShader(kCircularGradient, x, y, colors, positions, count, key,
Romain Guy14830942010-10-07 15:07:45 -0700264 tileMode, matrix, blend) {
265 SkMatrix unitMatrix;
266 toCircularUnitMatrix(x, y, radius, &unitMatrix);
267 mUnitMatrix.load(unitMatrix);
268
269 updateLocalMatrix(matrix);
Romain Guyddb80be2010-09-20 19:04:33 -0700270}
271
Romain Guy24c00212011-01-14 15:31:00 -0800272SkiaShader* SkiaCircularGradientShader::copy() {
273 SkiaCircularGradientShader* copy = new SkiaCircularGradientShader();
274 copy->copyFrom(*this);
Romain Guy43ccf462011-01-14 18:51:01 -0800275 copy->mColors = new uint32_t[mCount];
276 memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount);
277 copy->mPositions = new float[mCount];
278 memcpy(copy->mPositions, mPositions, sizeof(float) * mCount);
Romain Guy24c00212011-01-14 15:31:00 -0800279 copy->mCount = mCount;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700280 copy->mIsSimple = mIsSimple;
Romain Guy24c00212011-01-14 15:31:00 -0800281 return copy;
282}
283
Romain Guyddb80be2010-09-20 19:04:33 -0700284void SkiaCircularGradientShader::describe(ProgramDescription& description,
285 const Extensions& extensions) {
286 description.hasGradient = true;
287 description.gradientType = ProgramDescription::kGradientCircular;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700288 description.isSimpleGradient = mIsSimple;
Romain Guyddb80be2010-09-20 19:04:33 -0700289}
290
Romain Guyddb80be2010-09-20 19:04:33 -0700291///////////////////////////////////////////////////////////////////////////////
Romain Guyee916f12010-09-20 17:53:08 -0700292// Sweep gradient shader
293///////////////////////////////////////////////////////////////////////////////
294
Romain Guy14830942010-10-07 15:07:45 -0700295static void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
296 matrix->setTranslate(-x, -y);
297}
298
Romain Guyee916f12010-09-20 17:53:08 -0700299SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors,
300 float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend):
301 SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode,
302 SkShader::kClamp_TileMode, matrix, blend),
Romain Guy14830942010-10-07 15:07:45 -0700303 mColors(colors), mPositions(positions), mCount(count) {
304 SkMatrix unitMatrix;
305 toSweepUnitMatrix(x, y, &unitMatrix);
306 mUnitMatrix.load(unitMatrix);
307
308 updateLocalMatrix(matrix);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700309
310 mIsSimple = count == 2;
Romain Guyee916f12010-09-20 17:53:08 -0700311}
312
Romain Guyddb80be2010-09-20 19:04:33 -0700313SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors,
314 float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
315 SkMatrix* matrix, bool blend):
316 SkiaShader(type, key, tileMode, tileMode, matrix, blend),
Romain Guy14830942010-10-07 15:07:45 -0700317 mColors(colors), mPositions(positions), mCount(count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700318
319 mIsSimple = count == 2 && tileMode == SkShader::kClamp_TileMode;
Romain Guyddb80be2010-09-20 19:04:33 -0700320}
321
Romain Guyee916f12010-09-20 17:53:08 -0700322SkiaSweepGradientShader::~SkiaSweepGradientShader() {
323 delete[] mColors;
324 delete[] mPositions;
325}
326
Romain Guy24c00212011-01-14 15:31:00 -0800327SkiaShader* SkiaSweepGradientShader::copy() {
328 SkiaSweepGradientShader* copy = new SkiaSweepGradientShader();
329 copy->copyFrom(*this);
Romain Guy43ccf462011-01-14 18:51:01 -0800330 copy->mColors = new uint32_t[mCount];
331 memcpy(copy->mColors, mColors, sizeof(uint32_t) * mCount);
332 copy->mPositions = new float[mCount];
333 memcpy(copy->mPositions, mPositions, sizeof(float) * mCount);
Romain Guy24c00212011-01-14 15:31:00 -0800334 copy->mCount = mCount;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700335 copy->mIsSimple = mIsSimple;
Romain Guy24c00212011-01-14 15:31:00 -0800336 return copy;
337}
338
Romain Guyee916f12010-09-20 17:53:08 -0700339void SkiaSweepGradientShader::describe(ProgramDescription& description,
340 const Extensions& extensions) {
341 description.hasGradient = true;
342 description.gradientType = ProgramDescription::kGradientSweep;
Romain Guy42e1e0d2012-07-30 14:47:51 -0700343 description.isSimpleGradient = mIsSimple;
Romain Guyee916f12010-09-20 17:53:08 -0700344}
345
346void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView,
347 const Snapshot& snapshot, GLuint* textureUnit) {
Romain Guy42e1e0d2012-07-30 14:47:51 -0700348 if (CC_UNLIKELY(!mIsSimple)) {
349 GLuint textureSlot = (*textureUnit)++;
350 Caches::getInstance().activeTexture(textureSlot);
Romain Guyee916f12010-09-20 17:53:08 -0700351
Romain Guy42e1e0d2012-07-30 14:47:51 -0700352 Texture* texture = mGradientCache->get(mColors, mPositions, mCount);
353
354 // Uniforms
355 bindTexture(texture, gTileModes[mTileX], gTileModes[mTileY]);
356 glUniform1i(program->getUniform("gradientSampler"), textureSlot);
357 } else {
358 bindUniformColor(program->getUniform("startColor"), mColors[0]);
359 bindUniformColor(program->getUniform("endColor"), mColors[1]);
360 }
Romain Guyee916f12010-09-20 17:53:08 -0700361
Romain Guy211efea2012-07-31 21:16:07 -0700362 Caches::getInstance().dither.setupProgram(program, textureUnit);
363
Romain Guy14830942010-10-07 15:07:45 -0700364 mat4 screenSpace;
365 computeScreenSpaceMatrix(screenSpace, modelView);
Romain Guyee916f12010-09-20 17:53:08 -0700366 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
367}
368
Romain Guyee916f12010-09-20 17:53:08 -0700369///////////////////////////////////////////////////////////////////////////////
Romain Guy06f96e22010-07-30 19:18:16 -0700370// Compose shader
371///////////////////////////////////////////////////////////////////////////////
372
373SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second,
374 SkXfermode::Mode mode, SkShader* key):
375 SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
Romain Guy43ccf462011-01-14 18:51:01 -0800376 NULL, first->blend() || second->blend()),
377 mFirst(first), mSecond(second), mMode(mode), mCleanup(false) {
378}
379
380SkiaComposeShader::~SkiaComposeShader() {
381 if (mCleanup) {
382 delete mFirst;
383 delete mSecond;
384 }
Romain Guy06f96e22010-07-30 19:18:16 -0700385}
386
Romain Guy24c00212011-01-14 15:31:00 -0800387SkiaShader* SkiaComposeShader::copy() {
388 SkiaComposeShader* copy = new SkiaComposeShader();
389 copy->copyFrom(*this);
Romain Guy43ccf462011-01-14 18:51:01 -0800390 copy->mFirst = mFirst->copy();
391 copy->mSecond = mSecond->copy();
Romain Guy24c00212011-01-14 15:31:00 -0800392 copy->mMode = mMode;
Romain Guy43ccf462011-01-14 18:51:01 -0800393 copy->cleanup();
Romain Guy24c00212011-01-14 15:31:00 -0800394 return copy;
395}
396
Romain Guy06f96e22010-07-30 19:18:16 -0700397void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) {
398 SkiaShader::set(textureCache, gradientCache);
399 mFirst->set(textureCache, gradientCache);
400 mSecond->set(textureCache, gradientCache);
401}
402
403void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) {
404 mFirst->describe(description, extensions);
405 mSecond->describe(description, extensions);
406 if (mFirst->type() == kBitmap) {
407 description.isBitmapFirst = true;
408 }
409 description.shadersMode = mMode;
410}
411
412void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView,
413 const Snapshot& snapshot, GLuint* textureUnit) {
414 mFirst->setupProgram(program, modelView, snapshot, textureUnit);
415 mSecond->setupProgram(program, modelView, snapshot, textureUnit);
416}
417
418}; // namespace uirenderer
419}; // namespace android