blob: 3c1fe2a8090a48d43f333b7f7b2fd760155092f0 [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 Guydda57022010-07-06 11:39:32 -070044
Romain Guy06f96e22010-07-30 19:18:16 -070045#define REQUIRED_TEXTURE_UNITS_COUNT 3
46
Romain Guy121e2242010-07-01 18:26:52 -070047// Converts a number of mega-bytes into bytes
48#define MB(s) s * 1024 * 1024
49
Romain Guydda57022010-07-06 11:39:32 -070050// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070051#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070052
53///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy026c5e162010-06-28 17:12:22 -070057// This array is never used directly but used as a memcpy source in the
58// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070059static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070060 FV(0.0f, 0.0f, 0.0f, 0.0f),
61 FV(1.0f, 0.0f, 1.0f, 0.0f),
62 FV(0.0f, 1.0f, 0.0f, 1.0f),
63 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070064};
Romain Guyac670c02010-07-27 17:39:27 -070065static const GLsizei gMeshStride = sizeof(TextureVertex);
66static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070067
Romain Guy889f8d12010-07-29 14:37:42 -070068/**
69 * Structure mapping Skia xfermodes to OpenGL blending factors.
70 */
71struct Blender {
72 SkXfermode::Mode mode;
73 GLenum src;
74 GLenum dst;
75}; // struct Blender
76
Romain Guy026c5e162010-06-28 17:12:22 -070077// In this array, the index of each Blender equals the value of the first
78// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
79static const Blender gBlends[] = {
80 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
81 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
82 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
83 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
84 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
85 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
86 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
87 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
91 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
92};
Romain Guye4d01122010-06-16 18:44:05 -070093
Romain Guy889f8d12010-07-29 14:37:42 -070094static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070095 GL_TEXTURE0,
96 GL_TEXTURE1,
97 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070098};
99
Romain Guyf6a11b82010-06-23 17:47:49 -0700100///////////////////////////////////////////////////////////////////////////////
101// Constructors/destructor
102///////////////////////////////////////////////////////////////////////////////
103
Romain Guydda57022010-07-06 11:39:32 -0700104OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700105 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700106 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700107 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
Romain Guyc0ac1932010-07-19 18:43:02 -0700108 mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)),
Romain Guy7fbcc042010-08-04 15:40:07 -0700109 mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700110 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700111 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700112
Romain Guy121e2242010-07-01 18:26:52 -0700113 char property[PROPERTY_VALUE_MAX];
114 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700115 LOGD(" Setting texture cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 mTextureCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700117 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE);
Romain Guydda57022010-07-06 11:39:32 -0700119 }
120
121 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
122 LOGD(" Setting layer cache size to %sMB", property);
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 mLayerCache.setMaxSize(MB(atof(property)));
Romain Guydda57022010-07-06 11:39:32 -0700124 } else {
Romain Guyc0ac1932010-07-19 18:43:02 -0700125 LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE);
126 }
127
128 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
129 LOGD(" Setting gradient cache size to %sMB", property);
Romain Guy7fbcc042010-08-04 15:40:07 -0700130 mGradientCache.setMaxSize(MB(atof(property)));
Romain Guyc0ac1932010-07-19 18:43:02 -0700131 } else {
132 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700133 }
134
Romain Guy7fbcc042010-08-04 15:40:07 -0700135 if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) {
136 LOGD(" Setting path cache size to %sMB", property);
137 mPathCache.setMaxSize(MB(atof(property)));
138 } else {
139 LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE);
140 }
141
Romain Guy889f8d12010-07-29 14:37:42 -0700142 mCurrentProgram = NULL;
Romain Guy06f96e22010-07-30 19:18:16 -0700143 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700144 mColorFilter = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700145
Romain Guyac670c02010-07-27 17:39:27 -0700146 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
147
Romain Guyae5575b2010-07-29 18:48:04 -0700148 mFirstSnapshot = new Snapshot;
149
150 GLint maxTextureUnits;
151 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
152 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700153 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
154 }
Romain Guye4d01122010-06-16 18:44:05 -0700155}
156
Romain Guy85bf02f2010-06-22 13:11:24 -0700157OpenGLRenderer::~OpenGLRenderer() {
158 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700159
160 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700161 mLayerCache.clear();
Romain Guyc0ac1932010-07-19 18:43:02 -0700162 mGradientCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700163 mPathCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700164 mPatchCache.clear();
Romain Guy959c91f2010-08-11 19:35:53 -0700165 mProgramCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guyf6a11b82010-06-23 17:47:49 -0700168///////////////////////////////////////////////////////////////////////////////
169// Setup
170///////////////////////////////////////////////////////////////////////////////
171
Romain Guy85bf02f2010-06-22 13:11:24 -0700172void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700173 glViewport(0, 0, width, height);
174
Romain Guy260e1022010-07-12 14:41:06 -0700175 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700176
177 mWidth = width;
178 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700179 mFirstSnapshot->height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700180}
181
Romain Guy85bf02f2010-06-22 13:11:24 -0700182void OpenGLRenderer::prepare() {
Romain Guyb82da652010-07-30 11:36:12 -0700183 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700184 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700185
Romain Guy08ae3172010-06-21 19:35:50 -0700186 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700187
Romain Guy08ae3172010-06-21 19:35:50 -0700188 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
189 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700190
Romain Guy08ae3172010-06-21 19:35:50 -0700191 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700192 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700193
Romain Guyb82da652010-07-30 11:36:12 -0700194 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
Romain Guyf6a11b82010-06-23 17:47:49 -0700197///////////////////////////////////////////////////////////////////////////////
198// State management
199///////////////////////////////////////////////////////////////////////////////
200
Romain Guybb9524b2010-06-22 18:56:38 -0700201int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700203}
204
205int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
209void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700211
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 if (restoreSnapshot()) {
213 setScissorFromClip();
214 }
Romain Guybb9524b2010-06-22 18:56:38 -0700215}
216
217void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700219
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700221
Romain Guy7ae7ac42010-06-25 13:46:18 -0700222 while (mSaveCount != saveCount - 1) {
223 restoreClip |= restoreSnapshot();
224 }
Romain Guybb9524b2010-06-22 18:56:38 -0700225
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 if (restoreClip) {
227 setScissorFromClip();
228 }
Romain Guybb9524b2010-06-22 18:56:38 -0700229}
230
231int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700232 mSnapshot = new Snapshot(mSnapshot);
233 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700234}
235
236bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700237 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700238 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700239 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700240
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 sp<Snapshot> current = mSnapshot;
242 sp<Snapshot> previous = mSnapshot->previous;
243
Romain Guyf86ef572010-07-01 11:05:42 -0700244 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700245 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700246 }
247
Romain Guybd6b79b2010-06-26 00:13:53 -0700248 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700249 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700250 }
251
252 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700253 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700254
Romain Guy7ae7ac42010-06-25 13:46:18 -0700255 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700256}
257
Romain Guyd55a8612010-06-28 17:42:46 -0700258void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700259 if (!current->layer) {
260 LOGE("Attempting to compose a layer that does not exist");
261 return;
262 }
263
Romain Guyd55a8612010-06-28 17:42:46 -0700264 // Unbind current FBO and restore previous one
265 // Most of the time, previous->fbo will be 0 to bind the default buffer
266 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
267
268 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700269 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700270 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
271
Romain Guydda57022010-07-06 11:39:32 -0700272 Layer* layer = current->layer;
Romain Guydda57022010-07-06 11:39:32 -0700273 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700274
Romain Guydda57022010-07-06 11:39:32 -0700275 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700276 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700277
Romain Guydda57022010-07-06 11:39:32 -0700278 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700279 // Failing to add the layer to the cache should happen only if the
280 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700281 if (!mLayerCache.put(size, layer)) {
282 LAYER_LOGD("Deleting layer");
283
284 glDeleteFramebuffers(1, &layer->fbo);
285 glDeleteTextures(1, &layer->texture);
286
287 delete layer;
288 }
Romain Guyd55a8612010-06-28 17:42:46 -0700289}
290
Romain Guyf6a11b82010-06-23 17:47:49 -0700291///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700292// Layers
293///////////////////////////////////////////////////////////////////////////////
294
295int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
296 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700297 int count = saveSnapshot();
298
299 int alpha = 255;
300 SkXfermode::Mode mode;
301
302 if (p) {
303 alpha = p->getAlpha();
304 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
305 if (!isMode) {
306 // Assume SRC_OVER
307 mode = SkXfermode::kSrcOver_Mode;
308 }
309 } else {
310 mode = SkXfermode::kSrcOver_Mode;
311 }
312
313 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
314
315 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700316}
317
318int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
319 int alpha, int flags) {
320 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700321 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
322 return count;
323}
Romain Guybd6b79b2010-06-26 00:13:53 -0700324
Romain Guyd55a8612010-06-28 17:42:46 -0700325bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
326 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700327 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700328 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700329
Romain Guyf18fd992010-07-08 11:45:51 -0700330 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
331 LayerSize size(right - left, bottom - top);
332
Romain Guyf18fd992010-07-08 11:45:51 -0700333 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700334 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700335 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700336 }
337
Romain Guyf18fd992010-07-08 11:45:51 -0700338 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
339
Romain Guyf86ef572010-07-01 11:05:42 -0700340 // Clear the FBO
341 glDisable(GL_SCISSOR_TEST);
342 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
343 glClear(GL_COLOR_BUFFER_BIT);
344 glEnable(GL_SCISSOR_TEST);
345
Romain Guydda57022010-07-06 11:39:32 -0700346 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700347 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700348 layer->mode = mode;
349 layer->alpha = alpha / 255.0f;
350 layer->layer.set(left, top, right, bottom);
351
352 snapshot->layer = layer;
353 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700354
Romain Guyf86ef572010-07-01 11:05:42 -0700355 // Creates a new snapshot to draw into the FBO
356 saveSnapshot();
357 // TODO: This doesn't preserve other transformations (check Skia first)
358 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700359 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700360 mSnapshot->height = bottom - top;
361 setScissorFromClip();
362
Romain Guy079ba2c2010-07-16 14:12:24 -0700363 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700364 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700365
366 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700367 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700368
Romain Guyd55a8612010-06-28 17:42:46 -0700369 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700370}
371
372///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700373// Transforms
374///////////////////////////////////////////////////////////////////////////////
375
376void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700377 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700386}
387
388void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700390}
391
392void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700393 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700394}
395
396void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700397 mat4 m(*matrix);
398 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700399}
400
401///////////////////////////////////////////////////////////////////////////////
402// Clipping
403///////////////////////////////////////////////////////////////////////////////
404
Romain Guybb9524b2010-06-22 18:56:38 -0700405void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700406 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700407 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700408}
409
410const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700411 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700412}
413
Romain Guyc7d53492010-06-25 13:41:57 -0700414bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy959c91f2010-08-11 19:35:53 -0700415 SkRect sr;
416 sr.set(left, top, right, bottom);
417
418 SkMatrix m;
419 mSnapshot->transform.copyTo(m);
420 m.mapRect(&sr);
421
422 Rect r(sr.fLeft, sr.fTop, sr.fRight, sr.fBottom);
Romain Guyc7d53492010-06-25 13:41:57 -0700423 return !mSnapshot->clipRect.intersects(r);
424}
425
Romain Guy079ba2c2010-07-16 14:12:24 -0700426bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
427 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700428 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700429 setScissorFromClip();
430 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700431 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700432}
433
Romain Guyf6a11b82010-06-23 17:47:49 -0700434///////////////////////////////////////////////////////////////////////////////
435// Drawing
436///////////////////////////////////////////////////////////////////////////////
437
Romain Guyc1396e92010-06-30 17:56:19 -0700438void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700439 const float right = left + bitmap->width();
440 const float bottom = top + bitmap->height();
441
442 if (quickReject(left, top, right, bottom)) {
443 return;
444 }
445
Romain Guy61c8c9c2010-08-09 20:48:09 -0700446 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700447 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700448 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700449 const AutoTexture autoCleanup(texture);
450
Romain Guy6926c722010-07-12 20:20:03 -0700451 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700452}
453
Romain Guyf86ef572010-07-01 11:05:42 -0700454void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
455 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
456 const mat4 transform(*matrix);
457 transform.mapRect(r);
458
Romain Guy6926c722010-07-12 20:20:03 -0700459 if (quickReject(r.left, r.top, r.right, r.bottom)) {
460 return;
461 }
462
Romain Guy61c8c9c2010-08-09 20:48:09 -0700463 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700464 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700465 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700466 const AutoTexture autoCleanup(texture);
467
Romain Guy82ba8142010-07-09 13:25:56 -0700468 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700469}
470
Romain Guy8ba548f2010-06-30 19:21:21 -0700471void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
472 float srcLeft, float srcTop, float srcRight, float srcBottom,
473 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700474 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700475 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
476 return;
477 }
478
Romain Guy61c8c9c2010-08-09 20:48:09 -0700479 glActiveTexture(GL_TEXTURE0);
Romain Guyf86ef572010-07-01 11:05:42 -0700480 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700481 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700482 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700483
Romain Guy8ba548f2010-06-30 19:21:21 -0700484 const float width = texture->width;
485 const float height = texture->height;
486
487 const float u1 = srcLeft / width;
488 const float v1 = srcTop / height;
489 const float u2 = srcRight / width;
490 const float v2 = srcBottom / height;
491
492 resetDrawTextureTexCoords(u1, v1, u2, v2);
493
Romain Guy82ba8142010-07-09 13:25:56 -0700494 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700495
496 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
497}
498
Romain Guydeba7852010-07-07 17:54:48 -0700499void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
500 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700501 if (quickReject(left, top, right, bottom)) {
502 return;
503 }
504
Romain Guy61c8c9c2010-08-09 20:48:09 -0700505 glActiveTexture(GL_TEXTURE0);
Romain Guyf7f93552010-07-08 19:17:03 -0700506 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700507 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700508 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700509
510 int alpha;
511 SkXfermode::Mode mode;
512 getAlphaAndMode(paint, &alpha, &mode);
513
Romain Guyf7f93552010-07-08 19:17:03 -0700514 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700515 mesh->updateVertices(bitmap, left, top, right, bottom,
516 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700517
518 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
519 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700520 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
521 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700522 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700523}
524
Romain Guy85bf02f2010-06-22 13:11:24 -0700525void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700526 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700527 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700528}
Romain Guy9d5316e2010-06-24 19:30:36 -0700529
Romain Guybd6b79b2010-06-26 00:13:53 -0700530void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700531 if (quickReject(left, top, right, bottom)) {
532 return;
533 }
534
Romain Guy026c5e162010-06-28 17:12:22 -0700535 SkXfermode::Mode mode;
536
537 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
538 if (!isMode) {
539 // Assume SRC_OVER
540 mode = SkXfermode::kSrcOver_Mode;
541 }
542
543 // Skia draws using the color's alpha channel if < 255
544 // Otherwise, it uses the paint's alpha
545 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700546 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700547 color |= p->getAlpha() << 24;
548 }
549
550 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700551}
Romain Guy9d5316e2010-06-24 19:30:36 -0700552
Romain Guya674ab72010-08-10 17:26:42 -0700553#define kStdStrikeThru_Offset (-6.0f / 21.0f)
554#define kStdUnderline_Offset (1.0f / 9.0f)
555#define kStdUnderline_Thickness (1.0f / 18.0f)
556
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
581 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
587 mModelView.loadIdentity();
588
Romain Guy06f96e22010-07-30 19:18:16 -0700589 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700590 // Needs to be set prior to calling FontRenderer::getTexture()
591 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700592
Romain Guy889f8d12010-07-29 14:37:42 -0700593 ProgramDescription description;
594 description.hasTexture = true;
595 description.hasAlpha8Texture = true;
Romain Guy06f96e22010-07-30 19:18:16 -0700596 if (mShader) {
597 mShader->describe(description, mExtensions);
598 }
Romain Guydb1938e2010-08-02 18:50:22 -0700599 if (mColorFilter) {
600 mColorFilter->describe(description, mExtensions);
601 }
Romain Guy889f8d12010-07-29 14:37:42 -0700602
603 useProgram(mProgramCache.get(description));
604 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy694b5192010-07-21 21:33:20 -0700605
Romain Guydb1938e2010-08-02 18:50:22 -0700606 // Text is always blended, no need to check the shader
Romain Guy694b5192010-07-21 21:33:20 -0700607 chooseBlending(true, mode);
Romain Guy06f96e22010-07-30 19:18:16 -0700608 bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
609 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
Romain Guy889f8d12010-07-29 14:37:42 -0700610
611 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
612 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy694b5192010-07-21 21:33:20 -0700613
614 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700615 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy694b5192010-07-21 21:33:20 -0700616
Romain Guy06f96e22010-07-30 19:18:16 -0700617 textureUnit++;
618 // Setup attributes and uniforms required by the shaders
619 if (mShader) {
620 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
621 }
Romain Guydb1938e2010-08-02 18:50:22 -0700622 if (mColorFilter) {
623 mColorFilter->setupProgram(mCurrentProgram);
624 }
Romain Guy06f96e22010-07-30 19:18:16 -0700625
Romain Guy09147fb2010-07-22 13:08:20 -0700626 const Rect& clip = mSnapshot->getLocalClip();
Alex Sakhartchouk65ef9092010-07-26 11:09:33 -0700627 mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize());
Romain Guye8e62a42010-07-23 18:55:21 -0700628 mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700629
630 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700631 glDisableVertexAttribArray(texCoordsSlot);
Romain Guya674ab72010-08-10 17:26:42 -0700632
633 // Handle underline and strike-through
634 uint32_t flags = paint->getFlags();
635 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
636 float underlineWidth = length;
637 // If length is > 0.0f, we already measured the text for the text alignment
638 if (length <= 0.0f) {
639 underlineWidth = paint->measureText(text, bytesCount);
640 }
641
642 float offsetX = 0;
643 switch (paint->getTextAlign()) {
644 case SkPaint::kCenter_Align:
645 offsetX = underlineWidth * 0.5f;
646 break;
647 case SkPaint::kRight_Align:
648 offsetX = underlineWidth;
649 break;
650 default:
651 break;
652 }
653
654 if (underlineWidth > 0.0f) {
655 float textSize = paint->getTextSize();
656 float height = textSize * kStdUnderline_Thickness;
657
658 float left = x - offsetX;
659 float top = 0.0f;
660 float right = left + underlineWidth;
661 float bottom = 0.0f;
662
663 if (flags & SkPaint::kUnderlineText_Flag) {
664 top = y + textSize * kStdUnderline_Offset;
665 bottom = top + height;
666 drawRect(left, top, right, bottom, paint);
667 }
668
669 if (flags & SkPaint::kStrikeThruText_Flag) {
670 top = y + textSize * kStdStrikeThru_Offset;
671 bottom = top + height;
672 drawRect(left, top, right, bottom, paint);
673 }
674 }
675 }
Romain Guy694b5192010-07-21 21:33:20 -0700676}
677
Romain Guy7fbcc042010-08-04 15:40:07 -0700678void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
679 GLuint textureUnit = 0;
680 glActiveTexture(gTextureUnits[textureUnit]);
681
Romain Guy22158e12010-08-06 11:18:34 -0700682 const PathTexture* texture = mPathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700683 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700684 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700685
686 int alpha;
687 SkXfermode::Mode mode;
688 getAlphaAndMode(paint, &alpha, &mode);
689
690 uint32_t color = paint->getColor();
691 const GLfloat a = alpha / 255.0f;
692 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
693 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
694 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
695
696 // Describe the required shaders
697 ProgramDescription description;
698 description.hasTexture = true;
699 description.hasAlpha8Texture = true;
700 if (mShader) {
701 mShader->describe(description, mExtensions);
702 }
703 if (mColorFilter) {
704 mColorFilter->describe(description, mExtensions);
705 }
706
707 // Build and use the appropriate shader
708 useProgram(mProgramCache.get(description));
709
710 // Setup the blending mode
711 chooseBlending(true, mode);
712 bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
713 glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit);
714
715 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
716 glEnableVertexAttribArray(texCoordsSlot);
717
718 // Setup attributes
719 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
720 gMeshStride, &mMeshVertices[0].position[0]);
721 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
722 gMeshStride, &mMeshVertices[0].texture[0]);
723
724 // Setup uniforms
725 mModelView.loadTranslate(texture->left - texture->offset,
726 texture->top - texture->offset, 0.0f);
727 mModelView.scale(texture->width, texture->height, 1.0f);
728 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
729
730 glUniform4f(mCurrentProgram->color, r, g, b, a);
731
732 textureUnit++;
733 // Setup attributes and uniforms required by the shaders
734 if (mShader) {
735 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
736 }
737 if (mColorFilter) {
738 mColorFilter->setupProgram(mCurrentProgram);
739 }
740
741 // Draw the mesh
742 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
743
744 glDisableVertexAttribArray(texCoordsSlot);
745}
746
Romain Guy6926c722010-07-12 20:20:03 -0700747///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700748// Shaders
749///////////////////////////////////////////////////////////////////////////////
750
751void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700752 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700753}
754
Romain Guy06f96e22010-07-30 19:18:16 -0700755void OpenGLRenderer::setupShader(SkiaShader* shader) {
756 mShader = shader;
757 if (mShader) {
758 mShader->set(&mTextureCache, &mGradientCache);
759 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700760}
761
Romain Guyd27977d2010-07-14 19:18:51 -0700762///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700763// Color filters
764///////////////////////////////////////////////////////////////////////////////
765
766void OpenGLRenderer::resetColorFilter() {
767 mColorFilter = NULL;
768}
769
770void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
771 mColorFilter = filter;
772}
773
774///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700775// Drawing implementation
776///////////////////////////////////////////////////////////////////////////////
777
Romain Guy026c5e162010-06-28 17:12:22 -0700778void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700779 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700780 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700781 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700782 color |= 0x00ffffff;
783 }
784
785 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700786 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700787 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700788 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
789 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
790 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
791
Romain Guy06f96e22010-07-30 19:18:16 -0700792 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700793
Romain Guy06f96e22010-07-30 19:18:16 -0700794 // Setup the blending mode
795 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700796
Romain Guy06f96e22010-07-30 19:18:16 -0700797 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700798 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700799 if (mShader) {
800 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700801 }
Romain Guydb1938e2010-08-02 18:50:22 -0700802 if (mColorFilter) {
803 mColorFilter->describe(description, mExtensions);
804 }
Romain Guyd27977d2010-07-14 19:18:51 -0700805
Romain Guy06f96e22010-07-30 19:18:16 -0700806 // Build and use the appropriate shader
807 useProgram(mProgramCache.get(description));
808
809 // Setup attributes
810 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
811 gMeshStride, &mMeshVertices[0].position[0]);
812
813 // Setup uniforms
814 mModelView.loadTranslate(left, top, 0.0f);
815 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700816 if (!ignoreTransform) {
Romain Guy889f8d12010-07-29 14:37:42 -0700817 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700818 } else {
819 mat4 identity;
Romain Guy889f8d12010-07-29 14:37:42 -0700820 mCurrentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700821 }
Romain Guy889f8d12010-07-29 14:37:42 -0700822 glUniform4f(mCurrentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700823
Romain Guy06f96e22010-07-30 19:18:16 -0700824 // Setup attributes and uniforms required by the shaders
825 if (mShader) {
826 mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700827 }
Romain Guydb1938e2010-08-02 18:50:22 -0700828 if (mColorFilter) {
829 mColorFilter->setupProgram(mCurrentProgram);
830 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700831
Romain Guy06f96e22010-07-30 19:18:16 -0700832 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700833 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700834}
835
Romain Guy82ba8142010-07-09 13:25:56 -0700836void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700837 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700838 int alpha;
839 SkXfermode::Mode mode;
840 getAlphaAndMode(paint, &alpha, &mode);
841
Romain Guya9794742010-07-13 11:37:54 -0700842 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700843 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700844}
845
Romain Guybd6b79b2010-06-26 00:13:53 -0700846void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700847 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
848 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700849 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700850}
851
852void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700853 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700854 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700855 ProgramDescription description;
856 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700857 if (mColorFilter) {
858 mColorFilter->describe(description, mExtensions);
859 }
Romain Guy889f8d12010-07-29 14:37:42 -0700860
Romain Guybd6b79b2010-06-26 00:13:53 -0700861 mModelView.loadTranslate(left, top, 0.0f);
862 mModelView.scale(right - left, bottom - top, 1.0f);
863
Romain Guy889f8d12010-07-29 14:37:42 -0700864 useProgram(mProgramCache.get(description));
865 mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700866
Romain Guya9794742010-07-13 11:37:54 -0700867 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700868
869 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700870 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guy889f8d12010-07-29 14:37:42 -0700871 glUniform1i(mCurrentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700872
Romain Guya9794742010-07-13 11:37:54 -0700873 // Always premultiplied
Romain Guy889f8d12010-07-29 14:37:42 -0700874 glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700875
Romain Guy889f8d12010-07-29 14:37:42 -0700876 // Mesh
877 int texCoordsSlot = mCurrentProgram->getAttrib("texCoords");
878 glEnableVertexAttribArray(texCoordsSlot);
879 glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700880 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700881 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700882
Romain Guydb1938e2010-08-02 18:50:22 -0700883 // Color filter
884 if (mColorFilter) {
885 mColorFilter->setupProgram(mCurrentProgram);
886 }
887
Romain Guyf7f93552010-07-08 19:17:03 -0700888 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700889 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700890 } else {
891 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
892 }
Romain Guy889f8d12010-07-29 14:37:42 -0700893 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700894}
895
896void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700897 blend = blend || mode != SkXfermode::kSrcOver_Mode;
898 if (blend) {
899 if (!mBlend) {
900 glEnable(GL_BLEND);
901 }
902
903 GLenum sourceMode = gBlends[mode].src;
904 GLenum destMode = gBlends[mode].dst;
905 if (!isPremultiplied && sourceMode == GL_ONE) {
906 sourceMode = GL_SRC_ALPHA;
907 }
908
909 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
910 glBlendFunc(sourceMode, destMode);
911 mLastSrcMode = sourceMode;
912 mLastDstMode = destMode;
913 }
914 } else if (mBlend) {
915 glDisable(GL_BLEND);
916 }
917 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700918}
919
Romain Guy889f8d12010-07-29 14:37:42 -0700920bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700921 if (!program->isInUse()) {
Romain Guy889f8d12010-07-29 14:37:42 -0700922 if (mCurrentProgram != NULL) mCurrentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700923 program->use();
924 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700925 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700926 }
Romain Guy6926c722010-07-12 20:20:03 -0700927 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700928}
929
Romain Guy026c5e162010-06-28 17:12:22 -0700930void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700931 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700932 TextureVertex::setUV(v++, u1, v1);
933 TextureVertex::setUV(v++, u2, v1);
934 TextureVertex::setUV(v++, u1, v2);
935 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700936}
937
938void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
939 if (paint) {
940 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
941 if (!isMode) {
942 // Assume SRC_OVER
943 *mode = SkXfermode::kSrcOver_Mode;
944 }
945
946 // Skia draws using the color's alpha channel if < 255
947 // Otherwise, it uses the paint's alpha
948 int color = paint->getColor();
949 *alpha = (color >> 24) & 0xFF;
950 if (*alpha == 255) {
951 *alpha = paint->getAlpha();
952 }
953 } else {
954 *mode = SkXfermode::kSrcOver_Mode;
955 *alpha = 255;
956 }
Romain Guy026c5e162010-06-28 17:12:22 -0700957}
958
Romain Guy889f8d12010-07-29 14:37:42 -0700959void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
960 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700961 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700962 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
963 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
964}
965
Romain Guy9d5316e2010-06-24 19:30:36 -0700966}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700967}; // namespace android