blob: cc8e6bc60b2379028711da08ee418a9729e1e98b [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guy121e22422010-07-01 18:26:52 -070026#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
28
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include "OpenGLRenderer.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guye4d01122010-06-16 18:44:05 -070031
32namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070033namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyc0ac1932010-07-19 18:43:02 -070039#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
40#define DEFAULT_LAYER_CACHE_SIZE 10.0f
Romain Guyf7f93552010-07-08 19:17:03 -070041#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070042#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guydda570202010-07-06 11:39:32 -070043
Romain Guy121e22422010-07-01 18:26:52 -070044// Converts a number of mega-bytes into bytes
45#define MB(s) s * 1024 * 1024
46
Romain Guydda570202010-07-06 11:39:32 -070047// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070048#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070049
50///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy026c5e162010-06-28 17:12:22 -070054// This array is never used directly but used as a memcpy source in the
55// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070056static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070057 FV(0.0f, 0.0f, 0.0f, 0.0f),
58 FV(1.0f, 0.0f, 1.0f, 0.0f),
59 FV(0.0f, 1.0f, 0.0f, 1.0f),
60 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070061};
Romain Guyac670c02010-07-27 17:39:27 -070062static const GLsizei gMeshStride = sizeof(TextureVertex);
63static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070064
Romain Guy889f8d12010-07-29 14:37:42 -070065/**
66 * Structure mapping Skia xfermodes to OpenGL blending factors.
67 */
68struct Blender {
69 SkXfermode::Mode mode;
70 GLenum src;
71 GLenum dst;
72}; // struct Blender
73
Romain Guy026c5e162010-06-28 17:12:22 -070074// In this array, the index of each Blender equals the value of the first
75// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
76static const Blender gBlends[] = {
77 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
78 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
79 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
80 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
82 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
83 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
84 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
88 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
89};
Romain Guye4d01122010-06-16 18:44:05 -070090
Romain Guyd27977d2010-07-14 19:18:51 -070091static const GLint gTileModes[] = {
Romain Guy889f8d12010-07-29 14:37:42 -070092 GL_CLAMP_TO_EDGE, // SkShader::kClamp_TileMode
93 GL_REPEAT, // SkShader::kRepeat_Mode
94 GL_MIRRORED_REPEAT // SkShader::kMirror_TileMode
95};
96
97static const GLenum gTextureUnits[] = {
98 GL_TEXTURE0, // Bitmap or text
99 GL_TEXTURE1, // Gradient
100 GL_TEXTURE2 // Bitmap shader
Romain Guyd27977d2010-07-14 19:18:51 -0700101};
102
Romain Guyf6a11b82010-06-23 17:47:49 -0700103///////////////////////////////////////////////////////////////////////////////
104// Constructors/destructor
105///////////////////////////////////////////////////////////////////////////////
106
Romain Guydda570202010-07-06 11:39:32 -0700107OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700108 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -0700109 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700110 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700111 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700112 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700113 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700114
Romain Guy121e22422010-07-01 18:26:52 -0700115 char property[PROPERTY_VALUE_MAX];
116 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700117 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700119 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700120 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda570202010-07-06 11:39:32 -0700121 }
122
123 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
124 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700125 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda570202010-07-06 11:39:32 -0700126 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700127 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
128 }
129
130 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
131 LOGD(" Setting gradient cache size to %sMB", property);
132 mLayerCache.setMaxSize(MB(atof(property)));
133 } else {
134 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700135 }
136
Romain Guy889f8d12010-07-29 14:37:42 -0700137 mCurrentProgram = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700138
139 mShader = kShaderNone;
Romain Guya1db5742010-07-20 13:09:13 -0700140 mShaderTileX = GL_CLAMP_TO_EDGE;
141 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700142 mShaderMatrix = NULL;
143 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700144
Romain Guyac670c02010-07-27 17:39:27 -0700145 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
146
Romain Guyae5575b2010-07-29 18:48:04 -0700147 mFirstSnapshot = new Snapshot;
148
149 GLint maxTextureUnits;
150 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
151 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700152 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
153 }
Romain Guye4d01122010-06-16 18:44:05 -0700154}
155
Romain Guy85bf02f2010-06-22 13:11:24 -0700156OpenGLRenderer::~OpenGLRenderer() {
157 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700158
159 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700160 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700161 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700162 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700163}
164
Romain Guyf6a11b82010-06-23 17:47:49 -0700165///////////////////////////////////////////////////////////////////////////////
166// Setup
167///////////////////////////////////////////////////////////////////////////////
168
Romain Guy85bf02f2010-06-22 13:11:24 -0700169void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700170 glViewport(0, 0, width, height);
171
Romain Guy260e1022010-07-12 14:41:06 -0700172 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700173
174 mWidth = width;
175 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700176 mFirstSnapshot->height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700177}
178
Romain Guy85bf02f2010-06-22 13:11:24 -0700179void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700180 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700181 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700182
Romain Guy08ae3172010-06-21 19:35:50 -0700183 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy08ae3172010-06-21 19:35:50 -0700185 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
186 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700187
Romain Guy08ae3172010-06-21 19:35:50 -0700188 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700189 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700190
Romain Guyb82da652010-07-30 11:36:12 -0700191 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700192}
193
Romain Guyf6a11b82010-06-23 17:47:49 -0700194///////////////////////////////////////////////////////////////////////////////
195// State management
196///////////////////////////////////////////////////////////////////////////////
197
Romain Guybb9524b2010-06-22 18:56:38 -0700198int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700199 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
202int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700203 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700204}
205
206void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700208
Romain Guy7ae7ac42010-06-25 13:46:18 -0700209 if (restoreSnapshot()) {
210 setScissorFromClip();
211 }
Romain Guybb9524b2010-06-22 18:56:38 -0700212}
213
214void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700216
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700218
Romain Guy7ae7ac42010-06-25 13:46:18 -0700219 while (mSaveCount != saveCount - 1) {
220 restoreClip |= restoreSnapshot();
221 }
Romain Guybb9524b2010-06-22 18:56:38 -0700222
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 if (restoreClip) {
224 setScissorFromClip();
225 }
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
228int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700229 mSnapshot = new Snapshot(mSnapshot);
230 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700231}
232
233bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700234 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700235 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700236 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700237
Romain Guybd6b79b2010-06-26 00:13:53 -0700238 sp<Snapshot> current = mSnapshot;
239 sp<Snapshot> previous = mSnapshot->previous;
240
Romain Guyf86ef572010-07-01 11:05:42 -0700241 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700242 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700243 }
244
Romain Guybd6b79b2010-06-26 00:13:53 -0700245 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700246 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700247 }
248
249 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700250 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700251
Romain Guy7ae7ac42010-06-25 13:46:18 -0700252 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700253}
254
Romain Guyd55a8612010-06-28 17:42:46 -0700255void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700256 if (!current->layer) {
257 LOGE("Attempting to compose a layer that does not exist");
258 return;
259 }
260
Romain Guyd55a8612010-06-28 17:42:46 -0700261 // Unbind current FBO and restore previous one
262 // Most of the time, previous->fbo will be 0 to bind the default buffer
263 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
264
265 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700266 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700267 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
268
Romain Guydda570202010-07-06 11:39:32 -0700269 Layer* layer = current->layer;
Romain Guydda570202010-07-06 11:39:32 -0700270 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700271
Romain Guydda570202010-07-06 11:39:32 -0700272 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700273 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700274
Romain Guydda570202010-07-06 11:39:32 -0700275 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700276 // Failing to add the layer to the cache should happen only if the
277 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700278 if (!mLayerCache.put(size, layer)) {
279 LAYER_LOGD("Deleting layer");
280
281 glDeleteFramebuffers(1, &layer->fbo);
282 glDeleteTextures(1, &layer->texture);
283
284 delete layer;
285 }
Romain Guyd55a8612010-06-28 17:42:46 -0700286}
287
Romain Guyf6a11b82010-06-23 17:47:49 -0700288///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700289// Layers
290///////////////////////////////////////////////////////////////////////////////
291
292int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
293 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700294 int count = saveSnapshot();
295
296 int alpha = 255;
297 SkXfermode::Mode mode;
298
299 if (p) {
300 alpha = p->getAlpha();
301 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
302 if (!isMode) {
303 // Assume SRC_OVER
304 mode = SkXfermode::kSrcOver_Mode;
305 }
306 } else {
307 mode = SkXfermode::kSrcOver_Mode;
308 }
309
310 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
311
312 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700313}
314
315int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
316 int alpha, int flags) {
317 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700318 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
319 return count;
320}
Romain Guybd6b79b2010-06-26 00:13:53 -0700321
Romain Guyd55a8612010-06-28 17:42:46 -0700322bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
323 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700324 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700325 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700326
Romain Guyf18fd992010-07-08 11:45:51 -0700327 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
328 LayerSize size(right - left, bottom - top);
329
Romain Guyf18fd992010-07-08 11:45:51 -0700330 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700331 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700332 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700333 }
334
Romain Guyf18fd992010-07-08 11:45:51 -0700335 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
336
Romain Guyf86ef572010-07-01 11:05:42 -0700337 // Clear the FBO
338 glDisable(GL_SCISSOR_TEST);
339 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
340 glClear(GL_COLOR_BUFFER_BIT);
341 glEnable(GL_SCISSOR_TEST);
342
Romain Guydda570202010-07-06 11:39:32 -0700343 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700344 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700345 layer->mode = mode;
346 layer->alpha = alpha / 255.0f;
347 layer->layer.set(left, top, right, bottom);
348
349 snapshot->layer = layer;
350 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700351
Romain Guyf86ef572010-07-01 11:05:42 -0700352 // Creates a new snapshot to draw into the FBO
353 saveSnapshot();
354 // TODO: This doesn't preserve other transformations (check Skia first)
355 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700356 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700357 mSnapshot->height = bottom - top;
358 setScissorFromClip();
359
Romain Guy079ba2c2010-07-16 14:12:24 -0700360 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700361 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700362
363 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700364 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700365
Romain Guyd55a8612010-06-28 17:42:46 -0700366 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700367}
368
369///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700370// Transforms
371///////////////////////////////////////////////////////////////////////////////
372
373void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700383}
384
385void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700387}
388
389void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700390 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700391}
392
393void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700394 mat4 m(*matrix);
395 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700396}
397
398///////////////////////////////////////////////////////////////////////////////
399// Clipping
400///////////////////////////////////////////////////////////////////////////////
401
Romain Guybb9524b2010-06-22 18:56:38 -0700402void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700403 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700404 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700405}
406
407const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700408 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700409}
410
Romain Guyc7d53492010-06-25 13:41:57 -0700411bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700412 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700413 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700414 return !mSnapshot->clipRect.intersects(r);
415}
416
Romain Guy079ba2c2010-07-16 14:12:24 -0700417bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
418 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700419 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700420 setScissorFromClip();
421 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700422 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700423}
424
Romain Guyf6a11b82010-06-23 17:47:49 -0700425///////////////////////////////////////////////////////////////////////////////
426// Drawing
427///////////////////////////////////////////////////////////////////////////////
428
Romain Guyc1396e92010-06-30 17:56:19 -0700429void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700430 const float right = left + bitmap->width();
431 const float bottom = top + bitmap->height();
432
433 if (quickReject(left, top, right, bottom)) {
434 return;
435 }
436
Romain Guyf86ef572010-07-01 11:05:42 -0700437 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700438 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700439}
440
Romain Guyf86ef572010-07-01 11:05:42 -0700441void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
442 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
443 const mat4 transform(*matrix);
444 transform.mapRect(r);
445
Romain Guy6926c722010-07-12 20:20:03 -0700446 if (quickReject(r.left, r.top, r.right, r.bottom)) {
447 return;
448 }
449
Romain Guyf86ef572010-07-01 11:05:42 -0700450 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700451 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700452}
453
Romain Guy8ba548f2010-06-30 19:21:21 -0700454void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
455 float srcLeft, float srcTop, float srcRight, float srcBottom,
456 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700457 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700458 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
459 return;
460 }
461
Romain Guyf86ef572010-07-01 11:05:42 -0700462 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700463
Romain Guy8ba548f2010-06-30 19:21:21 -0700464 const float width = texture->width;
465 const float height = texture->height;
466
467 const float u1 = srcLeft / width;
468 const float v1 = srcTop / height;
469 const float u2 = srcRight / width;
470 const float v2 = srcBottom / height;
471
Romain Guy889f8d12010-07-29 14:37:42 -0700472 // TODO: Do this in the shader
Romain Guy8ba548f2010-06-30 19:21:21 -0700473 resetDrawTextureTexCoords(u1, v1, u2, v2);
474
Romain Guy82ba8142010-07-09 13:25:56 -0700475 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700476
477 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
478}
479
Romain Guydeba7852010-07-07 17:54:48 -0700480void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
481 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700482 if (quickReject(left, top, right, bottom)) {
483 return;
484 }
485
Romain Guyf7f93552010-07-08 19:17:03 -0700486 const Texture* texture = mTextureCache.get(bitmap);
487
488 int alpha;
489 SkXfermode::Mode mode;
490 getAlphaAndMode(paint, &alpha, &mode);
491
Romain Guyf7f93552010-07-08 19:17:03 -0700492 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700493 mesh->updateVertices(bitmap, left, top, right, bottom,
494 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700495
496 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
497 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700498 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
499 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700500 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700501}
502
Romain Guy85bf02f2010-06-22 13:11:24 -0700503void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700504 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700505 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700506}
Romain Guy9d5316e2010-06-24 19:30:36 -0700507
Romain Guybd6b79b2010-06-26 00:13:53 -0700508void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700509 if (quickReject(left, top, right, bottom)) {
510 return;
511 }
512
Romain Guy026c5e162010-06-28 17:12:22 -0700513 SkXfermode::Mode mode;
514
515 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
516 if (!isMode) {
517 // Assume SRC_OVER
518 mode = SkXfermode::kSrcOver_Mode;
519 }
520
521 // Skia draws using the color's alpha channel if < 255
522 // Otherwise, it uses the paint's alpha
523 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700524 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700525 color |= p->getAlpha() << 24;
526 }
527
528 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700529}
Romain Guy9d5316e2010-06-24 19:30:36 -0700530
Romain Guye8e62a42010-07-23 18:55:21 -0700531void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
532 float x, float y, SkPaint* paint) {
533 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
534 return;
535 }
536
537 float length;
538 switch (paint->getTextAlign()) {
539 case SkPaint::kCenter_Align:
540 length = paint->measureText(text, bytesCount);
541 x -= length / 2.0f;
542 break;
543 case SkPaint::kRight_Align:
544 length = paint->measureText(text, bytesCount);
545 x -= length;
546 break;
547 default:
548 break;
549 }
550
Romain Guy694b5192010-07-21 21:33:20 -0700551 int alpha;
552 SkXfermode::Mode mode;
553 getAlphaAndMode(paint, &alpha, &mode);
554
555 uint32_t color = paint->getColor();
556 const GLfloat a = alpha / 255.0f;
557 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
558 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
559 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
560
561 mModelView.loadIdentity();
562
Romain Guy889f8d12010-07-29 14:37:42 -0700563 ProgramDescription description;
564 description.hasTexture = true;
565 description.hasAlpha8Texture = true;
566
567 useProgram(mProgramCache.get(description));
568 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700569
570 chooseBlending(true, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700571 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyb82da652010-07-30 11:36:12 -0700572 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700573
574 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
575 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700576
577 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700578 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700579
Romain Guy09147fb2010-07-22 13:08:20 -0700580 // TODO: Implement scale properly
581 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700582 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700583 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700584
585 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700586 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700587}
588
Romain Guy6926c722010-07-12 20:20:03 -0700589///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700590// Shaders
591///////////////////////////////////////////////////////////////////////////////
592
593void OpenGLRenderer::resetShader() {
594 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700595 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700596 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700597 mShaderTileX = GL_CLAMP_TO_EDGE;
598 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700599}
600
601void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
602 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700603 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700604 mShaderBlend = hasAlpha;
605 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700606 mShaderTileX = gTileModes[tileX];
607 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700608 mShaderMatrix = matrix;
609}
610
Romain Guyc0ac1932010-07-19 18:43:02 -0700611void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
Romain Guy889f8d12010-07-29 14:37:42 -0700612 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700613 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700614 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700615 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700616 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700617 mShaderTileX = gTileModes[tileMode];
618 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700619 mShaderMatrix = matrix;
620 mShaderBounds = bounds;
621 mShaderColors = colors;
622 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700623 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700624}
625
Romain Guyd27977d2010-07-14 19:18:51 -0700626///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700627// Drawing implementation
628///////////////////////////////////////////////////////////////////////////////
629
Romain Guy026c5e162010-06-28 17:12:22 -0700630void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700631 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700632 // If a shader is set, preserve only the alpha
633 if (mShader != kShaderNone) {
634 color |= 0x00ffffff;
635 }
636
637 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700638 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700639 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700640
641 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700642 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700643 drawBitmapShader(left, top, right, bottom, a, mode);
644 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700645 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700646 drawLinearGradientShader(left, top, right, bottom, a, mode);
647 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700648 default:
649 break;
650 }
Romain Guy026c5e162010-06-28 17:12:22 -0700651
Romain Guyc0ac1932010-07-19 18:43:02 -0700652 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
653 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
654 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
655
Romain Guy6926c722010-07-12 20:20:03 -0700656 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700657 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700658
Romain Guyc7d53492010-06-25 13:41:57 -0700659 mModelView.loadTranslate(left, top, 0.0f);
660 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700661
Romain Guy889f8d12010-07-29 14:37:42 -0700662 ProgramDescription description;
663 Program* program = mProgramCache.get(description);
664 if (!useProgram(program)) {
Romain Guyac670c02010-07-27 17:39:27 -0700665 const GLvoid* vertices = &mMeshVertices[0].position[0];
666 const GLvoid* texCoords = &mMeshVertices[0].texture[0];
667
Romain Guy889f8d12010-07-29 14:37:42 -0700668 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700669 gMeshStride, vertices);
Romain Guy6926c722010-07-12 20:20:03 -0700670 }
Romain Guyd27977d2010-07-14 19:18:51 -0700671
672 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700673 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700674 } else {
675 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700676 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700677 }
678
Romain Guy889f8d12010-07-29 14:37:42 -0700679 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700680
Romain Guyac670c02010-07-27 17:39:27 -0700681 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700682}
Romain Guy026c5e162010-06-28 17:12:22 -0700683
Romain Guyc0ac1932010-07-19 18:43:02 -0700684void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
685 float alpha, SkXfermode::Mode mode) {
686 Texture* texture = mGradientCache.get(mShaderKey);
687 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700688 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
689 switch (mShaderTileX) {
690 case GL_REPEAT:
691 tileMode = SkShader::kRepeat_TileMode;
692 break;
693 case GL_MIRRORED_REPEAT:
694 tileMode = SkShader::kMirror_TileMode;
695 break;
696 }
697
Romain Guyc0ac1932010-07-19 18:43:02 -0700698 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700699 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700700 }
701
Romain Guy889f8d12010-07-29 14:37:42 -0700702 ProgramDescription description;
703 description.hasGradient = true;
704
Romain Guyc0ac1932010-07-19 18:43:02 -0700705 mModelView.loadTranslate(left, top, 0.0f);
706 mModelView.scale(right - left, bottom - top, 1.0f);
707
Romain Guy889f8d12010-07-29 14:37:42 -0700708 useProgram(mProgramCache.get(description));
709 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyc0ac1932010-07-19 18:43:02 -0700710
711 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guyae5575b2010-07-29 18:48:04 -0700712 bindTexture(texture->id, mShaderTileX, mShaderTileY, 0);
713 glUniform1i(mCurrentProgram->getUniform("gradientSampler"), 0);
Romain Guyc0ac1932010-07-19 18:43:02 -0700714
715 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
716 if (mShaderMatrix) {
717 mat4 shaderMatrix(*mShaderMatrix);
718 shaderMatrix.mapRect(start);
719 }
720 mSnapshot->transform.mapRect(start);
721
722 const float gradientX = start.right - start.left;
723 const float gradientY = start.bottom - start.top;
724
725 mat4 screenSpace(mSnapshot->transform);
726 screenSpace.multiply(mModelView);
727
728 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700729 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
730 glUniform2f(mCurrentProgram->getUniform("gradientStart"), start.left, start.top);
731 glUniform2f(mCurrentProgram->getUniform("gradient"), gradientX, gradientY);
732 glUniform1f(mCurrentProgram->getUniform("gradientLength"),
Romain Guyc0ac1932010-07-19 18:43:02 -0700733 1.0f / (gradientX * gradientX + gradientY * gradientY));
Romain Guy889f8d12010-07-29 14:37:42 -0700734 glUniformMatrix4fv(mCurrentProgram->getUniform("screenSpace"), 1, GL_FALSE,
Romain Guyc0ac1932010-07-19 18:43:02 -0700735 &screenSpace.data[0]);
736
Romain Guy889f8d12010-07-29 14:37:42 -0700737 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700738 gMeshStride, &mMeshVertices[0].position[0]);
Romain Guyc0ac1932010-07-19 18:43:02 -0700739
Romain Guyac670c02010-07-27 17:39:27 -0700740 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyc0ac1932010-07-19 18:43:02 -0700741}
742
Romain Guyd27977d2010-07-14 19:18:51 -0700743void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
744 float alpha, SkXfermode::Mode mode) {
745 const Texture* texture = mTextureCache.get(mShaderBitmap);
746
747 const float width = texture->width;
748 const float height = texture->height;
749
Romain Guy889f8d12010-07-29 14:37:42 -0700750 mModelView.loadTranslate(left, top, 0.0f);
751 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700752
Romain Guy889f8d12010-07-29 14:37:42 -0700753 mat4 textureTransform;
Romain Guyd27977d2010-07-14 19:18:51 -0700754 if (mShaderMatrix) {
755 SkMatrix inverse;
756 mShaderMatrix->invert(&inverse);
Romain Guy889f8d12010-07-29 14:37:42 -0700757 textureTransform.load(inverse);
758 textureTransform.multiply(mModelView);
759 } else {
760 textureTransform.load(mModelView);
Romain Guyd27977d2010-07-14 19:18:51 -0700761 }
762
Romain Guy889f8d12010-07-29 14:37:42 -0700763 ProgramDescription description;
764 description.hasBitmap = true;
765 // The driver does not support non-power of two mirrored/repeated
766 // textures, so do it ourselves
767 if (!mExtensions.hasNPot()) {
768 description.isBitmapNpot = true;
769 description.bitmapWrapS = mShaderTileX;
770 description.bitmapWrapT = mShaderTileY;
771 }
Romain Guyd27977d2010-07-14 19:18:51 -0700772
Romain Guy889f8d12010-07-29 14:37:42 -0700773 useProgram(mProgramCache.get(description));
774 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700775
Romain Guy889f8d12010-07-29 14:37:42 -0700776 chooseBlending(texture->blend || alpha < 1.0f, mode);
Romain Guyd27977d2010-07-14 19:18:51 -0700777
Romain Guy889f8d12010-07-29 14:37:42 -0700778 // Texture
Romain Guyae5575b2010-07-29 18:48:04 -0700779 bindTexture(texture->id, mShaderTileX, mShaderTileY, 0);
780 glUniform1i(mCurrentProgram->getUniform("bitmapSampler"), 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700781 glUniformMatrix4fv(mCurrentProgram->getUniform("textureTransform"), 1,
782 GL_FALSE, &textureTransform.data[0]);
783 glUniform2f(mCurrentProgram->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
784
785 // Always premultiplied
786 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
787
788 // Mesh
789 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
790 gMeshStride, &mMeshVertices[0].position[0]);
791
792 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700793}
794
Romain Guy82ba8142010-07-09 13:25:56 -0700795void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700796 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700797 int alpha;
798 SkXfermode::Mode mode;
799 getAlphaAndMode(paint, &alpha, &mode);
800
Romain Guya9794742010-07-13 11:37:54 -0700801 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700802 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700803}
804
Romain Guybd6b79b2010-06-26 00:13:53 -0700805void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700806 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
807 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700808 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700809}
810
811void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700812 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700813 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700814 ProgramDescription description;
815 description.hasTexture = true;
816
Romain Guybd6b79b2010-06-26 00:13:53 -0700817 mModelView.loadTranslate(left, top, 0.0f);
818 mModelView.scale(right - left, bottom - top, 1.0f);
819
Romain Guy889f8d12010-07-29 14:37:42 -0700820 useProgram(mProgramCache.get(description));
821 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700822
Romain Guya9794742010-07-13 11:37:54 -0700823 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700824
825 // Texture
826 bindTexture(texture, mShaderTileX, mShaderTileY, 0);
827 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700828
Romain Guya9794742010-07-13 11:37:54 -0700829 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700830 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700831
Romain Guy889f8d12010-07-29 14:37:42 -0700832 // Mesh
833 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
834 glEnableVertexAttribArray(texCoordsSlot);
835 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700836 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700837 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700838
Romain Guyf7f93552010-07-08 19:17:03 -0700839 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700840 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700841 } else {
842 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
843 }
Romain Guy889f8d12010-07-29 14:37:42 -0700844 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700845}
846
847void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
848 // In theory we should not blend if the mode is Src, but it's rare enough
849 // that it's not worth it
850 blend = blend || mode != SkXfermode::kSrcOver_Mode;
851 if (blend) {
852 if (!mBlend) {
853 glEnable(GL_BLEND);
854 }
855
856 GLenum sourceMode = gBlends[mode].src;
857 GLenum destMode = gBlends[mode].dst;
858 if (!isPremultiplied && sourceMode == GL_ONE) {
859 sourceMode = GL_SRC_ALPHA;
860 }
861
862 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
863 glBlendFunc(sourceMode, destMode);
864 mLastSrcMode = sourceMode;
865 mLastDstMode = destMode;
866 }
867 } else if (mBlend) {
868 glDisable(GL_BLEND);
869 }
870 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700871}
872
Romain Guy889f8d12010-07-29 14:37:42 -0700873bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700874 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700875 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700876 program->use();
877 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700878 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700879 }
Romain Guy6926c722010-07-12 20:20:03 -0700880 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700881}
882
Romain Guy026c5e162010-06-28 17:12:22 -0700883void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700884 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700885 TextureVertex::setUV(v++, u1, v1);
886 TextureVertex::setUV(v++, u2, v1);
887 TextureVertex::setUV(v++, u1, v2);
888 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700889}
890
891void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
892 if (paint) {
893 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
894 if (!isMode) {
895 // Assume SRC_OVER
896 *mode = SkXfermode::kSrcOver_Mode;
897 }
898
899 // Skia draws using the color's alpha channel if < 255
900 // Otherwise, it uses the paint's alpha
901 int color = paint->getColor();
902 *alpha = (color >> 24) & 0xFF;
903 if (*alpha == 255) {
904 *alpha = paint->getAlpha();
905 }
906 } else {
907 *mode = SkXfermode::kSrcOver_Mode;
908 *alpha = 255;
909 }
Romain Guy026c5e162010-06-28 17:12:22 -0700910}
911
Romain Guy889f8d12010-07-29 14:37:42 -0700912void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
913 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700914 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700915 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
916 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
917}
918
Romain Guy9d5316e2010-06-24 19:30:36 -0700919}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700920}; // namespace android