blob: 49d49da44f44e84225fa66026663bc363f179961 [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
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 Guydda570202010-07-06 11:39:32 -070045
Romain Guy06f96e22010-07-30 19:18:16 -070046#define REQUIRED_TEXTURE_UNITS_COUNT 3
47
Romain Guy121e22422010-07-01 18:26:52 -070048// Converts a number of mega-bytes into bytes
49#define MB(s) s * 1024 * 1024
50
Romain Guydda570202010-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 Guydda570202010-07-06 11:39:32 -0700105OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700106 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-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 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);
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 Guy121e22422010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guy9cccc2b92010-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 Guy9cccc2b92010-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 Guy9cccc2b92010-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 Guy9cccc2b92010-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 Guya80d32f2010-08-20 18:42:37 -0700578 float scaleX = paint->getTextScaleX();
579 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
580 if (applyScaleX) {
581 save(0);
582 translate(x - (x * scaleX), 0.0f);
583 scale(scaleX, 1.0f);
584 }
585
Romain Guya674ab72010-08-10 17:26:42 -0700586 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700587 switch (paint->getTextAlign()) {
588 case SkPaint::kCenter_Align:
589 length = paint->measureText(text, bytesCount);
590 x -= length / 2.0f;
591 break;
592 case SkPaint::kRight_Align:
593 length = paint->measureText(text, bytesCount);
594 x -= length;
595 break;
596 default:
597 break;
598 }
599
Romain Guy694b5192010-07-21 21:33:20 -0700600 int alpha;
601 SkXfermode::Mode mode;
602 getAlphaAndMode(paint, &alpha, &mode);
603
Romain Guy2542d192010-08-18 11:47:12 -0700604 uint32_t color = paint->getColor();
605 const GLfloat a = alpha / 255.0f;
606 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
607 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
608 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
609
Romain Guy1e45aae2010-08-13 19:39:53 -0700610 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
611 if (mHasShadow) {
612 glActiveTexture(gTextureUnits[0]);
613 const ShadowTexture* shadow = mDropShadowCache.get(paint, text, bytesCount,
614 count, mShadowRadius);
615 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700616
Romain Guy2542d192010-08-18 11:47:12 -0700617 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700618
619 // Draw the mesh
620 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
621 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700622 }
623
Romain Guy06f96e22010-07-30 19:18:16 -0700624 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700625 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700626
Romain Guy0a417492010-08-16 20:26:20 -0700627 setupTextureAlpha8(mFontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
628 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700629
Romain Guy09147fb2010-07-22 13:08:20 -0700630 const Rect& clip = mSnapshot->getLocalClip();
Romain Guye8e62a42010-07-23 18:55:21 -0700631 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700632
633 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy0a417492010-08-16 20:26:20 -0700634 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700635
Romain Guy0a417492010-08-16 20:26:20 -0700636 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700637
638 if (applyScaleX) {
639 restore();
640 }
Romain Guy694b5192010-07-21 21:33:20 -0700641}
642
Romain Guy7fbcc042010-08-04 15:40:07 -0700643void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700644 if (mSnapshot->invisible) return;
645
Romain Guy7fbcc042010-08-04 15:40:07 -0700646 GLuint textureUnit = 0;
647 glActiveTexture(gTextureUnits[textureUnit]);
648
Romain Guy22158e12010-08-06 11:18:34 -0700649 const PathTexture* texture = mPathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700650 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700651 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700652
Romain Guy8b55f372010-08-18 17:10:07 -0700653 const float x = texture->left - texture->offset;
654 const float y = texture->top - texture->offset;
655
656 if (quickReject(x, y, x + texture->width, y + texture->height)) {
657 return;
658 }
659
Romain Guy7fbcc042010-08-04 15:40:07 -0700660 int alpha;
661 SkXfermode::Mode mode;
662 getAlphaAndMode(paint, &alpha, &mode);
663
664 uint32_t color = paint->getColor();
665 const GLfloat a = alpha / 255.0f;
666 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
667 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
668 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
669
Romain Guy0a417492010-08-16 20:26:20 -0700670 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
671
672 // Draw the mesh
673 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
674 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700675}
676
Romain Guy6926c722010-07-12 20:20:03 -0700677///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700678// Shaders
679///////////////////////////////////////////////////////////////////////////////
680
681void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700682 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700683}
684
Romain Guy06f96e22010-07-30 19:18:16 -0700685void OpenGLRenderer::setupShader(SkiaShader* shader) {
686 mShader = shader;
687 if (mShader) {
688 mShader->set(&mTextureCache, &mGradientCache);
689 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700690}
691
Romain Guyd27977d2010-07-14 19:18:51 -0700692///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700693// Color filters
694///////////////////////////////////////////////////////////////////////////////
695
696void OpenGLRenderer::resetColorFilter() {
697 mColorFilter = NULL;
698}
699
700void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
701 mColorFilter = filter;
702}
703
704///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700705// Drop shadow
706///////////////////////////////////////////////////////////////////////////////
707
708void OpenGLRenderer::resetShadow() {
709 mHasShadow = false;
710}
711
712void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
713 mHasShadow = true;
714 mShadowRadius = radius;
715 mShadowDx = dx;
716 mShadowDy = dy;
717 mShadowColor = color;
718}
719
720///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700721// Drawing implementation
722///////////////////////////////////////////////////////////////////////////////
723
Romain Guy0a417492010-08-16 20:26:20 -0700724void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700725 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700726 const float sx = x - texture->left + mShadowDx;
727 const float sy = y - texture->top + mShadowDy;
728
Romain Guy2542d192010-08-18 11:47:12 -0700729 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
730 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700731 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
732 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
733 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
734
735 GLuint textureUnit = 0;
736 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
737}
738
739void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
740 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
741 bool transforms, bool applyFilters) {
742 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
743 x, y, r, g, b, a, mode, transforms, applyFilters);
744}
745
746void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
747 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
748 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
749 // Describe the required shaders
750 ProgramDescription description;
751 description.hasTexture = true;
752 description.hasAlpha8Texture = true;
753
754 if (applyFilters) {
755 if (mShader) {
756 mShader->describe(description, mExtensions);
757 }
758 if (mColorFilter) {
759 mColorFilter->describe(description, mExtensions);
760 }
761 }
762
763 // Build and use the appropriate shader
764 useProgram(mProgramCache.get(description));
765
766 // Setup the blending mode
767 chooseBlending(true, mode);
768 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
769 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
770
771 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
772 glEnableVertexAttribArray(texCoordsSlot);
773
774 // Setup attributes
775 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
776 gMeshStride, &mMeshVertices[0].position[0]);
777 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
778 gMeshStride, &mMeshVertices[0].texture[0]);
779
780 // Setup uniforms
781 if (transforms) {
782 mModelView.loadTranslate(x, y, 0.0f);
783 mModelView.scale(width, height, 1.0f);
784 } else {
785 mModelView.loadIdentity();
786 }
787 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy0a417492010-08-16 20:26:20 -0700788 glUniform4f(mCurrentProgram->color, r, g, b, a);
789
790 textureUnit++;
791 if (applyFilters) {
792 // Setup attributes and uniforms required by the shaders
793 if (mShader) {
794 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
795 }
796 if (mColorFilter) {
797 mColorFilter->setupProgram(mCurrentProgram);
798 }
799 }
800}
801
802#define kStdStrikeThru_Offset (-6.0f / 21.0f)
803#define kStdUnderline_Offset (1.0f / 9.0f)
804#define kStdUnderline_Thickness (1.0f / 18.0f)
805
806void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
807 float x, float y, SkPaint* paint) {
808 // Handle underline and strike-through
809 uint32_t flags = paint->getFlags();
810 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
811 float underlineWidth = length;
812 // If length is > 0.0f, we already measured the text for the text alignment
813 if (length <= 0.0f) {
814 underlineWidth = paint->measureText(text, bytesCount);
815 }
816
817 float offsetX = 0;
818 switch (paint->getTextAlign()) {
819 case SkPaint::kCenter_Align:
820 offsetX = underlineWidth * 0.5f;
821 break;
822 case SkPaint::kRight_Align:
823 offsetX = underlineWidth;
824 break;
825 default:
826 break;
827 }
828
829 if (underlineWidth > 0.0f) {
830 float textSize = paint->getTextSize();
831 float height = textSize * kStdUnderline_Thickness;
832
833 float left = x - offsetX;
834 float top = 0.0f;
835 float right = left + underlineWidth;
836 float bottom = 0.0f;
837
838 if (flags & SkPaint::kUnderlineText_Flag) {
839 top = y + textSize * kStdUnderline_Offset;
840 bottom = top + height;
841 drawRect(left, top, right, bottom, paint);
842 }
843
844 if (flags & SkPaint::kStrikeThruText_Flag) {
845 top = y + textSize * kStdStrikeThru_Offset;
846 bottom = top + height;
847 drawRect(left, top, right, bottom, paint);
848 }
849 }
850 }
851}
852
Romain Guy026c5e162010-06-28 17:12:22 -0700853void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700854 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700855 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700856 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700857 color |= 0x00ffffff;
858 }
859
860 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700861 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700862 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700863 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
864 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
865 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
866
Romain Guy06f96e22010-07-30 19:18:16 -0700867 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700868
Romain Guy06f96e22010-07-30 19:18:16 -0700869 // Setup the blending mode
870 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700871
Romain Guy06f96e22010-07-30 19:18:16 -0700872 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700873 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700874 if (mShader) {
875 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700876 }
Romain Guydb1938e2010-08-02 18:50:22 -0700877 if (mColorFilter) {
878 mColorFilter->describe(description, mExtensions);
879 }
Romain Guyd27977d2010-07-14 19:18:51 -0700880
Romain Guy06f96e22010-07-30 19:18:16 -0700881 // Build and use the appropriate shader
882 useProgram(mProgramCache.get(description));
883
884 // Setup attributes
885 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
886 gMeshStride, &mMeshVertices[0].position[0]);
887
888 // Setup uniforms
889 mModelView.loadTranslate(left, top, 0.0f);
890 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700891 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700892 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700893 } else {
894 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700895 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700896 }
Romain Guy889f8d12010-07-29 14:37:42 -0700897 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700898
Romain Guy06f96e22010-07-30 19:18:16 -0700899 // Setup attributes and uniforms required by the shaders
900 if (mShader) {
901 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700902 }
Romain Guydb1938e2010-08-02 18:50:22 -0700903 if (mColorFilter) {
904 mColorFilter->setupProgram(mCurrentProgram);
905 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700906
Romain Guy06f96e22010-07-30 19:18:16 -0700907 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700908 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700909}
910
Romain Guy82ba8142010-07-09 13:25:56 -0700911void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700912 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700913 int alpha;
914 SkXfermode::Mode mode;
915 getAlphaAndMode(paint, &alpha, &mode);
916
Romain Guya9794742010-07-13 11:37:54 -0700917 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700918 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700919}
920
Romain Guybd6b79b2010-06-26 00:13:53 -0700921void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700922 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
923 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700924 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700925}
926
927void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700928 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700929 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700930 ProgramDescription description;
931 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700932 if (mColorFilter) {
933 mColorFilter->describe(description, mExtensions);
934 }
Romain Guy889f8d12010-07-29 14:37:42 -0700935
Romain Guybd6b79b2010-06-26 00:13:53 -0700936 mModelView.loadTranslate(left, top, 0.0f);
937 mModelView.scale(right - left, bottom - top, 1.0f);
938
Romain Guy889f8d12010-07-29 14:37:42 -0700939 useProgram(mProgramCache.get(description));
940 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700941
Romain Guya9794742010-07-13 11:37:54 -0700942 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700943
944 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700945 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700946 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700947
Romain Guya9794742010-07-13 11:37:54 -0700948 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700949 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700950
Romain Guy889f8d12010-07-29 14:37:42 -0700951 // Mesh
952 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
953 glEnableVertexAttribArray(texCoordsSlot);
954 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700955 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700956 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700957
Romain Guydb1938e2010-08-02 18:50:22 -0700958 // Color filter
959 if (mColorFilter) {
960 mColorFilter->setupProgram(mCurrentProgram);
961 }
962
Romain Guyf7f93552010-07-08 19:17:03 -0700963 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700964 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700965 } else {
966 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
967 }
Romain Guy889f8d12010-07-29 14:37:42 -0700968 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700969}
970
971void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700972 blend = blend || mode != SkXfermode::kSrcOver_Mode;
973 if (blend) {
974 if (!mBlend) {
975 glEnable(GL_BLEND);
976 }
977
978 GLenum sourceMode = gBlends[mode].src;
979 GLenum destMode = gBlends[mode].dst;
980 if (!isPremultiplied && sourceMode == GL_ONE) {
981 sourceMode = GL_SRC_ALPHA;
982 }
983
984 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
985 glBlendFunc(sourceMode, destMode);
986 mLastSrcMode = sourceMode;
987 mLastDstMode = destMode;
988 }
989 } else if (mBlend) {
990 glDisable(GL_BLEND);
991 }
992 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700993}
994
Romain Guy889f8d12010-07-29 14:37:42 -0700995bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700996 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700997 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700998 program->use();
999 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001000 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001001 }
Romain Guy6926c722010-07-12 20:20:03 -07001002 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001003}
1004
Romain Guy026c5e162010-06-28 17:12:22 -07001005void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001006 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001007 TextureVertex::setUV(v++, u1, v1);
1008 TextureVertex::setUV(v++, u2, v1);
1009 TextureVertex::setUV(v++, u1, v2);
1010 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001011}
1012
1013void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1014 if (paint) {
1015 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1016 if (!isMode) {
1017 // Assume SRC_OVER
1018 *mode = SkXfermode::kSrcOver_Mode;
1019 }
1020
1021 // Skia draws using the color's alpha channel if < 255
1022 // Otherwise, it uses the paint's alpha
1023 int color = paint->getColor();
1024 *alpha = (color >> 24) & 0xFF;
1025 if (*alpha == 255) {
1026 *alpha = paint->getAlpha();
1027 }
1028 } else {
1029 *mode = SkXfermode::kSrcOver_Mode;
1030 *alpha = 255;
1031 }
Romain Guy026c5e162010-06-28 17:12:22 -07001032}
1033
Romain Guy889f8d12010-07-29 14:37:42 -07001034void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1035 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001036 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001037 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1038 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1039}
1040
Romain Guy9d5316e2010-06-24 19:30:36 -07001041}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001042}; // namespace android