blob: d950ffac7f4ecb2070e454729e721279d7538a68 [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>
24
Romain Guy121e2242010-07-01 18:26:52 -070025#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy121e2242010-07-01 18:26:52 -070037// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
40
Romain Guydda57022010-07-06 11:39:32 -070041#define DEFAULT_TEXTURE_CACHE_SIZE 20
42#define DEFAULT_LAYER_CACHE_SIZE 10
Romain Guyf7f93552010-07-08 19:17:03 -070043#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guydda57022010-07-06 11:39:32 -070044
Romain Guy121e2242010-07-01 18:26:52 -070045// Converts a number of mega-bytes into bytes
46#define MB(s) s * 1024 * 1024
47
Romain Guydda57022010-07-06 11:39:32 -070048// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070049#define SV(x, y) { { x, y } }
50#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070051
52///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy026c5e162010-06-28 17:12:22 -070056static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070057 SV(0.0f, 0.0f),
58 SV(1.0f, 0.0f),
59 SV(0.0f, 1.0f),
60 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070061};
Romain Guy026c5e162010-06-28 17:12:22 -070062static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
63static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070064
Romain Guy026c5e162010-06-28 17:12:22 -070065// This array is never used directly but used as a memcpy source in the
66// OpenGLRenderer constructor
67static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070068 FV(0.0f, 0.0f, 0.0f, 0.0f),
69 FV(1.0f, 0.0f, 1.0f, 0.0f),
70 FV(0.0f, 1.0f, 0.0f, 1.0f),
71 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070072};
Romain Guy026c5e162010-06-28 17:12:22 -070073static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
74static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070075
Romain Guy026c5e162010-06-28 17:12:22 -070076// In this array, the index of each Blender equals the value of the first
77// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
78static const Blender gBlends[] = {
79 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
80 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
81 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
82 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
84 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
86 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
90 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
91};
Romain Guye4d01122010-06-16 18:44:05 -070092
Romain Guyd27977d2010-07-14 19:18:51 -070093static const GLint gTileModes[] = {
94 GL_CLAMP_TO_EDGE, // == SkShader::kClamp_TileMode
95 GL_REPEAT, // == SkShader::kRepeat_Mode
96 GL_MIRRORED_REPEAT // == SkShader::kMirror_TileMode
97};
98
Romain Guyf6a11b82010-06-23 17:47:49 -070099///////////////////////////////////////////////////////////////////////////////
100// Constructors/destructor
101///////////////////////////////////////////////////////////////////////////////
102
Romain Guydda57022010-07-06 11:39:32 -0700103OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700104 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-07-06 11:39:32 -0700105 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700106 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
107 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700108 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700109
Romain Guy121e2242010-07-01 18:26:52 -0700110 char property[PROPERTY_VALUE_MAX];
111 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700112 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e2242010-07-01 18:26:52 -0700113 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda57022010-07-06 11:39:32 -0700114 } else {
115 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE);
116 }
117
118 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
119 LOGD(" Setting layer cache size to %sMB", property);
120 mLayerCache.setMaxSize(MB(atoi(property)));
121 } else {
122 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700123 }
124
Romain Guyd27977d2010-07-14 19:18:51 -0700125 mDrawColorProgram = new DrawColorProgram;
126 mDrawTextureProgram = new DrawTextureProgram;
127 mCurrentProgram = mDrawTextureProgram;
128
129 mShader = kShaderNone;
130 mShaderTileX = SkShader::kClamp_TileMode;
131 mShaderTileY = SkShader::kClamp_TileMode;
132 mShaderMatrix = NULL;
133 mShaderBitmap = NULL;
Romain Guy026c5e162010-06-28 17:12:22 -0700134
135 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy85bf02f2010-06-22 13:11:24 -0700138OpenGLRenderer::~OpenGLRenderer() {
139 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700140
141 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700142 mLayerCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700143 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700144}
145
Romain Guyf6a11b82010-06-23 17:47:49 -0700146///////////////////////////////////////////////////////////////////////////////
147// Setup
148///////////////////////////////////////////////////////////////////////////////
149
Romain Guy85bf02f2010-06-22 13:11:24 -0700150void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700151 glViewport(0, 0, width, height);
152
Romain Guy260e1022010-07-12 14:41:06 -0700153 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700154
155 mWidth = width;
156 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700157 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700158}
159
Romain Guy85bf02f2010-06-22 13:11:24 -0700160void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 mSnapshot = &mFirstSnapshot;
162 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700163
Romain Guy08ae3172010-06-21 19:35:50 -0700164 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700165
Romain Guy08ae3172010-06-21 19:35:50 -0700166 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
167 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700168
Romain Guy08ae3172010-06-21 19:35:50 -0700169 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700170 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700171
Romain Guybb9524b2010-06-22 18:56:38 -0700172 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
173}
174
Romain Guyf6a11b82010-06-23 17:47:49 -0700175///////////////////////////////////////////////////////////////////////////////
176// State management
177///////////////////////////////////////////////////////////////////////////////
178
Romain Guybb9524b2010-06-22 18:56:38 -0700179int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700180 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700181}
182
183int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700184 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
187void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700188 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700189
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 if (restoreSnapshot()) {
191 setScissorFromClip();
192 }
Romain Guybb9524b2010-06-22 18:56:38 -0700193}
194
195void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700197
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700199
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 while (mSaveCount != saveCount - 1) {
201 restoreClip |= restoreSnapshot();
202 }
Romain Guybb9524b2010-06-22 18:56:38 -0700203
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 if (restoreClip) {
205 setScissorFromClip();
206 }
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
209int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 mSnapshot = new Snapshot(mSnapshot);
211 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700212}
213
214bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700215 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700216 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700217 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700218
Romain Guybd6b79b2010-06-26 00:13:53 -0700219 sp<Snapshot> current = mSnapshot;
220 sp<Snapshot> previous = mSnapshot->previous;
221
Romain Guyf86ef572010-07-01 11:05:42 -0700222 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700223 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700224 }
225
Romain Guybd6b79b2010-06-26 00:13:53 -0700226 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700227 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700228 }
229
230 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700232
Romain Guy7ae7ac42010-06-25 13:46:18 -0700233 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700234}
235
Romain Guyd55a8612010-06-28 17:42:46 -0700236void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700237 if (!current->layer) {
238 LOGE("Attempting to compose a layer that does not exist");
239 return;
240 }
241
Romain Guyd55a8612010-06-28 17:42:46 -0700242 // Unbind current FBO and restore previous one
243 // Most of the time, previous->fbo will be 0 to bind the default buffer
244 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
245
246 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700247 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700248 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
249
Romain Guydda57022010-07-06 11:39:32 -0700250 Layer* layer = current->layer;
251
Romain Guyd55a8612010-06-28 17:42:46 -0700252 // Compute the correct texture coordinates for the FBO texture
253 // The texture is currently as big as the window but drawn with
254 // a quad of the appropriate size
Romain Guydda57022010-07-06 11:39:32 -0700255 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700256
Romain Guydda57022010-07-06 11:39:32 -0700257 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700258 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700259
Romain Guydda57022010-07-06 11:39:32 -0700260 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700261 // Failing to add the layer to the cache should happen only if the
262 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700263 if (!mLayerCache.put(size, layer)) {
264 LAYER_LOGD("Deleting layer");
265
266 glDeleteFramebuffers(1, &layer->fbo);
267 glDeleteTextures(1, &layer->texture);
268
269 delete layer;
270 }
Romain Guyd55a8612010-06-28 17:42:46 -0700271}
272
Romain Guyf6a11b82010-06-23 17:47:49 -0700273///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700274// Layers
275///////////////////////////////////////////////////////////////////////////////
276
277int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
278 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700279 int count = saveSnapshot();
280
281 int alpha = 255;
282 SkXfermode::Mode mode;
283
284 if (p) {
285 alpha = p->getAlpha();
286 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
287 if (!isMode) {
288 // Assume SRC_OVER
289 mode = SkXfermode::kSrcOver_Mode;
290 }
291 } else {
292 mode = SkXfermode::kSrcOver_Mode;
293 }
294
295 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
296
297 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700298}
299
300int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
301 int alpha, int flags) {
302 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700303 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
304 return count;
305}
Romain Guybd6b79b2010-06-26 00:13:53 -0700306
Romain Guyd55a8612010-06-28 17:42:46 -0700307bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
308 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700309
Romain Guyd27977d2010-07-14 19:18:51 -0700310 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda57022010-07-06 11:39:32 -0700311 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700312
Romain Guyf18fd992010-07-08 11:45:51 -0700313 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
314 LayerSize size(right - left, bottom - top);
315
Romain Guyf18fd992010-07-08 11:45:51 -0700316 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700317 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700318 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700319 }
320
Romain Guyf18fd992010-07-08 11:45:51 -0700321 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
322
Romain Guyf86ef572010-07-01 11:05:42 -0700323 // Clear the FBO
324 glDisable(GL_SCISSOR_TEST);
325 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
326 glClear(GL_COLOR_BUFFER_BIT);
327 glEnable(GL_SCISSOR_TEST);
328
Romain Guydda57022010-07-06 11:39:32 -0700329 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700330 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700331 layer->mode = mode;
332 layer->alpha = alpha / 255.0f;
333 layer->layer.set(left, top, right, bottom);
334
335 snapshot->layer = layer;
336 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700337
Romain Guyf86ef572010-07-01 11:05:42 -0700338 // Creates a new snapshot to draw into the FBO
339 saveSnapshot();
340 // TODO: This doesn't preserve other transformations (check Skia first)
341 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700342 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700343 mSnapshot->height = bottom - top;
344 setScissorFromClip();
345
Romain Guy079ba2c2010-07-16 14:12:24 -0700346 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700347 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700348
349 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700350 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700351
Romain Guyd55a8612010-06-28 17:42:46 -0700352 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700353}
354
355///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700356// Transforms
357///////////////////////////////////////////////////////////////////////////////
358
359void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700360 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700361}
362
363void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700364 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700365}
366
367void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700368 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700369}
370
371void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700372 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700373}
374
375void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700376 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700377}
378
379void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 mat4 m(*matrix);
381 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384///////////////////////////////////////////////////////////////////////////////
385// Clipping
386///////////////////////////////////////////////////////////////////////////////
387
Romain Guybb9524b2010-06-22 18:56:38 -0700388void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700389 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700390 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700391}
392
393const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700394 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700395}
396
Romain Guyc7d53492010-06-25 13:41:57 -0700397bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700398 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700399 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700400 return !mSnapshot->clipRect.intersects(r);
401}
402
Romain Guy079ba2c2010-07-16 14:12:24 -0700403bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
404 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700405 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700406 setScissorFromClip();
407 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700408 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700409}
410
Romain Guyf6a11b82010-06-23 17:47:49 -0700411///////////////////////////////////////////////////////////////////////////////
412// Drawing
413///////////////////////////////////////////////////////////////////////////////
414
Romain Guyc1396e92010-06-30 17:56:19 -0700415void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700416 const float right = left + bitmap->width();
417 const float bottom = top + bitmap->height();
418
419 if (quickReject(left, top, right, bottom)) {
420 return;
421 }
422
Romain Guyf86ef572010-07-01 11:05:42 -0700423 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700424 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700425}
426
Romain Guyf86ef572010-07-01 11:05:42 -0700427void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
428 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
429 const mat4 transform(*matrix);
430 transform.mapRect(r);
431
Romain Guy6926c722010-07-12 20:20:03 -0700432 if (quickReject(r.left, r.top, r.right, r.bottom)) {
433 return;
434 }
435
Romain Guyf86ef572010-07-01 11:05:42 -0700436 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700437 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700438}
439
Romain Guy8ba548f2010-06-30 19:21:21 -0700440void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
441 float srcLeft, float srcTop, float srcRight, float srcBottom,
442 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700443 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700444 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
445 return;
446 }
447
Romain Guyf86ef572010-07-01 11:05:42 -0700448 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700449
Romain Guy8ba548f2010-06-30 19:21:21 -0700450 const float width = texture->width;
451 const float height = texture->height;
452
453 const float u1 = srcLeft / width;
454 const float v1 = srcTop / height;
455 const float u2 = srcRight / width;
456 const float v2 = srcBottom / height;
457
458 resetDrawTextureTexCoords(u1, v1, u2, v2);
459
Romain Guy82ba8142010-07-09 13:25:56 -0700460 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700461
462 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
463}
464
Romain Guydeba7852010-07-07 17:54:48 -0700465void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
466 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700467 if (quickReject(left, top, right, bottom)) {
468 return;
469 }
470
Romain Guyf7f93552010-07-08 19:17:03 -0700471 const Texture* texture = mTextureCache.get(bitmap);
472
473 int alpha;
474 SkXfermode::Mode mode;
475 getAlphaAndMode(paint, &alpha, &mode);
476
Romain Guyf7f93552010-07-08 19:17:03 -0700477 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700478 mesh->updateVertices(bitmap, left, top, right, bottom,
479 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700480
481 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
482 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700483 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
484 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700485 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700486}
487
Romain Guy85bf02f2010-06-22 13:11:24 -0700488void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700489 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700490 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700491}
Romain Guy9d5316e2010-06-24 19:30:36 -0700492
Romain Guybd6b79b2010-06-26 00:13:53 -0700493void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700494 if (quickReject(left, top, right, bottom)) {
495 return;
496 }
497
Romain Guy026c5e162010-06-28 17:12:22 -0700498 SkXfermode::Mode mode;
499
500 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
501 if (!isMode) {
502 // Assume SRC_OVER
503 mode = SkXfermode::kSrcOver_Mode;
504 }
505
506 // Skia draws using the color's alpha channel if < 255
507 // Otherwise, it uses the paint's alpha
508 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700509 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700510 color |= p->getAlpha() << 24;
511 }
512
513 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700514}
Romain Guy9d5316e2010-06-24 19:30:36 -0700515
Romain Guy6926c722010-07-12 20:20:03 -0700516///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700517// Shaders
518///////////////////////////////////////////////////////////////////////////////
519
520void OpenGLRenderer::resetShader() {
521 mShader = OpenGLRenderer::kShaderNone;
522 mShaderBlend = false;
523 mShaderTileX = SkShader::kClamp_TileMode;
524 mShaderTileY = SkShader::kClamp_TileMode;
525}
526
527void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
528 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
529 mShader = kShaderBitmap;
530 mShaderBlend = hasAlpha;
531 mShaderBitmap = bitmap;
532 mShaderTileX = tileX;
533 mShaderTileY = tileY;
534 mShaderMatrix = matrix;
535}
536
537///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700538// Drawing implementation
539///////////////////////////////////////////////////////////////////////////////
540
Romain Guy026c5e162010-06-28 17:12:22 -0700541void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700542 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700543 // If a shader is set, preserve only the alpha
544 if (mShader != kShaderNone) {
545 color |= 0x00ffffff;
546 }
547
548 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700549 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700550 const GLfloat a = alpha / 255.0f;
551 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
552 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
553 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
554
555 switch (mShader) {
556 case kShaderBitmap:
557 drawBitmapShader(left, top, right, bottom, a, mode);
558 return;
559 default:
560 break;
561 }
Romain Guy026c5e162010-06-28 17:12:22 -0700562
Romain Guy6926c722010-07-12 20:20:03 -0700563 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700564 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700565
Romain Guyc7d53492010-06-25 13:41:57 -0700566 mModelView.loadTranslate(left, top, 0.0f);
567 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700568
Romain Guy079ba2c2010-07-16 14:12:24 -0700569 if (!useProgram(mDrawColorProgram)) {
Romain Guy6926c722010-07-12 20:20:03 -0700570 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy079ba2c2010-07-16 14:12:24 -0700571 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700572 gDrawColorVertexStride, p);
573 }
Romain Guyd27977d2010-07-14 19:18:51 -0700574
575 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700576 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700577 } else {
578 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700579 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700580 }
581
Romain Guy079ba2c2010-07-16 14:12:24 -0700582 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700583
Romain Guyc7d53492010-06-25 13:41:57 -0700584 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700585}
Romain Guy026c5e162010-06-28 17:12:22 -0700586
Romain Guyd27977d2010-07-14 19:18:51 -0700587void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
588 float alpha, SkXfermode::Mode mode) {
589 const Texture* texture = mTextureCache.get(mShaderBitmap);
590
591 const float width = texture->width;
592 const float height = texture->height;
593
594 // This could be done in the vertex shader but we have only 4 vertices
595 float u1 = 0.0f;
596 float v1 = 0.0f;
597 float u2 = right - left;
598 float v2 = bottom - top;
599
600 if (mShaderMatrix) {
601 SkMatrix inverse;
602 mShaderMatrix->invert(&inverse);
603 mat4 m(inverse);
604 Rect r(u1, v1, u2, v2);
605 m.mapRect(r);
606
607 u1 = r.left;
608 u2 = r.right;
609 v1 = r.top;
610 v2 = r.bottom;
611 }
612
613 u1 /= width;
614 u2 /= width;
615 v1 /= height;
616 v2 /= height;
617
618 resetDrawTextureTexCoords(u1, v1, u2, v2);
619
620 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
621 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
622
623 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
624}
625
Romain Guy82ba8142010-07-09 13:25:56 -0700626void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700627 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700628 int alpha;
629 SkXfermode::Mode mode;
630 getAlphaAndMode(paint, &alpha, &mode);
631
Romain Guya9794742010-07-13 11:37:54 -0700632 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
633 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700634}
635
Romain Guybd6b79b2010-06-26 00:13:53 -0700636void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700637 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
638 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700639 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
640}
641
642void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700643 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700644 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700645 mModelView.loadTranslate(left, top, 0.0f);
646 mModelView.scale(right - left, bottom - top, 1.0f);
647
Romain Guyd27977d2010-07-14 19:18:51 -0700648 useProgram(mDrawTextureProgram);
649 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700650
Romain Guya9794742010-07-13 11:37:54 -0700651 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guyd55a8612010-06-28 17:42:46 -0700652
Romain Guyd27977d2010-07-14 19:18:51 -0700653 // TODO: Only bind/set parameters when needed
Romain Guybd6b79b2010-06-26 00:13:53 -0700654 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guyd27977d2010-07-14 19:18:51 -0700655 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gTileModes[mShaderTileX]);
656 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gTileModes[mShaderTileY]);
Romain Guyc1396e92010-06-30 17:56:19 -0700657
Romain Guya9794742010-07-13 11:37:54 -0700658 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700659 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700660
Romain Guyd27977d2010-07-14 19:18:51 -0700661 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700662 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700663 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700664 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700665
Romain Guyf7f93552010-07-08 19:17:03 -0700666 if (!indices) {
667 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
668 } else {
669 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
670 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700671
Romain Guybd6b79b2010-06-26 00:13:53 -0700672 glBindTexture(GL_TEXTURE_2D, 0);
Romain Guy82ba8142010-07-09 13:25:56 -0700673}
674
675void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
676 // In theory we should not blend if the mode is Src, but it's rare enough
677 // that it's not worth it
678 blend = blend || mode != SkXfermode::kSrcOver_Mode;
679 if (blend) {
680 if (!mBlend) {
681 glEnable(GL_BLEND);
682 }
683
684 GLenum sourceMode = gBlends[mode].src;
685 GLenum destMode = gBlends[mode].dst;
686 if (!isPremultiplied && sourceMode == GL_ONE) {
687 sourceMode = GL_SRC_ALPHA;
688 }
689
690 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
691 glBlendFunc(sourceMode, destMode);
692 mLastSrcMode = sourceMode;
693 mLastDstMode = destMode;
694 }
695 } else if (mBlend) {
696 glDisable(GL_BLEND);
697 }
698 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700699}
700
Romain Guyd27977d2010-07-14 19:18:51 -0700701bool OpenGLRenderer::useProgram(const sp<Program>& program) {
702 if (!program->isInUse()) {
703 mCurrentProgram->remove();
704 program->use();
705 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700706 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700707 }
Romain Guy6926c722010-07-12 20:20:03 -0700708 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700709}
710
Romain Guy026c5e162010-06-28 17:12:22 -0700711void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700712 TextureVertex* v = &mDrawTextureVertices[0];
713 TextureVertex::setUV(v++, u1, v1);
714 TextureVertex::setUV(v++, u2, v1);
715 TextureVertex::setUV(v++, u1, v2);
716 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700717}
718
719void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
720 if (paint) {
721 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
722 if (!isMode) {
723 // Assume SRC_OVER
724 *mode = SkXfermode::kSrcOver_Mode;
725 }
726
727 // Skia draws using the color's alpha channel if < 255
728 // Otherwise, it uses the paint's alpha
729 int color = paint->getColor();
730 *alpha = (color >> 24) & 0xFF;
731 if (*alpha == 255) {
732 *alpha = paint->getAlpha();
733 }
734 } else {
735 *mode = SkXfermode::kSrcOver_Mode;
736 *alpha = 255;
737 }
Romain Guy026c5e162010-06-28 17:12:22 -0700738}
739
Romain Guy9d5316e2010-06-24 19:30:36 -0700740}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700741}; // namespace android