blob: d6940391145a4640fb30de9c3dd431f8eff20b42 [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
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 Guydda57022010-07-06 11:39:32 -070043
Romain Guy06f96e22010-07-30 19:18:16 -070044#define REQUIRED_TEXTURE_UNITS_COUNT 3
45
Romain Guy121e2242010-07-01 18:26:52 -070046// Converts a number of mega-bytes into bytes
47#define MB(s) s * 1024 * 1024
48
Romain Guydda57022010-07-06 11:39:32 -070049// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070050#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070051
52///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy026c5e162010-06-28 17:12:22 -070056// This array is never used directly but used as a memcpy source in the
57// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070058static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070059 FV(0.0f, 0.0f, 0.0f, 0.0f),
60 FV(1.0f, 0.0f, 1.0f, 0.0f),
61 FV(0.0f, 1.0f, 0.0f, 1.0f),
62 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070063};
Romain Guyac670c02010-07-27 17:39:27 -070064static const GLsizei gMeshStride = sizeof(TextureVertex);
65static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070066
Romain Guy889f8d12010-07-29 14:37:42 -070067/**
68 * Structure mapping Skia xfermodes to OpenGL blending factors.
69 */
70struct Blender {
71 SkXfermode::Mode mode;
72 GLenum src;
73 GLenum dst;
74}; // struct Blender
75
Romain Guy026c5e162010-06-28 17:12:22 -070076// In this array, the index of each Blender equals the value of the first
77// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
78static const Blender gBlends[] = {
79 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
80 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
81 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
82 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
84 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
86 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
90 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
91};
Romain Guye4d01122010-06-16 18:44:05 -070092
Romain Guy889f8d12010-07-29 14:37:42 -070093static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070094 GL_TEXTURE0,
95 GL_TEXTURE1,
96 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070097};
98
Romain Guyf6a11b82010-06-23 17:47:49 -070099///////////////////////////////////////////////////////////////////////////////
100// Constructors/destructor
101///////////////////////////////////////////////////////////////////////////////
102
Romain Guydda57022010-07-06 11:39:32 -0700103OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700104 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700105 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700106 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700107 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700108 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700109 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700110
Romain Guy121e2242010-07-01 18:26:52 -0700111 char property[PROPERTY_VALUE_MAX];
112 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700113 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700114 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700115 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda57022010-07-06 11:39:32 -0700117 }
118
119 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
120 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700121 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700122 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
124 }
125
126 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
127 LOGD(" Setting gradient cache size to %sMB", property);
128 mLayerCache.setMaxSize(MB(atof(property)));
129 } else {
130 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700131 }
132
Romain Guy889f8d12010-07-29 14:37:42 -0700133 mCurrentProgram = NULL;
Romain Guy06f96e22010-07-30 19:18:16 -0700134 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700135 mColorFilter = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700136
Romain Guyac670c02010-07-27 17:39:27 -0700137 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
138
Romain Guyae5575b2010-07-29 18:48:04 -0700139 mFirstSnapshot = new Snapshot;
140
141 GLint maxTextureUnits;
142 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
143 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700144 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
145 }
Romain Guye4d01122010-06-16 18:44:05 -0700146}
147
Romain Guy85bf02f2010-06-22 13:11:24 -0700148OpenGLRenderer::~OpenGLRenderer() {
149 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700150
151 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700152 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700153 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700154 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700155}
156
Romain Guyf6a11b82010-06-23 17:47:49 -0700157///////////////////////////////////////////////////////////////////////////////
158// Setup
159///////////////////////////////////////////////////////////////////////////////
160
Romain Guy85bf02f2010-06-22 13:11:24 -0700161void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700162 glViewport(0, 0, width, height);
163
Romain Guy260e1022010-07-12 14:41:06 -0700164 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700165
166 mWidth = width;
167 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700168 mFirstSnapshot->height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700169}
170
Romain Guy85bf02f2010-06-22 13:11:24 -0700171void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700172 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700173 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700174
Romain Guy08ae3172010-06-21 19:35:50 -0700175 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700176
Romain Guy08ae3172010-06-21 19:35:50 -0700177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700179
Romain Guy08ae3172010-06-21 19:35:50 -0700180 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700181 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700182
Romain Guyb82da652010-07-30 11:36:12 -0700183 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700184}
185
Romain Guyf6a11b82010-06-23 17:47:49 -0700186///////////////////////////////////////////////////////////////////////////////
187// State management
188///////////////////////////////////////////////////////////////////////////////
189
Romain Guybb9524b2010-06-22 18:56:38 -0700190int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700191 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700192}
193
194int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700196}
197
198void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700199 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700200
Romain Guy7ae7ac42010-06-25 13:46:18 -0700201 if (restoreSnapshot()) {
202 setScissorFromClip();
203 }
Romain Guybb9524b2010-06-22 18:56:38 -0700204}
205
206void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700208
Romain Guy7ae7ac42010-06-25 13:46:18 -0700209 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700210
Romain Guy7ae7ac42010-06-25 13:46:18 -0700211 while (mSaveCount != saveCount - 1) {
212 restoreClip |= restoreSnapshot();
213 }
Romain Guybb9524b2010-06-22 18:56:38 -0700214
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 if (restoreClip) {
216 setScissorFromClip();
217 }
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 mSnapshot = new Snapshot(mSnapshot);
222 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700223}
224
225bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700227 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700228 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700229
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 sp<Snapshot> current = mSnapshot;
231 sp<Snapshot> previous = mSnapshot->previous;
232
Romain Guyf86ef572010-07-01 11:05:42 -0700233 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700234 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700235 }
236
Romain Guybd6b79b2010-06-26 00:13:53 -0700237 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700238 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700239 }
240
241 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700242 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700243
Romain Guy7ae7ac42010-06-25 13:46:18 -0700244 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700245}
246
Romain Guyd55a8612010-06-28 17:42:46 -0700247void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700248 if (!current->layer) {
249 LOGE("Attempting to compose a layer that does not exist");
250 return;
251 }
252
Romain Guyd55a8612010-06-28 17:42:46 -0700253 // Unbind current FBO and restore previous one
254 // Most of the time, previous->fbo will be 0 to bind the default buffer
255 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
256
257 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700258 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700259 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
260
Romain Guydda57022010-07-06 11:39:32 -0700261 Layer* layer = current->layer;
Romain Guydda57022010-07-06 11:39:32 -0700262 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700263
Romain Guydda57022010-07-06 11:39:32 -0700264 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700265 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700266
Romain Guydda57022010-07-06 11:39:32 -0700267 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700268 // Failing to add the layer to the cache should happen only if the
269 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700270 if (!mLayerCache.put(size, layer)) {
271 LAYER_LOGD("Deleting layer");
272
273 glDeleteFramebuffers(1, &layer->fbo);
274 glDeleteTextures(1, &layer->texture);
275
276 delete layer;
277 }
Romain Guyd55a8612010-06-28 17:42:46 -0700278}
279
Romain Guyf6a11b82010-06-23 17:47:49 -0700280///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700281// Layers
282///////////////////////////////////////////////////////////////////////////////
283
284int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
285 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700286 int count = saveSnapshot();
287
288 int alpha = 255;
289 SkXfermode::Mode mode;
290
291 if (p) {
292 alpha = p->getAlpha();
293 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
294 if (!isMode) {
295 // Assume SRC_OVER
296 mode = SkXfermode::kSrcOver_Mode;
297 }
298 } else {
299 mode = SkXfermode::kSrcOver_Mode;
300 }
301
302 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
303
304 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700305}
306
307int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
308 int alpha, int flags) {
309 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700310 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
311 return count;
312}
Romain Guybd6b79b2010-06-26 00:13:53 -0700313
Romain Guyd55a8612010-06-28 17:42:46 -0700314bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
315 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700316 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700317 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700318
Romain Guyf18fd992010-07-08 11:45:51 -0700319 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
320 LayerSize size(right - left, bottom - top);
321
Romain Guyf18fd992010-07-08 11:45:51 -0700322 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700323 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700324 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700325 }
326
Romain Guyf18fd992010-07-08 11:45:51 -0700327 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
328
Romain Guyf86ef572010-07-01 11:05:42 -0700329 // Clear the FBO
330 glDisable(GL_SCISSOR_TEST);
331 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
332 glClear(GL_COLOR_BUFFER_BIT);
333 glEnable(GL_SCISSOR_TEST);
334
Romain Guydda57022010-07-06 11:39:32 -0700335 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700336 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700337 layer->mode = mode;
338 layer->alpha = alpha / 255.0f;
339 layer->layer.set(left, top, right, bottom);
340
341 snapshot->layer = layer;
342 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700343
Romain Guyf86ef572010-07-01 11:05:42 -0700344 // Creates a new snapshot to draw into the FBO
345 saveSnapshot();
346 // TODO: This doesn't preserve other transformations (check Skia first)
347 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700348 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700349 mSnapshot->height = bottom - top;
350 setScissorFromClip();
351
Romain Guy079ba2c2010-07-16 14:12:24 -0700352 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700353 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700354
355 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700356 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700357
Romain Guyd55a8612010-06-28 17:42:46 -0700358 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700359}
360
361///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700362// Transforms
363///////////////////////////////////////////////////////////////////////////////
364
365void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700366 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700370 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700383}
384
385void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 mat4 m(*matrix);
387 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390///////////////////////////////////////////////////////////////////////////////
391// Clipping
392///////////////////////////////////////////////////////////////////////////////
393
Romain Guybb9524b2010-06-22 18:56:38 -0700394void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700395 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700396 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700397}
398
399const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700400 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700401}
402
Romain Guyc7d53492010-06-25 13:41:57 -0700403bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700404 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700405 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700406 return !mSnapshot->clipRect.intersects(r);
407}
408
Romain Guy079ba2c2010-07-16 14:12:24 -0700409bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
410 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700411 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700412 setScissorFromClip();
413 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700414 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700415}
416
Romain Guyf6a11b82010-06-23 17:47:49 -0700417///////////////////////////////////////////////////////////////////////////////
418// Drawing
419///////////////////////////////////////////////////////////////////////////////
420
Romain Guyc1396e92010-06-30 17:56:19 -0700421void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700422 const float right = left + bitmap->width();
423 const float bottom = top + bitmap->height();
424
425 if (quickReject(left, top, right, bottom)) {
426 return;
427 }
428
Romain Guyf86ef572010-07-01 11:05:42 -0700429 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700430 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700431}
432
Romain Guyf86ef572010-07-01 11:05:42 -0700433void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
434 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
435 const mat4 transform(*matrix);
436 transform.mapRect(r);
437
Romain Guy6926c722010-07-12 20:20:03 -0700438 if (quickReject(r.left, r.top, r.right, r.bottom)) {
439 return;
440 }
441
Romain Guyf86ef572010-07-01 11:05:42 -0700442 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700443 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700444}
445
Romain Guy8ba548f2010-06-30 19:21:21 -0700446void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
447 float srcLeft, float srcTop, float srcRight, float srcBottom,
448 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700449 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700450 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
451 return;
452 }
453
Romain Guyf86ef572010-07-01 11:05:42 -0700454 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700455
Romain Guy8ba548f2010-06-30 19:21:21 -0700456 const float width = texture->width;
457 const float height = texture->height;
458
459 const float u1 = srcLeft / width;
460 const float v1 = srcTop / height;
461 const float u2 = srcRight / width;
462 const float v2 = srcBottom / height;
463
464 resetDrawTextureTexCoords(u1, v1, u2, v2);
465
Romain Guy82ba8142010-07-09 13:25:56 -0700466 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700467
468 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
469}
470
Romain Guydeba7852010-07-07 17:54:48 -0700471void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
472 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700473 if (quickReject(left, top, right, bottom)) {
474 return;
475 }
476
Romain Guyf7f93552010-07-08 19:17:03 -0700477 const Texture* texture = mTextureCache.get(bitmap);
478
479 int alpha;
480 SkXfermode::Mode mode;
481 getAlphaAndMode(paint, &alpha, &mode);
482
Romain Guyf7f93552010-07-08 19:17:03 -0700483 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700484 mesh->updateVertices(bitmap, left, top, right, bottom,
485 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700486
487 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
488 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700489 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
490 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700491 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700492}
493
Romain Guy85bf02f2010-06-22 13:11:24 -0700494void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700495 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700496 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700497}
Romain Guy9d5316e2010-06-24 19:30:36 -0700498
Romain Guybd6b79b2010-06-26 00:13:53 -0700499void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700500 if (quickReject(left, top, right, bottom)) {
501 return;
502 }
503
Romain Guy026c5e162010-06-28 17:12:22 -0700504 SkXfermode::Mode mode;
505
506 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
507 if (!isMode) {
508 // Assume SRC_OVER
509 mode = SkXfermode::kSrcOver_Mode;
510 }
511
512 // Skia draws using the color's alpha channel if < 255
513 // Otherwise, it uses the paint's alpha
514 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700515 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700516 color |= p->getAlpha() << 24;
517 }
518
519 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700520}
Romain Guy9d5316e2010-06-24 19:30:36 -0700521
Romain Guye8e62a42010-07-23 18:55:21 -0700522void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
523 float x, float y, SkPaint* paint) {
524 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
525 return;
526 }
527
528 float length;
529 switch (paint->getTextAlign()) {
530 case SkPaint::kCenter_Align:
531 length = paint->measureText(text, bytesCount);
532 x -= length / 2.0f;
533 break;
534 case SkPaint::kRight_Align:
535 length = paint->measureText(text, bytesCount);
536 x -= length;
537 break;
538 default:
539 break;
540 }
541
Romain Guy694b5192010-07-21 21:33:20 -0700542 int alpha;
543 SkXfermode::Mode mode;
544 getAlphaAndMode(paint, &alpha, &mode);
545
546 uint32_t color = paint->getColor();
547 const GLfloat a = alpha / 255.0f;
548 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
549 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
550 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
551
552 mModelView.loadIdentity();
553
Romain Guy06f96e22010-07-30 19:18:16 -0700554 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700555 // Needs to be set prior to calling FontRenderer::getTexture()
556 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700557
Romain Guy889f8d12010-07-29 14:37:42 -0700558 ProgramDescription description;
559 description.hasTexture = true;
560 description.hasAlpha8Texture = true;
Romain Guy06f96e22010-07-30 19:18:16 -0700561 if (mShader) {
562 mShader->describe(description, mExtensions);
563 }
Romain Guydb1938e2010-08-02 18:50:22 -0700564 if (mColorFilter) {
565 mColorFilter->describe(description, mExtensions);
566 }
Romain Guy889f8d12010-07-29 14:37:42 -0700567
568 useProgram(mProgramCache.get(description));
569 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700570
Romain Guydb1938e2010-08-02 18:50:22 -0700571 // Text is always blended, no need to check the shader
Romain Guy694b5192010-07-21 21:33:20 -0700572 chooseBlending(true, mode);
Romain Guy06f96e22010-07-30 19:18:16 -0700573 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
574 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
Romain Guy889f8d12010-07-29 14:37:42 -0700575
576 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
577 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700578
579 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700580 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700581
Romain Guy06f96e22010-07-30 19:18:16 -0700582 textureUnit++;
583 // Setup attributes and uniforms required by the shaders
584 if (mShader) {
585 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
586 }
Romain Guydb1938e2010-08-02 18:50:22 -0700587 if (mColorFilter) {
588 mColorFilter->setupProgram(mCurrentProgram);
589 }
Romain Guy06f96e22010-07-30 19:18:16 -0700590
Romain Guy09147fb2010-07-22 13:08:20 -0700591 // TODO: Implement scale properly
592 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700593 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700594 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700595
596 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700597 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700598}
599
Romain Guy6926c722010-07-12 20:20:03 -0700600///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700601// Shaders
602///////////////////////////////////////////////////////////////////////////////
603
604void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700605 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700606}
607
Romain Guy06f96e22010-07-30 19:18:16 -0700608void OpenGLRenderer::setupShader(SkiaShader* shader) {
609 mShader = shader;
610 if (mShader) {
611 mShader->set(&mTextureCache, &mGradientCache);
612 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700613}
614
Romain Guyd27977d2010-07-14 19:18:51 -0700615///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700616// Color filters
617///////////////////////////////////////////////////////////////////////////////
618
619void OpenGLRenderer::resetColorFilter() {
620 mColorFilter = NULL;
621}
622
623void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
624 mColorFilter = filter;
625}
626
627///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700628// Drawing implementation
629///////////////////////////////////////////////////////////////////////////////
630
Romain Guy026c5e162010-06-28 17:12:22 -0700631void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700632 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700633 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700634 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700635 color |= 0x00ffffff;
636 }
637
638 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700639 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700640 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700641 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
642 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
643 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
644
Romain Guy06f96e22010-07-30 19:18:16 -0700645 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700646
Romain Guy06f96e22010-07-30 19:18:16 -0700647 // Setup the blending mode
648 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700649
Romain Guy06f96e22010-07-30 19:18:16 -0700650 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700651 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700652 if (mShader) {
653 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700654 }
Romain Guydb1938e2010-08-02 18:50:22 -0700655 if (mColorFilter) {
656 mColorFilter->describe(description, mExtensions);
657 }
Romain Guyd27977d2010-07-14 19:18:51 -0700658
Romain Guy06f96e22010-07-30 19:18:16 -0700659 // Build and use the appropriate shader
660 useProgram(mProgramCache.get(description));
661
662 // Setup attributes
663 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
664 gMeshStride, &mMeshVertices[0].position[0]);
665
666 // Setup uniforms
667 mModelView.loadTranslate(left, top, 0.0f);
668 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700669 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700670 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700671 } else {
672 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700673 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700674 }
Romain Guy889f8d12010-07-29 14:37:42 -0700675 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700676
Romain Guy06f96e22010-07-30 19:18:16 -0700677 // Setup attributes and uniforms required by the shaders
678 if (mShader) {
679 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700680 }
Romain Guydb1938e2010-08-02 18:50:22 -0700681 if (mColorFilter) {
682 mColorFilter->setupProgram(mCurrentProgram);
683 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700684
Romain Guy06f96e22010-07-30 19:18:16 -0700685 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700686 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700687}
688
Romain Guy82ba8142010-07-09 13:25:56 -0700689void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700690 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700691 int alpha;
692 SkXfermode::Mode mode;
693 getAlphaAndMode(paint, &alpha, &mode);
694
Romain Guya9794742010-07-13 11:37:54 -0700695 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700696 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700697}
698
Romain Guybd6b79b2010-06-26 00:13:53 -0700699void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700700 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
701 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700702 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700703}
704
705void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700706 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700707 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700708 ProgramDescription description;
709 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700710 if (mColorFilter) {
711 mColorFilter->describe(description, mExtensions);
712 }
Romain Guy889f8d12010-07-29 14:37:42 -0700713
Romain Guybd6b79b2010-06-26 00:13:53 -0700714 mModelView.loadTranslate(left, top, 0.0f);
715 mModelView.scale(right - left, bottom - top, 1.0f);
716
Romain Guy889f8d12010-07-29 14:37:42 -0700717 useProgram(mProgramCache.get(description));
718 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700719
Romain Guya9794742010-07-13 11:37:54 -0700720 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700721
722 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700723 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700724 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700725
Romain Guya9794742010-07-13 11:37:54 -0700726 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700727 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700728
Romain Guy889f8d12010-07-29 14:37:42 -0700729 // Mesh
730 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
731 glEnableVertexAttribArray(texCoordsSlot);
732 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700733 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700734 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700735
Romain Guydb1938e2010-08-02 18:50:22 -0700736 // Color filter
737 if (mColorFilter) {
738 mColorFilter->setupProgram(mCurrentProgram);
739 }
740
Romain Guyf7f93552010-07-08 19:17:03 -0700741 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700742 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700743 } else {
744 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
745 }
Romain Guy889f8d12010-07-29 14:37:42 -0700746 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700747}
748
749void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700750 blend = blend || mode != SkXfermode::kSrcOver_Mode;
751 if (blend) {
752 if (!mBlend) {
753 glEnable(GL_BLEND);
754 }
755
756 GLenum sourceMode = gBlends[mode].src;
757 GLenum destMode = gBlends[mode].dst;
758 if (!isPremultiplied && sourceMode == GL_ONE) {
759 sourceMode = GL_SRC_ALPHA;
760 }
761
762 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
763 glBlendFunc(sourceMode, destMode);
764 mLastSrcMode = sourceMode;
765 mLastDstMode = destMode;
766 }
767 } else if (mBlend) {
768 glDisable(GL_BLEND);
769 }
770 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700771}
772
Romain Guy889f8d12010-07-29 14:37:42 -0700773bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700774 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700775 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700776 program->use();
777 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700778 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700779 }
Romain Guy6926c722010-07-12 20:20:03 -0700780 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700781}
782
Romain Guy026c5e162010-06-28 17:12:22 -0700783void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700784 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700785 TextureVertex::setUV(v++, u1, v1);
786 TextureVertex::setUV(v++, u2, v1);
787 TextureVertex::setUV(v++, u1, v2);
788 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700789}
790
791void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
792 if (paint) {
793 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
794 if (!isMode) {
795 // Assume SRC_OVER
796 *mode = SkXfermode::kSrcOver_Mode;
797 }
798
799 // Skia draws using the color's alpha channel if < 255
800 // Otherwise, it uses the paint's alpha
801 int color = paint->getColor();
802 *alpha = (color >> 24) & 0xFF;
803 if (*alpha == 255) {
804 *alpha = paint->getAlpha();
805 }
806 } else {
807 *mode = SkXfermode::kSrcOver_Mode;
808 *alpha = 255;
809 }
Romain Guy026c5e162010-06-28 17:12:22 -0700810}
811
Romain Guy889f8d12010-07-29 14:37:42 -0700812void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
813 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700814 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700815 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
816 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
817}
818
Romain Guy9d5316e2010-06-24 19:30:36 -0700819}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700820}; // namespace android