blob: 0ed62768b89955571c238e86008de90e20934376 [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>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guy121e22422010-07-01 18:26:52 -070026#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
28
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include "OpenGLRenderer.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guye4d01122010-06-16 18:44:05 -070031
32namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070033namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyc0ac1932010-07-19 18:43:02 -070039#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
40#define DEFAULT_LAYER_CACHE_SIZE 10.0f
Romain Guyf7f93552010-07-08 19:17:03 -070041#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070042#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guydda570202010-07-06 11:39:32 -070043
Romain Guy121e22422010-07-01 18:26:52 -070044// Converts a number of mega-bytes into bytes
45#define MB(s) s * 1024 * 1024
46
Romain Guydda570202010-07-06 11:39:32 -070047// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070048#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070049
50///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy026c5e162010-06-28 17:12:22 -070054// This array is never used directly but used as a memcpy source in the
55// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070056static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070057 FV(0.0f, 0.0f, 0.0f, 0.0f),
58 FV(1.0f, 0.0f, 1.0f, 0.0f),
59 FV(0.0f, 1.0f, 0.0f, 1.0f),
60 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070061};
Romain Guyac670c02010-07-27 17:39:27 -070062static const GLsizei gMeshStride = sizeof(TextureVertex);
63static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070064
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
68 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
80};
Romain Guye4d01122010-06-16 18:44:05 -070081
Romain Guyd27977d2010-07-14 19:18:51 -070082static const GLint gTileModes[] = {
83 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
84 GL_REPEAT, // == SkShader::kRepeat_Mode
85 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
86};
87
Romain Guyf6a11b82010-06-23 17:47:49 -070088///////////////////////////////////////////////////////////////////////////////
89// Constructors/destructor
90///////////////////////////////////////////////////////////////////////////////
91
Romain Guydda570202010-07-06 11:39:32 -070092OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -070093 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -070094 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -070095 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -070096 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -070097 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -070098 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070099
Romain Guy121e22422010-07-01 18:26:52 -0700100 char property[PROPERTY_VALUE_MAX];
101 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700102 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700103 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700104 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700105 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda570202010-07-06 11:39:32 -0700106 }
107
108 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
109 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700110 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700111 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700112 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
113 }
114
115 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
116 LOGD(" Setting gradient cache size to %sMB", property);
117 mLayerCache.setMaxSize(MB(atof(property)));
118 } else {
119 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700120 }
121
Romain Guyd27977d2010-07-14 19:18:51 -0700122 mDrawColorProgram = new DrawColorProgram;
123 mDrawTextureProgram = new DrawTextureProgram;
Romain Guy694b5192010-07-21 21:33:20 -0700124 mDrawTextProgram = new DrawTextProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700125 mDrawLinearGradientProgram = new DrawLinearGradientProgram;
Romain Guyd27977d2010-07-14 19:18:51 -0700126 mCurrentProgram = mDrawTextureProgram;
127
128 mShader = kShaderNone;
Romain Guya1db5742010-07-20 13:09:13 -0700129 mShaderTileX = GL_CLAMP_TO_EDGE;
130 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700131 mShaderMatrix = NULL;
132 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700133
Romain Guy1e793862010-07-16 15:07:42 -0700134 mLastTexture = 0;
135
Romain Guyac670c02010-07-27 17:39:27 -0700136 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
137
138 ProgramDescription d;
139 mProgramCache.get(d);
140 d.hasTexture = true;
141 mProgramCache.get(d);
142 d.hasAlpha8Texture = true;
143 d.hasGradient = true;
144 d.hasBitmap = true;
145 d.shadersMode = SkXfermode::kDstOut_Mode;
146 d.colorOp = ProgramDescription::kColorMatrix;
147 mProgramCache.get(d);
Romain Guye4d01122010-06-16 18:44:05 -0700148}
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150OpenGLRenderer::~OpenGLRenderer() {
151 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700152
153 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700154 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700155 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700156 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700157}
158
Romain Guyf6a11b82010-06-23 17:47:49 -0700159///////////////////////////////////////////////////////////////////////////////
160// Setup
161///////////////////////////////////////////////////////////////////////////////
162
Romain Guy85bf02f2010-06-22 13:11:24 -0700163void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700164 glViewport(0, 0, width, height);
165
Romain Guy260e1022010-07-12 14:41:06 -0700166 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700167
168 mWidth = width;
169 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700170 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700174 mSnapshot = &mFirstSnapshot;
175 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700176
Romain Guy08ae3172010-06-21 19:35:50 -0700177 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700178
Romain Guy08ae3172010-06-21 19:35:50 -0700179 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
180 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700181
Romain Guy08ae3172010-06-21 19:35:50 -0700182 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700183 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700184
Romain Guybb9524b2010-06-22 18:56:38 -0700185 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
186}
187
Romain Guyf6a11b82010-06-23 17:47:49 -0700188///////////////////////////////////////////////////////////////////////////////
189// State management
190///////////////////////////////////////////////////////////////////////////////
191
Romain Guybb9524b2010-06-22 18:56:38 -0700192int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700193 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700194}
195
196int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700197 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
200void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700201 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700202
Romain Guy7ae7ac42010-06-25 13:46:18 -0700203 if (restoreSnapshot()) {
204 setScissorFromClip();
205 }
Romain Guybb9524b2010-06-22 18:56:38 -0700206}
207
208void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700209 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700210
Romain Guy7ae7ac42010-06-25 13:46:18 -0700211 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700212
Romain Guy7ae7ac42010-06-25 13:46:18 -0700213 while (mSaveCount != saveCount - 1) {
214 restoreClip |= restoreSnapshot();
215 }
Romain Guybb9524b2010-06-22 18:56:38 -0700216
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 if (restoreClip) {
218 setScissorFromClip();
219 }
Romain Guybb9524b2010-06-22 18:56:38 -0700220}
221
222int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 mSnapshot = new Snapshot(mSnapshot);
224 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700225}
226
227bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700228 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700229 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700230 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700231
Romain Guybd6b79b2010-06-26 00:13:53 -0700232 sp<Snapshot> current = mSnapshot;
233 sp<Snapshot> previous = mSnapshot->previous;
234
Romain Guyf86ef572010-07-01 11:05:42 -0700235 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700236 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700237 }
238
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700240 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 }
242
243 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700244 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700245
Romain Guy7ae7ac42010-06-25 13:46:18 -0700246 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700247}
248
Romain Guyd55a8612010-06-28 17:42:46 -0700249void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700250 if (!current->layer) {
251 LOGE("Attempting to compose a layer that does not exist");
252 return;
253 }
254
Romain Guyd55a8612010-06-28 17:42:46 -0700255 // Unbind current FBO and restore previous one
256 // Most of the time, previous->fbo will be 0 to bind the default buffer
257 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
258
259 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700260 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700261 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
262
Romain Guydda570202010-07-06 11:39:32 -0700263 Layer* layer = current->layer;
264
Romain Guyd55a8612010-06-28 17:42:46 -0700265 // Compute the correct texture coordinates for the FBO texture
266 // The texture is currently as big as the window but drawn with
267 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700268 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700269
Romain Guydda570202010-07-06 11:39:32 -0700270 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700271 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700272
Romain Guydda570202010-07-06 11:39:32 -0700273 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700274 // Failing to add the layer to the cache should happen only if the
275 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700276 if (!mLayerCache.put(size, layer)) {
277 LAYER_LOGD("Deleting layer");
278
279 glDeleteFramebuffers(1, &layer->fbo);
280 glDeleteTextures(1, &layer->texture);
281
282 delete layer;
283 }
Romain Guyd55a8612010-06-28 17:42:46 -0700284}
285
Romain Guyf6a11b82010-06-23 17:47:49 -0700286///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700287// Layers
288///////////////////////////////////////////////////////////////////////////////
289
290int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
291 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700292 int count = saveSnapshot();
293
294 int alpha = 255;
295 SkXfermode::Mode mode;
296
297 if (p) {
298 alpha = p->getAlpha();
299 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
300 if (!isMode) {
301 // Assume SRC_OVER
302 mode = SkXfermode::kSrcOver_Mode;
303 }
304 } else {
305 mode = SkXfermode::kSrcOver_Mode;
306 }
307
308 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
309
310 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700311}
312
313int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
314 int alpha, int flags) {
315 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700316 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
317 return count;
318}
Romain Guybd6b79b2010-06-26 00:13:53 -0700319
Romain Guyd55a8612010-06-28 17:42:46 -0700320bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
321 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700322
Romain Guyd27977d2010-07-14 19:18:51 -0700323 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700324 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700325
Romain Guyf18fd992010-07-08 11:45:51 -0700326 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
327 LayerSize size(right - left, bottom - top);
328
Romain Guyf18fd992010-07-08 11:45:51 -0700329 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700330 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700331 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700332 }
333
Romain Guyf18fd992010-07-08 11:45:51 -0700334 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
335
Romain Guyf86ef572010-07-01 11:05:42 -0700336 // Clear the FBO
337 glDisable(GL_SCISSOR_TEST);
338 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
339 glClear(GL_COLOR_BUFFER_BIT);
340 glEnable(GL_SCISSOR_TEST);
341
Romain Guydda570202010-07-06 11:39:32 -0700342 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700343 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700344 layer->mode = mode;
345 layer->alpha = alpha / 255.0f;
346 layer->layer.set(left, top, right, bottom);
347
348 snapshot->layer = layer;
349 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700350
Romain Guyf86ef572010-07-01 11:05:42 -0700351 // Creates a new snapshot to draw into the FBO
352 saveSnapshot();
353 // TODO: This doesn't preserve other transformations (check Skia first)
354 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700355 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700356 mSnapshot->height = bottom - top;
357 setScissorFromClip();
358
Romain Guy079ba2c2010-07-16 14:12:24 -0700359 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700360 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700361
362 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700363 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700364
Romain Guyd55a8612010-06-28 17:42:46 -0700365 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700366}
367
368///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700369// Transforms
370///////////////////////////////////////////////////////////////////////////////
371
372void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700373 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700374}
375
376void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700386}
387
388void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700390}
391
392void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700393 mat4 m(*matrix);
394 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700395}
396
397///////////////////////////////////////////////////////////////////////////////
398// Clipping
399///////////////////////////////////////////////////////////////////////////////
400
Romain Guybb9524b2010-06-22 18:56:38 -0700401void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700402 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700403 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700404}
405
406const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700407 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700408}
409
Romain Guyc7d53492010-06-25 13:41:57 -0700410bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700411 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700412 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700413 return !mSnapshot->clipRect.intersects(r);
414}
415
Romain Guy079ba2c2010-07-16 14:12:24 -0700416bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
417 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700419 setScissorFromClip();
420 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700421 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700422}
423
Romain Guyf6a11b82010-06-23 17:47:49 -0700424///////////////////////////////////////////////////////////////////////////////
425// Drawing
426///////////////////////////////////////////////////////////////////////////////
427
Romain Guyc1396e92010-06-30 17:56:19 -0700428void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700429 const float right = left + bitmap->width();
430 const float bottom = top + bitmap->height();
431
432 if (quickReject(left, top, right, bottom)) {
433 return;
434 }
435
Romain Guyf86ef572010-07-01 11:05:42 -0700436 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700437 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700438}
439
Romain Guyf86ef572010-07-01 11:05:42 -0700440void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
441 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
442 const mat4 transform(*matrix);
443 transform.mapRect(r);
444
Romain Guy6926c722010-07-12 20:20:03 -0700445 if (quickReject(r.left, r.top, r.right, r.bottom)) {
446 return;
447 }
448
Romain Guyf86ef572010-07-01 11:05:42 -0700449 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700450 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700451}
452
Romain Guy8ba548f2010-06-30 19:21:21 -0700453void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
454 float srcLeft, float srcTop, float srcRight, float srcBottom,
455 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700456 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700457 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
458 return;
459 }
460
Romain Guyf86ef572010-07-01 11:05:42 -0700461 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700462
Romain Guy8ba548f2010-06-30 19:21:21 -0700463 const float width = texture->width;
464 const float height = texture->height;
465
466 const float u1 = srcLeft / width;
467 const float v1 = srcTop / height;
468 const float u2 = srcRight / width;
469 const float v2 = srcBottom / height;
470
471 resetDrawTextureTexCoords(u1, v1, u2, v2);
472
Romain Guy82ba8142010-07-09 13:25:56 -0700473 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700474
475 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
476}
477
Romain Guydeba7852010-07-07 17:54:48 -0700478void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
479 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700480 if (quickReject(left, top, right, bottom)) {
481 return;
482 }
483
Romain Guyf7f93552010-07-08 19:17:03 -0700484 const Texture* texture = mTextureCache.get(bitmap);
485
486 int alpha;
487 SkXfermode::Mode mode;
488 getAlphaAndMode(paint, &alpha, &mode);
489
Romain Guyf7f93552010-07-08 19:17:03 -0700490 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700491 mesh->updateVertices(bitmap, left, top, right, bottom,
492 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700493
494 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
495 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700496 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
497 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700498 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700499}
500
Romain Guy85bf02f2010-06-22 13:11:24 -0700501void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700502 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700503 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700504}
Romain Guy9d5316e2010-06-24 19:30:36 -0700505
Romain Guybd6b79b2010-06-26 00:13:53 -0700506void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700507 if (quickReject(left, top, right, bottom)) {
508 return;
509 }
510
Romain Guy026c5e162010-06-28 17:12:22 -0700511 SkXfermode::Mode mode;
512
513 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
514 if (!isMode) {
515 // Assume SRC_OVER
516 mode = SkXfermode::kSrcOver_Mode;
517 }
518
519 // Skia draws using the color's alpha channel if < 255
520 // Otherwise, it uses the paint's alpha
521 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700522 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700523 color |= p->getAlpha() << 24;
524 }
525
526 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700527}
Romain Guy9d5316e2010-06-24 19:30:36 -0700528
Romain Guye8e62a42010-07-23 18:55:21 -0700529void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
530 float x, float y, SkPaint* paint) {
531 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
532 return;
533 }
534
535 float length;
536 switch (paint->getTextAlign()) {
537 case SkPaint::kCenter_Align:
538 length = paint->measureText(text, bytesCount);
539 x -= length / 2.0f;
540 break;
541 case SkPaint::kRight_Align:
542 length = paint->measureText(text, bytesCount);
543 x -= length;
544 break;
545 default:
546 break;
547 }
548
Romain Guy694b5192010-07-21 21:33:20 -0700549 int alpha;
550 SkXfermode::Mode mode;
551 getAlphaAndMode(paint, &alpha, &mode);
552
553 uint32_t color = paint->getColor();
554 const GLfloat a = alpha / 255.0f;
555 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
556 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
557 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
558
559 mModelView.loadIdentity();
560
561 useProgram(mDrawTextProgram);
562 mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
563
564 chooseBlending(true, mode);
565 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
566
567 // Always premultiplied
568 glUniform4f(mDrawTextProgram->color, r, g, b, a);
569
Romain Guy09147fb2010-07-22 13:08:20 -0700570 // TODO: Implement scale properly
571 const Rect& clip = mSnapshot->getLocalClip();
572
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700573 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700574 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700575
576 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
577}
578
Romain Guy6926c722010-07-12 20:20:03 -0700579///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700580// Shaders
581///////////////////////////////////////////////////////////////////////////////
582
583void OpenGLRenderer::resetShader() {
584 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700585 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700586 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700587 mShaderTileX = GL_CLAMP_TO_EDGE;
588 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700589}
590
591void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
592 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700593 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700594 mShaderBlend = hasAlpha;
595 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700596 mShaderTileX = gTileModes[tileX];
597 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700598 mShaderMatrix = matrix;
599}
600
Romain Guyc0ac1932010-07-19 18:43:02 -0700601void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
602 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix,
603 bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700604 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700605 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700606 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700607 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700608 mShaderTileX = gTileModes[tileMode];
609 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700610 mShaderMatrix = matrix;
611 mShaderBounds = bounds;
612 mShaderColors = colors;
613 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700614 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700615}
616
Romain Guyd27977d2010-07-14 19:18:51 -0700617///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700618// Drawing implementation
619///////////////////////////////////////////////////////////////////////////////
620
Romain Guy026c5e162010-06-28 17:12:22 -0700621void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700622 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700623 // If a shader is set, preserve only the alpha
624 if (mShader != kShaderNone) {
625 color |= 0x00ffffff;
626 }
627
628 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700629 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700630 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700631
632 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700633 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700634 drawBitmapShader(left, top, right, bottom, a, mode);
635 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700636 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700637 drawLinearGradientShader(left, top, right, bottom, a, mode);
638 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700639 default:
640 break;
641 }
Romain Guy026c5e162010-06-28 17:12:22 -0700642
Romain Guyc0ac1932010-07-19 18:43:02 -0700643 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
644 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
645 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
646
Romain Guy6926c722010-07-12 20:20:03 -0700647 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700648 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700649
Romain Guyc7d53492010-06-25 13:41:57 -0700650 mModelView.loadTranslate(left, top, 0.0f);
651 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700652
Romain Guy079ba2c2010-07-16 14:12:24 -0700653 if (!useProgram(mDrawColorProgram)) {
Romain Guyac670c02010-07-27 17:39:27 -0700654 const GLvoid* vertices = &mMeshVertices[0].position[0];
655 const GLvoid* texCoords = &mMeshVertices[0].texture[0];
656
Romain Guy079ba2c2010-07-16 14:12:24 -0700657 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700658 gMeshStride, vertices);
659 glVertexAttribPointer(mDrawColorProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
660 gMeshStride, texCoords);
Romain Guy6926c722010-07-12 20:20:03 -0700661 }
Romain Guyd27977d2010-07-14 19:18:51 -0700662
663 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700664 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700665 } else {
666 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700667 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700668 }
669
Romain Guy079ba2c2010-07-16 14:12:24 -0700670 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700671
Romain Guyac670c02010-07-27 17:39:27 -0700672 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700673}
Romain Guy026c5e162010-06-28 17:12:22 -0700674
Romain Guyc0ac1932010-07-19 18:43:02 -0700675void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
676 float alpha, SkXfermode::Mode mode) {
677 Texture* texture = mGradientCache.get(mShaderKey);
678 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700679 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
680 switch (mShaderTileX) {
681 case GL_REPEAT:
682 tileMode = SkShader::kRepeat_TileMode;
683 break;
684 case GL_MIRRORED_REPEAT:
685 tileMode = SkShader::kMirror_TileMode;
686 break;
687 }
688
Romain Guyc0ac1932010-07-19 18:43:02 -0700689 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700690 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700691 }
692
693 mModelView.loadTranslate(left, top, 0.0f);
694 mModelView.scale(right - left, bottom - top, 1.0f);
695
696 useProgram(mDrawLinearGradientProgram);
697 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
698
699 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700700 bindTexture(texture->id, mShaderTileX, mShaderTileY);
Romain Guyc0ac1932010-07-19 18:43:02 -0700701
702 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
703 if (mShaderMatrix) {
704 mat4 shaderMatrix(*mShaderMatrix);
705 shaderMatrix.mapRect(start);
706 }
707 mSnapshot->transform.mapRect(start);
708
709 const float gradientX = start.right - start.left;
710 const float gradientY = start.bottom - start.top;
711
712 mat4 screenSpace(mSnapshot->transform);
713 screenSpace.multiply(mModelView);
714
715 // Always premultiplied
716 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha);
717 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top);
718 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY);
719 glUniform1f(mDrawLinearGradientProgram->gradientLength,
720 1.0f / (gradientX * gradientX + gradientY * gradientY));
721 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE,
722 &screenSpace.data[0]);
723
724 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700725 gMeshStride, &mMeshVertices[0].position[0]);
Romain Guyc0ac1932010-07-19 18:43:02 -0700726
Romain Guyac670c02010-07-27 17:39:27 -0700727 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyc0ac1932010-07-19 18:43:02 -0700728}
729
Romain Guyd27977d2010-07-14 19:18:51 -0700730void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
731 float alpha, SkXfermode::Mode mode) {
732 const Texture* texture = mTextureCache.get(mShaderBitmap);
733
734 const float width = texture->width;
735 const float height = texture->height;
736
737 // This could be done in the vertex shader but we have only 4 vertices
738 float u1 = 0.0f;
739 float v1 = 0.0f;
740 float u2 = right - left;
741 float v2 = bottom - top;
742
Romain Guy694b5192010-07-21 21:33:20 -0700743 // TODO: If the texture is not pow, use a shader to support repeat/mirror
Romain Guyd27977d2010-07-14 19:18:51 -0700744 if (mShaderMatrix) {
745 SkMatrix inverse;
746 mShaderMatrix->invert(&inverse);
747 mat4 m(inverse);
748 Rect r(u1, v1, u2, v2);
749 m.mapRect(r);
750
751 u1 = r.left;
752 u2 = r.right;
753 v1 = r.top;
754 v2 = r.bottom;
755 }
756
757 u1 /= width;
758 u2 /= width;
759 v1 /= height;
760 v2 /= height;
761
762 resetDrawTextureTexCoords(u1, v1, u2, v2);
763
764 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700765 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyd27977d2010-07-14 19:18:51 -0700766
767 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
768}
769
Romain Guy82ba8142010-07-09 13:25:56 -0700770void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700771 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700772 int alpha;
773 SkXfermode::Mode mode;
774 getAlphaAndMode(paint, &alpha, &mode);
775
Romain Guya9794742010-07-13 11:37:54 -0700776 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700777 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700778}
779
Romain Guybd6b79b2010-06-26 00:13:53 -0700780void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700781 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
782 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700783 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700784}
785
786void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700787 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700788 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700789 mModelView.loadTranslate(left, top, 0.0f);
790 mModelView.scale(right - left, bottom - top, 1.0f);
791
Romain Guyd27977d2010-07-14 19:18:51 -0700792 useProgram(mDrawTextureProgram);
793 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700794
Romain Guya9794742010-07-13 11:37:54 -0700795 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700796 bindTexture(texture, mShaderTileX, mShaderTileY);
Romain Guyc1396e92010-06-30 17:56:19 -0700797
Romain Guya9794742010-07-13 11:37:54 -0700798 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700799 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700800
Romain Guyd27977d2010-07-14 19:18:51 -0700801 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700802 gMeshStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700803 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700804 gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700805
Romain Guyf7f93552010-07-08 19:17:03 -0700806 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700807 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700808 } else {
809 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
810 }
Romain Guy82ba8142010-07-09 13:25:56 -0700811}
812
813void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
814 // In theory we should not blend if the mode is Src, but it's rare enough
815 // that it's not worth it
816 blend = blend || mode != SkXfermode::kSrcOver_Mode;
817 if (blend) {
818 if (!mBlend) {
819 glEnable(GL_BLEND);
820 }
821
822 GLenum sourceMode = gBlends[mode].src;
823 GLenum destMode = gBlends[mode].dst;
824 if (!isPremultiplied && sourceMode == GL_ONE) {
825 sourceMode = GL_SRC_ALPHA;
826 }
827
828 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
829 glBlendFunc(sourceMode, destMode);
830 mLastSrcMode = sourceMode;
831 mLastDstMode = destMode;
832 }
833 } else if (mBlend) {
834 glDisable(GL_BLEND);
835 }
836 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700837}
838
Romain Guyd27977d2010-07-14 19:18:51 -0700839bool OpenGLRenderer::useProgram(const sp<Program>& program) {
840 if (!program->isInUse()) {
841 mCurrentProgram->remove();
842 program->use();
843 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700844 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700845 }
Romain Guy6926c722010-07-12 20:20:03 -0700846 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700847}
848
Romain Guy026c5e162010-06-28 17:12:22 -0700849void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700850 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700851 TextureVertex::setUV(v++, u1, v1);
852 TextureVertex::setUV(v++, u2, v1);
853 TextureVertex::setUV(v++, u1, v2);
854 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700855}
856
857void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
858 if (paint) {
859 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
860 if (!isMode) {
861 // Assume SRC_OVER
862 *mode = SkXfermode::kSrcOver_Mode;
863 }
864
865 // Skia draws using the color's alpha channel if < 255
866 // Otherwise, it uses the paint's alpha
867 int color = paint->getColor();
868 *alpha = (color >> 24) & 0xFF;
869 if (*alpha == 255) {
870 *alpha = paint->getAlpha();
871 }
872 } else {
873 *mode = SkXfermode::kSrcOver_Mode;
874 *alpha = 255;
875 }
Romain Guy026c5e162010-06-28 17:12:22 -0700876}
877
Romain Guya1db5742010-07-20 13:09:13 -0700878void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) {
879 if (texture != mLastTexture) {
880 glBindTexture(GL_TEXTURE_2D, texture);
881 mLastTexture = texture;
882 }
883 // TODO: Don't set the texture parameters every time
884 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
885 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
886}
887
Romain Guy9d5316e2010-06-24 19:30:36 -0700888}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700889}; // namespace android