blob: 344068751e055c0fb2bfdb0641b79df2ceade343 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guy121e2242010-07-01 18:26:52 -070026#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
28
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include "OpenGLRenderer.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guye4d01122010-06-16 18:44:05 -070031
32namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070033namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyc0ac1932010-07-19 18:43:02 -070039#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
Romain Guy7fbcc042010-08-04 15:40:07 -070040#define DEFAULT_LAYER_CACHE_SIZE 6.0f
41#define DEFAULT_PATH_CACHE_SIZE 6.0f
Romain Guyf7f93552010-07-08 19:17:03 -070042#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070043#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guy8b55f372010-08-18 17:10:07 -070044#define DEFAULT_DROP_SHADOW_CACHE_SIZE 2.0f
Romain Guydda57022010-07-06 11:39:32 -070045
Romain Guy06f96e22010-07-30 19:18:16 -070046#define REQUIRED_TEXTURE_UNITS_COUNT 3
47
Romain Guy121e2242010-07-01 18:26:52 -070048// Converts a number of mega-bytes into bytes
49#define MB(s) s * 1024 * 1024
50
Romain Guydda57022010-07-06 11:39:32 -070051// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070052#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070053
54///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy026c5e162010-06-28 17:12:22 -070058// This array is never used directly but used as a memcpy source in the
59// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070060static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070061 FV(0.0f, 0.0f, 0.0f, 0.0f),
62 FV(1.0f, 0.0f, 1.0f, 0.0f),
63 FV(0.0f, 1.0f, 0.0f, 1.0f),
64 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070065};
Romain Guyac670c02010-07-27 17:39:27 -070066static const GLsizei gMeshStride = sizeof(TextureVertex);
67static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070068
Romain Guy889f8d12010-07-29 14:37:42 -070069/**
70 * Structure mapping Skia xfermodes to OpenGL blending factors.
71 */
72struct Blender {
73 SkXfermode::Mode mode;
74 GLenum src;
75 GLenum dst;
76}; // struct Blender
77
Romain Guy026c5e162010-06-28 17:12:22 -070078// In this array, the index of each Blender equals the value of the first
79// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
80static const Blender gBlends[] = {
81 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
82 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
83 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
93};
Romain Guye4d01122010-06-16 18:44:05 -070094
Romain Guy889f8d12010-07-29 14:37:42 -070095static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070096 GL_TEXTURE0,
97 GL_TEXTURE1,
98 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070099};
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Constructors/destructor
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guydda57022010-07-06 11:39:32 -0700105OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700106 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700107 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700108 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700109 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guy7fbcc042010-08-04 15:40:07 -0700110 mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)),
Romain Guy1e45aae2010-08-13 19:39:53 -0700111 mPatchCache(DEFAULT_PATCH_CACHE_SIZE),
112 mDropShadowCache(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700113 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700114
Romain Guy121e2242010-07-01 18:26:52 -0700115 char property[PROPERTY_VALUE_MAX];
116 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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);
Romain Guy7fbcc042010-08-04 15:40:07 -0700132 mGradientCache.setMaxSize(MB(atof(property)));
Romain Guyc0ac1932010-07-19 18:43:02 -0700133 } else {
134 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700135 }
136
Romain Guy7fbcc042010-08-04 15:40:07 -0700137 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) {
138 LOGD(" Setting path cache size to %sMB", property);
139 mPathCache.setMaxSize(MB(atof(property)));
140 } else {
141 LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
142 }
143
Romain Guy1e45aae2010-08-13 19:39:53 -0700144 if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
145 LOGD(" Setting drop shadow cache size to %sMB", property);
146 mDropShadowCache.setMaxSize(MB(atof(property)));
147 } else {
148 LOGD(" Using default drop shadow cache size of %.2fMB", DEFAULT_DROP_SHADOW_CACHE_SIZE);
149 }
150 mDropShadowCache.setFontRenderer(mFontRenderer);
151
Romain Guy889f8d12010-07-29 14:37:42 -0700152 mCurrentProgram = NULL;
Romain Guy06f96e22010-07-30 19:18:16 -0700153 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700154 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700155 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700156
Romain Guyac670c02010-07-27 17:39:27 -0700157 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
158
Romain Guyae5575b2010-07-29 18:48:04 -0700159 mFirstSnapshot = new Snapshot;
160
161 GLint maxTextureUnits;
162 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
163 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700164 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
165 }
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy85bf02f2010-06-22 13:11:24 -0700168OpenGLRenderer::~OpenGLRenderer() {
169 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700170
171 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700172 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700173 mGradientCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700174 mPathCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700175 mPatchCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700176 mProgramCache.clear();
Romain Guy1e45aae2010-08-13 19:39:53 -0700177 mDropShadowCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700178}
179
Romain Guyf6a11b82010-06-23 17:47:49 -0700180///////////////////////////////////////////////////////////////////////////////
181// Setup
182///////////////////////////////////////////////////////////////////////////////
183
Romain Guy85bf02f2010-06-22 13:11:24 -0700184void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700185 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700186 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700187
188 mWidth = width;
189 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700190 mFirstSnapshot->height = height;
Romain Guy1d83e192010-08-17 11:37:00 -0700191 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700192}
193
Romain Guy85bf02f2010-06-22 13:11:24 -0700194void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700195 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy8fb95422010-08-17 18:38:51 -0700196 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700197
Romain Guy08ae3172010-06-21 19:35:50 -0700198 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700199
Romain Guy08ae3172010-06-21 19:35:50 -0700200 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
201 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700202
Romain Guy08ae3172010-06-21 19:35:50 -0700203 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700204 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700205
Romain Guyb82da652010-07-30 11:36:12 -0700206 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
Romain Guyf6a11b82010-06-23 17:47:49 -0700209///////////////////////////////////////////////////////////////////////////////
210// State management
211///////////////////////////////////////////////////////////////////////////////
212
Romain Guybb9524b2010-06-22 18:56:38 -0700213int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700214 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700215}
216
217int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
221void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700222 if (mSaveCount > 1) {
223 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 }
Romain Guybb9524b2010-06-22 18:56:38 -0700225}
226
227void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700228 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700229
Romain Guy8fb95422010-08-17 18:38:51 -0700230 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700231 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 }
Romain Guybb9524b2010-06-22 18:56:38 -0700233}
234
235int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700236 mSnapshot = new Snapshot(mSnapshot);
Romain Guy8fb95422010-08-17 18:38:51 -0700237 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700238}
239
240bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700241 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700242 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700243 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700244
Romain Guybd6b79b2010-06-26 00:13:53 -0700245 sp<Snapshot> current = mSnapshot;
246 sp<Snapshot> previous = mSnapshot->previous;
247
Romain Guyf86ef572010-07-01 11:05:42 -0700248 if (restoreOrtho) {
Romain Guy1d83e192010-08-17 11:37:00 -0700249 Rect& r = previous->viewport;
250 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guy260e1022010-07-12 14:41:06 -0700251 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700252 }
253
Romain Guy8b55f372010-08-18 17:10:07 -0700254 mSaveCount--;
255 mSnapshot = previous;
256
Romain Guybd6b79b2010-06-26 00:13:53 -0700257 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700258 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 }
260
Romain Guy2542d192010-08-18 11:47:12 -0700261 if (restoreClip) {
262 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700263 }
Romain Guy2542d192010-08-18 11:47:12 -0700264
265 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
Romain Guyf6a11b82010-06-23 17:47:49 -0700268///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700269// Layers
270///////////////////////////////////////////////////////////////////////////////
271
272int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
273 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700274 int count = saveSnapshot();
275
276 int alpha = 255;
277 SkXfermode::Mode mode;
278
279 if (p) {
280 alpha = p->getAlpha();
281 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
282 if (!isMode) {
283 // Assume SRC_OVER
284 mode = SkXfermode::kSrcOver_Mode;
285 }
286 } else {
287 mode = SkXfermode::kSrcOver_Mode;
288 }
289
Romain Guy8b55f372010-08-18 17:10:07 -0700290 if (alpha > 0 && !mSnapshot->invisible) {
291 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
292 } else {
293 mSnapshot->invisible = true;
294 }
Romain Guyd55a8612010-06-28 17:42:46 -0700295
296 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700297}
298
299int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
300 int alpha, int flags) {
301 int count = saveSnapshot();
Romain Guy8b55f372010-08-18 17:10:07 -0700302 if (alpha > 0 && !mSnapshot->invisible) {
303 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
304 } else {
305 mSnapshot->invisible = true;
306 }
Romain Guyd55a8612010-06-28 17:42:46 -0700307 return count;
308}
Romain Guybd6b79b2010-06-26 00:13:53 -0700309
Romain Guyd55a8612010-06-28 17:42:46 -0700310bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
311 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700312 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700313 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700314
Romain Guyf18fd992010-07-08 11:45:51 -0700315 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
316 LayerSize size(right - left, bottom - top);
317
Romain Guyf18fd992010-07-08 11:45:51 -0700318 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700319 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700320 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700321 }
322
Romain Guyf18fd992010-07-08 11:45:51 -0700323 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
324
Romain Guyf86ef572010-07-01 11:05:42 -0700325 // Clear the FBO
326 glDisable(GL_SCISSOR_TEST);
327 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
328 glClear(GL_COLOR_BUFFER_BIT);
329 glEnable(GL_SCISSOR_TEST);
330
Romain Guydda57022010-07-06 11:39:32 -0700331 layer->mode = mode;
332 layer->alpha = alpha / 255.0f;
333 layer->layer.set(left, top, right, bottom);
334
Romain Guy8fb95422010-08-17 18:38:51 -0700335 // Save the layer in the snapshot
336 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700337 snapshot->layer = layer;
338 snapshot->fbo = layer->fbo;
Romain Guy2542d192010-08-18 11:47:12 -0700339 snapshot->transform.loadTranslate(-left, -top, 0.0f);
340 snapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
341 snapshot->viewport.set(0.0f, 0.0f, right - left, bottom - top);
342 snapshot->height = bottom - top;
343 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
344 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700345
Romain Guyf86ef572010-07-01 11:05:42 -0700346 setScissorFromClip();
347
Romain Guyf86ef572010-07-01 11:05:42 -0700348 // Change the ortho projection
Romain Guy1d83e192010-08-17 11:37:00 -0700349 glViewport(0, 0, right - left, bottom - top);
Romain Guy8b55f372010-08-18 17:10:07 -0700350 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700351
Romain Guyd55a8612010-06-28 17:42:46 -0700352 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700353}
354
Romain Guy1d83e192010-08-17 11:37:00 -0700355void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
356 if (!current->layer) {
357 LOGE("Attempting to compose a layer that does not exist");
358 return;
359 }
360
361 // Unbind current FBO and restore previous one
362 // Most of the time, previous->fbo will be 0 to bind the default buffer
363 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
364
365 // Restore the clip from the previous snapshot
366 const Rect& clip = previous->clipRect;
367 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
368
369 Layer* layer = current->layer;
370 const Rect& rect = layer->layer;
371
Romain Guy8b55f372010-08-18 17:10:07 -0700372 // FBOs are already drawn with a top-left origin, don't flip the texture
373 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
374
Romain Guy1d83e192010-08-17 11:37:00 -0700375 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
376 layer->texture, layer->alpha, layer->mode, layer->blend);
377
Romain Guy8b55f372010-08-18 17:10:07 -0700378 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
379
Romain Guy1d83e192010-08-17 11:37:00 -0700380 LayerSize size(rect.getWidth(), rect.getHeight());
381 // Failing to add the layer to the cache should happen only if the
382 // layer is too large
383 if (!mLayerCache.put(size, layer)) {
384 LAYER_LOGD("Deleting layer");
385
386 glDeleteFramebuffers(1, &layer->fbo);
387 glDeleteTextures(1, &layer->texture);
388
389 delete layer;
390 }
391}
392
Romain Guybd6b79b2010-06-26 00:13:53 -0700393///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700394// Transforms
395///////////////////////////////////////////////////////////////////////////////
396
397void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700398 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700399}
400
401void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700402 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700403}
404
405void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700406 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700407}
408
409void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700410 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700411}
412
413void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700414 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700415}
416
417void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 mat4 m(*matrix);
419 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700420}
421
422///////////////////////////////////////////////////////////////////////////////
423// Clipping
424///////////////////////////////////////////////////////////////////////////////
425
Romain Guybb9524b2010-06-22 18:56:38 -0700426void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700427 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700428 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700429}
430
431const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700432 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700433}
434
Romain Guyc7d53492010-06-25 13:41:57 -0700435bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700436 if (mSnapshot->invisible) return true;
437
Romain Guy1d83e192010-08-17 11:37:00 -0700438 Rect r(left, top, right, bottom);
439 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700440 return !mSnapshot->clipRect.intersects(r);
441}
442
Romain Guy079ba2c2010-07-16 14:12:24 -0700443bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
444 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700445 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700446 setScissorFromClip();
447 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700448 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700449}
450
Romain Guyf6a11b82010-06-23 17:47:49 -0700451///////////////////////////////////////////////////////////////////////////////
452// Drawing
453///////////////////////////////////////////////////////////////////////////////
454
Romain Guyc1396e92010-06-30 17:56:19 -0700455void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700456 const float right = left + bitmap->width();
457 const float bottom = top + bitmap->height();
458
459 if (quickReject(left, top, right, bottom)) {
460 return;
461 }
462
Romain Guy61c8c9c2010-08-09 20:48:09 -0700463 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700464 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700465 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700466 const AutoTexture autoCleanup(texture);
467
Romain Guy6926c722010-07-12 20:20:03 -0700468 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700469}
470
Romain Guyf86ef572010-07-01 11:05:42 -0700471void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
472 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
473 const mat4 transform(*matrix);
474 transform.mapRect(r);
475
Romain Guy6926c722010-07-12 20:20:03 -0700476 if (quickReject(r.left, r.top, r.right, r.bottom)) {
477 return;
478 }
479
Romain Guy61c8c9c2010-08-09 20:48:09 -0700480 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700481 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700482 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700483 const AutoTexture autoCleanup(texture);
484
Romain Guy82ba8142010-07-09 13:25:56 -0700485 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700486}
487
Romain Guy8ba548f2010-06-30 19:21:21 -0700488void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
489 float srcLeft, float srcTop, float srcRight, float srcBottom,
490 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700491 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700492 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
493 return;
494 }
495
Romain Guy61c8c9c2010-08-09 20:48:09 -0700496 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700497 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700498 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700499 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700500
Romain Guy8ba548f2010-06-30 19:21:21 -0700501 const float width = texture->width;
502 const float height = texture->height;
503
504 const float u1 = srcLeft / width;
505 const float v1 = srcTop / height;
506 const float u2 = srcRight / width;
507 const float v2 = srcBottom / height;
508
509 resetDrawTextureTexCoords(u1, v1, u2, v2);
510
Romain Guy82ba8142010-07-09 13:25:56 -0700511 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700512
513 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
514}
515
Romain Guydeba7852010-07-07 17:54:48 -0700516void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
517 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700518 if (quickReject(left, top, right, bottom)) {
519 return;
520 }
521
Romain Guy61c8c9c2010-08-09 20:48:09 -0700522 glActiveTexture(GL_TEXTURE0);
Romain Guyf7f93552010-07-08 19:17:03 -0700523 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700524 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700525 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700526
527 int alpha;
528 SkXfermode::Mode mode;
529 getAlphaAndMode(paint, &alpha, &mode);
530
Romain Guyf7f93552010-07-08 19:17:03 -0700531 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700532 mesh->updateVertices(bitmap, left, top, right, bottom,
533 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700534
535 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
536 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700537 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
538 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700539 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700540}
541
Romain Guy85bf02f2010-06-22 13:11:24 -0700542void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700543 if (mSnapshot->invisible) return;
Romain Guy079ba2c2010-07-16 14:12:24 -0700544 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700545 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700546}
Romain Guy9d5316e2010-06-24 19:30:36 -0700547
Romain Guybd6b79b2010-06-26 00:13:53 -0700548void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700549 if (quickReject(left, top, right, bottom)) {
550 return;
551 }
552
Romain Guy026c5e162010-06-28 17:12:22 -0700553 SkXfermode::Mode mode;
554
555 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
556 if (!isMode) {
557 // Assume SRC_OVER
558 mode = SkXfermode::kSrcOver_Mode;
559 }
560
561 // Skia draws using the color's alpha channel if < 255
562 // Otherwise, it uses the paint's alpha
563 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700564 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700565 color |= p->getAlpha() << 24;
566 }
567
568 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700569}
Romain Guy9d5316e2010-06-24 19:30:36 -0700570
Romain Guye8e62a42010-07-23 18:55:21 -0700571void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
572 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700573 if (mSnapshot->invisible || text == NULL || count == 0 ||
574 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700575 return;
576 }
577
Romain Guya674ab72010-08-10 17:26:42 -0700578 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700579 switch (paint->getTextAlign()) {
580 case SkPaint::kCenter_Align:
581 length = paint->measureText(text, bytesCount);
582 x -= length / 2.0f;
583 break;
584 case SkPaint::kRight_Align:
585 length = paint->measureText(text, bytesCount);
586 x -= length;
587 break;
588 default:
589 break;
590 }
591
Romain Guy694b5192010-07-21 21:33:20 -0700592 int alpha;
593 SkXfermode::Mode mode;
594 getAlphaAndMode(paint, &alpha, &mode);
595
Romain Guy2542d192010-08-18 11:47:12 -0700596 uint32_t color = paint->getColor();
597 const GLfloat a = alpha / 255.0f;
598 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
599 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
600 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
601
Romain Guy1e45aae2010-08-13 19:39:53 -0700602 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
603 if (mHasShadow) {
604 glActiveTexture(gTextureUnits[0]);
605 const ShadowTexture* shadow = mDropShadowCache.get(paint, text, bytesCount,
606 count, mShadowRadius);
607 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700608
Romain Guy2542d192010-08-18 11:47:12 -0700609 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700610
611 // Draw the mesh
612 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
613 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700614 }
615
Romain Guy06f96e22010-07-30 19:18:16 -0700616 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700617 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700618
Romain Guy0a417492010-08-16 20:26:20 -0700619 setupTextureAlpha8(mFontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
620 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700621
Romain Guy09147fb2010-07-22 13:08:20 -0700622 const Rect& clip = mSnapshot->getLocalClip();
Romain Guye8e62a42010-07-23 18:55:21 -0700623 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700624
625 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy0a417492010-08-16 20:26:20 -0700626 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700627
Romain Guy0a417492010-08-16 20:26:20 -0700628 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700629}
630
Romain Guy7fbcc042010-08-04 15:40:07 -0700631void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700632 if (mSnapshot->invisible) return;
633
Romain Guy7fbcc042010-08-04 15:40:07 -0700634 GLuint textureUnit = 0;
635 glActiveTexture(gTextureUnits[textureUnit]);
636
Romain Guy22158e12010-08-06 11:18:34 -0700637 const PathTexture* texture = mPathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700638 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700639 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700640
Romain Guy8b55f372010-08-18 17:10:07 -0700641 const float x = texture->left - texture->offset;
642 const float y = texture->top - texture->offset;
643
644 if (quickReject(x, y, x + texture->width, y + texture->height)) {
645 return;
646 }
647
Romain Guy7fbcc042010-08-04 15:40:07 -0700648 int alpha;
649 SkXfermode::Mode mode;
650 getAlphaAndMode(paint, &alpha, &mode);
651
652 uint32_t color = paint->getColor();
653 const GLfloat a = alpha / 255.0f;
654 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
655 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
656 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
657
Romain Guy0a417492010-08-16 20:26:20 -0700658 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
659
660 // Draw the mesh
661 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
662 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700663}
664
Romain Guy6926c722010-07-12 20:20:03 -0700665///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700666// Shaders
667///////////////////////////////////////////////////////////////////////////////
668
669void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700670 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700671}
672
Romain Guy06f96e22010-07-30 19:18:16 -0700673void OpenGLRenderer::setupShader(SkiaShader* shader) {
674 mShader = shader;
675 if (mShader) {
676 mShader->set(&mTextureCache, &mGradientCache);
677 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700678}
679
Romain Guyd27977d2010-07-14 19:18:51 -0700680///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700681// Color filters
682///////////////////////////////////////////////////////////////////////////////
683
684void OpenGLRenderer::resetColorFilter() {
685 mColorFilter = NULL;
686}
687
688void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
689 mColorFilter = filter;
690}
691
692///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700693// Drop shadow
694///////////////////////////////////////////////////////////////////////////////
695
696void OpenGLRenderer::resetShadow() {
697 mHasShadow = false;
698}
699
700void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
701 mHasShadow = true;
702 mShadowRadius = radius;
703 mShadowDx = dx;
704 mShadowDy = dy;
705 mShadowColor = color;
706}
707
708///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700709// Drawing implementation
710///////////////////////////////////////////////////////////////////////////////
711
Romain Guy0a417492010-08-16 20:26:20 -0700712void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700713 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700714 const float sx = x - texture->left + mShadowDx;
715 const float sy = y - texture->top + mShadowDy;
716
Romain Guy2542d192010-08-18 11:47:12 -0700717 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
718 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700719 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
720 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
721 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
722
723 GLuint textureUnit = 0;
724 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
725}
726
727void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
728 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
729 bool transforms, bool applyFilters) {
730 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
731 x, y, r, g, b, a, mode, transforms, applyFilters);
732}
733
734void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
735 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
736 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
737 // Describe the required shaders
738 ProgramDescription description;
739 description.hasTexture = true;
740 description.hasAlpha8Texture = true;
741
742 if (applyFilters) {
743 if (mShader) {
744 mShader->describe(description, mExtensions);
745 }
746 if (mColorFilter) {
747 mColorFilter->describe(description, mExtensions);
748 }
749 }
750
751 // Build and use the appropriate shader
752 useProgram(mProgramCache.get(description));
753
754 // Setup the blending mode
755 chooseBlending(true, mode);
756 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
757 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
758
759 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
760 glEnableVertexAttribArray(texCoordsSlot);
761
762 // Setup attributes
763 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
764 gMeshStride, &mMeshVertices[0].position[0]);
765 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
766 gMeshStride, &mMeshVertices[0].texture[0]);
767
768 // Setup uniforms
769 if (transforms) {
770 mModelView.loadTranslate(x, y, 0.0f);
771 mModelView.scale(width, height, 1.0f);
772 } else {
773 mModelView.loadIdentity();
774 }
775 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy0a417492010-08-16 20:26:20 -0700776 glUniform4f(mCurrentProgram->color, r, g, b, a);
777
778 textureUnit++;
779 if (applyFilters) {
780 // Setup attributes and uniforms required by the shaders
781 if (mShader) {
782 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
783 }
784 if (mColorFilter) {
785 mColorFilter->setupProgram(mCurrentProgram);
786 }
787 }
788}
789
790#define kStdStrikeThru_Offset (-6.0f / 21.0f)
791#define kStdUnderline_Offset (1.0f / 9.0f)
792#define kStdUnderline_Thickness (1.0f / 18.0f)
793
794void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
795 float x, float y, SkPaint* paint) {
796 // Handle underline and strike-through
797 uint32_t flags = paint->getFlags();
798 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
799 float underlineWidth = length;
800 // If length is > 0.0f, we already measured the text for the text alignment
801 if (length <= 0.0f) {
802 underlineWidth = paint->measureText(text, bytesCount);
803 }
804
805 float offsetX = 0;
806 switch (paint->getTextAlign()) {
807 case SkPaint::kCenter_Align:
808 offsetX = underlineWidth * 0.5f;
809 break;
810 case SkPaint::kRight_Align:
811 offsetX = underlineWidth;
812 break;
813 default:
814 break;
815 }
816
817 if (underlineWidth > 0.0f) {
818 float textSize = paint->getTextSize();
819 float height = textSize * kStdUnderline_Thickness;
820
821 float left = x - offsetX;
822 float top = 0.0f;
823 float right = left + underlineWidth;
824 float bottom = 0.0f;
825
826 if (flags & SkPaint::kUnderlineText_Flag) {
827 top = y + textSize * kStdUnderline_Offset;
828 bottom = top + height;
829 drawRect(left, top, right, bottom, paint);
830 }
831
832 if (flags & SkPaint::kStrikeThruText_Flag) {
833 top = y + textSize * kStdStrikeThru_Offset;
834 bottom = top + height;
835 drawRect(left, top, right, bottom, paint);
836 }
837 }
838 }
839}
840
Romain Guy026c5e162010-06-28 17:12:22 -0700841void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700842 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700843 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700844 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700845 color |= 0x00ffffff;
846 }
847
848 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700849 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700850 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700851 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
852 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
853 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
854
Romain Guy06f96e22010-07-30 19:18:16 -0700855 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700856
Romain Guy06f96e22010-07-30 19:18:16 -0700857 // Setup the blending mode
858 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700859
Romain Guy06f96e22010-07-30 19:18:16 -0700860 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700861 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700862 if (mShader) {
863 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700864 }
Romain Guydb1938e2010-08-02 18:50:22 -0700865 if (mColorFilter) {
866 mColorFilter->describe(description, mExtensions);
867 }
Romain Guyd27977d2010-07-14 19:18:51 -0700868
Romain Guy06f96e22010-07-30 19:18:16 -0700869 // Build and use the appropriate shader
870 useProgram(mProgramCache.get(description));
871
872 // Setup attributes
873 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
874 gMeshStride, &mMeshVertices[0].position[0]);
875
876 // Setup uniforms
877 mModelView.loadTranslate(left, top, 0.0f);
878 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700879 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700880 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700881 } else {
882 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700883 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700884 }
Romain Guy889f8d12010-07-29 14:37:42 -0700885 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700886
Romain Guy06f96e22010-07-30 19:18:16 -0700887 // Setup attributes and uniforms required by the shaders
888 if (mShader) {
889 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700890 }
Romain Guydb1938e2010-08-02 18:50:22 -0700891 if (mColorFilter) {
892 mColorFilter->setupProgram(mCurrentProgram);
893 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700894
Romain Guy06f96e22010-07-30 19:18:16 -0700895 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700896 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700897}
898
Romain Guy82ba8142010-07-09 13:25:56 -0700899void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700900 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700901 int alpha;
902 SkXfermode::Mode mode;
903 getAlphaAndMode(paint, &alpha, &mode);
904
Romain Guya9794742010-07-13 11:37:54 -0700905 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700906 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700907}
908
Romain Guybd6b79b2010-06-26 00:13:53 -0700909void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700910 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
911 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700912 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700913}
914
915void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700916 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700917 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700918 ProgramDescription description;
919 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700920 if (mColorFilter) {
921 mColorFilter->describe(description, mExtensions);
922 }
Romain Guy889f8d12010-07-29 14:37:42 -0700923
Romain Guybd6b79b2010-06-26 00:13:53 -0700924 mModelView.loadTranslate(left, top, 0.0f);
925 mModelView.scale(right - left, bottom - top, 1.0f);
926
Romain Guy889f8d12010-07-29 14:37:42 -0700927 useProgram(mProgramCache.get(description));
928 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700929
Romain Guya9794742010-07-13 11:37:54 -0700930 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700931
932 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700933 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700934 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700935
Romain Guya9794742010-07-13 11:37:54 -0700936 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700937 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700938
Romain Guy889f8d12010-07-29 14:37:42 -0700939 // Mesh
940 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
941 glEnableVertexAttribArray(texCoordsSlot);
942 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700943 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700944 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700945
Romain Guydb1938e2010-08-02 18:50:22 -0700946 // Color filter
947 if (mColorFilter) {
948 mColorFilter->setupProgram(mCurrentProgram);
949 }
950
Romain Guyf7f93552010-07-08 19:17:03 -0700951 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700952 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700953 } else {
954 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
955 }
Romain Guy889f8d12010-07-29 14:37:42 -0700956 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700957}
958
959void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700960 blend = blend || mode != SkXfermode::kSrcOver_Mode;
961 if (blend) {
962 if (!mBlend) {
963 glEnable(GL_BLEND);
964 }
965
966 GLenum sourceMode = gBlends[mode].src;
967 GLenum destMode = gBlends[mode].dst;
968 if (!isPremultiplied && sourceMode == GL_ONE) {
969 sourceMode = GL_SRC_ALPHA;
970 }
971
972 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
973 glBlendFunc(sourceMode, destMode);
974 mLastSrcMode = sourceMode;
975 mLastDstMode = destMode;
976 }
977 } else if (mBlend) {
978 glDisable(GL_BLEND);
979 }
980 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700981}
982
Romain Guy889f8d12010-07-29 14:37:42 -0700983bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700984 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700985 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700986 program->use();
987 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700988 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700989 }
Romain Guy6926c722010-07-12 20:20:03 -0700990 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700991}
992
Romain Guy026c5e162010-06-28 17:12:22 -0700993void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700994 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700995 TextureVertex::setUV(v++, u1, v1);
996 TextureVertex::setUV(v++, u2, v1);
997 TextureVertex::setUV(v++, u1, v2);
998 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700999}
1000
1001void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1002 if (paint) {
1003 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1004 if (!isMode) {
1005 // Assume SRC_OVER
1006 *mode = SkXfermode::kSrcOver_Mode;
1007 }
1008
1009 // Skia draws using the color's alpha channel if < 255
1010 // Otherwise, it uses the paint's alpha
1011 int color = paint->getColor();
1012 *alpha = (color >> 24) & 0xFF;
1013 if (*alpha == 255) {
1014 *alpha = paint->getAlpha();
1015 }
1016 } else {
1017 *mode = SkXfermode::kSrcOver_Mode;
1018 *alpha = 255;
1019 }
Romain Guy026c5e162010-06-28 17:12:22 -07001020}
1021
Romain Guy889f8d12010-07-29 14:37:42 -07001022void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1023 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001024 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001025 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1026 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1027}
1028
Romain Guy9d5316e2010-06-24 19:30:36 -07001029}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001030}; // namespace android