blob: 2e44e122ea398344f85c253f0381fa586fb990ee [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 SV(x, y) { { x, y } }
49#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070050
51///////////////////////////////////////////////////////////////////////////////
52// Globals
53///////////////////////////////////////////////////////////////////////////////
54
Romain Guy026c5e162010-06-28 17:12:22 -070055static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070056 SV(0.0f, 0.0f),
57 SV(1.0f, 0.0f),
58 SV(0.0f, 1.0f),
59 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070060};
Romain Guy026c5e162010-06-28 17:12:22 -070061static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
62static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070063
Romain Guy026c5e162010-06-28 17:12:22 -070064// This array is never used directly but used as a memcpy source in the
65// OpenGLRenderer constructor
66static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070067 FV(0.0f, 0.0f, 0.0f, 0.0f),
68 FV(1.0f, 0.0f, 1.0f, 0.0f),
69 FV(0.0f, 1.0f, 0.0f, 1.0f),
70 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070071};
Romain Guy026c5e162010-06-28 17:12:22 -070072static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
73static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070074
Romain Guy026c5e162010-06-28 17:12:22 -070075// In this array, the index of each Blender equals the value of the first
76// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
77static const Blender gBlends[] = {
78 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
79 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
80 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
81 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
83 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
84 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
85 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
86 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
89 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
90};
Romain Guye4d01122010-06-16 18:44:05 -070091
Romain Guyd27977d2010-07-14 19:18:51 -070092static const GLint gTileModes[] = {
93 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
94 GL_REPEAT, // == SkShader::kRepeat_Mode
95 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
96};
97
Romain Guyf6a11b82010-06-23 17:47:49 -070098///////////////////////////////////////////////////////////////////////////////
99// Constructors/destructor
100///////////////////////////////////////////////////////////////////////////////
101
Romain Guydda570202010-07-06 11:39:32 -0700102OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700103 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -0700104 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700105 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700106 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700107 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700108 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700109
Romain Guy121e22422010-07-01 18:26:52 -0700110 char property[PROPERTY_VALUE_MAX];
111 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700112 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700113 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700114 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700115 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda570202010-07-06 11:39:32 -0700116 }
117
118 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
119 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700120 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700121 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700122 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
123 }
124
125 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
126 LOGD(" Setting gradient cache size to %sMB", property);
127 mLayerCache.setMaxSize(MB(atof(property)));
128 } else {
129 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700130 }
131
Romain Guyd27977d2010-07-14 19:18:51 -0700132 mDrawColorProgram = new DrawColorProgram;
133 mDrawTextureProgram = new DrawTextureProgram;
Romain Guy694b5192010-07-21 21:33:20 -0700134 mDrawTextProgram = new DrawTextProgram;
Romain Guyf9764a42010-07-16 23:13:33 -0700135 mDrawLinearGradientProgram = new DrawLinearGradientProgram;
Romain Guyd27977d2010-07-14 19:18:51 -0700136 mCurrentProgram = mDrawTextureProgram;
137
138 mShader = kShaderNone;
Romain Guya1db5742010-07-20 13:09:13 -0700139 mShaderTileX = GL_CLAMP_TO_EDGE;
140 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700141 mShaderMatrix = NULL;
142 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700143
Romain Guy1e793862010-07-16 15:07:42 -0700144 mLastTexture = 0;
145
Romain Guy026c5e162010-06-28 17:12:22 -0700146 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700147}
148
Romain Guy85bf02f2010-06-22 13:11:24 -0700149OpenGLRenderer::~OpenGLRenderer() {
150 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700151
152 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700153 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700154 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700155 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700156}
157
Romain Guyf6a11b82010-06-23 17:47:49 -0700158///////////////////////////////////////////////////////////////////////////////
159// Setup
160///////////////////////////////////////////////////////////////////////////////
161
Romain Guy85bf02f2010-06-22 13:11:24 -0700162void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700163 glViewport(0, 0, width, height);
164
Romain Guy260e1022010-07-12 14:41:06 -0700165 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700166
167 mWidth = width;
168 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700169 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700170}
171
Romain Guy85bf02f2010-06-22 13:11:24 -0700172void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700173 mSnapshot = &mFirstSnapshot;
174 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700175
Romain Guy08ae3172010-06-21 19:35:50 -0700176 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700177
Romain Guy08ae3172010-06-21 19:35:50 -0700178 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
179 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700180
Romain Guy08ae3172010-06-21 19:35:50 -0700181 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700182 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700183
Romain Guybb9524b2010-06-22 18:56:38 -0700184 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
185}
186
Romain Guyf6a11b82010-06-23 17:47:49 -0700187///////////////////////////////////////////////////////////////////////////////
188// State management
189///////////////////////////////////////////////////////////////////////////////
190
Romain Guybb9524b2010-06-22 18:56:38 -0700191int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700192 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700193}
194
195int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700197}
198
199void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700201
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 if (restoreSnapshot()) {
203 setScissorFromClip();
204 }
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
207void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700209
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700211
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 while (mSaveCount != saveCount - 1) {
213 restoreClip |= restoreSnapshot();
214 }
Romain Guybb9524b2010-06-22 18:56:38 -0700215
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 if (restoreClip) {
217 setScissorFromClip();
218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
221int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700222 mSnapshot = new Snapshot(mSnapshot);
223 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700227 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700228 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700229 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700230
Romain Guybd6b79b2010-06-26 00:13:53 -0700231 sp<Snapshot> current = mSnapshot;
232 sp<Snapshot> previous = mSnapshot->previous;
233
Romain Guyf86ef572010-07-01 11:05:42 -0700234 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700235 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700236 }
237
Romain Guybd6b79b2010-06-26 00:13:53 -0700238 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700239 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700240 }
241
242 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700243 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700244
Romain Guy7ae7ac42010-06-25 13:46:18 -0700245 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700246}
247
Romain Guyd55a8612010-06-28 17:42:46 -0700248void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700249 if (!current->layer) {
250 LOGE("Attempting to compose a layer that does not exist");
251 return;
252 }
253
Romain Guyd55a8612010-06-28 17:42:46 -0700254 // Unbind current FBO and restore previous one
255 // Most of the time, previous->fbo will be 0 to bind the default buffer
256 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
257
258 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700259 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700260 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
261
Romain Guydda570202010-07-06 11:39:32 -0700262 Layer* layer = current->layer;
263
Romain Guyd55a8612010-06-28 17:42:46 -0700264 // Compute the correct texture coordinates for the FBO texture
265 // The texture is currently as big as the window but drawn with
266 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700267 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700268
Romain Guydda570202010-07-06 11:39:32 -0700269 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700270 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700271
Romain Guydda570202010-07-06 11:39:32 -0700272 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700273 // Failing to add the layer to the cache should happen only if the
274 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700275 if (!mLayerCache.put(size, layer)) {
276 LAYER_LOGD("Deleting layer");
277
278 glDeleteFramebuffers(1, &layer->fbo);
279 glDeleteTextures(1, &layer->texture);
280
281 delete layer;
282 }
Romain Guyd55a8612010-06-28 17:42:46 -0700283}
284
Romain Guyf6a11b82010-06-23 17:47:49 -0700285///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700286// Layers
287///////////////////////////////////////////////////////////////////////////////
288
289int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
290 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700291 int count = saveSnapshot();
292
293 int alpha = 255;
294 SkXfermode::Mode mode;
295
296 if (p) {
297 alpha = p->getAlpha();
298 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
299 if (!isMode) {
300 // Assume SRC_OVER
301 mode = SkXfermode::kSrcOver_Mode;
302 }
303 } else {
304 mode = SkXfermode::kSrcOver_Mode;
305 }
306
307 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
308
309 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700310}
311
312int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
313 int alpha, int flags) {
314 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700315 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
316 return count;
317}
Romain Guybd6b79b2010-06-26 00:13:53 -0700318
Romain Guyd55a8612010-06-28 17:42:46 -0700319bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
320 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700321
Romain Guyd27977d2010-07-14 19:18:51 -0700322 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700323 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700324
Romain Guyf18fd992010-07-08 11:45:51 -0700325 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
326 LayerSize size(right - left, bottom - top);
327
Romain Guyf18fd992010-07-08 11:45:51 -0700328 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700329 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700330 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700331 }
332
Romain Guyf18fd992010-07-08 11:45:51 -0700333 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
334
Romain Guyf86ef572010-07-01 11:05:42 -0700335 // Clear the FBO
336 glDisable(GL_SCISSOR_TEST);
337 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
338 glClear(GL_COLOR_BUFFER_BIT);
339 glEnable(GL_SCISSOR_TEST);
340
Romain Guydda570202010-07-06 11:39:32 -0700341 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700342 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700343 layer->mode = mode;
344 layer->alpha = alpha / 255.0f;
345 layer->layer.set(left, top, right, bottom);
346
347 snapshot->layer = layer;
348 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700349
Romain Guyf86ef572010-07-01 11:05:42 -0700350 // Creates a new snapshot to draw into the FBO
351 saveSnapshot();
352 // TODO: This doesn't preserve other transformations (check Skia first)
353 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700354 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700355 mSnapshot->height = bottom - top;
356 setScissorFromClip();
357
Romain Guy079ba2c2010-07-16 14:12:24 -0700358 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700359 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700360
361 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700362 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700363
Romain Guyd55a8612010-06-28 17:42:46 -0700364 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700365}
366
367///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700368// Transforms
369///////////////////////////////////////////////////////////////////////////////
370
371void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700372 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700373}
374
375void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700376 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700377}
378
379void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700381}
382
383void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700384 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700385}
386
387void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700389}
390
391void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700392 mat4 m(*matrix);
393 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700394}
395
396///////////////////////////////////////////////////////////////////////////////
397// Clipping
398///////////////////////////////////////////////////////////////////////////////
399
Romain Guybb9524b2010-06-22 18:56:38 -0700400void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700401 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700402 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700403}
404
405const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700406 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700407}
408
Romain Guyc7d53492010-06-25 13:41:57 -0700409bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700410 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700411 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700412 return !mSnapshot->clipRect.intersects(r);
413}
414
Romain Guy079ba2c2010-07-16 14:12:24 -0700415bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
416 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700417 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 setScissorFromClip();
419 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700420 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700421}
422
Romain Guyf6a11b82010-06-23 17:47:49 -0700423///////////////////////////////////////////////////////////////////////////////
424// Drawing
425///////////////////////////////////////////////////////////////////////////////
426
Romain Guyc1396e92010-06-30 17:56:19 -0700427void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700428 const float right = left + bitmap->width();
429 const float bottom = top + bitmap->height();
430
431 if (quickReject(left, top, right, bottom)) {
432 return;
433 }
434
Romain Guyf86ef572010-07-01 11:05:42 -0700435 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700436 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700437}
438
Romain Guyf86ef572010-07-01 11:05:42 -0700439void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
440 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
441 const mat4 transform(*matrix);
442 transform.mapRect(r);
443
Romain Guy6926c722010-07-12 20:20:03 -0700444 if (quickReject(r.left, r.top, r.right, r.bottom)) {
445 return;
446 }
447
Romain Guyf86ef572010-07-01 11:05:42 -0700448 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700449 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700450}
451
Romain Guy8ba548f2010-06-30 19:21:21 -0700452void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
453 float srcLeft, float srcTop, float srcRight, float srcBottom,
454 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700455 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700456 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
457 return;
458 }
459
Romain Guyf86ef572010-07-01 11:05:42 -0700460 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700461
Romain Guy8ba548f2010-06-30 19:21:21 -0700462 const float width = texture->width;
463 const float height = texture->height;
464
465 const float u1 = srcLeft / width;
466 const float v1 = srcTop / height;
467 const float u2 = srcRight / width;
468 const float v2 = srcBottom / height;
469
470 resetDrawTextureTexCoords(u1, v1, u2, v2);
471
Romain Guy82ba8142010-07-09 13:25:56 -0700472 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700473
474 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
475}
476
Romain Guydeba7852010-07-07 17:54:48 -0700477void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
478 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700479 if (quickReject(left, top, right, bottom)) {
480 return;
481 }
482
Romain Guyf7f93552010-07-08 19:17:03 -0700483 const Texture* texture = mTextureCache.get(bitmap);
484
485 int alpha;
486 SkXfermode::Mode mode;
487 getAlphaAndMode(paint, &alpha, &mode);
488
Romain Guyf7f93552010-07-08 19:17:03 -0700489 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700490 mesh->updateVertices(bitmap, left, top, right, bottom,
491 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700492
493 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
494 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700495 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
496 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700497 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700498}
499
Romain Guy85bf02f2010-06-22 13:11:24 -0700500void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700501 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700502 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700503}
Romain Guy9d5316e2010-06-24 19:30:36 -0700504
Romain Guybd6b79b2010-06-26 00:13:53 -0700505void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700506 if (quickReject(left, top, right, bottom)) {
507 return;
508 }
509
Romain Guy026c5e162010-06-28 17:12:22 -0700510 SkXfermode::Mode mode;
511
512 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
513 if (!isMode) {
514 // Assume SRC_OVER
515 mode = SkXfermode::kSrcOver_Mode;
516 }
517
518 // Skia draws using the color's alpha channel if < 255
519 // Otherwise, it uses the paint's alpha
520 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700521 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700522 color |= p->getAlpha() << 24;
523 }
524
525 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700526}
Romain Guy9d5316e2010-06-24 19:30:36 -0700527
Romain Guye8e62a42010-07-23 18:55:21 -0700528void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
529 float x, float y, SkPaint* paint) {
530 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
531 return;
532 }
533
534 float length;
535 switch (paint->getTextAlign()) {
536 case SkPaint::kCenter_Align:
537 length = paint->measureText(text, bytesCount);
538 x -= length / 2.0f;
539 break;
540 case SkPaint::kRight_Align:
541 length = paint->measureText(text, bytesCount);
542 x -= length;
543 break;
544 default:
545 break;
546 }
547
Romain Guy694b5192010-07-21 21:33:20 -0700548 int alpha;
549 SkXfermode::Mode mode;
550 getAlphaAndMode(paint, &alpha, &mode);
551
552 uint32_t color = paint->getColor();
553 const GLfloat a = alpha / 255.0f;
554 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
555 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
556 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
557
558 mModelView.loadIdentity();
559
560 useProgram(mDrawTextProgram);
561 mDrawTextProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
562
563 chooseBlending(true, mode);
564 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
565
566 // Always premultiplied
567 glUniform4f(mDrawTextProgram->color, r, g, b, a);
568
Romain Guy09147fb2010-07-22 13:08:20 -0700569 // TODO: Implement scale properly
570 const Rect& clip = mSnapshot->getLocalClip();
571
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700572 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700573 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700574
575 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
576}
577
Romain Guy6926c722010-07-12 20:20:03 -0700578///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700579// Shaders
580///////////////////////////////////////////////////////////////////////////////
581
582void OpenGLRenderer::resetShader() {
583 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700584 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700585 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700586 mShaderTileX = GL_CLAMP_TO_EDGE;
587 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700588}
589
590void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
591 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700592 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700593 mShaderBlend = hasAlpha;
594 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700595 mShaderTileX = gTileModes[tileX];
596 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700597 mShaderMatrix = matrix;
598}
599
Romain Guyc0ac1932010-07-19 18:43:02 -0700600void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
601 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix,
602 bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700603 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700604 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700605 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700606 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700607 mShaderTileX = gTileModes[tileMode];
608 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700609 mShaderMatrix = matrix;
610 mShaderBounds = bounds;
611 mShaderColors = colors;
612 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700613 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700614}
615
Romain Guyd27977d2010-07-14 19:18:51 -0700616///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700617// Drawing implementation
618///////////////////////////////////////////////////////////////////////////////
619
Romain Guy026c5e162010-06-28 17:12:22 -0700620void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700621 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700622 // If a shader is set, preserve only the alpha
623 if (mShader != kShaderNone) {
624 color |= 0x00ffffff;
625 }
626
627 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700628 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700629 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700630
631 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700632 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700633 drawBitmapShader(left, top, right, bottom, a, mode);
634 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700635 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700636 drawLinearGradientShader(left, top, right, bottom, a, mode);
637 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700638 default:
639 break;
640 }
Romain Guy026c5e162010-06-28 17:12:22 -0700641
Romain Guyc0ac1932010-07-19 18:43:02 -0700642 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
643 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
644 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
645
Romain Guy6926c722010-07-12 20:20:03 -0700646 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700647 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700648
Romain Guyc7d53492010-06-25 13:41:57 -0700649 mModelView.loadTranslate(left, top, 0.0f);
650 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700651
Romain Guy079ba2c2010-07-16 14:12:24 -0700652 if (!useProgram(mDrawColorProgram)) {
Romain Guy6926c722010-07-12 20:20:03 -0700653 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy079ba2c2010-07-16 14:12:24 -0700654 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700655 gDrawColorVertexStride, p);
656 }
Romain Guyd27977d2010-07-14 19:18:51 -0700657
658 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700659 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700660 } else {
661 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700662 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700663 }
664
Romain Guy079ba2c2010-07-16 14:12:24 -0700665 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700666
Romain Guyc7d53492010-06-25 13:41:57 -0700667 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700668}
Romain Guy026c5e162010-06-28 17:12:22 -0700669
Romain Guyc0ac1932010-07-19 18:43:02 -0700670void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
671 float alpha, SkXfermode::Mode mode) {
672 Texture* texture = mGradientCache.get(mShaderKey);
673 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700674 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
675 switch (mShaderTileX) {
676 case GL_REPEAT:
677 tileMode = SkShader::kRepeat_TileMode;
678 break;
679 case GL_MIRRORED_REPEAT:
680 tileMode = SkShader::kMirror_TileMode;
681 break;
682 }
683
Romain Guyc0ac1932010-07-19 18:43:02 -0700684 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700685 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700686 }
687
688 mModelView.loadTranslate(left, top, 0.0f);
689 mModelView.scale(right - left, bottom - top, 1.0f);
690
691 useProgram(mDrawLinearGradientProgram);
692 mDrawLinearGradientProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
693
694 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700695 bindTexture(texture->id, mShaderTileX, mShaderTileY);
Romain Guyc0ac1932010-07-19 18:43:02 -0700696
697 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
698 if (mShaderMatrix) {
699 mat4 shaderMatrix(*mShaderMatrix);
700 shaderMatrix.mapRect(start);
701 }
702 mSnapshot->transform.mapRect(start);
703
704 const float gradientX = start.right - start.left;
705 const float gradientY = start.bottom - start.top;
706
707 mat4 screenSpace(mSnapshot->transform);
708 screenSpace.multiply(mModelView);
709
710 // Always premultiplied
711 glUniform4f(mDrawLinearGradientProgram->color, alpha, alpha, alpha, alpha);
712 glUniform2f(mDrawLinearGradientProgram->start, start.left, start.top);
713 glUniform2f(mDrawLinearGradientProgram->gradient, gradientX, gradientY);
714 glUniform1f(mDrawLinearGradientProgram->gradientLength,
715 1.0f / (gradientX * gradientX + gradientY * gradientY));
716 glUniformMatrix4fv(mDrawLinearGradientProgram->screenSpace, 1, GL_FALSE,
717 &screenSpace.data[0]);
718
719 glVertexAttribPointer(mDrawLinearGradientProgram->position, 2, GL_FLOAT, GL_FALSE,
720 gDrawTextureVertexStride, &mDrawTextureVertices[0].position[0]);
721
722 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
723}
724
Romain Guyd27977d2010-07-14 19:18:51 -0700725void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
726 float alpha, SkXfermode::Mode mode) {
727 const Texture* texture = mTextureCache.get(mShaderBitmap);
728
729 const float width = texture->width;
730 const float height = texture->height;
731
732 // This could be done in the vertex shader but we have only 4 vertices
733 float u1 = 0.0f;
734 float v1 = 0.0f;
735 float u2 = right - left;
736 float v2 = bottom - top;
737
Romain Guy694b5192010-07-21 21:33:20 -0700738 // TODO: If the texture is not pow, use a shader to support repeat/mirror
Romain Guyd27977d2010-07-14 19:18:51 -0700739 if (mShaderMatrix) {
740 SkMatrix inverse;
741 mShaderMatrix->invert(&inverse);
742 mat4 m(inverse);
743 Rect r(u1, v1, u2, v2);
744 m.mapRect(r);
745
746 u1 = r.left;
747 u2 = r.right;
748 v1 = r.top;
749 v2 = r.bottom;
750 }
751
752 u1 /= width;
753 u2 /= width;
754 v1 /= height;
755 v2 /= height;
756
757 resetDrawTextureTexCoords(u1, v1, u2, v2);
758
759 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
760 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
761
762 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
763}
764
Romain Guy82ba8142010-07-09 13:25:56 -0700765void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700766 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700767 int alpha;
768 SkXfermode::Mode mode;
769 getAlphaAndMode(paint, &alpha, &mode);
770
Romain Guya9794742010-07-13 11:37:54 -0700771 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
772 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700773}
774
Romain Guybd6b79b2010-06-26 00:13:53 -0700775void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700776 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
777 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700778 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
779}
780
781void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700782 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700783 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700784 mModelView.loadTranslate(left, top, 0.0f);
785 mModelView.scale(right - left, bottom - top, 1.0f);
786
Romain Guyd27977d2010-07-14 19:18:51 -0700787 useProgram(mDrawTextureProgram);
788 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700789
Romain Guya9794742010-07-13 11:37:54 -0700790 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guya1db5742010-07-20 13:09:13 -0700791 bindTexture(texture, mShaderTileX, mShaderTileY);
Romain Guyc1396e92010-06-30 17:56:19 -0700792
Romain Guya9794742010-07-13 11:37:54 -0700793 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700794 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700795
Romain Guyd27977d2010-07-14 19:18:51 -0700796 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700797 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700798 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700799 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700800
Romain Guyf7f93552010-07-08 19:17:03 -0700801 if (!indices) {
802 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
803 } else {
804 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
805 }
Romain Guy82ba8142010-07-09 13:25:56 -0700806}
807
808void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
809 // In theory we should not blend if the mode is Src, but it's rare enough
810 // that it's not worth it
811 blend = blend || mode != SkXfermode::kSrcOver_Mode;
812 if (blend) {
813 if (!mBlend) {
814 glEnable(GL_BLEND);
815 }
816
817 GLenum sourceMode = gBlends[mode].src;
818 GLenum destMode = gBlends[mode].dst;
819 if (!isPremultiplied && sourceMode == GL_ONE) {
820 sourceMode = GL_SRC_ALPHA;
821 }
822
823 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
824 glBlendFunc(sourceMode, destMode);
825 mLastSrcMode = sourceMode;
826 mLastDstMode = destMode;
827 }
828 } else if (mBlend) {
829 glDisable(GL_BLEND);
830 }
831 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700832}
833
Romain Guyd27977d2010-07-14 19:18:51 -0700834bool OpenGLRenderer::useProgram(const sp<Program>& program) {
835 if (!program->isInUse()) {
836 mCurrentProgram->remove();
837 program->use();
838 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700839 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700840 }
Romain Guy6926c722010-07-12 20:20:03 -0700841 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700842}
843
Romain Guy026c5e162010-06-28 17:12:22 -0700844void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700845 TextureVertex* v = &mDrawTextureVertices[0];
846 TextureVertex::setUV(v++, u1, v1);
847 TextureVertex::setUV(v++, u2, v1);
848 TextureVertex::setUV(v++, u1, v2);
849 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700850}
851
852void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
853 if (paint) {
854 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
855 if (!isMode) {
856 // Assume SRC_OVER
857 *mode = SkXfermode::kSrcOver_Mode;
858 }
859
860 // Skia draws using the color's alpha channel if < 255
861 // Otherwise, it uses the paint's alpha
862 int color = paint->getColor();
863 *alpha = (color >> 24) & 0xFF;
864 if (*alpha == 255) {
865 *alpha = paint->getAlpha();
866 }
867 } else {
868 *mode = SkXfermode::kSrcOver_Mode;
869 *alpha = 255;
870 }
Romain Guy026c5e162010-06-28 17:12:22 -0700871}
872
Romain Guya1db5742010-07-20 13:09:13 -0700873void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT) {
874 if (texture != mLastTexture) {
875 glBindTexture(GL_TEXTURE_2D, texture);
876 mLastTexture = texture;
877 }
878 // TODO: Don't set the texture parameters every time
879 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
880 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
881}
882
Romain Guy9d5316e2010-06-24 19:30:36 -0700883}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700884}; // namespace android