blob: 0a5ff5751683795542bf06e509c297f82fa8b542 [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 Guy1e45aae2010-08-13 19:39:53 -070044#define DEFAULT_DROP_SHADOW_CACHE_SIZE 1.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 Guybd6b79b2010-06-26 00:13:53 -0700254 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700255 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700256 }
257
Romain Guy1f8c9602010-08-18 12:57:31 -0700258 mSaveCount--;
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 mSnapshot = previous;
Romain Guyf6a11b82010-06-23 17:47:49 -0700260
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
290 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
291
292 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700293}
294
295int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
296 int alpha, int flags) {
297 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700298 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
299 return count;
300}
Romain Guybd6b79b2010-06-26 00:13:53 -0700301
Romain Guyd55a8612010-06-28 17:42:46 -0700302bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
303 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700304 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700305 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700306
Romain Guyf18fd992010-07-08 11:45:51 -0700307 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
308 LayerSize size(right - left, bottom - top);
309
Romain Guyf18fd992010-07-08 11:45:51 -0700310 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700311 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700312 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700313 }
314
Romain Guyf18fd992010-07-08 11:45:51 -0700315 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
316
Romain Guyf86ef572010-07-01 11:05:42 -0700317 // Clear the FBO
318 glDisable(GL_SCISSOR_TEST);
319 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
320 glClear(GL_COLOR_BUFFER_BIT);
321 glEnable(GL_SCISSOR_TEST);
322
Romain Guydda570202010-07-06 11:39:32 -0700323 layer->mode = mode;
324 layer->alpha = alpha / 255.0f;
325 layer->layer.set(left, top, right, bottom);
326
Romain Guy8fb95422010-08-17 18:38:51 -0700327 // Save the layer in the snapshot
328 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700329 snapshot->layer = layer;
330 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700331
Romain Guy2542d192010-08-18 11:47:12 -0700332 snapshot->transform.loadTranslate(-left, -top, 0.0f);
333 snapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
334 snapshot->viewport.set(0.0f, 0.0f, right - left, bottom - top);
335 snapshot->height = bottom - top;
336 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
337 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700338
Romain Guyf86ef572010-07-01 11:05:42 -0700339 setScissorFromClip();
340
Romain Guyf86ef572010-07-01 11:05:42 -0700341 // Change the ortho projection
Romain Guy1d83e192010-08-17 11:37:00 -0700342 glViewport(0, 0, right - left, bottom - top);
343 // Don't flip the FBO, it will get flipped when drawing back to the framebuffer
344 mOrthoMatrix.loadOrtho(0.0f, right - left, 0.0f, bottom - top, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700345
Romain Guyd55a8612010-06-28 17:42:46 -0700346 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700347}
348
Romain Guy1d83e192010-08-17 11:37:00 -0700349void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
350 if (!current->layer) {
351 LOGE("Attempting to compose a layer that does not exist");
352 return;
353 }
354
355 // Unbind current FBO and restore previous one
356 // Most of the time, previous->fbo will be 0 to bind the default buffer
357 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
358
359 // Restore the clip from the previous snapshot
360 const Rect& clip = previous->clipRect;
361 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
362
363 Layer* layer = current->layer;
364 const Rect& rect = layer->layer;
365
366 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
367 layer->texture, layer->alpha, layer->mode, layer->blend);
368
369 LayerSize size(rect.getWidth(), rect.getHeight());
370 // Failing to add the layer to the cache should happen only if the
371 // layer is too large
372 if (!mLayerCache.put(size, layer)) {
373 LAYER_LOGD("Deleting layer");
374
375 glDeleteFramebuffers(1, &layer->fbo);
376 glDeleteTextures(1, &layer->texture);
377
378 delete layer;
379 }
380}
381
Romain Guybd6b79b2010-06-26 00:13:53 -0700382///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700383// Transforms
384///////////////////////////////////////////////////////////////////////////////
385
386void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700387 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700391 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700392}
393
394void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700395 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700396}
397
398void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700399 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700400}
401
402void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700403 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700404}
405
406void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700407 mat4 m(*matrix);
408 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700409}
410
411///////////////////////////////////////////////////////////////////////////////
412// Clipping
413///////////////////////////////////////////////////////////////////////////////
414
Romain Guybb9524b2010-06-22 18:56:38 -0700415void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700416 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700417 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700418}
419
420const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700421 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700422}
423
Romain Guyc7d53492010-06-25 13:41:57 -0700424bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy1d83e192010-08-17 11:37:00 -0700425 Rect r(left, top, right, bottom);
426 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700427 return !mSnapshot->clipRect.intersects(r);
428}
429
Romain Guy079ba2c2010-07-16 14:12:24 -0700430bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
431 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700432 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700433 setScissorFromClip();
434 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700435 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700436}
437
Romain Guyf6a11b82010-06-23 17:47:49 -0700438///////////////////////////////////////////////////////////////////////////////
439// Drawing
440///////////////////////////////////////////////////////////////////////////////
441
Romain Guyc1396e92010-06-30 17:56:19 -0700442void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700443 const float right = left + bitmap->width();
444 const float bottom = top + bitmap->height();
445
446 if (quickReject(left, top, right, bottom)) {
447 return;
448 }
449
Romain Guy61c8c9c2010-08-09 20:48:09 -0700450 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700451 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700452 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700453 const AutoTexture autoCleanup(texture);
454
Romain Guy6926c722010-07-12 20:20:03 -0700455 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700456}
457
Romain Guyf86ef572010-07-01 11:05:42 -0700458void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
459 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
460 const mat4 transform(*matrix);
461 transform.mapRect(r);
462
Romain Guy6926c722010-07-12 20:20:03 -0700463 if (quickReject(r.left, r.top, r.right, r.bottom)) {
464 return;
465 }
466
Romain Guy61c8c9c2010-08-09 20:48:09 -0700467 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700468 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700469 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700470 const AutoTexture autoCleanup(texture);
471
Romain Guy82ba8142010-07-09 13:25:56 -0700472 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700473}
474
Romain Guy8ba548f2010-06-30 19:21:21 -0700475void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
476 float srcLeft, float srcTop, float srcRight, float srcBottom,
477 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700478 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700479 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
480 return;
481 }
482
Romain Guy61c8c9c2010-08-09 20:48:09 -0700483 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700484 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700485 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700486 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700487
Romain Guy8ba548f2010-06-30 19:21:21 -0700488 const float width = texture->width;
489 const float height = texture->height;
490
491 const float u1 = srcLeft / width;
492 const float v1 = srcTop / height;
493 const float u2 = srcRight / width;
494 const float v2 = srcBottom / height;
495
496 resetDrawTextureTexCoords(u1, v1, u2, v2);
497
Romain Guy82ba8142010-07-09 13:25:56 -0700498 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700499
500 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
501}
502
Romain Guydeba7852010-07-07 17:54:48 -0700503void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
504 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700505 if (quickReject(left, top, right, bottom)) {
506 return;
507 }
508
Romain Guy61c8c9c2010-08-09 20:48:09 -0700509 glActiveTexture(GL_TEXTURE0);
Romain Guyf7f93552010-07-08 19:17:03 -0700510 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700511 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700512 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700513
514 int alpha;
515 SkXfermode::Mode mode;
516 getAlphaAndMode(paint, &alpha, &mode);
517
Romain Guyf7f93552010-07-08 19:17:03 -0700518 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700519 mesh->updateVertices(bitmap, left, top, right, bottom,
520 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700521
522 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
523 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700524 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
525 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700526 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700527}
528
Romain Guy85bf02f2010-06-22 13:11:24 -0700529void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700530 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700531 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700532}
Romain Guy9d5316e2010-06-24 19:30:36 -0700533
Romain Guybd6b79b2010-06-26 00:13:53 -0700534void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700535 if (quickReject(left, top, right, bottom)) {
536 return;
537 }
538
Romain Guy026c5e162010-06-28 17:12:22 -0700539 SkXfermode::Mode mode;
540
541 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
542 if (!isMode) {
543 // Assume SRC_OVER
544 mode = SkXfermode::kSrcOver_Mode;
545 }
546
547 // Skia draws using the color's alpha channel if < 255
548 // Otherwise, it uses the paint's alpha
549 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700550 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700551 color |= p->getAlpha() << 24;
552 }
553
554 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700555}
Romain Guy9d5316e2010-06-24 19:30:36 -0700556
Romain Guye8e62a42010-07-23 18:55:21 -0700557void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
558 float x, float y, SkPaint* paint) {
559 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
560 return;
561 }
562
Romain Guya674ab72010-08-10 17:26:42 -0700563 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700564 switch (paint->getTextAlign()) {
565 case SkPaint::kCenter_Align:
566 length = paint->measureText(text, bytesCount);
567 x -= length / 2.0f;
568 break;
569 case SkPaint::kRight_Align:
570 length = paint->measureText(text, bytesCount);
571 x -= length;
572 break;
573 default:
574 break;
575 }
576
Romain Guy694b5192010-07-21 21:33:20 -0700577 int alpha;
578 SkXfermode::Mode mode;
579 getAlphaAndMode(paint, &alpha, &mode);
580
Romain Guy2542d192010-08-18 11:47:12 -0700581 uint32_t color = paint->getColor();
582 const GLfloat a = alpha / 255.0f;
583 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
584 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
585 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
586
Romain Guy1e45aae2010-08-13 19:39:53 -0700587 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
588 if (mHasShadow) {
589 glActiveTexture(gTextureUnits[0]);
590 const ShadowTexture* shadow = mDropShadowCache.get(paint, text, bytesCount,
591 count, mShadowRadius);
592 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700593
Romain Guy2542d192010-08-18 11:47:12 -0700594 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700595
596 // Draw the mesh
597 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
598 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700599 }
600
Romain Guy06f96e22010-07-30 19:18:16 -0700601 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700602 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700603
Romain Guy0a417492010-08-16 20:26:20 -0700604 setupTextureAlpha8(mFontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
605 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700606
Romain Guy09147fb2010-07-22 13:08:20 -0700607 const Rect& clip = mSnapshot->getLocalClip();
Romain Guye8e62a42010-07-23 18:55:21 -0700608 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700609
610 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy0a417492010-08-16 20:26:20 -0700611 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700612
Romain Guy0a417492010-08-16 20:26:20 -0700613 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -0700614}
615
Romain Guy7fbcc042010-08-04 15:40:07 -0700616void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
617 GLuint textureUnit = 0;
618 glActiveTexture(gTextureUnits[textureUnit]);
619
Romain Guy22158e12010-08-06 11:18:34 -0700620 const PathTexture* texture = mPathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700621 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700622 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700623
624 int alpha;
625 SkXfermode::Mode mode;
626 getAlphaAndMode(paint, &alpha, &mode);
627
628 uint32_t color = paint->getColor();
629 const GLfloat a = alpha / 255.0f;
630 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
631 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
632 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
633
Romain Guy1e45aae2010-08-13 19:39:53 -0700634 const float x = texture->left - texture->offset;
635 const float y = texture->top - texture->offset;
Romain Guy0a417492010-08-16 20:26:20 -0700636
637 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
638
639 // Draw the mesh
640 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
641 glDisableVertexAttribArray(mCurrentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700642}
643
Romain Guy6926c722010-07-12 20:20:03 -0700644///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700645// Shaders
646///////////////////////////////////////////////////////////////////////////////
647
648void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700649 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700650}
651
Romain Guy06f96e22010-07-30 19:18:16 -0700652void OpenGLRenderer::setupShader(SkiaShader* shader) {
653 mShader = shader;
654 if (mShader) {
655 mShader->set(&mTextureCache, &mGradientCache);
656 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700657}
658
Romain Guyd27977d2010-07-14 19:18:51 -0700659///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700660// Color filters
661///////////////////////////////////////////////////////////////////////////////
662
663void OpenGLRenderer::resetColorFilter() {
664 mColorFilter = NULL;
665}
666
667void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
668 mColorFilter = filter;
669}
670
671///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700672// Drop shadow
673///////////////////////////////////////////////////////////////////////////////
674
675void OpenGLRenderer::resetShadow() {
676 mHasShadow = false;
677}
678
679void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
680 mHasShadow = true;
681 mShadowRadius = radius;
682 mShadowDx = dx;
683 mShadowDy = dy;
684 mShadowColor = color;
685}
686
687///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700688// Drawing implementation
689///////////////////////////////////////////////////////////////////////////////
690
Romain Guy0a417492010-08-16 20:26:20 -0700691void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700692 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700693 const float sx = x - texture->left + mShadowDx;
694 const float sy = y - texture->top + mShadowDy;
695
Romain Guy2542d192010-08-18 11:47:12 -0700696 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
697 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700698 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
699 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
700 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
701
702 GLuint textureUnit = 0;
703 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
704}
705
706void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
707 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
708 bool transforms, bool applyFilters) {
709 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
710 x, y, r, g, b, a, mode, transforms, applyFilters);
711}
712
713void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
714 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
715 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
716 // Describe the required shaders
717 ProgramDescription description;
718 description.hasTexture = true;
719 description.hasAlpha8Texture = true;
720
721 if (applyFilters) {
722 if (mShader) {
723 mShader->describe(description, mExtensions);
724 }
725 if (mColorFilter) {
726 mColorFilter->describe(description, mExtensions);
727 }
728 }
729
730 // Build and use the appropriate shader
731 useProgram(mProgramCache.get(description));
732
733 // Setup the blending mode
734 chooseBlending(true, mode);
735 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
736 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
737
738 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
739 glEnableVertexAttribArray(texCoordsSlot);
740
741 // Setup attributes
742 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
743 gMeshStride, &mMeshVertices[0].position[0]);
744 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
745 gMeshStride, &mMeshVertices[0].texture[0]);
746
747 // Setup uniforms
748 if (transforms) {
749 mModelView.loadTranslate(x, y, 0.0f);
750 mModelView.scale(width, height, 1.0f);
751 } else {
752 mModelView.loadIdentity();
753 }
754 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy0a417492010-08-16 20:26:20 -0700755 glUniform4f(mCurrentProgram->color, r, g, b, a);
756
757 textureUnit++;
758 if (applyFilters) {
759 // Setup attributes and uniforms required by the shaders
760 if (mShader) {
761 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
762 }
763 if (mColorFilter) {
764 mColorFilter->setupProgram(mCurrentProgram);
765 }
766 }
767}
768
769#define kStdStrikeThru_Offset (-6.0f / 21.0f)
770#define kStdUnderline_Offset (1.0f / 9.0f)
771#define kStdUnderline_Thickness (1.0f / 18.0f)
772
773void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
774 float x, float y, SkPaint* paint) {
775 // Handle underline and strike-through
776 uint32_t flags = paint->getFlags();
777 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
778 float underlineWidth = length;
779 // If length is > 0.0f, we already measured the text for the text alignment
780 if (length <= 0.0f) {
781 underlineWidth = paint->measureText(text, bytesCount);
782 }
783
784 float offsetX = 0;
785 switch (paint->getTextAlign()) {
786 case SkPaint::kCenter_Align:
787 offsetX = underlineWidth * 0.5f;
788 break;
789 case SkPaint::kRight_Align:
790 offsetX = underlineWidth;
791 break;
792 default:
793 break;
794 }
795
796 if (underlineWidth > 0.0f) {
797 float textSize = paint->getTextSize();
798 float height = textSize * kStdUnderline_Thickness;
799
800 float left = x - offsetX;
801 float top = 0.0f;
802 float right = left + underlineWidth;
803 float bottom = 0.0f;
804
805 if (flags & SkPaint::kUnderlineText_Flag) {
806 top = y + textSize * kStdUnderline_Offset;
807 bottom = top + height;
808 drawRect(left, top, right, bottom, paint);
809 }
810
811 if (flags & SkPaint::kStrikeThruText_Flag) {
812 top = y + textSize * kStdStrikeThru_Offset;
813 bottom = top + height;
814 drawRect(left, top, right, bottom, paint);
815 }
816 }
817 }
818}
819
Romain Guy026c5e162010-06-28 17:12:22 -0700820void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700821 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700822 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700823 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700824 color |= 0x00ffffff;
825 }
826
827 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700828 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700829 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700830 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
831 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
832 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
833
Romain Guy06f96e22010-07-30 19:18:16 -0700834 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700835
Romain Guy06f96e22010-07-30 19:18:16 -0700836 // Setup the blending mode
837 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700838
Romain Guy06f96e22010-07-30 19:18:16 -0700839 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700840 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700841 if (mShader) {
842 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700843 }
Romain Guydb1938e2010-08-02 18:50:22 -0700844 if (mColorFilter) {
845 mColorFilter->describe(description, mExtensions);
846 }
Romain Guyd27977d2010-07-14 19:18:51 -0700847
Romain Guy06f96e22010-07-30 19:18:16 -0700848 // Build and use the appropriate shader
849 useProgram(mProgramCache.get(description));
850
851 // Setup attributes
852 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
853 gMeshStride, &mMeshVertices[0].position[0]);
854
855 // Setup uniforms
856 mModelView.loadTranslate(left, top, 0.0f);
857 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700858 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700859 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700860 } else {
861 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700862 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700863 }
Romain Guy889f8d12010-07-29 14:37:42 -0700864 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700865
Romain Guy06f96e22010-07-30 19:18:16 -0700866 // Setup attributes and uniforms required by the shaders
867 if (mShader) {
868 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700869 }
Romain Guydb1938e2010-08-02 18:50:22 -0700870 if (mColorFilter) {
871 mColorFilter->setupProgram(mCurrentProgram);
872 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700873
Romain Guy06f96e22010-07-30 19:18:16 -0700874 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700875 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700876}
877
Romain Guy82ba8142010-07-09 13:25:56 -0700878void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700879 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700880 int alpha;
881 SkXfermode::Mode mode;
882 getAlphaAndMode(paint, &alpha, &mode);
883
Romain Guya9794742010-07-13 11:37:54 -0700884 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700885 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700886}
887
Romain Guybd6b79b2010-06-26 00:13:53 -0700888void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700889 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
890 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700891 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700892}
893
894void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700895 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700896 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700897 ProgramDescription description;
898 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700899 if (mColorFilter) {
900 mColorFilter->describe(description, mExtensions);
901 }
Romain Guy889f8d12010-07-29 14:37:42 -0700902
Romain Guybd6b79b2010-06-26 00:13:53 -0700903 mModelView.loadTranslate(left, top, 0.0f);
904 mModelView.scale(right - left, bottom - top, 1.0f);
905
Romain Guy889f8d12010-07-29 14:37:42 -0700906 useProgram(mProgramCache.get(description));
907 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700908
Romain Guya9794742010-07-13 11:37:54 -0700909 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700910
911 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700912 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700913 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700914
Romain Guya9794742010-07-13 11:37:54 -0700915 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700916 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700917
Romain Guy889f8d12010-07-29 14:37:42 -0700918 // Mesh
919 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
920 glEnableVertexAttribArray(texCoordsSlot);
921 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700922 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700923 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700924
Romain Guydb1938e2010-08-02 18:50:22 -0700925 // Color filter
926 if (mColorFilter) {
927 mColorFilter->setupProgram(mCurrentProgram);
928 }
929
Romain Guyf7f93552010-07-08 19:17:03 -0700930 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700931 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700932 } else {
933 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
934 }
Romain Guy889f8d12010-07-29 14:37:42 -0700935 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700936}
937
938void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700939 blend = blend || mode != SkXfermode::kSrcOver_Mode;
940 if (blend) {
941 if (!mBlend) {
942 glEnable(GL_BLEND);
943 }
944
945 GLenum sourceMode = gBlends[mode].src;
946 GLenum destMode = gBlends[mode].dst;
947 if (!isPremultiplied && sourceMode == GL_ONE) {
948 sourceMode = GL_SRC_ALPHA;
949 }
950
951 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
952 glBlendFunc(sourceMode, destMode);
953 mLastSrcMode = sourceMode;
954 mLastDstMode = destMode;
955 }
956 } else if (mBlend) {
957 glDisable(GL_BLEND);
958 }
959 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700960}
961
Romain Guy889f8d12010-07-29 14:37:42 -0700962bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700963 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700964 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700965 program->use();
966 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700967 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700968 }
Romain Guy6926c722010-07-12 20:20:03 -0700969 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700970}
971
Romain Guy026c5e162010-06-28 17:12:22 -0700972void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700973 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700974 TextureVertex::setUV(v++, u1, v1);
975 TextureVertex::setUV(v++, u2, v1);
976 TextureVertex::setUV(v++, u1, v2);
977 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700978}
979
980void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
981 if (paint) {
982 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
983 if (!isMode) {
984 // Assume SRC_OVER
985 *mode = SkXfermode::kSrcOver_Mode;
986 }
987
988 // Skia draws using the color's alpha channel if < 255
989 // Otherwise, it uses the paint's alpha
990 int color = paint->getColor();
991 *alpha = (color >> 24) & 0xFF;
992 if (*alpha == 255) {
993 *alpha = paint->getAlpha();
994 }
995 } else {
996 *mode = SkXfermode::kSrcOver_Mode;
997 *alpha = 255;
998 }
Romain Guy026c5e162010-06-28 17:12:22 -0700999}
1000
Romain Guy889f8d12010-07-29 14:37:42 -07001001void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1002 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001003 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001004 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1005 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1006}
1007
Romain Guy9d5316e2010-06-24 19:30:36 -07001008}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001009}; // namespace android