blob: 165c0da815defc655cec435ccfdacf50dea9692c [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
23#include "SkiaShader.h"
24#include "Texture.h"
25#include "Matrix.h"
26
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Support
32///////////////////////////////////////////////////////////////////////////////
33
34static const GLenum gTextureUnitsMap[] = {
35 GL_TEXTURE0,
36 GL_TEXTURE1,
37 GL_TEXTURE2
38};
39
40static const GLint gTileModes[] = {
41 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
42 GL_REPEAT, // == SkShader::kRepeat_Mode
43 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
44};
45
46///////////////////////////////////////////////////////////////////////////////
47// Base shader
48///////////////////////////////////////////////////////////////////////////////
49
50SkiaShader::SkiaShader(Type type, SkShader* key, SkShader::TileMode tileX,
51 SkShader::TileMode tileY, SkMatrix* matrix, bool blend):
52 mType(type), mKey(key), mTileX(tileX), mTileY(tileY), mMatrix(matrix), mBlend(blend) {
53}
54
55SkiaShader::~SkiaShader() {
56}
57
58void SkiaShader::describe(ProgramDescription& description, const Extensions& extensions) {
59}
60
61void SkiaShader::setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
62 GLuint* textureUnit) {
63}
64
65void SkiaShader::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
66 glActiveTexture(gTextureUnitsMap[textureUnit]);
67 glBindTexture(GL_TEXTURE_2D, texture);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
70}
71
72///////////////////////////////////////////////////////////////////////////////
73// Bitmap shader
74///////////////////////////////////////////////////////////////////////////////
75
76SkiaBitmapShader::SkiaBitmapShader(SkBitmap* bitmap, SkShader* key, SkShader::TileMode tileX,
77 SkShader::TileMode tileY, SkMatrix* matrix, bool blend):
Romain Guy9cccc2b2010-08-07 23:46:15 -070078 SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap), mTexture(NULL) {
Romain Guy06f96e22010-07-30 19:18:16 -070079}
80
Romain Guy06f96e22010-07-30 19:18:16 -070081void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) {
82 const Texture* texture = mTextureCache->get(mBitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -070083 if (!texture) return;
84 mTexture = texture;
Romain Guy06f96e22010-07-30 19:18:16 -070085
86 const float width = texture->width;
87 const float height = texture->height;
88
89 description.hasBitmap = true;
90 // The driver does not support non-power of two mirrored/repeated
91 // textures, so do it ourselves
Romain Guy61c8c9c2010-08-09 20:48:09 -070092 if (!extensions.hasNPot() && (!isPowerOfTwo(width) || !isPowerOfTwo(height)) &&
93 (mTileX != SkShader::kClamp_TileMode || mTileY != SkShader::kClamp_TileMode)) {
Romain Guy06f96e22010-07-30 19:18:16 -070094 description.isBitmapNpot = true;
95 description.bitmapWrapS = gTileModes[mTileX];
96 description.bitmapWrapT = gTileModes[mTileY];
Romain Guy29d89972010-09-22 16:10:57 -070097 mWrapS = GL_CLAMP_TO_EDGE;
98 mWrapT = GL_CLAMP_TO_EDGE;
99 } else {
100 mWrapS = gTileModes[mTileX];
101 mWrapT = gTileModes[mTileY];
Romain Guy06f96e22010-07-30 19:18:16 -0700102 }
103}
104
105void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
106 const Snapshot& snapshot, GLuint* textureUnit) {
107 GLuint textureSlot = (*textureUnit)++;
108 glActiveTexture(gTextureUnitsMap[textureSlot]);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700109
110 const Texture* texture = mTexture;
111 mTexture = NULL;
112 if (!texture) return;
113 const AutoTexture autoCleanup(texture);
Romain Guy06f96e22010-07-30 19:18:16 -0700114
115 const float width = texture->width;
116 const float height = texture->height;
117
118 mat4 textureTransform;
119 if (mMatrix) {
120 SkMatrix inverse;
121 mMatrix->invert(&inverse);
122 textureTransform.load(inverse);
123 textureTransform.multiply(modelView);
124 } else {
125 textureTransform.load(modelView);
126 }
127
128 // Uniforms
Romain Guy29d89972010-09-22 16:10:57 -0700129 bindTexture(texture->id, mWrapS, mWrapT, textureSlot);
Romain Guy06f96e22010-07-30 19:18:16 -0700130 glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
131 glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
132 GL_FALSE, &textureTransform.data[0]);
133 glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
134}
135
Romain Guy759ea802010-09-16 20:49:46 -0700136void SkiaBitmapShader::updateTransforms(Program* program, const mat4& modelView,
137 const Snapshot& snapshot) {
138 mat4 textureTransform;
139 if (mMatrix) {
140 SkMatrix inverse;
141 mMatrix->invert(&inverse);
142 textureTransform.load(inverse);
143 textureTransform.multiply(modelView);
144 } else {
145 textureTransform.load(modelView);
146 }
147
148 glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
149 GL_FALSE, &textureTransform.data[0]);
150}
151
Romain Guy06f96e22010-07-30 19:18:16 -0700152///////////////////////////////////////////////////////////////////////////////
153// Linear gradient shader
154///////////////////////////////////////////////////////////////////////////////
155
156SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors,
157 float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
158 SkMatrix* matrix, bool blend):
159 SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend),
160 mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) {
161}
162
163SkiaLinearGradientShader::~SkiaLinearGradientShader() {
Romain Guy25ee0372010-08-06 10:57:58 -0700164 delete[] mBounds;
165 delete[] mColors;
166 delete[] mPositions;
Romain Guy06f96e22010-07-30 19:18:16 -0700167}
168
169void SkiaLinearGradientShader::describe(ProgramDescription& description,
170 const Extensions& extensions) {
171 description.hasGradient = true;
Romain Guyee916f12010-09-20 17:53:08 -0700172 description.gradientType = ProgramDescription::kGradientLinear;
Romain Guy06f96e22010-07-30 19:18:16 -0700173}
174
175void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView,
176 const Snapshot& snapshot, GLuint* textureUnit) {
177 GLuint textureSlot = (*textureUnit)++;
178 glActiveTexture(gTextureUnitsMap[textureSlot]);
179
180 Texture* texture = mGradientCache->get(mKey);
181 if (!texture) {
Romain Guyee916f12010-09-20 17:53:08 -0700182 texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount, mTileX);
Romain Guy06f96e22010-07-30 19:18:16 -0700183 }
184
185 Rect start(mBounds[0], mBounds[1], mBounds[2], mBounds[3]);
186 if (mMatrix) {
187 mat4 shaderMatrix(*mMatrix);
Romain Guy0ba681b2010-08-12 15:37:00 -0700188 shaderMatrix.mapPoint(start.left, start.top);
189 shaderMatrix.mapPoint(start.right, start.bottom);
Romain Guy06f96e22010-07-30 19:18:16 -0700190 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700191 snapshot.transform->mapRect(start);
Romain Guy06f96e22010-07-30 19:18:16 -0700192
193 const float gradientX = start.right - start.left;
194 const float gradientY = start.bottom - start.top;
195
Romain Guy8aef54f2010-09-01 15:13:49 -0700196 mat4 screenSpace(*snapshot.transform);
Romain Guy06f96e22010-07-30 19:18:16 -0700197 screenSpace.multiply(modelView);
198
199 // Uniforms
200 bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
201 glUniform1i(program->getUniform("gradientSampler"), textureSlot);
202 glUniform2f(program->getUniform("gradientStart"), start.left, start.top);
203 glUniform2f(program->getUniform("gradient"), gradientX, gradientY);
204 glUniform1f(program->getUniform("gradientLength"),
205 1.0f / (gradientX * gradientX + gradientY * gradientY));
206 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
207}
208
Romain Guy759ea802010-09-16 20:49:46 -0700209void SkiaLinearGradientShader::updateTransforms(Program* program, const mat4& modelView,
210 const Snapshot& snapshot) {
211 mat4 screenSpace(*snapshot.transform);
212 screenSpace.multiply(modelView);
213 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
214}
215
Romain Guy06f96e22010-07-30 19:18:16 -0700216///////////////////////////////////////////////////////////////////////////////
Romain Guyddb80be2010-09-20 19:04:33 -0700217// Circular gradient shader
218///////////////////////////////////////////////////////////////////////////////
219
220SkiaCircularGradientShader::SkiaCircularGradientShader(float x, float y, float radius,
221 uint32_t* colors, float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
222 SkMatrix* matrix, bool blend):
223 SkiaSweepGradientShader(kCircularGradient, x, y, colors, positions, count, key,
224 tileMode, matrix, blend),
225 mRadius(radius) {
226}
227
228void SkiaCircularGradientShader::describe(ProgramDescription& description,
229 const Extensions& extensions) {
230 description.hasGradient = true;
231 description.gradientType = ProgramDescription::kGradientCircular;
232}
233
234void SkiaCircularGradientShader::setupProgram(Program* program, const mat4& modelView,
235 const Snapshot& snapshot, GLuint* textureUnit) {
236 SkiaSweepGradientShader::setupProgram(program, modelView, snapshot, textureUnit);
237 glUniform1f(program->getUniform("gradientRadius"), 1.0f / mRadius);
238}
239
240///////////////////////////////////////////////////////////////////////////////
Romain Guyee916f12010-09-20 17:53:08 -0700241// Sweep gradient shader
242///////////////////////////////////////////////////////////////////////////////
243
244SkiaSweepGradientShader::SkiaSweepGradientShader(float x, float y, uint32_t* colors,
245 float* positions, int count, SkShader* key, SkMatrix* matrix, bool blend):
246 SkiaShader(kSweepGradient, key, SkShader::kClamp_TileMode,
247 SkShader::kClamp_TileMode, matrix, blend),
248 mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) {
249}
250
Romain Guyddb80be2010-09-20 19:04:33 -0700251SkiaSweepGradientShader::SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors,
252 float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
253 SkMatrix* matrix, bool blend):
254 SkiaShader(type, key, tileMode, tileMode, matrix, blend),
255 mX(x), mY(y), mColors(colors), mPositions(positions), mCount(count) {
256}
257
Romain Guyee916f12010-09-20 17:53:08 -0700258SkiaSweepGradientShader::~SkiaSweepGradientShader() {
259 delete[] mColors;
260 delete[] mPositions;
261}
262
263void SkiaSweepGradientShader::describe(ProgramDescription& description,
264 const Extensions& extensions) {
265 description.hasGradient = true;
266 description.gradientType = ProgramDescription::kGradientSweep;
267}
268
269void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelView,
270 const Snapshot& snapshot, GLuint* textureUnit) {
271 GLuint textureSlot = (*textureUnit)++;
272 glActiveTexture(gTextureUnitsMap[textureSlot]);
273
274 Texture* texture = mGradientCache->get(mKey);
275 if (!texture) {
276 texture = mGradientCache->addLinearGradient(mKey, mColors, mPositions, mCount);
277 }
278
279 float left = mX;
280 float top = mY;
281
Romain Guyddb80be2010-09-20 19:04:33 -0700282 mat4 shaderMatrix;
Romain Guyee916f12010-09-20 17:53:08 -0700283 if (mMatrix) {
Romain Guyddb80be2010-09-20 19:04:33 -0700284 shaderMatrix.load(*mMatrix);
Romain Guyee916f12010-09-20 17:53:08 -0700285 shaderMatrix.mapPoint(left, top);
286 }
Romain Guyddb80be2010-09-20 19:04:33 -0700287
288 mat4 copy(shaderMatrix);
289 shaderMatrix.loadInverse(copy);
290
Romain Guyee916f12010-09-20 17:53:08 -0700291 snapshot.transform->mapPoint(left, top);
292
293 mat4 screenSpace(*snapshot.transform);
294 screenSpace.multiply(modelView);
295
296 // Uniforms
297 bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
298 glUniform1i(program->getUniform("gradientSampler"), textureSlot);
Romain Guyddb80be2010-09-20 19:04:33 -0700299 glUniformMatrix4fv(program->getUniform("gradientMatrix"), 1, GL_FALSE, &shaderMatrix.data[0]);
Romain Guyee916f12010-09-20 17:53:08 -0700300 glUniform2f(program->getUniform("gradientStart"), left, top);
301 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
302}
303
304void SkiaSweepGradientShader::updateTransforms(Program* program, const mat4& modelView,
305 const Snapshot& snapshot) {
306 mat4 screenSpace(*snapshot.transform);
307 screenSpace.multiply(modelView);
308 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
309}
310
311///////////////////////////////////////////////////////////////////////////////
Romain Guy06f96e22010-07-30 19:18:16 -0700312// Compose shader
313///////////////////////////////////////////////////////////////////////////////
314
315SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second,
316 SkXfermode::Mode mode, SkShader* key):
317 SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
318 NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) {
319}
320
Romain Guy06f96e22010-07-30 19:18:16 -0700321void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) {
322 SkiaShader::set(textureCache, gradientCache);
323 mFirst->set(textureCache, gradientCache);
324 mSecond->set(textureCache, gradientCache);
325}
326
327void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) {
328 mFirst->describe(description, extensions);
329 mSecond->describe(description, extensions);
330 if (mFirst->type() == kBitmap) {
331 description.isBitmapFirst = true;
332 }
333 description.shadersMode = mMode;
334}
335
336void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView,
337 const Snapshot& snapshot, GLuint* textureUnit) {
338 mFirst->setupProgram(program, modelView, snapshot, textureUnit);
339 mSecond->setupProgram(program, modelView, snapshot, textureUnit);
340}
341
342}; // namespace uirenderer
343}; // namespace android