blob: 3d9aa261edeb2c5428576259e9d71cdcc289fa65 [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 Guy889f8d12010-07-29 14:37:42 -0700147 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mMaxTextureUnits);
148 if (mMaxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
149 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
150 }
151 mLastTexture[0] = mLastTexture[1] = mLastTexture[2] = 0;
Romain Guye4d01122010-06-16 18:44:05 -0700152}
153
Romain Guy85bf02f2010-06-22 13:11:24 -0700154OpenGLRenderer::~OpenGLRenderer() {
155 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700156
157 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700158 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700159 mGradientCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700160 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guyf6a11b82010-06-23 17:47:49 -0700163///////////////////////////////////////////////////////////////////////////////
164// Setup
165///////////////////////////////////////////////////////////////////////////////
166
Romain Guy85bf02f2010-06-22 13:11:24 -0700167void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700168 glViewport(0, 0, width, height);
169
Romain Guy260e1022010-07-12 14:41:06 -0700170 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700171
172 mWidth = width;
173 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700174 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700175}
176
Romain Guy85bf02f2010-06-22 13:11:24 -0700177void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700178 mSnapshot = &mFirstSnapshot;
179 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180
Romain Guy08ae3172010-06-21 19:35:50 -0700181 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700182
Romain Guy08ae3172010-06-21 19:35:50 -0700183 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
184 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700185
Romain Guy08ae3172010-06-21 19:35:50 -0700186 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700187 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700188
Romain Guybb9524b2010-06-22 18:56:38 -0700189 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
190}
191
Romain Guyf6a11b82010-06-23 17:47:49 -0700192///////////////////////////////////////////////////////////////////////////////
193// State management
194///////////////////////////////////////////////////////////////////////////////
195
Romain Guybb9524b2010-06-22 18:56:38 -0700196int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700197 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
200int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700201 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700202}
203
204void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700205 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700206
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 if (restoreSnapshot()) {
208 setScissorFromClip();
209 }
Romain Guybb9524b2010-06-22 18:56:38 -0700210}
211
212void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700213 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700214
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700216
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 while (mSaveCount != saveCount - 1) {
218 restoreClip |= restoreSnapshot();
219 }
Romain Guybb9524b2010-06-22 18:56:38 -0700220
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 if (restoreClip) {
222 setScissorFromClip();
223 }
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700227 mSnapshot = new Snapshot(mSnapshot);
228 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700229}
230
231bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700233 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700234 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700235
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 sp<Snapshot> current = mSnapshot;
237 sp<Snapshot> previous = mSnapshot->previous;
238
Romain Guyf86ef572010-07-01 11:05:42 -0700239 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700240 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700241 }
242
Romain Guybd6b79b2010-06-26 00:13:53 -0700243 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700244 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700245 }
246
247 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700248 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700249
Romain Guy7ae7ac42010-06-25 13:46:18 -0700250 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700251}
252
Romain Guyd55a8612010-06-28 17:42:46 -0700253void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700254 if (!current->layer) {
255 LOGE("Attempting to compose a layer that does not exist");
256 return;
257 }
258
Romain Guyd55a8612010-06-28 17:42:46 -0700259 // Unbind current FBO and restore previous one
260 // Most of the time, previous->fbo will be 0 to bind the default buffer
261 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
262
263 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700264 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700265 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
266
Romain Guydda570202010-07-06 11:39:32 -0700267 Layer* layer = current->layer;
268
Romain Guyd55a8612010-06-28 17:42:46 -0700269 // Compute the correct texture coordinates for the FBO texture
270 // The texture is currently as big as the window but drawn with
271 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700272 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700273
Romain Guydda570202010-07-06 11:39:32 -0700274 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700275 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700276
Romain Guydda570202010-07-06 11:39:32 -0700277 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700278 // Failing to add the layer to the cache should happen only if the
279 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700280 if (!mLayerCache.put(size, layer)) {
281 LAYER_LOGD("Deleting layer");
282
283 glDeleteFramebuffers(1, &layer->fbo);
284 glDeleteTextures(1, &layer->texture);
285
286 delete layer;
287 }
Romain Guyd55a8612010-06-28 17:42:46 -0700288}
289
Romain Guyf6a11b82010-06-23 17:47:49 -0700290///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700291// Layers
292///////////////////////////////////////////////////////////////////////////////
293
294int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
295 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700296 int count = saveSnapshot();
297
298 int alpha = 255;
299 SkXfermode::Mode mode;
300
301 if (p) {
302 alpha = p->getAlpha();
303 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
304 if (!isMode) {
305 // Assume SRC_OVER
306 mode = SkXfermode::kSrcOver_Mode;
307 }
308 } else {
309 mode = SkXfermode::kSrcOver_Mode;
310 }
311
312 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
313
314 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700315}
316
317int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
318 int alpha, int flags) {
319 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700320 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
321 return count;
322}
Romain Guybd6b79b2010-06-26 00:13:53 -0700323
Romain Guyd55a8612010-06-28 17:42:46 -0700324bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
325 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700326
Romain Guyd27977d2010-07-14 19:18:51 -0700327 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700328 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700329
Romain Guyf18fd992010-07-08 11:45:51 -0700330 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
331 LayerSize size(right - left, bottom - top);
332
Romain Guyf18fd992010-07-08 11:45:51 -0700333 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700334 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700335 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700336 }
337
Romain Guyf18fd992010-07-08 11:45:51 -0700338 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
339
Romain Guyf86ef572010-07-01 11:05:42 -0700340 // Clear the FBO
341 glDisable(GL_SCISSOR_TEST);
342 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
343 glClear(GL_COLOR_BUFFER_BIT);
344 glEnable(GL_SCISSOR_TEST);
345
Romain Guydda570202010-07-06 11:39:32 -0700346 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700347 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700348 layer->mode = mode;
349 layer->alpha = alpha / 255.0f;
350 layer->layer.set(left, top, right, bottom);
351
352 snapshot->layer = layer;
353 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700354
Romain Guyf86ef572010-07-01 11:05:42 -0700355 // Creates a new snapshot to draw into the FBO
356 saveSnapshot();
357 // TODO: This doesn't preserve other transformations (check Skia first)
358 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700359 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700360 mSnapshot->height = bottom - top;
361 setScissorFromClip();
362
Romain Guy079ba2c2010-07-16 14:12:24 -0700363 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700364 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700365
366 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700367 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700368
Romain Guyd55a8612010-06-28 17:42:46 -0700369 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700370}
371
372///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700373// Transforms
374///////////////////////////////////////////////////////////////////////////////
375
376void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700386}
387
388void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700390}
391
392void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700393 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700394}
395
396void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700397 mat4 m(*matrix);
398 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700399}
400
401///////////////////////////////////////////////////////////////////////////////
402// Clipping
403///////////////////////////////////////////////////////////////////////////////
404
Romain Guybb9524b2010-06-22 18:56:38 -0700405void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700406 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700407 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700408}
409
410const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700411 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700412}
413
Romain Guyc7d53492010-06-25 13:41:57 -0700414bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700415 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700416 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700417 return !mSnapshot->clipRect.intersects(r);
418}
419
Romain Guy079ba2c2010-07-16 14:12:24 -0700420bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
421 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700422 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700423 setScissorFromClip();
424 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700425 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700426}
427
Romain Guyf6a11b82010-06-23 17:47:49 -0700428///////////////////////////////////////////////////////////////////////////////
429// Drawing
430///////////////////////////////////////////////////////////////////////////////
431
Romain Guyc1396e92010-06-30 17:56:19 -0700432void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700433 const float right = left + bitmap->width();
434 const float bottom = top + bitmap->height();
435
436 if (quickReject(left, top, right, bottom)) {
437 return;
438 }
439
Romain Guyf86ef572010-07-01 11:05:42 -0700440 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700441 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700442}
443
Romain Guyf86ef572010-07-01 11:05:42 -0700444void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
445 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
446 const mat4 transform(*matrix);
447 transform.mapRect(r);
448
Romain Guy6926c722010-07-12 20:20:03 -0700449 if (quickReject(r.left, r.top, r.right, r.bottom)) {
450 return;
451 }
452
Romain Guyf86ef572010-07-01 11:05:42 -0700453 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700454 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700455}
456
Romain Guy8ba548f2010-06-30 19:21:21 -0700457void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
458 float srcLeft, float srcTop, float srcRight, float srcBottom,
459 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700460 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700461 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
462 return;
463 }
464
Romain Guyf86ef572010-07-01 11:05:42 -0700465 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700466
Romain Guy8ba548f2010-06-30 19:21:21 -0700467 const float width = texture->width;
468 const float height = texture->height;
469
470 const float u1 = srcLeft / width;
471 const float v1 = srcTop / height;
472 const float u2 = srcRight / width;
473 const float v2 = srcBottom / height;
474
Romain Guy889f8d12010-07-29 14:37:42 -0700475 // TODO: Do this in the shader
Romain Guy8ba548f2010-06-30 19:21:21 -0700476 resetDrawTextureTexCoords(u1, v1, u2, v2);
477
Romain Guy82ba8142010-07-09 13:25:56 -0700478 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700479
480 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
481}
482
Romain Guydeba7852010-07-07 17:54:48 -0700483void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
484 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700485 if (quickReject(left, top, right, bottom)) {
486 return;
487 }
488
Romain Guyf7f93552010-07-08 19:17:03 -0700489 const Texture* texture = mTextureCache.get(bitmap);
490
491 int alpha;
492 SkXfermode::Mode mode;
493 getAlphaAndMode(paint, &alpha, &mode);
494
Romain Guyf7f93552010-07-08 19:17:03 -0700495 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700496 mesh->updateVertices(bitmap, left, top, right, bottom,
497 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700498
499 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
500 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700501 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
502 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700503 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700504}
505
Romain Guy85bf02f2010-06-22 13:11:24 -0700506void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700507 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700508 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700509}
Romain Guy9d5316e2010-06-24 19:30:36 -0700510
Romain Guybd6b79b2010-06-26 00:13:53 -0700511void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700512 if (quickReject(left, top, right, bottom)) {
513 return;
514 }
515
Romain Guy026c5e162010-06-28 17:12:22 -0700516 SkXfermode::Mode mode;
517
518 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
519 if (!isMode) {
520 // Assume SRC_OVER
521 mode = SkXfermode::kSrcOver_Mode;
522 }
523
524 // Skia draws using the color's alpha channel if < 255
525 // Otherwise, it uses the paint's alpha
526 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700527 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700528 color |= p->getAlpha() << 24;
529 }
530
531 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700532}
Romain Guy9d5316e2010-06-24 19:30:36 -0700533
Romain Guye8e62a42010-07-23 18:55:21 -0700534void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
535 float x, float y, SkPaint* paint) {
536 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
537 return;
538 }
539
Romain Guy889f8d12010-07-29 14:37:42 -0700540 glActiveTexture(GL_TEXTURE0);
541
Romain Guye8e62a42010-07-23 18:55:21 -0700542 float length;
543 switch (paint->getTextAlign()) {
544 case SkPaint::kCenter_Align:
545 length = paint->measureText(text, bytesCount);
546 x -= length / 2.0f;
547 break;
548 case SkPaint::kRight_Align:
549 length = paint->measureText(text, bytesCount);
550 x -= length;
551 break;
552 default:
553 break;
554 }
555
Romain Guy694b5192010-07-21 21:33:20 -0700556 int alpha;
557 SkXfermode::Mode mode;
558 getAlphaAndMode(paint, &alpha, &mode);
559
560 uint32_t color = paint->getColor();
561 const GLfloat a = alpha / 255.0f;
562 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
563 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
564 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
565
566 mModelView.loadIdentity();
567
Romain Guy889f8d12010-07-29 14:37:42 -0700568 ProgramDescription description;
569 description.hasTexture = true;
570 description.hasAlpha8Texture = true;
571
572 useProgram(mProgramCache.get(description));
573 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700574
575 chooseBlending(true, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700576 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
577
578 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
579 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700580
581 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700582 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700583
Romain Guy09147fb2010-07-22 13:08:20 -0700584 // TODO: Implement scale properly
585 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700586 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700587 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700588
589 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700590 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700591}
592
Romain Guy6926c722010-07-12 20:20:03 -0700593///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700594// Shaders
595///////////////////////////////////////////////////////////////////////////////
596
597void OpenGLRenderer::resetShader() {
598 mShader = OpenGLRenderer::kShaderNone;
Romain Guyf9764a42010-07-16 23:13:33 -0700599 mShaderKey = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700600 mShaderBlend = false;
Romain Guya1db5742010-07-20 13:09:13 -0700601 mShaderTileX = GL_CLAMP_TO_EDGE;
602 mShaderTileY = GL_CLAMP_TO_EDGE;
Romain Guyd27977d2010-07-14 19:18:51 -0700603}
604
605void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
606 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700607 mShader = OpenGLRenderer::kShaderBitmap;
Romain Guyd27977d2010-07-14 19:18:51 -0700608 mShaderBlend = hasAlpha;
609 mShaderBitmap = bitmap;
Romain Guya1db5742010-07-20 13:09:13 -0700610 mShaderTileX = gTileModes[tileX];
611 mShaderTileY = gTileModes[tileY];
Romain Guyd27977d2010-07-14 19:18:51 -0700612 mShaderMatrix = matrix;
613}
614
Romain Guyc0ac1932010-07-19 18:43:02 -0700615void OpenGLRenderer::setupLinearGradientShader(SkShader* shader, float* bounds, uint32_t* colors,
Romain Guy889f8d12010-07-29 14:37:42 -0700616 float* positions, int count, SkShader::TileMode tileMode, SkMatrix* matrix, bool hasAlpha) {
Romain Guyf9764a42010-07-16 23:13:33 -0700617 // TODO: We should use a struct to describe each shader
Romain Guy7fac2e12010-07-16 17:10:13 -0700618 mShader = OpenGLRenderer::kShaderLinearGradient;
Romain Guyf9764a42010-07-16 23:13:33 -0700619 mShaderKey = shader;
Romain Guy7fac2e12010-07-16 17:10:13 -0700620 mShaderBlend = hasAlpha;
Romain Guya1db5742010-07-20 13:09:13 -0700621 mShaderTileX = gTileModes[tileMode];
622 mShaderTileY = gTileModes[tileMode];
Romain Guy7fac2e12010-07-16 17:10:13 -0700623 mShaderMatrix = matrix;
624 mShaderBounds = bounds;
625 mShaderColors = colors;
626 mShaderPositions = positions;
Romain Guyc0ac1932010-07-19 18:43:02 -0700627 mShaderCount = count;
Romain Guy7fac2e12010-07-16 17:10:13 -0700628}
629
Romain Guyd27977d2010-07-14 19:18:51 -0700630///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700631// Drawing implementation
632///////////////////////////////////////////////////////////////////////////////
633
Romain Guy026c5e162010-06-28 17:12:22 -0700634void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700635 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700636 // If a shader is set, preserve only the alpha
637 if (mShader != kShaderNone) {
638 color |= 0x00ffffff;
639 }
640
641 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700642 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700643 const GLfloat a = alpha / 255.0f;
Romain Guyd27977d2010-07-14 19:18:51 -0700644
645 switch (mShader) {
Romain Guy7fac2e12010-07-16 17:10:13 -0700646 case OpenGLRenderer::kShaderBitmap:
Romain Guyd27977d2010-07-14 19:18:51 -0700647 drawBitmapShader(left, top, right, bottom, a, mode);
648 return;
Romain Guy7fac2e12010-07-16 17:10:13 -0700649 case OpenGLRenderer::kShaderLinearGradient:
Romain Guyc0ac1932010-07-19 18:43:02 -0700650 drawLinearGradientShader(left, top, right, bottom, a, mode);
651 return;
Romain Guyd27977d2010-07-14 19:18:51 -0700652 default:
653 break;
654 }
Romain Guy026c5e162010-06-28 17:12:22 -0700655
Romain Guyc0ac1932010-07-19 18:43:02 -0700656 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
657 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
658 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
659
Romain Guy6926c722010-07-12 20:20:03 -0700660 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700661 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700662
Romain Guyc7d53492010-06-25 13:41:57 -0700663 mModelView.loadTranslate(left, top, 0.0f);
664 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700665
Romain Guy889f8d12010-07-29 14:37:42 -0700666 ProgramDescription description;
667 Program* program = mProgramCache.get(description);
668 if (!useProgram(program)) {
Romain Guyac670c02010-07-27 17:39:27 -0700669 const GLvoid* vertices = &mMeshVertices[0].position[0];
670 const GLvoid* texCoords = &mMeshVertices[0].texture[0];
671
Romain Guy889f8d12010-07-29 14:37:42 -0700672 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700673 gMeshStride, vertices);
Romain Guy6926c722010-07-12 20:20:03 -0700674 }
Romain Guyd27977d2010-07-14 19:18:51 -0700675
676 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700677 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700678 } else {
679 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700680 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700681 }
682
Romain Guy889f8d12010-07-29 14:37:42 -0700683 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700684
Romain Guyac670c02010-07-27 17:39:27 -0700685 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700686}
Romain Guy026c5e162010-06-28 17:12:22 -0700687
Romain Guyc0ac1932010-07-19 18:43:02 -0700688void OpenGLRenderer::drawLinearGradientShader(float left, float top, float right, float bottom,
689 float alpha, SkXfermode::Mode mode) {
Romain Guy889f8d12010-07-29 14:37:42 -0700690 glActiveTexture(GL_TEXTURE1);
Romain Guyc0ac1932010-07-19 18:43:02 -0700691 Texture* texture = mGradientCache.get(mShaderKey);
692 if (!texture) {
Romain Guya1db5742010-07-20 13:09:13 -0700693 SkShader::TileMode tileMode = SkShader::kClamp_TileMode;
694 switch (mShaderTileX) {
695 case GL_REPEAT:
696 tileMode = SkShader::kRepeat_TileMode;
697 break;
698 case GL_MIRRORED_REPEAT:
699 tileMode = SkShader::kMirror_TileMode;
700 break;
701 }
702
Romain Guyc0ac1932010-07-19 18:43:02 -0700703 texture = mGradientCache.addLinearGradient(mShaderKey, mShaderBounds, mShaderColors,
Romain Guya1db5742010-07-20 13:09:13 -0700704 mShaderPositions, mShaderCount, tileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -0700705 }
706
Romain Guy889f8d12010-07-29 14:37:42 -0700707 ProgramDescription description;
708 description.hasGradient = true;
709
Romain Guyc0ac1932010-07-19 18:43:02 -0700710 mModelView.loadTranslate(left, top, 0.0f);
711 mModelView.scale(right - left, bottom - top, 1.0f);
712
Romain Guy889f8d12010-07-29 14:37:42 -0700713 useProgram(mProgramCache.get(description));
714 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyc0ac1932010-07-19 18:43:02 -0700715
716 chooseBlending(mShaderBlend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700717 bindTexture(texture->id, mShaderTileX, mShaderTileY, 1);
718 glUniform1i(mCurrentProgram->getUniform("gradientSampler"), 1);
Romain Guyc0ac1932010-07-19 18:43:02 -0700719
720 Rect start(mShaderBounds[0], mShaderBounds[1], mShaderBounds[2], mShaderBounds[3]);
721 if (mShaderMatrix) {
722 mat4 shaderMatrix(*mShaderMatrix);
723 shaderMatrix.mapRect(start);
724 }
725 mSnapshot->transform.mapRect(start);
726
727 const float gradientX = start.right - start.left;
728 const float gradientY = start.bottom - start.top;
729
730 mat4 screenSpace(mSnapshot->transform);
731 screenSpace.multiply(mModelView);
732
733 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700734 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
735 glUniform2f(mCurrentProgram->getUniform("gradientStart"), start.left, start.top);
736 glUniform2f(mCurrentProgram->getUniform("gradient"), gradientX, gradientY);
737 glUniform1f(mCurrentProgram->getUniform("gradientLength"),
Romain Guyc0ac1932010-07-19 18:43:02 -0700738 1.0f / (gradientX * gradientX + gradientY * gradientY));
Romain Guy889f8d12010-07-29 14:37:42 -0700739 glUniformMatrix4fv(mCurrentProgram->getUniform("screenSpace"), 1, GL_FALSE,
Romain Guyc0ac1932010-07-19 18:43:02 -0700740 &screenSpace.data[0]);
741
Romain Guy889f8d12010-07-29 14:37:42 -0700742 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700743 gMeshStride, &mMeshVertices[0].position[0]);
Romain Guyc0ac1932010-07-19 18:43:02 -0700744
Romain Guyac670c02010-07-27 17:39:27 -0700745 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyc0ac1932010-07-19 18:43:02 -0700746}
747
Romain Guyd27977d2010-07-14 19:18:51 -0700748void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
749 float alpha, SkXfermode::Mode mode) {
Romain Guy889f8d12010-07-29 14:37:42 -0700750 glActiveTexture(GL_TEXTURE2);
Romain Guyd27977d2010-07-14 19:18:51 -0700751 const Texture* texture = mTextureCache.get(mShaderBitmap);
752
753 const float width = texture->width;
754 const float height = texture->height;
755
Romain Guy889f8d12010-07-29 14:37:42 -0700756 mModelView.loadTranslate(left, top, 0.0f);
757 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700758
Romain Guy889f8d12010-07-29 14:37:42 -0700759 mat4 textureTransform;
Romain Guyd27977d2010-07-14 19:18:51 -0700760 if (mShaderMatrix) {
761 SkMatrix inverse;
762 mShaderMatrix->invert(&inverse);
Romain Guy889f8d12010-07-29 14:37:42 -0700763 textureTransform.load(inverse);
764 textureTransform.multiply(mModelView);
765 } else {
766 textureTransform.load(mModelView);
Romain Guyd27977d2010-07-14 19:18:51 -0700767 }
768
Romain Guy889f8d12010-07-29 14:37:42 -0700769 ProgramDescription description;
770 description.hasBitmap = true;
771 // The driver does not support non-power of two mirrored/repeated
772 // textures, so do it ourselves
773 if (!mExtensions.hasNPot()) {
774 description.isBitmapNpot = true;
775 description.bitmapWrapS = mShaderTileX;
776 description.bitmapWrapT = mShaderTileY;
777 }
Romain Guyd27977d2010-07-14 19:18:51 -0700778
Romain Guy889f8d12010-07-29 14:37:42 -0700779 useProgram(mProgramCache.get(description));
780 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700781
Romain Guy889f8d12010-07-29 14:37:42 -0700782 chooseBlending(texture->blend || alpha < 1.0f, mode);
Romain Guyd27977d2010-07-14 19:18:51 -0700783
Romain Guy889f8d12010-07-29 14:37:42 -0700784 // Texture
785 bindTexture(texture->id, mShaderTileX, mShaderTileY, 2);
786 glUniform1i(mCurrentProgram->getUniform("bitmapSampler"), 2);
787 glUniformMatrix4fv(mCurrentProgram->getUniform("textureTransform"), 1,
788 GL_FALSE, &textureTransform.data[0]);
789 glUniform2f(mCurrentProgram->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
790
791 // Always premultiplied
792 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
793
794 // Mesh
795 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
796 gMeshStride, &mMeshVertices[0].position[0]);
797
798 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700799}
800
Romain Guy82ba8142010-07-09 13:25:56 -0700801void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700802 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700803 int alpha;
804 SkXfermode::Mode mode;
805 getAlphaAndMode(paint, &alpha, &mode);
806
Romain Guya9794742010-07-13 11:37:54 -0700807 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700808 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700809}
810
Romain Guybd6b79b2010-06-26 00:13:53 -0700811void OpenGLRenderer::drawTextureRect(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) {
813 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700814 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700815}
816
817void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700818 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700819 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700820 ProgramDescription description;
821 description.hasTexture = true;
822
Romain Guybd6b79b2010-06-26 00:13:53 -0700823 mModelView.loadTranslate(left, top, 0.0f);
824 mModelView.scale(right - left, bottom - top, 1.0f);
825
Romain Guy889f8d12010-07-29 14:37:42 -0700826 useProgram(mProgramCache.get(description));
827 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700828
Romain Guya9794742010-07-13 11:37:54 -0700829 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700830
831 // Texture
832 bindTexture(texture, mShaderTileX, mShaderTileY, 0);
833 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700834
Romain Guya9794742010-07-13 11:37:54 -0700835 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700836 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700837
Romain Guy889f8d12010-07-29 14:37:42 -0700838 // Mesh
839 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
840 glEnableVertexAttribArray(texCoordsSlot);
841 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700842 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700843 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700844
Romain Guyf7f93552010-07-08 19:17:03 -0700845 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700846 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700847 } else {
848 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
849 }
Romain Guy889f8d12010-07-29 14:37:42 -0700850 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700851}
852
853void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
854 // In theory we should not blend if the mode is Src, but it's rare enough
855 // that it's not worth it
856 blend = blend || mode != SkXfermode::kSrcOver_Mode;
857 if (blend) {
858 if (!mBlend) {
859 glEnable(GL_BLEND);
860 }
861
862 GLenum sourceMode = gBlends[mode].src;
863 GLenum destMode = gBlends[mode].dst;
864 if (!isPremultiplied && sourceMode == GL_ONE) {
865 sourceMode = GL_SRC_ALPHA;
866 }
867
868 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
869 glBlendFunc(sourceMode, destMode);
870 mLastSrcMode = sourceMode;
871 mLastDstMode = destMode;
872 }
873 } else if (mBlend) {
874 glDisable(GL_BLEND);
875 }
876 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700877}
878
Romain Guy889f8d12010-07-29 14:37:42 -0700879bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700880 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700881 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700882 program->use();
883 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700884 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700885 }
Romain Guy6926c722010-07-12 20:20:03 -0700886 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700887}
888
Romain Guy026c5e162010-06-28 17:12:22 -0700889void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700890 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700891 TextureVertex::setUV(v++, u1, v1);
892 TextureVertex::setUV(v++, u2, v1);
893 TextureVertex::setUV(v++, u1, v2);
894 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700895}
896
897void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
898 if (paint) {
899 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
900 if (!isMode) {
901 // Assume SRC_OVER
902 *mode = SkXfermode::kSrcOver_Mode;
903 }
904
905 // Skia draws using the color's alpha channel if < 255
906 // Otherwise, it uses the paint's alpha
907 int color = paint->getColor();
908 *alpha = (color >> 24) & 0xFF;
909 if (*alpha == 255) {
910 *alpha = paint->getAlpha();
911 }
912 } else {
913 *mode = SkXfermode::kSrcOver_Mode;
914 *alpha = 255;
915 }
Romain Guy026c5e162010-06-28 17:12:22 -0700916}
917
Romain Guy889f8d12010-07-29 14:37:42 -0700918void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
919 glActiveTexture(gTextureUnits[textureUnit]);
920 if (texture != mLastTexture[textureUnit]) {
Romain Guya1db5742010-07-20 13:09:13 -0700921 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guy889f8d12010-07-29 14:37:42 -0700922 mLastTexture[textureUnit] = texture;
Romain Guya1db5742010-07-20 13:09:13 -0700923 }
Romain Guya1db5742010-07-20 13:09:13 -0700924 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
925 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
926}
927
Romain Guy9d5316e2010-06-24 19:30:36 -0700928}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700929}; // namespace android