blob: 42c0621787843ec034c8321e4020d84704f5a663 [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 Guy9cccc2b92010-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 Guy9cccc2b92010-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
92 if (!extensions.hasNPot() && !isPowerOfTwo(width) && !isPowerOfTwo(height)) {
93 description.isBitmapNpot = true;
94 description.bitmapWrapS = gTileModes[mTileX];
95 description.bitmapWrapT = gTileModes[mTileY];
96 }
97}
98
99void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
100 const Snapshot& snapshot, GLuint* textureUnit) {
101 GLuint textureSlot = (*textureUnit)++;
102 glActiveTexture(gTextureUnitsMap[textureSlot]);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700103
104 const Texture* texture = mTexture;
105 mTexture = NULL;
106 if (!texture) return;
107 const AutoTexture autoCleanup(texture);
Romain Guy06f96e22010-07-30 19:18:16 -0700108
109 const float width = texture->width;
110 const float height = texture->height;
111
112 mat4 textureTransform;
113 if (mMatrix) {
114 SkMatrix inverse;
115 mMatrix->invert(&inverse);
116 textureTransform.load(inverse);
117 textureTransform.multiply(modelView);
118 } else {
119 textureTransform.load(modelView);
120 }
121
122 // Uniforms
123 bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
124 glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
125 glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
126 GL_FALSE, &textureTransform.data[0]);
127 glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
128}
129
130///////////////////////////////////////////////////////////////////////////////
131// Linear gradient shader
132///////////////////////////////////////////////////////////////////////////////
133
134SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors,
135 float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
136 SkMatrix* matrix, bool blend):
137 SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend),
138 mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) {
139}
140
141SkiaLinearGradientShader::~SkiaLinearGradientShader() {
Romain Guy25ee0372010-08-06 10:57:58 -0700142 delete[] mBounds;
143 delete[] mColors;
144 delete[] mPositions;
Romain Guy06f96e22010-07-30 19:18:16 -0700145}
146
147void SkiaLinearGradientShader::describe(ProgramDescription& description,
148 const Extensions& extensions) {
149 description.hasGradient = true;
150}
151
152void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView,
153 const Snapshot& snapshot, GLuint* textureUnit) {
154 GLuint textureSlot = (*textureUnit)++;
155 glActiveTexture(gTextureUnitsMap[textureSlot]);
156
157 Texture* texture = mGradientCache->get(mKey);
158 if (!texture) {
159 texture = mGradientCache->addLinearGradient(mKey, mBounds, mColors, mPositions,
160 mCount, mTileX);
161 }
162
163 Rect start(mBounds[0], mBounds[1], mBounds[2], mBounds[3]);
164 if (mMatrix) {
165 mat4 shaderMatrix(*mMatrix);
166 shaderMatrix.mapRect(start);
167 }
168 snapshot.transform.mapRect(start);
169
170 const float gradientX = start.right - start.left;
171 const float gradientY = start.bottom - start.top;
172
173 mat4 screenSpace(snapshot.transform);
174 screenSpace.multiply(modelView);
175
176 // Uniforms
177 bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
178 glUniform1i(program->getUniform("gradientSampler"), textureSlot);
179 glUniform2f(program->getUniform("gradientStart"), start.left, start.top);
180 glUniform2f(program->getUniform("gradient"), gradientX, gradientY);
181 glUniform1f(program->getUniform("gradientLength"),
182 1.0f / (gradientX * gradientX + gradientY * gradientY));
183 glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
184}
185
186///////////////////////////////////////////////////////////////////////////////
187// Compose shader
188///////////////////////////////////////////////////////////////////////////////
189
190SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second,
191 SkXfermode::Mode mode, SkShader* key):
192 SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
193 NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) {
194}
195
Romain Guy06f96e22010-07-30 19:18:16 -0700196void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) {
197 SkiaShader::set(textureCache, gradientCache);
198 mFirst->set(textureCache, gradientCache);
199 mSecond->set(textureCache, gradientCache);
200}
201
202void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) {
203 mFirst->describe(description, extensions);
204 mSecond->describe(description, extensions);
205 if (mFirst->type() == kBitmap) {
206 description.isBitmapFirst = true;
207 }
208 description.shadersMode = mMode;
209}
210
211void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView,
212 const Snapshot& snapshot, GLuint* textureUnit) {
213 mFirst->setupProgram(program, modelView, snapshot, textureUnit);
214 mSecond->setupProgram(program, modelView, snapshot, textureUnit);
215}
216
217}; // namespace uirenderer
218}; // namespace android