blob: da5b9dd0097abbe0733b3fc8e1fab0a4c00a560a [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guy121e2242010-07-01 18:26:52 -070026#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
28
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include "OpenGLRenderer.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guye4d01122010-06-16 18:44:05 -070031
32namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070033namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guyc0ac1932010-07-19 18:43:02 -070039#define DEFAULT_TEXTURE_CACHE_SIZE 20.0f
Romain Guy7fbcc042010-08-04 15:40:07 -070040#define DEFAULT_LAYER_CACHE_SIZE 6.0f
41#define DEFAULT_PATH_CACHE_SIZE 6.0f
Romain Guyf7f93552010-07-08 19:17:03 -070042#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guyc0ac1932010-07-19 18:43:02 -070043#define DEFAULT_GRADIENT_CACHE_SIZE 0.5f
Romain Guy1e45aae2010-08-13 19:39:53 -070044#define DEFAULT_DROP_SHADOW_CACHE_SIZE 1.0f
Romain Guydda57022010-07-06 11:39:32 -070045
Romain Guy06f96e22010-07-30 19:18:16 -070046#define REQUIRED_TEXTURE_UNITS_COUNT 3
47
Romain Guy121e2242010-07-01 18:26:52 -070048// Converts a number of mega-bytes into bytes
49#define MB(s) s * 1024 * 1024
50
Romain Guydda57022010-07-06 11:39:32 -070051// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070052#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070053
54///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy026c5e162010-06-28 17:12:22 -070058// This array is never used directly but used as a memcpy source in the
59// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070060static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070061 FV(0.0f, 0.0f, 0.0f, 0.0f),
62 FV(1.0f, 0.0f, 1.0f, 0.0f),
63 FV(0.0f, 1.0f, 0.0f, 1.0f),
64 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070065};
Romain Guyac670c02010-07-27 17:39:27 -070066static const GLsizei gMeshStride = sizeof(TextureVertex);
67static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070068
Romain Guy889f8d12010-07-29 14:37:42 -070069/**
70 * Structure mapping Skia xfermodes to OpenGL blending factors.
71 */
72struct Blender {
73 SkXfermode::Mode mode;
74 GLenum src;
75 GLenum dst;
76}; // struct Blender
77
Romain Guy026c5e162010-06-28 17:12:22 -070078// In this array, the index of each Blender equals the value of the first
79// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
80static const Blender gBlends[] = {
81 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
82 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
83 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
93};
Romain Guye4d01122010-06-16 18:44:05 -070094
Romain Guy889f8d12010-07-29 14:37:42 -070095static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070096 GL_TEXTURE0,
97 GL_TEXTURE1,
98 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070099};
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Constructors/destructor
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guydda57022010-07-06 11:39:32 -0700105OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700106 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700107 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700108 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700109 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guy7fbcc042010-08-04 15:40:07 -0700110 mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)),
Romain Guy1e45aae2010-08-13 19:39:53 -0700111 mPatchCache(DEFAULT_PATCH_CACHE_SIZE),
112 mDropShadowCache(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700113 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700114
Romain Guy121e2242010-07-01 18:26:52 -0700115 char property[PROPERTY_VALUE_MAX];
116 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700117 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700119 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700120 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda57022010-07-06 11:39:32 -0700121 }
122
123 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
124 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700125 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700126 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700127 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
128 }
129
130 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
131 LOGD(" Setting gradient cache size to %sMB", property);
Romain Guy7fbcc042010-08-04 15:40:07 -0700132 mGradientCache.setMaxSize(MB(atof(property)));
Romain Guyc0ac1932010-07-19 18:43:02 -0700133 } else {
134 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700135 }
136
Romain Guy7fbcc042010-08-04 15:40:07 -0700137 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) {
138 LOGD(" Setting path cache size to %sMB", property);
139 mPathCache.setMaxSize(MB(atof(property)));
140 } else {
141 LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
142 }
143
Romain Guy1e45aae2010-08-13 19:39:53 -0700144 if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
145 LOGD(" Setting drop shadow cache size to %sMB", property);
146 mDropShadowCache.setMaxSize(MB(atof(property)));
147 } else {
148 LOGD(" Using default drop shadow cache size of %.2fMB", DEFAULT_DROP_SHADOW_CACHE_SIZE);
149 }
150 mDropShadowCache.setFontRenderer(mFontRenderer);
151
Romain Guy889f8d12010-07-29 14:37:42 -0700152 mCurrentProgram = NULL;
Romain Guy06f96e22010-07-30 19:18:16 -0700153 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700154 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700155 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700156
Romain Guyac670c02010-07-27 17:39:27 -0700157 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
158
Romain Guyae5575b2010-07-29 18:48:04 -0700159 mFirstSnapshot = new Snapshot;
160
161 GLint maxTextureUnits;
162 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
163 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700164 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
165 }
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy85bf02f2010-06-22 13:11:24 -0700168OpenGLRenderer::~OpenGLRenderer() {
169 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700170
171 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700172 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700173 mGradientCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700174 mPathCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700175 mPatchCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700176 mProgramCache.clear();
Romain Guy1e45aae2010-08-13 19:39:53 -0700177 mDropShadowCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700178}
179
Romain Guyf6a11b82010-06-23 17:47:49 -0700180///////////////////////////////////////////////////////////////////////////////
181// Setup
182///////////////////////////////////////////////////////////////////////////////
183
Romain Guy85bf02f2010-06-22 13:11:24 -0700184void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700185 glViewport(0, 0, width, height);
186
Romain Guy260e1022010-07-12 14:41:06 -0700187 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700188
189 mWidth = width;
190 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700191 mFirstSnapshot->height = 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 Guy7ae7ac42010-06-25 13:46:18 -0700196 mSaveCount = 0;
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 Guy7ae7ac42010-06-25 13:46:18 -0700222 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guy7ae7ac42010-06-25 13:46:18 -0700224 if (restoreSnapshot()) {
225 setScissorFromClip();
226 }
Romain Guybb9524b2010-06-22 18:56:38 -0700227}
228
229void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700230 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700231
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700233
Romain Guy7ae7ac42010-06-25 13:46:18 -0700234 while (mSaveCount != saveCount - 1) {
235 restoreClip |= restoreSnapshot();
236 }
Romain Guybb9524b2010-06-22 18:56:38 -0700237
Romain Guy7ae7ac42010-06-25 13:46:18 -0700238 if (restoreClip) {
239 setScissorFromClip();
240 }
Romain Guybb9524b2010-06-22 18:56:38 -0700241}
242
243int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700244 mSnapshot = new Snapshot(mSnapshot);
245 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700246}
247
248bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700249 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700250 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700251 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700252
Romain Guybd6b79b2010-06-26 00:13:53 -0700253 sp<Snapshot> current = mSnapshot;
254 sp<Snapshot> previous = mSnapshot->previous;
255
Romain Guyf86ef572010-07-01 11:05:42 -0700256 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700257 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700258 }
259
Romain Guybd6b79b2010-06-26 00:13:53 -0700260 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700261 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700262 }
263
264 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700265 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700266
Romain Guy7ae7ac42010-06-25 13:46:18 -0700267 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700268}
269
Romain Guyd55a8612010-06-28 17:42:46 -0700270void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700271 if (!current->layer) {
272 LOGE("Attempting to compose a layer that does not exist");
273 return;
274 }
275
Romain Guyd55a8612010-06-28 17:42:46 -0700276 // Unbind current FBO and restore previous one
277 // Most of the time, previous->fbo will be 0 to bind the default buffer
278 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
279
280 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700281 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700282 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
283
Romain Guydda57022010-07-06 11:39:32 -0700284 Layer* layer = current->layer;
Romain Guydda57022010-07-06 11:39:32 -0700285 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700286
Romain Guydda57022010-07-06 11:39:32 -0700287 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700288 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700289
Romain Guydda57022010-07-06 11:39:32 -0700290 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700291 // Failing to add the layer to the cache should happen only if the
292 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700293 if (!mLayerCache.put(size, layer)) {
294 LAYER_LOGD("Deleting layer");
295
296 glDeleteFramebuffers(1, &layer->fbo);
297 glDeleteTextures(1, &layer->texture);
298
299 delete layer;
300 }
Romain Guyd55a8612010-06-28 17:42:46 -0700301}
302
Romain Guyf6a11b82010-06-23 17:47:49 -0700303///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700304// Layers
305///////////////////////////////////////////////////////////////////////////////
306
307int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
308 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700309 int count = saveSnapshot();
310
311 int alpha = 255;
312 SkXfermode::Mode mode;
313
314 if (p) {
315 alpha = p->getAlpha();
316 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
317 if (!isMode) {
318 // Assume SRC_OVER
319 mode = SkXfermode::kSrcOver_Mode;
320 }
321 } else {
322 mode = SkXfermode::kSrcOver_Mode;
323 }
324
325 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
326
327 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700328}
329
330int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
331 int alpha, int flags) {
332 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700333 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
334 return count;
335}
Romain Guybd6b79b2010-06-26 00:13:53 -0700336
Romain Guyd55a8612010-06-28 17:42:46 -0700337bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
338 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700339 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700340 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700341
Romain Guyf18fd992010-07-08 11:45:51 -0700342 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
343 LayerSize size(right - left, bottom - top);
344
Romain Guyf18fd992010-07-08 11:45:51 -0700345 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700346 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700347 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700348 }
349
Romain Guyf18fd992010-07-08 11:45:51 -0700350 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
351
Romain Guyf86ef572010-07-01 11:05:42 -0700352 // Clear the FBO
353 glDisable(GL_SCISSOR_TEST);
354 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
355 glClear(GL_COLOR_BUFFER_BIT);
356 glEnable(GL_SCISSOR_TEST);
357
Romain Guydda57022010-07-06 11:39:32 -0700358 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700359 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700360 layer->mode = mode;
361 layer->alpha = alpha / 255.0f;
362 layer->layer.set(left, top, right, bottom);
363
364 snapshot->layer = layer;
365 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700366
Romain Guyf86ef572010-07-01 11:05:42 -0700367 // Creates a new snapshot to draw into the FBO
368 saveSnapshot();
369 // TODO: This doesn't preserve other transformations (check Skia first)
370 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700371 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700372 mSnapshot->height = bottom - top;
373 setScissorFromClip();
374
Romain Guy079ba2c2010-07-16 14:12:24 -0700375 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700376 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700377
378 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700379 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700380
Romain Guyd55a8612010-06-28 17:42:46 -0700381 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700382}
383
384///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700385// Transforms
386///////////////////////////////////////////////////////////////////////////////
387
388void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700390}
391
392void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700393 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700394}
395
396void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700397 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700398}
399
400void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700401 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700402}
403
404void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700405 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700406}
407
408void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700409 mat4 m(*matrix);
410 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700411}
412
413///////////////////////////////////////////////////////////////////////////////
414// Clipping
415///////////////////////////////////////////////////////////////////////////////
416
Romain Guybb9524b2010-06-22 18:56:38 -0700417void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700418 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700419 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700420}
421
422const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700423 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700424}
425
Romain Guyc7d53492010-06-25 13:41:57 -0700426bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy959c91f2010-08-11 19:35:53 -0700427 SkRect sr;
428 sr.set(left, top, right, bottom);
429
430 SkMatrix m;
431 mSnapshot->transform.copyTo(m);
432 m.mapRect(&sr);
433
434 Rect r(sr.fLeft, sr.fTop, sr.fRight, sr.fBottom);
Romain Guyc7d53492010-06-25 13:41:57 -0700435 return !mSnapshot->clipRect.intersects(r);
436}
437
Romain Guy079ba2c2010-07-16 14:12:24 -0700438bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
439 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700440 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700441 setScissorFromClip();
442 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700443 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700444}
445
Romain Guyf6a11b82010-06-23 17:47:49 -0700446///////////////////////////////////////////////////////////////////////////////
447// Drawing
448///////////////////////////////////////////////////////////////////////////////
449
Romain Guyc1396e92010-06-30 17:56:19 -0700450void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700451 const float right = left + bitmap->width();
452 const float bottom = top + bitmap->height();
453
454 if (quickReject(left, top, right, bottom)) {
455 return;
456 }
457
Romain Guy61c8c9c2010-08-09 20:48:09 -0700458 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700459 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700460 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700461 const AutoTexture autoCleanup(texture);
462
Romain Guy6926c722010-07-12 20:20:03 -0700463 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700464}
465
Romain Guyf86ef572010-07-01 11:05:42 -0700466void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
467 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
468 const mat4 transform(*matrix);
469 transform.mapRect(r);
470
Romain Guy6926c722010-07-12 20:20:03 -0700471 if (quickReject(r.left, r.top, r.right, r.bottom)) {
472 return;
473 }
474
Romain Guy61c8c9c2010-08-09 20:48:09 -0700475 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700476 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700477 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700478 const AutoTexture autoCleanup(texture);
479
Romain Guy82ba8142010-07-09 13:25:56 -0700480 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700481}
482
Romain Guy8ba548f2010-06-30 19:21:21 -0700483void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
484 float srcLeft, float srcTop, float srcRight, float srcBottom,
485 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700486 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700487 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
488 return;
489 }
490
Romain Guy61c8c9c2010-08-09 20:48:09 -0700491 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700492 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700493 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700494 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700495
Romain Guy8ba548f2010-06-30 19:21:21 -0700496 const float width = texture->width;
497 const float height = texture->height;
498
499 const float u1 = srcLeft / width;
500 const float v1 = srcTop / height;
501 const float u2 = srcRight / width;
502 const float v2 = srcBottom / height;
503
504 resetDrawTextureTexCoords(u1, v1, u2, v2);
505
Romain Guy82ba8142010-07-09 13:25:56 -0700506 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700507
508 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
509}
510
Romain Guydeba7852010-07-07 17:54:48 -0700511void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
512 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700513 if (quickReject(left, top, right, bottom)) {
514 return;
515 }
516
Romain Guy61c8c9c2010-08-09 20:48:09 -0700517 glActiveTexture(GL_TEXTURE0);
Romain Guyf7f93552010-07-08 19:17:03 -0700518 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700519 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700520 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700521
522 int alpha;
523 SkXfermode::Mode mode;
524 getAlphaAndMode(paint, &alpha, &mode);
525
Romain Guyf7f93552010-07-08 19:17:03 -0700526 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700527 mesh->updateVertices(bitmap, left, top, right, bottom,
528 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700529
530 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
531 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700532 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
533 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700534 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700535}
536
Romain Guy85bf02f2010-06-22 13:11:24 -0700537void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700538 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700539 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700540}
Romain Guy9d5316e2010-06-24 19:30:36 -0700541
Romain Guybd6b79b2010-06-26 00:13:53 -0700542void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700543 if (quickReject(left, top, right, bottom)) {
544 return;
545 }
546
Romain Guy026c5e162010-06-28 17:12:22 -0700547 SkXfermode::Mode mode;
548
549 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
550 if (!isMode) {
551 // Assume SRC_OVER
552 mode = SkXfermode::kSrcOver_Mode;
553 }
554
555 // Skia draws using the color's alpha channel if < 255
556 // Otherwise, it uses the paint's alpha
557 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700558 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700559 color |= p->getAlpha() << 24;
560 }
561
562 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700563}
Romain Guy9d5316e2010-06-24 19:30:36 -0700564
Romain Guy1e45aae2010-08-13 19:39:53 -0700565void OpenGLRenderer::renderShadow(const ShadowTexture* texture, float x, float y,
566 SkXfermode::Mode mode) {
567 const float sx = x - texture->left + mShadowDx;
568 const float sy = y - texture->top + mShadowDy;
569
570 const GLfloat a = ((mShadowColor >> 24) & 0xFF) / 255.0f;
571 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
572 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
573 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
574
575 GLuint textureUnit = 0;
576 renderTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, false);
577}
578
579void OpenGLRenderer::renderTextureAlpha8(const Texture* texture, GLuint& textureUnit,
580 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
581 bool applyFilters) {
582 // Describe the required shaders
583 ProgramDescription description;
584 description.hasTexture = true;
585 description.hasAlpha8Texture = true;
586
587 if (applyFilters) {
588 if (mShader) {
589 mShader->describe(description, mExtensions);
590 }
591 if (mColorFilter) {
592 mColorFilter->describe(description, mExtensions);
593 }
594 }
595
596 // Build and use the appropriate shader
597 useProgram(mProgramCache.get(description));
598
599 // Setup the blending mode
600 chooseBlending(true, mode);
601 bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
602 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
603
604 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
605 glEnableVertexAttribArray(texCoordsSlot);
606
607 // Setup attributes
608 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
609 gMeshStride, &mMeshVertices[0].position[0]);
610 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
611 gMeshStride, &mMeshVertices[0].texture[0]);
612
613 // Setup uniforms
614 mModelView.loadTranslate(x, y, 0.0f);
615 mModelView.scale(texture->width, texture->height, 1.0f);
616 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
617
618 glUniform4f(mCurrentProgram->color, r, g, b, a);
619
620 textureUnit++;
621 if (applyFilters) {
622 // Setup attributes and uniforms required by the shaders
623 if (mShader) {
624 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
625 }
626 if (mColorFilter) {
627 mColorFilter->setupProgram(mCurrentProgram);
628 }
629 }
630
631 // Draw the mesh
632 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
633
634 glDisableVertexAttribArray(texCoordsSlot);
635}
636
Romain Guya674ab72010-08-10 17:26:42 -0700637#define kStdStrikeThru_Offset (-6.0f / 21.0f)
638#define kStdUnderline_Offset (1.0f / 9.0f)
639#define kStdUnderline_Thickness (1.0f / 18.0f)
640
Romain Guye8e62a42010-07-23 18:55:21 -0700641void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
642 float x, float y, SkPaint* paint) {
643 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
644 return;
645 }
646
Romain Guya674ab72010-08-10 17:26:42 -0700647 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700648 switch (paint->getTextAlign()) {
649 case SkPaint::kCenter_Align:
650 length = paint->measureText(text, bytesCount);
651 x -= length / 2.0f;
652 break;
653 case SkPaint::kRight_Align:
654 length = paint->measureText(text, bytesCount);
655 x -= length;
656 break;
657 default:
658 break;
659 }
660
Romain Guy694b5192010-07-21 21:33:20 -0700661 int alpha;
662 SkXfermode::Mode mode;
663 getAlphaAndMode(paint, &alpha, &mode);
664
Romain Guy1e45aae2010-08-13 19:39:53 -0700665 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
666 if (mHasShadow) {
667 glActiveTexture(gTextureUnits[0]);
668 const ShadowTexture* shadow = mDropShadowCache.get(paint, text, bytesCount,
669 count, mShadowRadius);
670 const AutoTexture autoCleanup(shadow);
671 renderShadow(shadow, x, y, mode);
672 }
673
Romain Guy694b5192010-07-21 21:33:20 -0700674 uint32_t color = paint->getColor();
675 const GLfloat a = alpha / 255.0f;
676 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
677 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
678 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
679
680 mModelView.loadIdentity();
681
Romain Guy06f96e22010-07-30 19:18:16 -0700682 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700683 // Needs to be set prior to calling FontRenderer::getTexture()
684 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700685
Romain Guy889f8d12010-07-29 14:37:42 -0700686 ProgramDescription description;
687 description.hasTexture = true;
688 description.hasAlpha8Texture = true;
Romain Guy06f96e22010-07-30 19:18:16 -0700689 if (mShader) {
690 mShader->describe(description, mExtensions);
691 }
Romain Guydb1938e2010-08-02 18:50:22 -0700692 if (mColorFilter) {
693 mColorFilter->describe(description, mExtensions);
694 }
Romain Guy889f8d12010-07-29 14:37:42 -0700695
696 useProgram(mProgramCache.get(description));
697 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700698
Romain Guydb1938e2010-08-02 18:50:22 -0700699 // Text is always blended, no need to check the shader
Romain Guy694b5192010-07-21 21:33:20 -0700700 chooseBlending(true, mode);
Romain Guy06f96e22010-07-30 19:18:16 -0700701 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
702 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
Romain Guy889f8d12010-07-29 14:37:42 -0700703
704 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
705 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700706
707 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700708 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700709
Romain Guy06f96e22010-07-30 19:18:16 -0700710 textureUnit++;
711 // Setup attributes and uniforms required by the shaders
712 if (mShader) {
713 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
714 }
Romain Guydb1938e2010-08-02 18:50:22 -0700715 if (mColorFilter) {
716 mColorFilter->setupProgram(mCurrentProgram);
717 }
Romain Guy06f96e22010-07-30 19:18:16 -0700718
Romain Guy09147fb2010-07-22 13:08:20 -0700719 const Rect& clip = mSnapshot->getLocalClip();
Romain Guye8e62a42010-07-23 18:55:21 -0700720 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700721
722 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700723 glDisableVertexAttribArray(texCoordsSlot);
Romain Guya674ab72010-08-10 17:26:42 -0700724
725 // Handle underline and strike-through
726 uint32_t flags = paint->getFlags();
727 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
728 float underlineWidth = length;
729 // If length is > 0.0f, we already measured the text for the text alignment
730 if (length <= 0.0f) {
731 underlineWidth = paint->measureText(text, bytesCount);
732 }
733
734 float offsetX = 0;
735 switch (paint->getTextAlign()) {
736 case SkPaint::kCenter_Align:
737 offsetX = underlineWidth * 0.5f;
738 break;
739 case SkPaint::kRight_Align:
740 offsetX = underlineWidth;
741 break;
742 default:
743 break;
744 }
745
746 if (underlineWidth > 0.0f) {
747 float textSize = paint->getTextSize();
748 float height = textSize * kStdUnderline_Thickness;
749
750 float left = x - offsetX;
751 float top = 0.0f;
752 float right = left + underlineWidth;
753 float bottom = 0.0f;
754
755 if (flags & SkPaint::kUnderlineText_Flag) {
756 top = y + textSize * kStdUnderline_Offset;
757 bottom = top + height;
758 drawRect(left, top, right, bottom, paint);
759 }
760
761 if (flags & SkPaint::kStrikeThruText_Flag) {
762 top = y + textSize * kStdStrikeThru_Offset;
763 bottom = top + height;
764 drawRect(left, top, right, bottom, paint);
765 }
766 }
767 }
Romain Guy694b5192010-07-21 21:33:20 -0700768}
769
Romain Guy7fbcc042010-08-04 15:40:07 -0700770void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
771 GLuint textureUnit = 0;
772 glActiveTexture(gTextureUnits[textureUnit]);
773
Romain Guy22158e12010-08-06 11:18:34 -0700774 const PathTexture* texture = mPathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700775 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700776 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700777
778 int alpha;
779 SkXfermode::Mode mode;
780 getAlphaAndMode(paint, &alpha, &mode);
781
782 uint32_t color = paint->getColor();
783 const GLfloat a = alpha / 255.0f;
784 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
785 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
786 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
787
Romain Guy1e45aae2010-08-13 19:39:53 -0700788 const float x = texture->left - texture->offset;
789 const float y = texture->top - texture->offset;
790 renderTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true);
Romain Guy7fbcc042010-08-04 15:40:07 -0700791}
792
Romain Guy6926c722010-07-12 20:20:03 -0700793///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700794// Shaders
795///////////////////////////////////////////////////////////////////////////////
796
797void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700798 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700799}
800
Romain Guy06f96e22010-07-30 19:18:16 -0700801void OpenGLRenderer::setupShader(SkiaShader* shader) {
802 mShader = shader;
803 if (mShader) {
804 mShader->set(&mTextureCache, &mGradientCache);
805 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700806}
807
Romain Guyd27977d2010-07-14 19:18:51 -0700808///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700809// Color filters
810///////////////////////////////////////////////////////////////////////////////
811
812void OpenGLRenderer::resetColorFilter() {
813 mColorFilter = NULL;
814}
815
816void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
817 mColorFilter = filter;
818}
819
820///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700821// Drop shadow
822///////////////////////////////////////////////////////////////////////////////
823
824void OpenGLRenderer::resetShadow() {
825 mHasShadow = false;
826}
827
828void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
829 mHasShadow = true;
830 mShadowRadius = radius;
831 mShadowDx = dx;
832 mShadowDy = dy;
833 mShadowColor = color;
834}
835
836///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700837// Drawing implementation
838///////////////////////////////////////////////////////////////////////////////
839
Romain Guy026c5e162010-06-28 17:12:22 -0700840void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700841 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700842 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700843 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700844 color |= 0x00ffffff;
845 }
846
847 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700848 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700849 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700850 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
851 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
852 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
853
Romain Guy06f96e22010-07-30 19:18:16 -0700854 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700855
Romain Guy06f96e22010-07-30 19:18:16 -0700856 // Setup the blending mode
857 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700858
Romain Guy06f96e22010-07-30 19:18:16 -0700859 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700860 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700861 if (mShader) {
862 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700863 }
Romain Guydb1938e2010-08-02 18:50:22 -0700864 if (mColorFilter) {
865 mColorFilter->describe(description, mExtensions);
866 }
Romain Guyd27977d2010-07-14 19:18:51 -0700867
Romain Guy06f96e22010-07-30 19:18:16 -0700868 // Build and use the appropriate shader
869 useProgram(mProgramCache.get(description));
870
871 // Setup attributes
872 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
873 gMeshStride, &mMeshVertices[0].position[0]);
874
875 // Setup uniforms
876 mModelView.loadTranslate(left, top, 0.0f);
877 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700878 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700879 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700880 } else {
881 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700882 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700883 }
Romain Guy889f8d12010-07-29 14:37:42 -0700884 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700885
Romain Guy06f96e22010-07-30 19:18:16 -0700886 // Setup attributes and uniforms required by the shaders
887 if (mShader) {
888 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700889 }
Romain Guydb1938e2010-08-02 18:50:22 -0700890 if (mColorFilter) {
891 mColorFilter->setupProgram(mCurrentProgram);
892 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700893
Romain Guy06f96e22010-07-30 19:18:16 -0700894 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700895 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700896}
897
Romain Guy82ba8142010-07-09 13:25:56 -0700898void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700899 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700900 int alpha;
901 SkXfermode::Mode mode;
902 getAlphaAndMode(paint, &alpha, &mode);
903
Romain Guya9794742010-07-13 11:37:54 -0700904 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700905 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700906}
907
Romain Guybd6b79b2010-06-26 00:13:53 -0700908void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700909 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
910 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700911 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700912}
913
914void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700915 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700916 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700917 ProgramDescription description;
918 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700919 if (mColorFilter) {
920 mColorFilter->describe(description, mExtensions);
921 }
Romain Guy889f8d12010-07-29 14:37:42 -0700922
Romain Guybd6b79b2010-06-26 00:13:53 -0700923 mModelView.loadTranslate(left, top, 0.0f);
924 mModelView.scale(right - left, bottom - top, 1.0f);
925
Romain Guy889f8d12010-07-29 14:37:42 -0700926 useProgram(mProgramCache.get(description));
927 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700928
Romain Guya9794742010-07-13 11:37:54 -0700929 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700930
931 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700932 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700933 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700934
Romain Guya9794742010-07-13 11:37:54 -0700935 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700936 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700937
Romain Guy889f8d12010-07-29 14:37:42 -0700938 // Mesh
939 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
940 glEnableVertexAttribArray(texCoordsSlot);
941 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700942 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700943 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700944
Romain Guydb1938e2010-08-02 18:50:22 -0700945 // Color filter
946 if (mColorFilter) {
947 mColorFilter->setupProgram(mCurrentProgram);
948 }
949
Romain Guyf7f93552010-07-08 19:17:03 -0700950 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700951 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700952 } else {
953 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
954 }
Romain Guy889f8d12010-07-29 14:37:42 -0700955 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700956}
957
958void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700959 blend = blend || mode != SkXfermode::kSrcOver_Mode;
960 if (blend) {
961 if (!mBlend) {
962 glEnable(GL_BLEND);
963 }
964
965 GLenum sourceMode = gBlends[mode].src;
966 GLenum destMode = gBlends[mode].dst;
967 if (!isPremultiplied && sourceMode == GL_ONE) {
968 sourceMode = GL_SRC_ALPHA;
969 }
970
971 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
972 glBlendFunc(sourceMode, destMode);
973 mLastSrcMode = sourceMode;
974 mLastDstMode = destMode;
975 }
976 } else if (mBlend) {
977 glDisable(GL_BLEND);
978 }
979 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700980}
981
Romain Guy889f8d12010-07-29 14:37:42 -0700982bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700983 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700984 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700985 program->use();
986 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700987 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700988 }
Romain Guy6926c722010-07-12 20:20:03 -0700989 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700990}
991
Romain Guy026c5e162010-06-28 17:12:22 -0700992void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700993 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700994 TextureVertex::setUV(v++, u1, v1);
995 TextureVertex::setUV(v++, u2, v1);
996 TextureVertex::setUV(v++, u1, v2);
997 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700998}
999
1000void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1001 if (paint) {
1002 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1003 if (!isMode) {
1004 // Assume SRC_OVER
1005 *mode = SkXfermode::kSrcOver_Mode;
1006 }
1007
1008 // Skia draws using the color's alpha channel if < 255
1009 // Otherwise, it uses the paint's alpha
1010 int color = paint->getColor();
1011 *alpha = (color >> 24) & 0xFF;
1012 if (*alpha == 255) {
1013 *alpha = paint->getAlpha();
1014 }
1015 } else {
1016 *mode = SkXfermode::kSrcOver_Mode;
1017 *alpha = 255;
1018 }
Romain Guy026c5e162010-06-28 17:12:22 -07001019}
1020
Romain Guy889f8d12010-07-29 14:37:42 -07001021void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1022 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001023 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001024 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1025 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1026}
1027
Romain Guy9d5316e2010-06-24 19:30:36 -07001028}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001029}; // namespace android