SkiaShader.cpp revision 25ee037fee1607e8a77b1f2e259356e74dd6f645
1/*
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):
78        SkiaShader(kBitmap, key, tileX, tileY, matrix, blend), mBitmap(bitmap) {
79}
80
81void SkiaBitmapShader::describe(ProgramDescription& description, const Extensions& extensions) {
82    const Texture* texture = mTextureCache->get(mBitmap);
83
84    const float width = texture->width;
85    const float height = texture->height;
86
87    description.hasBitmap = true;
88    // The driver does not support non-power of two mirrored/repeated
89    // textures, so do it ourselves
90    if (!extensions.hasNPot() && !isPowerOfTwo(width) && !isPowerOfTwo(height)) {
91        description.isBitmapNpot = true;
92        description.bitmapWrapS = gTileModes[mTileX];
93        description.bitmapWrapT = gTileModes[mTileY];
94    }
95}
96
97void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
98        const Snapshot& snapshot, GLuint* textureUnit) {
99    GLuint textureSlot = (*textureUnit)++;
100    glActiveTexture(gTextureUnitsMap[textureSlot]);
101    const Texture* texture = mTextureCache->get(mBitmap);
102
103    const float width = texture->width;
104    const float height = texture->height;
105
106    mat4 textureTransform;
107    if (mMatrix) {
108        SkMatrix inverse;
109        mMatrix->invert(&inverse);
110        textureTransform.load(inverse);
111        textureTransform.multiply(modelView);
112    } else {
113        textureTransform.load(modelView);
114    }
115
116    // Uniforms
117    bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
118    glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
119    glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
120            GL_FALSE, &textureTransform.data[0]);
121    glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
122}
123
124///////////////////////////////////////////////////////////////////////////////
125// Linear gradient shader
126///////////////////////////////////////////////////////////////////////////////
127
128SkiaLinearGradientShader::SkiaLinearGradientShader(float* bounds, uint32_t* colors,
129        float* positions, int count, SkShader* key, SkShader::TileMode tileMode,
130        SkMatrix* matrix, bool blend):
131        SkiaShader(kLinearGradient, key, tileMode, tileMode, matrix, blend),
132        mBounds(bounds), mColors(colors), mPositions(positions), mCount(count) {
133}
134
135SkiaLinearGradientShader::~SkiaLinearGradientShader() {
136    delete[] mBounds;
137    delete[] mColors;
138    delete[] mPositions;
139}
140
141void SkiaLinearGradientShader::describe(ProgramDescription& description,
142        const Extensions& extensions) {
143    description.hasGradient = true;
144}
145
146void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelView,
147        const Snapshot& snapshot, GLuint* textureUnit) {
148    GLuint textureSlot = (*textureUnit)++;
149    glActiveTexture(gTextureUnitsMap[textureSlot]);
150
151    Texture* texture = mGradientCache->get(mKey);
152    if (!texture) {
153        texture = mGradientCache->addLinearGradient(mKey, mBounds, mColors, mPositions,
154                mCount, mTileX);
155    }
156
157    Rect start(mBounds[0], mBounds[1], mBounds[2], mBounds[3]);
158    if (mMatrix) {
159        mat4 shaderMatrix(*mMatrix);
160        shaderMatrix.mapRect(start);
161    }
162    snapshot.transform.mapRect(start);
163
164    const float gradientX = start.right - start.left;
165    const float gradientY = start.bottom - start.top;
166
167    mat4 screenSpace(snapshot.transform);
168    screenSpace.multiply(modelView);
169
170    // Uniforms
171    bindTexture(texture->id, gTileModes[mTileX], gTileModes[mTileY], textureSlot);
172    glUniform1i(program->getUniform("gradientSampler"), textureSlot);
173    glUniform2f(program->getUniform("gradientStart"), start.left, start.top);
174    glUniform2f(program->getUniform("gradient"), gradientX, gradientY);
175    glUniform1f(program->getUniform("gradientLength"),
176            1.0f / (gradientX * gradientX + gradientY * gradientY));
177    glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
178}
179
180///////////////////////////////////////////////////////////////////////////////
181// Compose shader
182///////////////////////////////////////////////////////////////////////////////
183
184SkiaComposeShader::SkiaComposeShader(SkiaShader* first, SkiaShader* second,
185        SkXfermode::Mode mode, SkShader* key):
186        SkiaShader(kCompose, key, SkShader::kClamp_TileMode, SkShader::kClamp_TileMode,
187        NULL, first->blend() || second->blend()), mFirst(first), mSecond(second), mMode(mode) {
188}
189
190void SkiaComposeShader::set(TextureCache* textureCache, GradientCache* gradientCache) {
191    SkiaShader::set(textureCache, gradientCache);
192    mFirst->set(textureCache, gradientCache);
193    mSecond->set(textureCache, gradientCache);
194}
195
196void SkiaComposeShader::describe(ProgramDescription& description, const Extensions& extensions) {
197    mFirst->describe(description, extensions);
198    mSecond->describe(description, extensions);
199    if (mFirst->type() == kBitmap) {
200        description.isBitmapFirst = true;
201    }
202    description.shadersMode = mMode;
203}
204
205void SkiaComposeShader::setupProgram(Program* program, const mat4& modelView,
206        const Snapshot& snapshot, GLuint* textureUnit) {
207    mFirst->setupProgram(program, modelView, snapshot, textureUnit);
208    mSecond->setupProgram(program, modelView, snapshot, textureUnit);
209}
210
211}; // namespace uirenderer
212}; // namespace android
213