blob: db8c8637aa67f25b368442e9b88e183235ec7474 [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 Guy121e2242010-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
Romain Guy7fbcc042010-08-04 15:40:07 -070040#define DEFAULT_LAYER_CACHE_SIZE 6.0f
41#define DEFAULT_PATH_CACHE_SIZE 6.0f
Romain Guyf7f93552010-07-08 19:17:03 -070042#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070043#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guydda57022010-07-06 11:39:32 -070044
Romain Guy06f96e22010-07-30 19:18:16 -070045#define REQUIRED_TEXTURE_UNITS_COUNT 3
46
Romain Guy121e2242010-07-01 18:26:52 -070047// Converts a number of mega-bytes into bytes
48#define MB(s) s * 1024 * 1024
49
Romain Guydda57022010-07-06 11:39:32 -070050// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070051#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070052
53///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy026c5e162010-06-28 17:12:22 -070057// This array is never used directly but used as a memcpy source in the
58// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070059static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070060 FV(0.0f, 0.0f, 0.0f, 0.0f),
61 FV(1.0f, 0.0f, 1.0f, 0.0f),
62 FV(0.0f, 1.0f, 0.0f, 1.0f),
63 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070064};
Romain Guyac670c02010-07-27 17:39:27 -070065static const GLsizei gMeshStride = sizeof(TextureVertex);
66static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070067
Romain Guy889f8d12010-07-29 14:37:42 -070068/**
69 * Structure mapping Skia xfermodes to OpenGL blending factors.
70 */
71struct Blender {
72 SkXfermode::Mode mode;
73 GLenum src;
74 GLenum dst;
75}; // struct Blender
76
Romain Guy026c5e162010-06-28 17:12:22 -070077// In this array, the index of each Blender equals the value of the first
78// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
79static const Blender gBlends[] = {
80 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
81 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
82 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
83 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
84 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
85 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
86 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
87 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
91 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
92};
Romain Guye4d01122010-06-16 18:44:05 -070093
Romain Guy889f8d12010-07-29 14:37:42 -070094static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070095 GL_TEXTURE0,
96 GL_TEXTURE1,
97 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070098};
99
Romain Guyf6a11b82010-06-23 17:47:49 -0700100///////////////////////////////////////////////////////////////////////////////
101// Constructors/destructor
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guydda57022010-07-06 11:39:32 -0700104OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700105 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700106 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700107 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700108 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guy7fbcc042010-08-04 15:40:07 -0700109 mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700110 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700111 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700112
Romain Guy121e2242010-07-01 18:26:52 -0700113 char property[PROPERTY_VALUE_MAX];
114 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700115 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700117 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda57022010-07-06 11:39:32 -0700119 }
120
121 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
122 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700124 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700125 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
126 }
127
128 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
129 LOGD(" Setting gradient cache size to %sMB", property);
Romain Guy7fbcc042010-08-04 15:40:07 -0700130 mGradientCache.setMaxSize(MB(atof(property)));
Romain Guyc0ac1932010-07-19 18:43:02 -0700131 } else {
132 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700133 }
134
Romain Guy7fbcc042010-08-04 15:40:07 -0700135 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) {
136 LOGD(" Setting path cache size to %sMB", property);
137 mPathCache.setMaxSize(MB(atof(property)));
138 } else {
139 LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
140 }
141
Romain Guy889f8d12010-07-29 14:37:42 -0700142 mCurrentProgram = NULL;
Romain Guy06f96e22010-07-30 19:18:16 -0700143 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700144 mColorFilter = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700145
Romain Guyac670c02010-07-27 17:39:27 -0700146 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
147
Romain Guyae5575b2010-07-29 18:48:04 -0700148 mFirstSnapshot = new Snapshot;
149
150 GLint maxTextureUnits;
151 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
152 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700153 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
154 }
Romain Guye4d01122010-06-16 18:44:05 -0700155}
156
Romain Guy85bf02f2010-06-22 13:11:24 -0700157OpenGLRenderer::~OpenGLRenderer() {
158 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700159
160 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700161 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700162 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700163 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700164}
165
Romain Guyf6a11b82010-06-23 17:47:49 -0700166///////////////////////////////////////////////////////////////////////////////
167// Setup
168///////////////////////////////////////////////////////////////////////////////
169
Romain Guy85bf02f2010-06-22 13:11:24 -0700170void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700171 glViewport(0, 0, width, height);
172
Romain Guy260e1022010-07-12 14:41:06 -0700173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700174
175 mWidth = width;
176 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700177 mFirstSnapshot->height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700178}
179
Romain Guy85bf02f2010-06-22 13:11:24 -0700180void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700181 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700182 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700183
Romain Guy08ae3172010-06-21 19:35:50 -0700184 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700185
Romain Guy08ae3172010-06-21 19:35:50 -0700186 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
187 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700188
Romain Guy08ae3172010-06-21 19:35:50 -0700189 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700190 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700191
Romain Guyb82da652010-07-30 11:36:12 -0700192 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700193}
194
Romain Guyf6a11b82010-06-23 17:47:49 -0700195///////////////////////////////////////////////////////////////////////////////
196// State management
197///////////////////////////////////////////////////////////////////////////////
198
Romain Guybb9524b2010-06-22 18:56:38 -0700199int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700201}
202
203int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
207void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700209
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 if (restoreSnapshot()) {
211 setScissorFromClip();
212 }
Romain Guybb9524b2010-06-22 18:56:38 -0700213}
214
215void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700217
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700219
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 while (mSaveCount != saveCount - 1) {
221 restoreClip |= restoreSnapshot();
222 }
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 if (restoreClip) {
225 setScissorFromClip();
226 }
Romain Guybb9524b2010-06-22 18:56:38 -0700227}
228
229int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700230 mSnapshot = new Snapshot(mSnapshot);
231 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
234bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700235 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700237 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700238
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 sp<Snapshot> current = mSnapshot;
240 sp<Snapshot> previous = mSnapshot->previous;
241
Romain Guyf86ef572010-07-01 11:05:42 -0700242 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700243 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700244 }
245
Romain Guybd6b79b2010-06-26 00:13:53 -0700246 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700247 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700248 }
249
250 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700251 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700252
Romain Guy7ae7ac42010-06-25 13:46:18 -0700253 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700254}
255
Romain Guyd55a8612010-06-28 17:42:46 -0700256void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700257 if (!current->layer) {
258 LOGE("Attempting to compose a layer that does not exist");
259 return;
260 }
261
Romain Guyd55a8612010-06-28 17:42:46 -0700262 // Unbind current FBO and restore previous one
263 // Most of the time, previous->fbo will be 0 to bind the default buffer
264 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
265
266 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700267 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700268 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
269
Romain Guydda57022010-07-06 11:39:32 -0700270 Layer* layer = current->layer;
Romain Guydda57022010-07-06 11:39:32 -0700271 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700272
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guyd27977d2010-07-14 19:18:51 -0700325 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700326 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700327
Romain Guyf18fd992010-07-08 11:45:51 -0700328 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
329 LayerSize size(right - left, bottom - top);
330
Romain Guyf18fd992010-07-08 11:45:51 -0700331 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700332 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700333 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700334 }
335
Romain Guyf18fd992010-07-08 11:45:51 -0700336 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
337
Romain Guyf86ef572010-07-01 11:05:42 -0700338 // Clear the FBO
339 glDisable(GL_SCISSOR_TEST);
340 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
341 glClear(GL_COLOR_BUFFER_BIT);
342 glEnable(GL_SCISSOR_TEST);
343
Romain Guydda57022010-07-06 11:39:32 -0700344 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700345 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700346 layer->mode = mode;
347 layer->alpha = alpha / 255.0f;
348 layer->layer.set(left, top, right, bottom);
349
350 snapshot->layer = layer;
351 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700352
Romain Guyf86ef572010-07-01 11:05:42 -0700353 // Creates a new snapshot to draw into the FBO
354 saveSnapshot();
355 // TODO: This doesn't preserve other transformations (check Skia first)
356 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700357 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700358 mSnapshot->height = bottom - top;
359 setScissorFromClip();
360
Romain Guy079ba2c2010-07-16 14:12:24 -0700361 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700362 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700363
364 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700365 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700366
Romain Guyd55a8612010-06-28 17:42:46 -0700367 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700368}
369
370///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700371// Transforms
372///////////////////////////////////////////////////////////////////////////////
373
374void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700375 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700376}
377
378void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700379 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700380}
381
382void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700383 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700384}
385
386void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700387 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700391 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700392}
393
394void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700395 mat4 m(*matrix);
396 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700397}
398
399///////////////////////////////////////////////////////////////////////////////
400// Clipping
401///////////////////////////////////////////////////////////////////////////////
402
Romain Guybb9524b2010-06-22 18:56:38 -0700403void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700404 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700405 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700406}
407
408const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700409 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700410}
411
Romain Guyc7d53492010-06-25 13:41:57 -0700412bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700413 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700414 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700415 return !mSnapshot->clipRect.intersects(r);
416}
417
Romain Guy079ba2c2010-07-16 14:12:24 -0700418bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
419 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700420 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700421 setScissorFromClip();
422 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700423 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700424}
425
Romain Guyf6a11b82010-06-23 17:47:49 -0700426///////////////////////////////////////////////////////////////////////////////
427// Drawing
428///////////////////////////////////////////////////////////////////////////////
429
Romain Guyc1396e92010-06-30 17:56:19 -0700430void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700431 const float right = left + bitmap->width();
432 const float bottom = top + bitmap->height();
433
434 if (quickReject(left, top, right, bottom)) {
435 return;
436 }
437
Romain Guyf86ef572010-07-01 11:05:42 -0700438 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy22158e12010-08-06 11:18:34 -0700439 const AutoTexture autoCleanup(texture);
440
Romain Guy6926c722010-07-12 20:20:03 -0700441 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700442}
443
Romain Guyf86ef572010-07-01 11:05:42 -0700444void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
445 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
446 const mat4 transform(*matrix);
447 transform.mapRect(r);
448
Romain Guy6926c722010-07-12 20:20:03 -0700449 if (quickReject(r.left, r.top, r.right, r.bottom)) {
450 return;
451 }
452
Romain Guyf86ef572010-07-01 11:05:42 -0700453 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy22158e12010-08-06 11:18:34 -0700454 const AutoTexture autoCleanup(texture);
455
Romain Guy82ba8142010-07-09 13:25:56 -0700456 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700457}
458
Romain Guy8ba548f2010-06-30 19:21:21 -0700459void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
460 float srcLeft, float srcTop, float srcRight, float srcBottom,
461 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700462 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700463 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
464 return;
465 }
466
Romain Guyf86ef572010-07-01 11:05:42 -0700467 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy22158e12010-08-06 11:18:34 -0700468 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700469
Romain Guy8ba548f2010-06-30 19:21:21 -0700470 const float width = texture->width;
471 const float height = texture->height;
472
473 const float u1 = srcLeft / width;
474 const float v1 = srcTop / height;
475 const float u2 = srcRight / width;
476 const float v2 = srcBottom / height;
477
478 resetDrawTextureTexCoords(u1, v1, u2, v2);
479
Romain Guy82ba8142010-07-09 13:25:56 -0700480 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700481
482 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
483}
484
Romain Guydeba7852010-07-07 17:54:48 -0700485void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
486 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700487 if (quickReject(left, top, right, bottom)) {
488 return;
489 }
490
Romain Guyf7f93552010-07-08 19:17:03 -0700491 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy22158e12010-08-06 11:18:34 -0700492 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700493
494 int alpha;
495 SkXfermode::Mode mode;
496 getAlphaAndMode(paint, &alpha, &mode);
497
Romain Guyf7f93552010-07-08 19:17:03 -0700498 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700499 mesh->updateVertices(bitmap, left, top, right, bottom,
500 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700501
502 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
503 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700504 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
505 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700506 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700507}
508
Romain Guy85bf02f2010-06-22 13:11:24 -0700509void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700510 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700511 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700512}
Romain Guy9d5316e2010-06-24 19:30:36 -0700513
Romain Guybd6b79b2010-06-26 00:13:53 -0700514void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700515 if (quickReject(left, top, right, bottom)) {
516 return;
517 }
518
Romain Guy026c5e162010-06-28 17:12:22 -0700519 SkXfermode::Mode mode;
520
521 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
522 if (!isMode) {
523 // Assume SRC_OVER
524 mode = SkXfermode::kSrcOver_Mode;
525 }
526
527 // Skia draws using the color's alpha channel if < 255
528 // Otherwise, it uses the paint's alpha
529 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700530 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700531 color |= p->getAlpha() << 24;
532 }
533
534 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700535}
Romain Guy9d5316e2010-06-24 19:30:36 -0700536
Romain Guye8e62a42010-07-23 18:55:21 -0700537void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
538 float x, float y, SkPaint* paint) {
539 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
540 return;
541 }
542
543 float length;
544 switch (paint->getTextAlign()) {
545 case SkPaint::kCenter_Align:
546 length = paint->measureText(text, bytesCount);
547 x -= length / 2.0f;
548 break;
549 case SkPaint::kRight_Align:
550 length = paint->measureText(text, bytesCount);
551 x -= length;
552 break;
553 default:
554 break;
555 }
556
Romain Guy694b5192010-07-21 21:33:20 -0700557 int alpha;
558 SkXfermode::Mode mode;
559 getAlphaAndMode(paint, &alpha, &mode);
560
561 uint32_t color = paint->getColor();
562 const GLfloat a = alpha / 255.0f;
563 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
564 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
565 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
566
567 mModelView.loadIdentity();
568
Romain Guy06f96e22010-07-30 19:18:16 -0700569 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700570 // Needs to be set prior to calling FontRenderer::getTexture()
571 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700572
Romain Guy889f8d12010-07-29 14:37:42 -0700573 ProgramDescription description;
574 description.hasTexture = true;
575 description.hasAlpha8Texture = true;
Romain Guy06f96e22010-07-30 19:18:16 -0700576 if (mShader) {
577 mShader->describe(description, mExtensions);
578 }
Romain Guydb1938e2010-08-02 18:50:22 -0700579 if (mColorFilter) {
580 mColorFilter->describe(description, mExtensions);
581 }
Romain Guy889f8d12010-07-29 14:37:42 -0700582
583 useProgram(mProgramCache.get(description));
584 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700585
Romain Guydb1938e2010-08-02 18:50:22 -0700586 // Text is always blended, no need to check the shader
Romain Guy694b5192010-07-21 21:33:20 -0700587 chooseBlending(true, mode);
Romain Guy06f96e22010-07-30 19:18:16 -0700588 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
589 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
Romain Guy889f8d12010-07-29 14:37:42 -0700590
591 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
592 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700593
594 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700595 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700596
Romain Guy06f96e22010-07-30 19:18:16 -0700597 textureUnit++;
598 // Setup attributes and uniforms required by the shaders
599 if (mShader) {
600 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
601 }
Romain Guydb1938e2010-08-02 18:50:22 -0700602 if (mColorFilter) {
603 mColorFilter->setupProgram(mCurrentProgram);
604 }
Romain Guy06f96e22010-07-30 19:18:16 -0700605
Romain Guy09147fb2010-07-22 13:08:20 -0700606 // TODO: Implement scale properly
607 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700608 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700609 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700610
611 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700612 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700613}
614
Romain Guy7fbcc042010-08-04 15:40:07 -0700615void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
616 GLuint textureUnit = 0;
617 glActiveTexture(gTextureUnits[textureUnit]);
618
Romain Guy22158e12010-08-06 11:18:34 -0700619 const PathTexture* texture = mPathCache.get(path, paint);
620 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700621
622 int alpha;
623 SkXfermode::Mode mode;
624 getAlphaAndMode(paint, &alpha, &mode);
625
626 uint32_t color = paint->getColor();
627 const GLfloat a = alpha / 255.0f;
628 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
629 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
630 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
631
632 // Describe the required shaders
633 ProgramDescription description;
634 description.hasTexture = true;
635 description.hasAlpha8Texture = true;
636 if (mShader) {
637 mShader->describe(description, mExtensions);
638 }
639 if (mColorFilter) {
640 mColorFilter->describe(description, mExtensions);
641 }
642
643 // Build and use the appropriate shader
644 useProgram(mProgramCache.get(description));
645
646 // Setup the blending mode
647 chooseBlending(true, mode);
648 bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
649 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
650
651 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
652 glEnableVertexAttribArray(texCoordsSlot);
653
654 // Setup attributes
655 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
656 gMeshStride, &mMeshVertices[0].position[0]);
657 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
658 gMeshStride, &mMeshVertices[0].texture[0]);
659
660 // Setup uniforms
661 mModelView.loadTranslate(texture->left - texture->offset,
662 texture->top - texture->offset, 0.0f);
663 mModelView.scale(texture->width, texture->height, 1.0f);
664 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
665
666 glUniform4f(mCurrentProgram->color, r, g, b, a);
667
668 textureUnit++;
669 // Setup attributes and uniforms required by the shaders
670 if (mShader) {
671 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
672 }
673 if (mColorFilter) {
674 mColorFilter->setupProgram(mCurrentProgram);
675 }
676
677 // Draw the mesh
678 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
679
680 glDisableVertexAttribArray(texCoordsSlot);
681}
682
Romain Guy6926c722010-07-12 20:20:03 -0700683///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700684// Shaders
685///////////////////////////////////////////////////////////////////////////////
686
687void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700688 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700689}
690
Romain Guy06f96e22010-07-30 19:18:16 -0700691void OpenGLRenderer::setupShader(SkiaShader* shader) {
692 mShader = shader;
693 if (mShader) {
694 mShader->set(&mTextureCache, &mGradientCache);
695 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700696}
697
Romain Guyd27977d2010-07-14 19:18:51 -0700698///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700699// Color filters
700///////////////////////////////////////////////////////////////////////////////
701
702void OpenGLRenderer::resetColorFilter() {
703 mColorFilter = NULL;
704}
705
706void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
707 mColorFilter = filter;
708}
709
710///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700711// Drawing implementation
712///////////////////////////////////////////////////////////////////////////////
713
Romain Guy026c5e162010-06-28 17:12:22 -0700714void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700715 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700716 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700717 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700718 color |= 0x00ffffff;
719 }
720
721 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700722 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700723 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700724 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
725 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
726 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
727
Romain Guy06f96e22010-07-30 19:18:16 -0700728 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700729
Romain Guy06f96e22010-07-30 19:18:16 -0700730 // Setup the blending mode
731 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700732
Romain Guy06f96e22010-07-30 19:18:16 -0700733 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700734 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700735 if (mShader) {
736 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700737 }
Romain Guydb1938e2010-08-02 18:50:22 -0700738 if (mColorFilter) {
739 mColorFilter->describe(description, mExtensions);
740 }
Romain Guyd27977d2010-07-14 19:18:51 -0700741
Romain Guy06f96e22010-07-30 19:18:16 -0700742 // Build and use the appropriate shader
743 useProgram(mProgramCache.get(description));
744
745 // Setup attributes
746 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
747 gMeshStride, &mMeshVertices[0].position[0]);
748
749 // Setup uniforms
750 mModelView.loadTranslate(left, top, 0.0f);
751 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700752 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700753 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700754 } else {
755 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700756 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700757 }
Romain Guy889f8d12010-07-29 14:37:42 -0700758 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700759
Romain Guy06f96e22010-07-30 19:18:16 -0700760 // Setup attributes and uniforms required by the shaders
761 if (mShader) {
762 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700763 }
Romain Guydb1938e2010-08-02 18:50:22 -0700764 if (mColorFilter) {
765 mColorFilter->setupProgram(mCurrentProgram);
766 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700767
Romain Guy06f96e22010-07-30 19:18:16 -0700768 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700769 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700770}
771
Romain Guy82ba8142010-07-09 13:25:56 -0700772void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700773 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700774 int alpha;
775 SkXfermode::Mode mode;
776 getAlphaAndMode(paint, &alpha, &mode);
777
Romain Guya9794742010-07-13 11:37:54 -0700778 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700779 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700780}
781
Romain Guybd6b79b2010-06-26 00:13:53 -0700782void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700783 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
784 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700785 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700786}
787
788void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700789 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700790 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700791 ProgramDescription description;
792 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700793 if (mColorFilter) {
794 mColorFilter->describe(description, mExtensions);
795 }
Romain Guy889f8d12010-07-29 14:37:42 -0700796
Romain Guybd6b79b2010-06-26 00:13:53 -0700797 mModelView.loadTranslate(left, top, 0.0f);
798 mModelView.scale(right - left, bottom - top, 1.0f);
799
Romain Guy889f8d12010-07-29 14:37:42 -0700800 useProgram(mProgramCache.get(description));
801 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700802
Romain Guya9794742010-07-13 11:37:54 -0700803 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700804
805 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700806 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700807 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700808
Romain Guya9794742010-07-13 11:37:54 -0700809 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700810 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700811
Romain Guy889f8d12010-07-29 14:37:42 -0700812 // Mesh
813 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
814 glEnableVertexAttribArray(texCoordsSlot);
815 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700816 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700817 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700818
Romain Guydb1938e2010-08-02 18:50:22 -0700819 // Color filter
820 if (mColorFilter) {
821 mColorFilter->setupProgram(mCurrentProgram);
822 }
823
Romain Guyf7f93552010-07-08 19:17:03 -0700824 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700825 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700826 } else {
827 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
828 }
Romain Guy889f8d12010-07-29 14:37:42 -0700829 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700830}
831
832void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700833 blend = blend || mode != SkXfermode::kSrcOver_Mode;
834 if (blend) {
835 if (!mBlend) {
836 glEnable(GL_BLEND);
837 }
838
839 GLenum sourceMode = gBlends[mode].src;
840 GLenum destMode = gBlends[mode].dst;
841 if (!isPremultiplied && sourceMode == GL_ONE) {
842 sourceMode = GL_SRC_ALPHA;
843 }
844
845 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
846 glBlendFunc(sourceMode, destMode);
847 mLastSrcMode = sourceMode;
848 mLastDstMode = destMode;
849 }
850 } else if (mBlend) {
851 glDisable(GL_BLEND);
852 }
853 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700854}
855
Romain Guy889f8d12010-07-29 14:37:42 -0700856bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700857 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700858 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700859 program->use();
860 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700861 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700862 }
Romain Guy6926c722010-07-12 20:20:03 -0700863 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700864}
865
Romain Guy026c5e162010-06-28 17:12:22 -0700866void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700867 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700868 TextureVertex::setUV(v++, u1, v1);
869 TextureVertex::setUV(v++, u2, v1);
870 TextureVertex::setUV(v++, u1, v2);
871 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700872}
873
874void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
875 if (paint) {
876 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
877 if (!isMode) {
878 // Assume SRC_OVER
879 *mode = SkXfermode::kSrcOver_Mode;
880 }
881
882 // Skia draws using the color's alpha channel if < 255
883 // Otherwise, it uses the paint's alpha
884 int color = paint->getColor();
885 *alpha = (color >> 24) & 0xFF;
886 if (*alpha == 255) {
887 *alpha = paint->getAlpha();
888 }
889 } else {
890 *mode = SkXfermode::kSrcOver_Mode;
891 *alpha = 255;
892 }
Romain Guy026c5e162010-06-28 17:12:22 -0700893}
894
Romain Guy889f8d12010-07-29 14:37:42 -0700895void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
896 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700897 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700898 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
899 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
900}
901
Romain Guy9d5316e2010-06-24 19:30:36 -0700902}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700903}; // namespace android