blob: 58a3a69ea601880aa0b1649bb325d9e9c90fce91 [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 Guye4d01122010-06-16 18:44:05 -070030
31namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070032namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Defines
36///////////////////////////////////////////////////////////////////////////////
37
Romain Guy121e22422010-07-01 18:26:52 -070038// These properties are defined in mega-bytes
39#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
40#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
Romain Guyc0ac1932010-07-19 18:43:02 -070041#define PROPERTY_GRADIENT_CACHE_SIZE "ro.hwui.gradient_cache_size"
Romain Guy121e22422010-07-01 18:26:52 -070042
Romain Guyc0ac1932010-07-19 18:43:02 -070043#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
44#define DEFAULT_LAYER_CACHE_SIZE 10.0f
Romain Guyf7f93552010-07-08 19:17:03 -070045#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070046#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guydda570202010-07-06 11:39:32 -070047
Romain Guy121e22422010-07-01 18:26:52 -070048// Converts a number of mega-bytes into bytes
49#define MB(s) s * 1024 * 1024
50
Romain Guydda570202010-07-06 11:39:32 -070051// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070052#define SV(x, y) { { x, y } }
53#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070054
55///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy026c5e162010-06-28 17:12:22 -070059static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070060 SV(0.0f, 0.0f),
61 SV(1.0f, 0.0f),
62 SV(0.0f, 1.0f),
63 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070064};
Romain Guy026c5e162010-06-28 17:12:22 -070065static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
66static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070067
Romain Guy026c5e162010-06-28 17:12:22 -070068// This array is never used directly but used as a memcpy source in the
69// OpenGLRenderer constructor
70static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070071 FV(0.0f, 0.0f, 0.0f, 0.0f),
72 FV(1.0f, 0.0f, 1.0f, 0.0f),
73 FV(0.0f, 1.0f, 0.0f, 1.0f),
74 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070075};
Romain Guy026c5e162010-06-28 17:12:22 -070076static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
77static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070078
Romain Guy026c5e162010-06-28 17:12:22 -070079// In this array, the index of each Blender equals the value of the first
80// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
81static const Blender gBlends[] = {
82 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
83 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
84 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
85 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
87 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
89 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
94};
Romain Guye4d01122010-06-16 18:44:05 -070095
Romain Guyd27977d2010-07-14 19:18:51 -070096static const GLint gTileModes[] = {
97 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
98 GL_REPEAT, // == SkShader::kRepeat_Mode
99 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
100};
101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guydda570202010-07-06 11:39:32 -0700106OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700107 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -0700108 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700109 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700110 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700111 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700112 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700113
Romain Guy121e22422010-07-01 18:26:52 -0700114 char property[PROPERTY_VALUE_MAX];
115 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700116 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700117 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700118 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700119 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda570202010-07-06 11:39:32 -0700120 }
121
122 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
123 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700124 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700125 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700126 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
127 }
128
129 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
130 LOGD(" Setting gradient cache size to %sMB", property);
131 mLayerCache.setMaxSize(MB(atof(property)));
132 } else {
133 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700134 }
135
Romain Guyd27977d2010-07-14 19:18:51 -0700136 mDrawColorProgram = new DrawColorProgram;
137 mDrawTextureProgram = new DrawTextureProgram;
Romain Guy694b5192010-07-21 21:33:20 -0700138 mDrawTextProgram = new DrawTextProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700139 mDrawLinearGradientProgram = new DrawLinearGradientProgram;
Romain Guyd27977d2010-07-14 19:18:51 -0700140 mCurrentProgram = mDrawTextureProgram;
141
142 mShader = kShaderNone;
Romain Guya1db5742010-07-20 13:09:13 -0700143 mShaderTileX = GL_CLAMP_TO_EDGE;
144 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700145 mShaderMatrix = NULL;
146 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700147
Romain Guy1e793862010-07-16 15:07:42 -0700148 mLastTexture = 0;
149
Romain Guy026c5e162010-06-28 17:12:22 -0700150 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700151}
152
Romain Guy85bf02f2010-06-22 13:11:24 -0700153OpenGLRenderer::~OpenGLRenderer() {
154 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700155
156 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700157 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700158 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700159 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700160}
161
Romain Guyf6a11b82010-06-23 17:47:49 -0700162///////////////////////////////////////////////////////////////////////////////
163// Setup
164///////////////////////////////////////////////////////////////////////////////
165
Romain Guy85bf02f2010-06-22 13:11:24 -0700166void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700167 glViewport(0, 0, width, height);
168
Romain Guy260e1022010-07-12 14:41:06 -0700169 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700170
171 mWidth = width;
172 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700173 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700174}
175
Romain Guy85bf02f2010-06-22 13:11:24 -0700176void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 mSnapshot = &mFirstSnapshot;
178 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700179
Romain Guy08ae3172010-06-21 19:35:50 -0700180 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700181
Romain Guy08ae3172010-06-21 19:35:50 -0700182 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
183 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy08ae3172010-06-21 19:35:50 -0700185 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700186 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700187
Romain Guybb9524b2010-06-22 18:56:38 -0700188 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
189}
190
Romain Guyf6a11b82010-06-23 17:47:49 -0700191///////////////////////////////////////////////////////////////////////////////
192// State management
193///////////////////////////////////////////////////////////////////////////////
194
Romain Guybb9524b2010-06-22 18:56:38 -0700195int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700197}
198
199int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700201}
202
203void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700205
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 if (restoreSnapshot()) {
207 setScissorFromClip();
208 }
Romain Guybb9524b2010-06-22 18:56:38 -0700209}
210
211void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700213
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700215
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 while (mSaveCount != saveCount - 1) {
217 restoreClip |= restoreSnapshot();
218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 if (restoreClip) {
221 setScissorFromClip();
222 }
Romain Guybb9524b2010-06-22 18:56:38 -0700223}
224
225int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 mSnapshot = new Snapshot(mSnapshot);
227 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700228}
229
230bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700232 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700233 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700234
Romain Guybd6b79b2010-06-26 00:13:53 -0700235 sp<Snapshot> current = mSnapshot;
236 sp<Snapshot> previous = mSnapshot->previous;
237
Romain Guyf86ef572010-07-01 11:05:42 -0700238 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700239 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700240 }
241
Romain Guybd6b79b2010-06-26 00:13:53 -0700242 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700243 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700244 }
245
246 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700247 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700248
Romain Guy7ae7ac42010-06-25 13:46:18 -0700249 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guyd55a8612010-06-28 17:42:46 -0700252void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700253 if (!current->layer) {
254 LOGE("Attempting to compose a layer that does not exist");
255 return;
256 }
257
Romain Guyd55a8612010-06-28 17:42:46 -0700258 // Unbind current FBO and restore previous one
259 // Most of the time, previous->fbo will be 0 to bind the default buffer
260 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
261
262 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700263 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700264 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
265
Romain Guydda570202010-07-06 11:39:32 -0700266 Layer* layer = current->layer;
267
Romain Guyd55a8612010-06-28 17:42:46 -0700268 // Compute the correct texture coordinates for the FBO texture
269 // The texture is currently as big as the window but drawn with
270 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700271 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700272
Romain Guydda570202010-07-06 11:39:32 -0700273 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700274 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700275
Romain Guydda570202010-07-06 11:39:32 -0700276 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700277 // Failing to add the layer to the cache should happen only if the
278 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700279 if (!mLayerCache.put(size, layer)) {
280 LAYER_LOGD("Deleting layer");
281
282 glDeleteFramebuffers(1, &layer->fbo);
283 glDeleteTextures(1, &layer->texture);
284
285 delete layer;
286 }
Romain Guyd55a8612010-06-28 17:42:46 -0700287}
288
Romain Guyf6a11b82010-06-23 17:47:49 -0700289///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700290// Layers
291///////////////////////////////////////////////////////////////////////////////
292
293int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
294 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700295 int count = saveSnapshot();
296
297 int alpha = 255;
298 SkXfermode::Mode mode;
299
300 if (p) {
301 alpha = p->getAlpha();
302 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
303 if (!isMode) {
304 // Assume SRC_OVER
305 mode = SkXfermode::kSrcOver_Mode;
306 }
307 } else {
308 mode = SkXfermode::kSrcOver_Mode;
309 }
310
311 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
312
313 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700314}
315
316int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
317 int alpha, int flags) {
318 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700319 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
320 return count;
321}
Romain Guybd6b79b2010-06-26 00:13:53 -0700322
Romain Guyd55a8612010-06-28 17:42:46 -0700323bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
324 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700325
Romain Guyd27977d2010-07-14 19:18:51 -0700326 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700327 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700328
Romain Guyf18fd992010-07-08 11:45:51 -0700329 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
330 LayerSize size(right - left, bottom - top);
331
Romain Guyf18fd992010-07-08 11:45:51 -0700332 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700333 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700334 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700335 }
336
Romain Guyf18fd992010-07-08 11:45:51 -0700337 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
338
Romain Guyf86ef572010-07-01 11:05:42 -0700339 // Clear the FBO
340 glDisable(GL_SCISSOR_TEST);
341 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
342 glClear(GL_COLOR_BUFFER_BIT);
343 glEnable(GL_SCISSOR_TEST);
344
Romain Guydda570202010-07-06 11:39:32 -0700345 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700346 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700347 layer->mode = mode;
348 layer->alpha = alpha / 255.0f;
349 layer->layer.set(left, top, right, bottom);
350
351 snapshot->layer = layer;
352 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700353
Romain Guyf86ef572010-07-01 11:05:42 -0700354 // Creates a new snapshot to draw into the FBO
355 saveSnapshot();
356 // TODO: This doesn't preserve other transformations (check Skia first)
357 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700358 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700359 mSnapshot->height = bottom - top;
360 setScissorFromClip();
361
Romain Guy079ba2c2010-07-16 14:12:24 -0700362 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700363 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700364
365 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700366 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700367
Romain Guyd55a8612010-06-28 17:42:46 -0700368 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700369}
370
371///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700372// Transforms
373///////////////////////////////////////////////////////////////////////////////
374
375void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700376 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700377}
378
379void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700381}
382
383void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700384 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700385}
386
387void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700389}
390
391void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700392 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700393}
394
395void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700396 mat4 m(*matrix);
397 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700398}
399
400///////////////////////////////////////////////////////////////////////////////
401// Clipping
402///////////////////////////////////////////////////////////////////////////////
403
Romain Guybb9524b2010-06-22 18:56:38 -0700404void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700405 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700406 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700407}
408
409const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700410 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700411}
412
Romain Guyc7d53492010-06-25 13:41:57 -0700413bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700414 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700415 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700416 return !mSnapshot->clipRect.intersects(r);
417}
418
Romain Guy079ba2c2010-07-16 14:12:24 -0700419bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
420 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700421 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700422 setScissorFromClip();
423 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700424 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700425}
426
Romain Guyf6a11b82010-06-23 17:47:49 -0700427///////////////////////////////////////////////////////////////////////////////
428// Drawing
429///////////////////////////////////////////////////////////////////////////////
430
Romain Guyc1396e92010-06-30 17:56:19 -0700431void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700432 const float right = left + bitmap->width();
433 const float bottom = top + bitmap->height();
434
435 if (quickReject(left, top, right, bottom)) {
436 return;
437 }
438
Romain Guyf86ef572010-07-01 11:05:42 -0700439 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700440 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700441}
442
Romain Guyf86ef572010-07-01 11:05:42 -0700443void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
444 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
445 const mat4 transform(*matrix);
446 transform.mapRect(r);
447
Romain Guy6926c722010-07-12 20:20:03 -0700448 if (quickReject(r.left, r.top, r.right, r.bottom)) {
449 return;
450 }
451
Romain Guyf86ef572010-07-01 11:05:42 -0700452 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700453 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700454}
455
Romain Guy8ba548f2010-06-30 19:21:21 -0700456void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
457 float srcLeft, float srcTop, float srcRight, float srcBottom,
458 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700459 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700460 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
461 return;
462 }
463
Romain Guyf86ef572010-07-01 11:05:42 -0700464 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700465
Romain Guy8ba548f2010-06-30 19:21:21 -0700466 const float width = texture->width;
467 const float height = texture->height;
468
469 const float u1 = srcLeft / width;
470 const float v1 = srcTop / height;
471 const float u2 = srcRight / width;
472 const float v2 = srcBottom / height;
473
474 resetDrawTextureTexCoords(u1, v1, u2, v2);
475
Romain Guy82ba8142010-07-09 13:25:56 -0700476 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700477
478 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
479}
480
Romain Guydeba7852010-07-07 17:54:48 -0700481void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
482 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700483 if (quickReject(left, top, right, bottom)) {
484 return;
485 }
486
Romain Guyf7f93552010-07-08 19:17:03 -0700487 const Texture* texture = mTextureCache.get(bitmap);
488
489 int alpha;
490 SkXfermode::Mode mode;
491 getAlphaAndMode(paint, &alpha, &mode);
492
Romain Guyf7f93552010-07-08 19:17:03 -0700493 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700494 mesh->updateVertices(bitmap, left, top, right, bottom,
495 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700496
497 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
498 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700499 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
500 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700501 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700502}
503
Romain Guy85bf02f2010-06-22 13:11:24 -0700504void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700505 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700506 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700507}
Romain Guy9d5316e2010-06-24 19:30:36 -0700508
Romain Guybd6b79b2010-06-26 00:13:53 -0700509void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700510 if (quickReject(left, top, right, bottom)) {
511 return;
512 }
513
Romain Guy026c5e162010-06-28 17:12:22 -0700514 SkXfermode::Mode mode;
515
516 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
517 if (!isMode) {
518 // Assume SRC_OVER
519 mode = SkXfermode::kSrcOver_Mode;
520 }
521
522 // Skia draws using the color's alpha channel if < 255
523 // Otherwise, it uses the paint's alpha
524 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700525 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700526 color |= p->getAlpha() << 24;
527 }
528
529 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700530}
Romain Guy9d5316e2010-06-24 19:30:36 -0700531
Romain Guy694b5192010-07-21 21:33:20 -0700532void OpenGLRenderer::drawText(const char* text, int count, float x, float y, SkPaint* paint) {
Romain Guy694b5192010-07-21 21:33:20 -0700533 int alpha;
534 SkXfermode::Mode mode;
535 getAlphaAndMode(paint, &alpha, &mode);
536
537 uint32_t color = paint->getColor();
538 const GLfloat a = alpha / 255.0f;
539 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
540 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
541 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
542
543 mModelView.loadIdentity();
544
545 useProgram(mDrawTextProgram);
546 mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
547
548 chooseBlending(true, mode);
549 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
550
551 // Always premultiplied
552 glUniform4f(mDrawTextProgram->color, r, g, b, a);
553
Romain Guy09147fb2010-07-22 13:08:20 -0700554 // TODO: Implement scale properly
555 const Rect& clip = mSnapshot->getLocalClip();
556
Romain Guy694b5192010-07-21 21:33:20 -0700557 mFontRenderer.setFont(SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guy09147fb2010-07-22 13:08:20 -0700558 mFontRenderer.renderText(paint, &clip, text, count, 0, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700559
560 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
561}
562
Romain Guy6926c722010-07-12 20:20:03 -0700563///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700564// Shaders
565///////////////////////////////////////////////////////////////////////////////
566
567void OpenGLRenderer::resetShader() {
568 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700569 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700570 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700571 mShaderTileX = GL_CLAMP_TO_EDGE;
572 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700573}
574
575void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
576 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700577 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700578 mShaderBlend = hasAlpha;
579 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700580 mShaderTileX = gTileModes[tileX];
581 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700582 mShaderMatrix = matrix;
583}
584
Romain Guyc0ac1932010-07-19 18:43:02 -0700585void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
586 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix,
587 bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700588 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700589 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700590 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700591 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700592 mShaderTileX = gTileModes[tileMode];
593 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700594 mShaderMatrix = matrix;
595 mShaderBounds = bounds;
596 mShaderColors = colors;
597 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700598 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700599}
600
Romain Guyd27977d2010-07-14 19:18:51 -0700601///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700602// Drawing implementation
603///////////////////////////////////////////////////////////////////////////////
604
Romain Guy026c5e162010-06-28 17:12:22 -0700605void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700606 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700607 // If a shader is set, preserve only the alpha
608 if (mShader != kShaderNone) {
609 color |= 0x00ffffff;
610 }
611
612 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700613 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700614 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700615
616 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700617 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700618 drawBitmapShader(left, top, right, bottom, a, mode);
619 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700620 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700621 drawLinearGradientShader(left, top, right, bottom, a, mode);
622 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700623 default:
624 break;
625 }
Romain Guy026c5e162010-06-28 17:12:22 -0700626
Romain Guyc0ac1932010-07-19 18:43:02 -0700627 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
628 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
629 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
630
Romain Guy6926c722010-07-12 20:20:03 -0700631 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700632 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700633
Romain Guyc7d53492010-06-25 13:41:57 -0700634 mModelView.loadTranslate(left, top, 0.0f);
635 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700636
Romain Guy079ba2c2010-07-16 14:12:24 -0700637 if (!useProgram(mDrawColorProgram)) {
Romain Guy6926c722010-07-12 20:20:03 -0700638 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy079ba2c2010-07-16 14:12:24 -0700639 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700640 gDrawColorVertexStride, p);
641 }
Romain Guyd27977d2010-07-14 19:18:51 -0700642
643 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700644 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700645 } else {
646 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700647 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700648 }
649
Romain Guy079ba2c2010-07-16 14:12:24 -0700650 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700651
Romain Guyc7d53492010-06-25 13:41:57 -0700652 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700653}
Romain Guy026c5e162010-06-28 17:12:22 -0700654
Romain Guyc0ac1932010-07-19 18:43:02 -0700655void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
656 float alpha, SkXfermode::Mode mode) {
657 Texture* texture = mGradientCache.get(mShaderKey);
658 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700659 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
660 switch (mShaderTileX) {
661 case GL_REPEAT:
662 tileMode = SkShader::kRepeat_TileMode;
663 break;
664 case GL_MIRRORED_REPEAT:
665 tileMode = SkShader::kMirror_TileMode;
666 break;
667 }
668
Romain Guyc0ac1932010-07-19 18:43:02 -0700669 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700670 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700671 }
672
673 mModelView.loadTranslate(left, top, 0.0f);
674 mModelView.scale(right - left, bottom - top, 1.0f);
675
676 useProgram(mDrawLinearGradientProgram);
677 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
678
679 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700680 bindTexture(texture->id, mShaderTileX, mShaderTileY);
Romain Guyc0ac1932010-07-19 18:43:02 -0700681
682 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
683 if (mShaderMatrix) {
684 mat4 shaderMatrix(*mShaderMatrix);
685 shaderMatrix.mapRect(start);
686 }
687 mSnapshot->transform.mapRect(start);
688
689 const float gradientX = start.right - start.left;
690 const float gradientY = start.bottom - start.top;
691
692 mat4 screenSpace(mSnapshot->transform);
693 screenSpace.multiply(mModelView);
694
695 // Always premultiplied
696 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha);
697 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top);
698 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY);
699 glUniform1f(mDrawLinearGradientProgram->gradientLength,
700 1.0f / (gradientX * gradientX + gradientY * gradientY));
701 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE,
702 &screenSpace.data[0]);
703
704 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE,
705 gDrawTextureVertexStride, &mDrawTextureVertices[0].position[0]);
706
707 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
708}
709
Romain Guyd27977d2010-07-14 19:18:51 -0700710void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
711 float alpha, SkXfermode::Mode mode) {
712 const Texture* texture = mTextureCache.get(mShaderBitmap);
713
714 const float width = texture->width;
715 const float height = texture->height;
716
717 // This could be done in the vertex shader but we have only 4 vertices
718 float u1 = 0.0f;
719 float v1 = 0.0f;
720 float u2 = right - left;
721 float v2 = bottom - top;
722
Romain Guy694b5192010-07-21 21:33:20 -0700723 // TODO: If the texture is not pow, use a shader to support repeat/mirror
Romain Guyd27977d2010-07-14 19:18:51 -0700724 if (mShaderMatrix) {
725 SkMatrix inverse;
726 mShaderMatrix->invert(&inverse);
727 mat4 m(inverse);
728 Rect r(u1, v1, u2, v2);
729 m.mapRect(r);
730
731 u1 = r.left;
732 u2 = r.right;
733 v1 = r.top;
734 v2 = r.bottom;
735 }
736
737 u1 /= width;
738 u2 /= width;
739 v1 /= height;
740 v2 /= height;
741
742 resetDrawTextureTexCoords(u1, v1, u2, v2);
743
744 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
745 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
746
747 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
748}
749
Romain Guy82ba8142010-07-09 13:25:56 -0700750void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700751 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700752 int alpha;
753 SkXfermode::Mode mode;
754 getAlphaAndMode(paint, &alpha, &mode);
755
Romain Guya9794742010-07-13 11:37:54 -0700756 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
757 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700758}
759
Romain Guybd6b79b2010-06-26 00:13:53 -0700760void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700761 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
762 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700763 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
764}
765
766void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700767 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700768 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700769 mModelView.loadTranslate(left, top, 0.0f);
770 mModelView.scale(right - left, bottom - top, 1.0f);
771
Romain Guyd27977d2010-07-14 19:18:51 -0700772 useProgram(mDrawTextureProgram);
773 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700774
Romain Guya9794742010-07-13 11:37:54 -0700775 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700776 bindTexture(texture, mShaderTileX, mShaderTileY);
Romain Guyc1396e92010-06-30 17:56:19 -0700777
Romain Guya9794742010-07-13 11:37:54 -0700778 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700779 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700780
Romain Guyd27977d2010-07-14 19:18:51 -0700781 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700782 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700783 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700784 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700785
Romain Guyf7f93552010-07-08 19:17:03 -0700786 if (!indices) {
787 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
788 } else {
789 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
790 }
Romain Guy82ba8142010-07-09 13:25:56 -0700791}
792
793void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
794 // In theory we should not blend if the mode is Src, but it's rare enough
795 // that it's not worth it
796 blend = blend || mode != SkXfermode::kSrcOver_Mode;
797 if (blend) {
798 if (!mBlend) {
799 glEnable(GL_BLEND);
800 }
801
802 GLenum sourceMode = gBlends[mode].src;
803 GLenum destMode = gBlends[mode].dst;
804 if (!isPremultiplied && sourceMode == GL_ONE) {
805 sourceMode = GL_SRC_ALPHA;
806 }
807
808 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
809 glBlendFunc(sourceMode, destMode);
810 mLastSrcMode = sourceMode;
811 mLastDstMode = destMode;
812 }
813 } else if (mBlend) {
814 glDisable(GL_BLEND);
815 }
816 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700817}
818
Romain Guyd27977d2010-07-14 19:18:51 -0700819bool OpenGLRenderer::useProgram(const sp<Program>& program) {
820 if (!program->isInUse()) {
821 mCurrentProgram->remove();
822 program->use();
823 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700824 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700825 }
Romain Guy6926c722010-07-12 20:20:03 -0700826 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700827}
828
Romain Guy026c5e162010-06-28 17:12:22 -0700829void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700830 TextureVertex* v = &mDrawTextureVertices[0];
831 TextureVertex::setUV(v++, u1, v1);
832 TextureVertex::setUV(v++, u2, v1);
833 TextureVertex::setUV(v++, u1, v2);
834 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700835}
836
837void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
838 if (paint) {
839 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
840 if (!isMode) {
841 // Assume SRC_OVER
842 *mode = SkXfermode::kSrcOver_Mode;
843 }
844
845 // Skia draws using the color's alpha channel if < 255
846 // Otherwise, it uses the paint's alpha
847 int color = paint->getColor();
848 *alpha = (color >> 24) & 0xFF;
849 if (*alpha == 255) {
850 *alpha = paint->getAlpha();
851 }
852 } else {
853 *mode = SkXfermode::kSrcOver_Mode;
854 *alpha = 255;
855 }
Romain Guy026c5e162010-06-28 17:12:22 -0700856}
857
Romain Guya1db5742010-07-20 13:09:13 -0700858void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) {
859 if (texture != mLastTexture) {
860 glBindTexture(GL_TEXTURE_2D, texture);
861 mLastTexture = texture;
862 }
863 // TODO: Don't set the texture parameters every time
864 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
865 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
866}
867
Romain Guy9d5316e2010-06-24 19:30:36 -0700868}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700869}; // namespace android