blob: 1fa76d26cfa3db33c42c7ccf92bb2cd862cc9dc1 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -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
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
24
Romain Guy121e22422010-07-01 18:26:52 -070025#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy121e22422010-07-01 18:26:52 -070037// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
Romain Guyc0ac1932010-07-19 18:43:02 -070040#define PROPERTY_GRADIENT_CACHE_SIZE "ro.hwui.gradient_cache_size"
Romain Guy121e22422010-07-01 18:26:52 -070041
Romain Guyc0ac1932010-07-19 18:43:02 -070042#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
43#define DEFAULT_LAYER_CACHE_SIZE 10.0f
Romain Guyf7f93552010-07-08 19:17:03 -070044#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070045#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guydda570202010-07-06 11:39:32 -070046
Romain Guy121e22422010-07-01 18:26:52 -070047// Converts a number of mega-bytes into bytes
48#define MB(s) s * 1024 * 1024
49
Romain Guydda570202010-07-06 11:39:32 -070050// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070051#define SV(x, y) { { x, y } }
52#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070053
54///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy026c5e162010-06-28 17:12:22 -070058static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070059 SV(0.0f, 0.0f),
60 SV(1.0f, 0.0f),
61 SV(0.0f, 1.0f),
62 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070063};
Romain Guy026c5e162010-06-28 17:12:22 -070064static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
65static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070066
Romain Guy026c5e162010-06-28 17:12:22 -070067// This array is never used directly but used as a memcpy source in the
68// OpenGLRenderer constructor
69static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070070 FV(0.0f, 0.0f, 0.0f, 0.0f),
71 FV(1.0f, 0.0f, 1.0f, 0.0f),
72 FV(0.0f, 1.0f, 0.0f, 1.0f),
73 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070074};
Romain Guy026c5e162010-06-28 17:12:22 -070075static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
76static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070077
Romain Guy026c5e162010-06-28 17:12:22 -070078// In this array, the index of each Blender equals the value of the first
79// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
80static const Blender gBlends[] = {
81 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
82 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
83 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
93};
Romain Guye4d01122010-06-16 18:44:05 -070094
Romain Guyd27977d2010-07-14 19:18:51 -070095static const GLint gTileModes[] = {
96 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
97 GL_REPEAT, // == SkShader::kRepeat_Mode
98 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
99};
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Constructors/destructor
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guydda570202010-07-06 11:39:32 -0700105OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700106 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -0700107 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700108 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700109 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700110 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700111 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700112
Romain Guy121e22422010-07-01 18:26:52 -0700113 char property[PROPERTY_VALUE_MAX];
114 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700115 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700117 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda570202010-07-06 11:39:32 -0700119 }
120
121 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
122 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700124 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700125 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
126 }
127
128 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
129 LOGD(" Setting gradient cache size to %sMB", property);
130 mLayerCache.setMaxSize(MB(atof(property)));
131 } else {
132 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700133 }
134
Romain Guyd27977d2010-07-14 19:18:51 -0700135 mDrawColorProgram = new DrawColorProgram;
136 mDrawTextureProgram = new DrawTextureProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700137 mDrawLinearGradientProgram = new DrawLinearGradientProgram;
Romain Guyd27977d2010-07-14 19:18:51 -0700138 mCurrentProgram = mDrawTextureProgram;
139
140 mShader = kShaderNone;
Romain Guya1db5742010-07-20 13:09:13 -0700141 mShaderTileX = GL_CLAMP_TO_EDGE;
142 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700143 mShaderMatrix = NULL;
144 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700145
Romain Guy1e793862010-07-16 15:07:42 -0700146 mLastTexture = 0;
147
Romain Guy026c5e162010-06-28 17:12:22 -0700148 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151OpenGLRenderer::~OpenGLRenderer() {
152 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700153
154 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700155 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700156 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700157 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700158}
159
Romain Guyf6a11b82010-06-23 17:47:49 -0700160///////////////////////////////////////////////////////////////////////////////
161// Setup
162///////////////////////////////////////////////////////////////////////////////
163
Romain Guy85bf02f2010-06-22 13:11:24 -0700164void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700165 glViewport(0, 0, width, height);
166
Romain Guy260e1022010-07-12 14:41:06 -0700167 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700168
169 mWidth = width;
170 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700171 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700172}
173
Romain Guy85bf02f2010-06-22 13:11:24 -0700174void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 mSnapshot = &mFirstSnapshot;
176 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700177
Romain Guy08ae3172010-06-21 19:35:50 -0700178 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700179
Romain Guy08ae3172010-06-21 19:35:50 -0700180 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
181 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700182
Romain Guy08ae3172010-06-21 19:35:50 -0700183 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700184 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700185
Romain Guybb9524b2010-06-22 18:56:38 -0700186 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
187}
188
Romain Guyf6a11b82010-06-23 17:47:49 -0700189///////////////////////////////////////////////////////////////////////////////
190// State management
191///////////////////////////////////////////////////////////////////////////////
192
Romain Guybb9524b2010-06-22 18:56:38 -0700193int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700203
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 if (restoreSnapshot()) {
205 setScissorFromClip();
206 }
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
209void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700211
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700213
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 while (mSaveCount != saveCount - 1) {
215 restoreClip |= restoreSnapshot();
216 }
Romain Guybb9524b2010-06-22 18:56:38 -0700217
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 if (restoreClip) {
219 setScissorFromClip();
220 }
Romain Guybb9524b2010-06-22 18:56:38 -0700221}
222
223int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 mSnapshot = new Snapshot(mSnapshot);
225 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
228bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700229 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700231 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700232
Romain Guybd6b79b2010-06-26 00:13:53 -0700233 sp<Snapshot> current = mSnapshot;
234 sp<Snapshot> previous = mSnapshot->previous;
235
Romain Guyf86ef572010-07-01 11:05:42 -0700236 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700237 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700238 }
239
Romain Guybd6b79b2010-06-26 00:13:53 -0700240 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700241 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700242 }
243
244 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700245 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700246
Romain Guy7ae7ac42010-06-25 13:46:18 -0700247 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700248}
249
Romain Guyd55a8612010-06-28 17:42:46 -0700250void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700251 if (!current->layer) {
252 LOGE("Attempting to compose a layer that does not exist");
253 return;
254 }
255
Romain Guyd55a8612010-06-28 17:42:46 -0700256 // Unbind current FBO and restore previous one
257 // Most of the time, previous->fbo will be 0 to bind the default buffer
258 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
259
260 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700261 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700262 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
263
Romain Guydda570202010-07-06 11:39:32 -0700264 Layer* layer = current->layer;
265
Romain Guyd55a8612010-06-28 17:42:46 -0700266 // Compute the correct texture coordinates for the FBO texture
267 // The texture is currently as big as the window but drawn with
268 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700269 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700270
Romain Guydda570202010-07-06 11:39:32 -0700271 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700272 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700273
Romain Guydda570202010-07-06 11:39:32 -0700274 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700275 // Failing to add the layer to the cache should happen only if the
276 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700277 if (!mLayerCache.put(size, layer)) {
278 LAYER_LOGD("Deleting layer");
279
280 glDeleteFramebuffers(1, &layer->fbo);
281 glDeleteTextures(1, &layer->texture);
282
283 delete layer;
284 }
Romain Guyd55a8612010-06-28 17:42:46 -0700285}
286
Romain Guyf6a11b82010-06-23 17:47:49 -0700287///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700288// Layers
289///////////////////////////////////////////////////////////////////////////////
290
291int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
292 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700293 int count = saveSnapshot();
294
295 int alpha = 255;
296 SkXfermode::Mode mode;
297
298 if (p) {
299 alpha = p->getAlpha();
300 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
301 if (!isMode) {
302 // Assume SRC_OVER
303 mode = SkXfermode::kSrcOver_Mode;
304 }
305 } else {
306 mode = SkXfermode::kSrcOver_Mode;
307 }
308
309 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
310
311 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700312}
313
314int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
315 int alpha, int flags) {
316 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700317 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
318 return count;
319}
Romain Guybd6b79b2010-06-26 00:13:53 -0700320
Romain Guyd55a8612010-06-28 17:42:46 -0700321bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
322 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700323
Romain Guyd27977d2010-07-14 19:18:51 -0700324 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700325 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700326
Romain Guyf18fd992010-07-08 11:45:51 -0700327 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
328 LayerSize size(right - left, bottom - top);
329
Romain Guyf18fd992010-07-08 11:45:51 -0700330 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700331 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700332 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700333 }
334
Romain Guyf18fd992010-07-08 11:45:51 -0700335 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
336
Romain Guyf86ef572010-07-01 11:05:42 -0700337 // Clear the FBO
338 glDisable(GL_SCISSOR_TEST);
339 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
340 glClear(GL_COLOR_BUFFER_BIT);
341 glEnable(GL_SCISSOR_TEST);
342
Romain Guydda570202010-07-06 11:39:32 -0700343 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700344 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700345 layer->mode = mode;
346 layer->alpha = alpha / 255.0f;
347 layer->layer.set(left, top, right, bottom);
348
349 snapshot->layer = layer;
350 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700351
Romain Guyf86ef572010-07-01 11:05:42 -0700352 // Creates a new snapshot to draw into the FBO
353 saveSnapshot();
354 // TODO: This doesn't preserve other transformations (check Skia first)
355 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700356 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700357 mSnapshot->height = bottom - top;
358 setScissorFromClip();
359
Romain Guy079ba2c2010-07-16 14:12:24 -0700360 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700361 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700362
363 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700364 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700365
Romain Guyd55a8612010-06-28 17:42:46 -0700366 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700367}
368
369///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700370// Transforms
371///////////////////////////////////////////////////////////////////////////////
372
373void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700383}
384
385void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700387}
388
389void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700390 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700391}
392
393void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700394 mat4 m(*matrix);
395 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700396}
397
398///////////////////////////////////////////////////////////////////////////////
399// Clipping
400///////////////////////////////////////////////////////////////////////////////
401
Romain Guybb9524b2010-06-22 18:56:38 -0700402void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700403 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700404 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700405}
406
407const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700408 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700409}
410
Romain Guyc7d53492010-06-25 13:41:57 -0700411bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700412 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700413 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700414 return !mSnapshot->clipRect.intersects(r);
415}
416
Romain Guy079ba2c2010-07-16 14:12:24 -0700417bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
418 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700419 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700420 setScissorFromClip();
421 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700422 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700423}
424
Romain Guyf6a11b82010-06-23 17:47:49 -0700425///////////////////////////////////////////////////////////////////////////////
426// Drawing
427///////////////////////////////////////////////////////////////////////////////
428
Romain Guyc1396e92010-06-30 17:56:19 -0700429void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700430 const float right = left + bitmap->width();
431 const float bottom = top + bitmap->height();
432
433 if (quickReject(left, top, right, bottom)) {
434 return;
435 }
436
Romain Guyf86ef572010-07-01 11:05:42 -0700437 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700438 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700439}
440
Romain Guyf86ef572010-07-01 11:05:42 -0700441void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
442 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
443 const mat4 transform(*matrix);
444 transform.mapRect(r);
445
Romain Guy6926c722010-07-12 20:20:03 -0700446 if (quickReject(r.left, r.top, r.right, r.bottom)) {
447 return;
448 }
449
Romain Guyf86ef572010-07-01 11:05:42 -0700450 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700451 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700452}
453
Romain Guy8ba548f2010-06-30 19:21:21 -0700454void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
455 float srcLeft, float srcTop, float srcRight, float srcBottom,
456 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700457 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700458 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
459 return;
460 }
461
Romain Guyf86ef572010-07-01 11:05:42 -0700462 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700463
Romain Guy8ba548f2010-06-30 19:21:21 -0700464 const float width = texture->width;
465 const float height = texture->height;
466
467 const float u1 = srcLeft / width;
468 const float v1 = srcTop / height;
469 const float u2 = srcRight / width;
470 const float v2 = srcBottom / height;
471
472 resetDrawTextureTexCoords(u1, v1, u2, v2);
473
Romain Guy82ba8142010-07-09 13:25:56 -0700474 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700475
476 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
477}
478
Romain Guydeba7852010-07-07 17:54:48 -0700479void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
480 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700481 if (quickReject(left, top, right, bottom)) {
482 return;
483 }
484
Romain Guyf7f93552010-07-08 19:17:03 -0700485 const Texture* texture = mTextureCache.get(bitmap);
486
487 int alpha;
488 SkXfermode::Mode mode;
489 getAlphaAndMode(paint, &alpha, &mode);
490
Romain Guyf7f93552010-07-08 19:17:03 -0700491 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700492 mesh->updateVertices(bitmap, left, top, right, bottom,
493 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700494
495 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
496 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700497 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
498 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700499 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700500}
501
Romain Guy85bf02f2010-06-22 13:11:24 -0700502void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700503 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700504 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700505}
Romain Guy9d5316e2010-06-24 19:30:36 -0700506
Romain Guybd6b79b2010-06-26 00:13:53 -0700507void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700508 if (quickReject(left, top, right, bottom)) {
509 return;
510 }
511
Romain Guy026c5e162010-06-28 17:12:22 -0700512 SkXfermode::Mode mode;
513
514 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
515 if (!isMode) {
516 // Assume SRC_OVER
517 mode = SkXfermode::kSrcOver_Mode;
518 }
519
520 // Skia draws using the color's alpha channel if < 255
521 // Otherwise, it uses the paint's alpha
522 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700523 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700524 color |= p->getAlpha() << 24;
525 }
526
527 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700528}
Romain Guy9d5316e2010-06-24 19:30:36 -0700529
Romain Guy6926c722010-07-12 20:20:03 -0700530///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700531// Shaders
532///////////////////////////////////////////////////////////////////////////////
533
534void OpenGLRenderer::resetShader() {
535 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700536 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700537 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700538 mShaderTileX = GL_CLAMP_TO_EDGE;
539 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700540}
541
542void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
543 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700544 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700545 mShaderBlend = hasAlpha;
546 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700547 mShaderTileX = gTileModes[tileX];
548 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700549 mShaderMatrix = matrix;
550}
551
Romain Guyc0ac1932010-07-19 18:43:02 -0700552void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
553 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix,
554 bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700555 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700556 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700557 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700558 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700559 mShaderTileX = gTileModes[tileMode];
560 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700561 mShaderMatrix = matrix;
562 mShaderBounds = bounds;
563 mShaderColors = colors;
564 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700565 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700566}
567
Romain Guyd27977d2010-07-14 19:18:51 -0700568///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700569// Drawing implementation
570///////////////////////////////////////////////////////////////////////////////
571
Romain Guy026c5e162010-06-28 17:12:22 -0700572void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700573 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700574 // If a shader is set, preserve only the alpha
575 if (mShader != kShaderNone) {
576 color |= 0x00ffffff;
577 }
578
579 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700580 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700581 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700582
583 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700584 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700585 drawBitmapShader(left, top, right, bottom, a, mode);
586 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700587 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700588 drawLinearGradientShader(left, top, right, bottom, a, mode);
589 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700590 default:
591 break;
592 }
Romain Guy026c5e162010-06-28 17:12:22 -0700593
Romain Guyc0ac1932010-07-19 18:43:02 -0700594 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
595 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
596 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
597
Romain Guy6926c722010-07-12 20:20:03 -0700598 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700599 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700600
Romain Guyc7d53492010-06-25 13:41:57 -0700601 mModelView.loadTranslate(left, top, 0.0f);
602 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700603
Romain Guy079ba2c2010-07-16 14:12:24 -0700604 if (!useProgram(mDrawColorProgram)) {
Romain Guy6926c722010-07-12 20:20:03 -0700605 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy079ba2c2010-07-16 14:12:24 -0700606 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700607 gDrawColorVertexStride, p);
608 }
Romain Guyd27977d2010-07-14 19:18:51 -0700609
610 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700611 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700612 } else {
613 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700614 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700615 }
616
Romain Guy079ba2c2010-07-16 14:12:24 -0700617 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700618
Romain Guyc7d53492010-06-25 13:41:57 -0700619 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700620}
Romain Guy026c5e162010-06-28 17:12:22 -0700621
Romain Guyc0ac1932010-07-19 18:43:02 -0700622void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
623 float alpha, SkXfermode::Mode mode) {
624 Texture* texture = mGradientCache.get(mShaderKey);
625 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700626 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
627 switch (mShaderTileX) {
628 case GL_REPEAT:
629 tileMode = SkShader::kRepeat_TileMode;
630 break;
631 case GL_MIRRORED_REPEAT:
632 tileMode = SkShader::kMirror_TileMode;
633 break;
634 }
635
Romain Guyc0ac1932010-07-19 18:43:02 -0700636 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700637 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700638 }
639
640 mModelView.loadTranslate(left, top, 0.0f);
641 mModelView.scale(right - left, bottom - top, 1.0f);
642
643 useProgram(mDrawLinearGradientProgram);
644 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
645
646 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700647 bindTexture(texture->id, mShaderTileX, mShaderTileY);
Romain Guyc0ac1932010-07-19 18:43:02 -0700648
649 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
650 if (mShaderMatrix) {
651 mat4 shaderMatrix(*mShaderMatrix);
652 shaderMatrix.mapRect(start);
653 }
654 mSnapshot->transform.mapRect(start);
655
656 const float gradientX = start.right - start.left;
657 const float gradientY = start.bottom - start.top;
658
659 mat4 screenSpace(mSnapshot->transform);
660 screenSpace.multiply(mModelView);
661
662 // Always premultiplied
663 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha);
664 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top);
665 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY);
666 glUniform1f(mDrawLinearGradientProgram->gradientLength,
667 1.0f / (gradientX * gradientX + gradientY * gradientY));
668 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE,
669 &screenSpace.data[0]);
670
671 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE,
672 gDrawTextureVertexStride, &mDrawTextureVertices[0].position[0]);
673
674 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
675}
676
Romain Guyd27977d2010-07-14 19:18:51 -0700677void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
678 float alpha, SkXfermode::Mode mode) {
679 const Texture* texture = mTextureCache.get(mShaderBitmap);
680
681 const float width = texture->width;
682 const float height = texture->height;
683
684 // This could be done in the vertex shader but we have only 4 vertices
685 float u1 = 0.0f;
686 float v1 = 0.0f;
687 float u2 = right - left;
688 float v2 = bottom - top;
689
690 if (mShaderMatrix) {
691 SkMatrix inverse;
692 mShaderMatrix->invert(&inverse);
693 mat4 m(inverse);
694 Rect r(u1, v1, u2, v2);
695 m.mapRect(r);
696
697 u1 = r.left;
698 u2 = r.right;
699 v1 = r.top;
700 v2 = r.bottom;
701 }
702
703 u1 /= width;
704 u2 /= width;
705 v1 /= height;
706 v2 /= height;
707
708 resetDrawTextureTexCoords(u1, v1, u2, v2);
709
710 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
711 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
712
713 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
714}
715
Romain Guy82ba8142010-07-09 13:25:56 -0700716void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700717 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700718 int alpha;
719 SkXfermode::Mode mode;
720 getAlphaAndMode(paint, &alpha, &mode);
721
Romain Guya9794742010-07-13 11:37:54 -0700722 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
723 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700724}
725
Romain Guybd6b79b2010-06-26 00:13:53 -0700726void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700727 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
728 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700729 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
730}
731
732void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700733 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700734 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700735 mModelView.loadTranslate(left, top, 0.0f);
736 mModelView.scale(right - left, bottom - top, 1.0f);
737
Romain Guyd27977d2010-07-14 19:18:51 -0700738 useProgram(mDrawTextureProgram);
739 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700740
Romain Guya9794742010-07-13 11:37:54 -0700741 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700742 bindTexture(texture, mShaderTileX, mShaderTileY);
Romain Guyc1396e92010-06-30 17:56:19 -0700743
Romain Guya9794742010-07-13 11:37:54 -0700744 // Always premultiplied
Romain Guyc0ac1932010-07-19 18:43:02 -0700745 //glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guyd27977d2010-07-14 19:18:51 -0700746 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700747
Romain Guyd27977d2010-07-14 19:18:51 -0700748 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700749 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700750 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700751 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700752
Romain Guyf7f93552010-07-08 19:17:03 -0700753 if (!indices) {
754 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
755 } else {
756 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
757 }
Romain Guy82ba8142010-07-09 13:25:56 -0700758}
759
760void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
761 // In theory we should not blend if the mode is Src, but it's rare enough
762 // that it's not worth it
763 blend = blend || mode != SkXfermode::kSrcOver_Mode;
764 if (blend) {
765 if (!mBlend) {
766 glEnable(GL_BLEND);
767 }
768
769 GLenum sourceMode = gBlends[mode].src;
770 GLenum destMode = gBlends[mode].dst;
771 if (!isPremultiplied && sourceMode == GL_ONE) {
772 sourceMode = GL_SRC_ALPHA;
773 }
774
775 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
776 glBlendFunc(sourceMode, destMode);
777 mLastSrcMode = sourceMode;
778 mLastDstMode = destMode;
779 }
780 } else if (mBlend) {
781 glDisable(GL_BLEND);
782 }
783 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700784}
785
Romain Guyd27977d2010-07-14 19:18:51 -0700786bool OpenGLRenderer::useProgram(const sp<Program>& program) {
787 if (!program->isInUse()) {
788 mCurrentProgram->remove();
789 program->use();
790 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700791 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700792 }
Romain Guy6926c722010-07-12 20:20:03 -0700793 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700794}
795
Romain Guy026c5e162010-06-28 17:12:22 -0700796void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700797 TextureVertex* v = &mDrawTextureVertices[0];
798 TextureVertex::setUV(v++, u1, v1);
799 TextureVertex::setUV(v++, u2, v1);
800 TextureVertex::setUV(v++, u1, v2);
801 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700802}
803
804void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
805 if (paint) {
806 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
807 if (!isMode) {
808 // Assume SRC_OVER
809 *mode = SkXfermode::kSrcOver_Mode;
810 }
811
812 // Skia draws using the color's alpha channel if < 255
813 // Otherwise, it uses the paint's alpha
814 int color = paint->getColor();
815 *alpha = (color >> 24) & 0xFF;
816 if (*alpha == 255) {
817 *alpha = paint->getAlpha();
818 }
819 } else {
820 *mode = SkXfermode::kSrcOver_Mode;
821 *alpha = 255;
822 }
Romain Guy026c5e162010-06-28 17:12:22 -0700823}
824
Romain Guya1db5742010-07-20 13:09:13 -0700825void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) {
826 if (texture != mLastTexture) {
827 glBindTexture(GL_TEXTURE_2D, texture);
828 mLastTexture = texture;
829 }
830 // TODO: Don't set the texture parameters every time
831 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
832 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
833}
834
Romain Guy9d5316e2010-06-24 19:30:36 -0700835}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700836}; // namespace android