blob: 187e9d864bbeda4a02cf439ba3b1a721a70c7159 [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 Guy026c5e162010-06-28 17:12:22 -0700135
Romain Guyac670c02010-07-27 17:39:27 -0700136 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
137
Romain Guyae5575b2010-07-29 18:48:04 -0700138 mFirstSnapshot = new Snapshot;
139
140 GLint maxTextureUnits;
141 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
142 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700143 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
144 }
Romain Guye4d01122010-06-16 18:44:05 -0700145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147OpenGLRenderer::~OpenGLRenderer() {
148 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700149
150 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700151 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700152 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700153 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700154}
155
Romain Guyf6a11b82010-06-23 17:47:49 -0700156///////////////////////////////////////////////////////////////////////////////
157// Setup
158///////////////////////////////////////////////////////////////////////////////
159
Romain Guy85bf02f2010-06-22 13:11:24 -0700160void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700161 glViewport(0, 0, width, height);
162
Romain Guy260e1022010-07-12 14:41:06 -0700163 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700164
165 mWidth = width;
166 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700167 mFirstSnapshot->height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700168}
169
Romain Guy85bf02f2010-06-22 13:11:24 -0700170void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700171 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700172 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700173
Romain Guy08ae3172010-06-21 19:35:50 -0700174 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700175
Romain Guy08ae3172010-06-21 19:35:50 -0700176 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
177 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700178
Romain Guy08ae3172010-06-21 19:35:50 -0700179 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700180 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700181
Romain Guyb82da652010-07-30 11:36:12 -0700182 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700183}
184
Romain Guyf6a11b82010-06-23 17:47:49 -0700185///////////////////////////////////////////////////////////////////////////////
186// State management
187///////////////////////////////////////////////////////////////////////////////
188
Romain Guybb9524b2010-06-22 18:56:38 -0700189int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700191}
192
193int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700199
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 if (restoreSnapshot()) {
201 setScissorFromClip();
202 }
Romain Guybb9524b2010-06-22 18:56:38 -0700203}
204
205void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700207
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700209
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 while (mSaveCount != saveCount - 1) {
211 restoreClip |= restoreSnapshot();
212 }
Romain Guybb9524b2010-06-22 18:56:38 -0700213
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 if (restoreClip) {
215 setScissorFromClip();
216 }
Romain Guybb9524b2010-06-22 18:56:38 -0700217}
218
219int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 mSnapshot = new Snapshot(mSnapshot);
221 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700222}
223
224bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700225 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700226 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700227 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700228
Romain Guybd6b79b2010-06-26 00:13:53 -0700229 sp<Snapshot> current = mSnapshot;
230 sp<Snapshot> previous = mSnapshot->previous;
231
Romain Guyf86ef572010-07-01 11:05:42 -0700232 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700233 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700234 }
235
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700237 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700238 }
239
240 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700241 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700242
Romain Guy7ae7ac42010-06-25 13:46:18 -0700243 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700244}
245
Romain Guyd55a8612010-06-28 17:42:46 -0700246void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700247 if (!current->layer) {
248 LOGE("Attempting to compose a layer that does not exist");
249 return;
250 }
251
Romain Guyd55a8612010-06-28 17:42:46 -0700252 // Unbind current FBO and restore previous one
253 // Most of the time, previous->fbo will be 0 to bind the default buffer
254 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
255
256 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700257 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700258 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
259
Romain Guydda57022010-07-06 11:39:32 -0700260 Layer* layer = current->layer;
Romain Guydda57022010-07-06 11:39:32 -0700261 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700262
Romain Guydda57022010-07-06 11:39:32 -0700263 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700264 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700265
Romain Guydda57022010-07-06 11:39:32 -0700266 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700267 // Failing to add the layer to the cache should happen only if the
268 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700269 if (!mLayerCache.put(size, layer)) {
270 LAYER_LOGD("Deleting layer");
271
272 glDeleteFramebuffers(1, &layer->fbo);
273 glDeleteTextures(1, &layer->texture);
274
275 delete layer;
276 }
Romain Guyd55a8612010-06-28 17:42:46 -0700277}
278
Romain Guyf6a11b82010-06-23 17:47:49 -0700279///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700280// Layers
281///////////////////////////////////////////////////////////////////////////////
282
283int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
284 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700285 int count = saveSnapshot();
286
287 int alpha = 255;
288 SkXfermode::Mode mode;
289
290 if (p) {
291 alpha = p->getAlpha();
292 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
293 if (!isMode) {
294 // Assume SRC_OVER
295 mode = SkXfermode::kSrcOver_Mode;
296 }
297 } else {
298 mode = SkXfermode::kSrcOver_Mode;
299 }
300
301 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
302
303 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700304}
305
306int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
307 int alpha, int flags) {
308 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700309 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
310 return count;
311}
Romain Guybd6b79b2010-06-26 00:13:53 -0700312
Romain Guyd55a8612010-06-28 17:42:46 -0700313bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
314 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700315 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700316 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700317
Romain Guyf18fd992010-07-08 11:45:51 -0700318 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
319 LayerSize size(right - left, bottom - top);
320
Romain Guyf18fd992010-07-08 11:45:51 -0700321 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700322 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700323 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700324 }
325
Romain Guyf18fd992010-07-08 11:45:51 -0700326 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
327
Romain Guyf86ef572010-07-01 11:05:42 -0700328 // Clear the FBO
329 glDisable(GL_SCISSOR_TEST);
330 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
331 glClear(GL_COLOR_BUFFER_BIT);
332 glEnable(GL_SCISSOR_TEST);
333
Romain Guydda57022010-07-06 11:39:32 -0700334 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700335 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700336 layer->mode = mode;
337 layer->alpha = alpha / 255.0f;
338 layer->layer.set(left, top, right, bottom);
339
340 snapshot->layer = layer;
341 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700342
Romain Guyf86ef572010-07-01 11:05:42 -0700343 // Creates a new snapshot to draw into the FBO
344 saveSnapshot();
345 // TODO: This doesn't preserve other transformations (check Skia first)
346 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700347 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700348 mSnapshot->height = bottom - top;
349 setScissorFromClip();
350
Romain Guy079ba2c2010-07-16 14:12:24 -0700351 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700352 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700353
354 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700355 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700356
Romain Guyd55a8612010-06-28 17:42:46 -0700357 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700358}
359
360///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700361// Transforms
362///////////////////////////////////////////////////////////////////////////////
363
364void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700365 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700366}
367
368void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700370}
371
372void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700373 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700374}
375
376void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mat4 m(*matrix);
386 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700387}
388
389///////////////////////////////////////////////////////////////////////////////
390// Clipping
391///////////////////////////////////////////////////////////////////////////////
392
Romain Guybb9524b2010-06-22 18:56:38 -0700393void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700394 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700395 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700396}
397
398const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700399 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700400}
401
Romain Guyc7d53492010-06-25 13:41:57 -0700402bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700403 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700404 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700405 return !mSnapshot->clipRect.intersects(r);
406}
407
Romain Guy079ba2c2010-07-16 14:12:24 -0700408bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
409 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700410 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700411 setScissorFromClip();
412 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700413 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700414}
415
Romain Guyf6a11b82010-06-23 17:47:49 -0700416///////////////////////////////////////////////////////////////////////////////
417// Drawing
418///////////////////////////////////////////////////////////////////////////////
419
Romain Guyc1396e92010-06-30 17:56:19 -0700420void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700421 const float right = left + bitmap->width();
422 const float bottom = top + bitmap->height();
423
424 if (quickReject(left, top, right, bottom)) {
425 return;
426 }
427
Romain Guyf86ef572010-07-01 11:05:42 -0700428 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700429 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700430}
431
Romain Guyf86ef572010-07-01 11:05:42 -0700432void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
433 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
434 const mat4 transform(*matrix);
435 transform.mapRect(r);
436
Romain Guy6926c722010-07-12 20:20:03 -0700437 if (quickReject(r.left, r.top, r.right, r.bottom)) {
438 return;
439 }
440
Romain Guyf86ef572010-07-01 11:05:42 -0700441 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700442 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700443}
444
Romain Guy8ba548f2010-06-30 19:21:21 -0700445void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
446 float srcLeft, float srcTop, float srcRight, float srcBottom,
447 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700448 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700449 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
450 return;
451 }
452
Romain Guyf86ef572010-07-01 11:05:42 -0700453 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700454
Romain Guy8ba548f2010-06-30 19:21:21 -0700455 const float width = texture->width;
456 const float height = texture->height;
457
458 const float u1 = srcLeft / width;
459 const float v1 = srcTop / height;
460 const float u2 = srcRight / width;
461 const float v2 = srcBottom / height;
462
Romain Guy889f8d12010-07-29 14:37:42 -0700463 // TODO: Do this in the shader
Romain Guy8ba548f2010-06-30 19:21:21 -0700464 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;
555
Romain Guy889f8d12010-07-29 14:37:42 -0700556 ProgramDescription description;
557 description.hasTexture = true;
558 description.hasAlpha8Texture = true;
Romain Guy06f96e22010-07-30 19:18:16 -0700559 if (mShader) {
560 mShader->describe(description, mExtensions);
561 }
Romain Guy889f8d12010-07-29 14:37:42 -0700562
563 useProgram(mProgramCache.get(description));
564 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700565
566 chooseBlending(true, mode);
Romain Guy06f96e22010-07-30 19:18:16 -0700567 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
568 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
Romain Guy889f8d12010-07-29 14:37:42 -0700569
570 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
571 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700572
573 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700574 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700575
Romain Guy06f96e22010-07-30 19:18:16 -0700576 textureUnit++;
577 // Setup attributes and uniforms required by the shaders
578 if (mShader) {
579 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
580 }
581
Romain Guy09147fb2010-07-22 13:08:20 -0700582 // TODO: Implement scale properly
583 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700584 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700585 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700586
587 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700588 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700589}
590
Romain Guy6926c722010-07-12 20:20:03 -0700591///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700592// Shaders
593///////////////////////////////////////////////////////////////////////////////
594
595void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700596 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700597}
598
Romain Guy06f96e22010-07-30 19:18:16 -0700599void OpenGLRenderer::setupShader(SkiaShader* shader) {
600 mShader = shader;
601 if (mShader) {
602 mShader->set(&mTextureCache, &mGradientCache);
603 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700604}
605
Romain Guyd27977d2010-07-14 19:18:51 -0700606///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700607// Drawing implementation
608///////////////////////////////////////////////////////////////////////////////
609
Romain Guy026c5e162010-06-28 17:12:22 -0700610void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700611 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700612 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700613 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700614 color |= 0x00ffffff;
615 }
616
617 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700618 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700619 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700620 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
621 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
622 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
623
Romain Guy06f96e22010-07-30 19:18:16 -0700624 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700625
Romain Guy06f96e22010-07-30 19:18:16 -0700626 // Setup the blending mode
627 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700628
Romain Guy06f96e22010-07-30 19:18:16 -0700629 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700630 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700631 if (mShader) {
632 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700633 }
Romain Guyd27977d2010-07-14 19:18:51 -0700634
Romain Guy06f96e22010-07-30 19:18:16 -0700635 // Build and use the appropriate shader
636 useProgram(mProgramCache.get(description));
637
638 // Setup attributes
639 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
640 gMeshStride, &mMeshVertices[0].position[0]);
641
642 // Setup uniforms
643 mModelView.loadTranslate(left, top, 0.0f);
644 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700645 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700646 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700647 } else {
648 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700649 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700650 }
Romain Guy889f8d12010-07-29 14:37:42 -0700651 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700652
Romain Guy06f96e22010-07-30 19:18:16 -0700653 // Setup attributes and uniforms required by the shaders
654 if (mShader) {
655 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700656 }
657
Romain Guy06f96e22010-07-30 19:18:16 -0700658 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700659 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700660}
661
Romain Guy82ba8142010-07-09 13:25:56 -0700662void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700663 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700664 int alpha;
665 SkXfermode::Mode mode;
666 getAlphaAndMode(paint, &alpha, &mode);
667
Romain Guya9794742010-07-13 11:37:54 -0700668 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700669 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700670}
671
Romain Guybd6b79b2010-06-26 00:13:53 -0700672void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700673 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
674 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700675 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700676}
677
678void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700679 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700680 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700681 ProgramDescription description;
682 description.hasTexture = true;
683
Romain Guybd6b79b2010-06-26 00:13:53 -0700684 mModelView.loadTranslate(left, top, 0.0f);
685 mModelView.scale(right - left, bottom - top, 1.0f);
686
Romain Guy889f8d12010-07-29 14:37:42 -0700687 useProgram(mProgramCache.get(description));
688 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700689
Romain Guya9794742010-07-13 11:37:54 -0700690 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700691
692 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700693 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700694 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700695
Romain Guya9794742010-07-13 11:37:54 -0700696 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700697 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700698
Romain Guy889f8d12010-07-29 14:37:42 -0700699 // Mesh
700 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
701 glEnableVertexAttribArray(texCoordsSlot);
702 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700703 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700704 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700705
Romain Guyf7f93552010-07-08 19:17:03 -0700706 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700707 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700708 } else {
709 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
710 }
Romain Guy889f8d12010-07-29 14:37:42 -0700711 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700712}
713
714void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
715 // In theory we should not blend if the mode is Src, but it's rare enough
716 // that it's not worth it
717 blend = blend || mode != SkXfermode::kSrcOver_Mode;
718 if (blend) {
719 if (!mBlend) {
720 glEnable(GL_BLEND);
721 }
722
723 GLenum sourceMode = gBlends[mode].src;
724 GLenum destMode = gBlends[mode].dst;
725 if (!isPremultiplied && sourceMode == GL_ONE) {
726 sourceMode = GL_SRC_ALPHA;
727 }
728
729 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
730 glBlendFunc(sourceMode, destMode);
731 mLastSrcMode = sourceMode;
732 mLastDstMode = destMode;
733 }
734 } else if (mBlend) {
735 glDisable(GL_BLEND);
736 }
737 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700738}
739
Romain Guy889f8d12010-07-29 14:37:42 -0700740bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700741 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700742 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700743 program->use();
744 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700745 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700746 }
Romain Guy6926c722010-07-12 20:20:03 -0700747 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700748}
749
Romain Guy026c5e162010-06-28 17:12:22 -0700750void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700751 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700752 TextureVertex::setUV(v++, u1, v1);
753 TextureVertex::setUV(v++, u2, v1);
754 TextureVertex::setUV(v++, u1, v2);
755 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700756}
757
758void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
759 if (paint) {
760 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
761 if (!isMode) {
762 // Assume SRC_OVER
763 *mode = SkXfermode::kSrcOver_Mode;
764 }
765
766 // Skia draws using the color's alpha channel if < 255
767 // Otherwise, it uses the paint's alpha
768 int color = paint->getColor();
769 *alpha = (color >> 24) & 0xFF;
770 if (*alpha == 255) {
771 *alpha = paint->getAlpha();
772 }
773 } else {
774 *mode = SkXfermode::kSrcOver_Mode;
775 *alpha = 255;
776 }
Romain Guy026c5e162010-06-28 17:12:22 -0700777}
778
Romain Guy889f8d12010-07-29 14:37:42 -0700779void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
780 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700781 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700782 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
783 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
784}
785
Romain Guy9d5316e2010-06-24 19:30:36 -0700786}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700787}; // namespace android