blob: 117fccd3d6dbe7a2ca75262bf591711115ffaf5c [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 Guy121e22422010-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 Guy121e22422010-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 Guydda570202010-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 Guydda570202010-07-06 11:39:32 -070044
Romain Guy121e22422010-07-01 18:26:52 -070045// Converts a number of mega-bytes into bytes
46#define MB(s) s * 1024 * 1024
47
Romain Guydda570202010-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 Guyf6a11b82010-06-23 17:47:49 -070093///////////////////////////////////////////////////////////////////////////////
94// Constructors/destructor
95///////////////////////////////////////////////////////////////////////////////
96
Romain Guydda570202010-07-06 11:39:32 -070097OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -070098 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-07-06 11:39:32 -070099 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -0700100 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
101 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700102 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700103
Romain Guy121e22422010-07-01 18:26:52 -0700104 char property[PROPERTY_VALUE_MAX];
105 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700106 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e22422010-07-01 18:26:52 -0700107 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda570202010-07-06 11:39:32 -0700108 } else {
109 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE);
110 }
111
112 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
113 LOGD(" Setting layer cache size to %sMB", property);
114 mLayerCache.setMaxSize(MB(atoi(property)));
115 } else {
116 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guy121e22422010-07-01 18:26:52 -0700117 }
118
Romain Guy9d5316e2010-06-24 19:30:36 -0700119 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -0700120 mDrawTextureShader = new DrawTextureProgram;
Romain Guy260e1022010-07-12 14:41:06 -0700121 mCurrentShader = mDrawTextureShader;
Romain Guy026c5e162010-06-28 17:12:22 -0700122
123 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
127 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700128
129 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700130 mLayerCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guyf6a11b82010-06-23 17:47:49 -0700133///////////////////////////////////////////////////////////////////////////////
134// Setup
135///////////////////////////////////////////////////////////////////////////////
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700138 glViewport(0, 0, width, height);
139
Romain Guy260e1022010-07-12 14:41:06 -0700140 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700141
142 mWidth = width;
143 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700144 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700148 mSnapshot = &mFirstSnapshot;
149 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700150
Romain Guy08ae3172010-06-21 19:35:50 -0700151 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
Romain Guy08ae3172010-06-21 19:35:50 -0700153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guy08ae3172010-06-21 19:35:50 -0700156 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700157 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700158
Romain Guybb9524b2010-06-22 18:56:38 -0700159 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
160}
161
Romain Guyf6a11b82010-06-23 17:47:49 -0700162///////////////////////////////////////////////////////////////////////////////
163// State management
164///////////////////////////////////////////////////////////////////////////////
165
Romain Guybb9524b2010-06-22 18:56:38 -0700166int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700167 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700168}
169
170int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700171 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700172}
173
174void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700176
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 if (restoreSnapshot()) {
178 setScissorFromClip();
179 }
Romain Guybb9524b2010-06-22 18:56:38 -0700180}
181
182void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700183 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700186
Romain Guy7ae7ac42010-06-25 13:46:18 -0700187 while (mSaveCount != saveCount - 1) {
188 restoreClip |= restoreSnapshot();
189 }
Romain Guybb9524b2010-06-22 18:56:38 -0700190
Romain Guy7ae7ac42010-06-25 13:46:18 -0700191 if (restoreClip) {
192 setScissorFromClip();
193 }
Romain Guybb9524b2010-06-22 18:56:38 -0700194}
195
196int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700197 mSnapshot = new Snapshot(mSnapshot);
198 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700203 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700204 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700205
Romain Guybd6b79b2010-06-26 00:13:53 -0700206 sp<Snapshot> current = mSnapshot;
207 sp<Snapshot> previous = mSnapshot->previous;
208
Romain Guyf86ef572010-07-01 11:05:42 -0700209 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700210 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700211 }
212
Romain Guybd6b79b2010-06-26 00:13:53 -0700213 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700214 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700215 }
216
217 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700219
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700221}
222
Romain Guyd55a8612010-06-28 17:42:46 -0700223void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700224 if (!current->layer) {
225 LOGE("Attempting to compose a layer that does not exist");
226 return;
227 }
228
Romain Guyd55a8612010-06-28 17:42:46 -0700229 // Unbind current FBO and restore previous one
230 // Most of the time, previous->fbo will be 0 to bind the default buffer
231 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
232
233 // Restore the clip from the previous snapshot
234 const Rect& clip = previous->getMappedClip();
235 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
236
Romain Guydda570202010-07-06 11:39:32 -0700237 Layer* layer = current->layer;
238
Romain Guyd55a8612010-06-28 17:42:46 -0700239 // Compute the correct texture coordinates for the FBO texture
240 // The texture is currently as big as the window but drawn with
241 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700242 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700243
Romain Guydda570202010-07-06 11:39:32 -0700244 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy82ba8142010-07-09 13:25:56 -0700245 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700246
Romain Guydda570202010-07-06 11:39:32 -0700247 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700248 // Failing to add the layer to the cache should happen only if the
249 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700250 if (!mLayerCache.put(size, layer)) {
251 LAYER_LOGD("Deleting layer");
252
253 glDeleteFramebuffers(1, &layer->fbo);
254 glDeleteTextures(1, &layer->texture);
255
256 delete layer;
257 }
Romain Guyd55a8612010-06-28 17:42:46 -0700258}
259
Romain Guyf6a11b82010-06-23 17:47:49 -0700260///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700261// Layers
262///////////////////////////////////////////////////////////////////////////////
263
264int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
265 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700266 int count = saveSnapshot();
267
268 int alpha = 255;
269 SkXfermode::Mode mode;
270
271 if (p) {
272 alpha = p->getAlpha();
273 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
274 if (!isMode) {
275 // Assume SRC_OVER
276 mode = SkXfermode::kSrcOver_Mode;
277 }
278 } else {
279 mode = SkXfermode::kSrcOver_Mode;
280 }
281
282 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
283
284 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700285}
286
287int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
288 int alpha, int flags) {
289 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700290 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
291 return count;
292}
Romain Guybd6b79b2010-06-26 00:13:53 -0700293
Romain Guyd55a8612010-06-28 17:42:46 -0700294bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
295 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700296
Romain Guydda570202010-07-06 11:39:32 -0700297 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height);
298 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700299
Romain Guyf18fd992010-07-08 11:45:51 -0700300 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
301 LayerSize size(right - left, bottom - top);
302
Romain Guyf18fd992010-07-08 11:45:51 -0700303 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700304 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700305 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700306 }
307
Romain Guyf18fd992010-07-08 11:45:51 -0700308 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
309
Romain Guyf86ef572010-07-01 11:05:42 -0700310 // Clear the FBO
311 glDisable(GL_SCISSOR_TEST);
312 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
313 glClear(GL_COLOR_BUFFER_BIT);
314 glEnable(GL_SCISSOR_TEST);
315
Romain Guydda570202010-07-06 11:39:32 -0700316 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700317 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700318 layer->mode = mode;
319 layer->alpha = alpha / 255.0f;
320 layer->layer.set(left, top, right, bottom);
321
322 snapshot->layer = layer;
323 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700324
Romain Guyf86ef572010-07-01 11:05:42 -0700325 // Creates a new snapshot to draw into the FBO
326 saveSnapshot();
327 // TODO: This doesn't preserve other transformations (check Skia first)
328 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
329 mSnapshot->clipRect.set(left, top, right, bottom);
330 mSnapshot->height = bottom - top;
331 setScissorFromClip();
332
333 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
334 Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700335 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700336
337 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700338 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700339
Romain Guyd55a8612010-06-28 17:42:46 -0700340 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700341}
342
343///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700344// Transforms
345///////////////////////////////////////////////////////////////////////////////
346
347void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700348 mSnapshot->transform.translate(dx, dy, 0.0f);
349 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700350}
351
352void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700353 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
354 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700355}
356
357void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700358 mSnapshot->transform.scale(sx, sy, 1.0f);
359 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700360}
361
362void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700363 mSnapshot->transform.load(*matrix);
364 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700365}
366
367void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700368 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700369}
370
371void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700372 mat4 m(*matrix);
373 mSnapshot->transform.multiply(m);
374 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377///////////////////////////////////////////////////////////////////////////////
378// Clipping
379///////////////////////////////////////////////////////////////////////////////
380
Romain Guybb9524b2010-06-22 18:56:38 -0700381void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700383 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700384}
385
386const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700387 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700388}
389
Romain Guyc7d53492010-06-25 13:41:57 -0700390bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
391 /*
392 * The documentation of quickReject() indicates that the specified rect
393 * is transformed before being compared to the clip rect. However, the
394 * clip rect is not stored transformed in the snapshot and can thus be
395 * compared directly
396 *
397 * The following code can be used instead to performed a mapped comparison:
398 *
399 * mSnapshot->transform.mapRect(r);
400 * const Rect& clip = mSnapshot->getMappedClip();
401 * return !clip.intersects(r);
402 */
Romain Guyc7d53492010-06-25 13:41:57 -0700403 Rect r(left, top, right, bottom);
404 return !mSnapshot->clipRect.intersects(r);
405}
406
Romain Guybb9524b2010-06-22 18:56:38 -0700407bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700408 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
409 if (clipped) {
410 mSnapshot->flags |= Snapshot::kFlagClipSet;
411 setScissorFromClip();
412 }
413 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700414}
415
Romain Guyf6a11b82010-06-23 17:47:49 -0700416///////////////////////////////////////////////////////////////////////////////
417// Drawing
418///////////////////////////////////////////////////////////////////////////////
419
Romain Guyc1396e92010-06-30 17:56:19 -0700420void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700421 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700422 drawTextureRect(left, top, left + texture->width, top + texture->height, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700423}
424
Romain Guyf86ef572010-07-01 11:05:42 -0700425void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
426 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
427 const mat4 transform(*matrix);
428 transform.mapRect(r);
429
430 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700431 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700432}
433
Romain Guy8ba548f2010-06-30 19:21:21 -0700434void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
435 float srcLeft, float srcTop, float srcRight, float srcBottom,
436 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700437 const SkPaint* paint) {
438 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700439
Romain Guy8ba548f2010-06-30 19:21:21 -0700440 const float width = texture->width;
441 const float height = texture->height;
442
443 const float u1 = srcLeft / width;
444 const float v1 = srcTop / height;
445 const float u2 = srcRight / width;
446 const float v2 = srcBottom / height;
447
448 resetDrawTextureTexCoords(u1, v1, u2, v2);
449
Romain Guy82ba8142010-07-09 13:25:56 -0700450 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700451
452 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
453}
454
Romain Guydeba7852010-07-07 17:54:48 -0700455void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
456 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guyf7f93552010-07-08 19:17:03 -0700457 const Texture* texture = mTextureCache.get(bitmap);
458
459 int alpha;
460 SkXfermode::Mode mode;
461 getAlphaAndMode(paint, &alpha, &mode);
462
Romain Guyf7f93552010-07-08 19:17:03 -0700463 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700464 mesh->updateVertices(bitmap, left, top, right, bottom,
465 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700466
467 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
468 // patch mesh already defines the final size
Romain Guy16202fc2010-07-09 16:13:28 -0700469 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, mode,
470 texture->blend, true, &mesh->vertices[0].position[0],
471 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700472}
473
Romain Guy85bf02f2010-06-22 13:11:24 -0700474void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700475 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700476 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700477}
Romain Guy9d5316e2010-06-24 19:30:36 -0700478
Romain Guybd6b79b2010-06-26 00:13:53 -0700479void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700480 SkXfermode::Mode mode;
481
482 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
483 if (!isMode) {
484 // Assume SRC_OVER
485 mode = SkXfermode::kSrcOver_Mode;
486 }
487
488 // Skia draws using the color's alpha channel if < 255
489 // Otherwise, it uses the paint's alpha
490 int color = p->getColor();
491 if (((color >> 24) & 0xFF) == 255) {
492 color |= p->getAlpha() << 24;
493 }
494
495 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700496}
Romain Guy9d5316e2010-06-24 19:30:36 -0700497
Romain Guy026c5e162010-06-28 17:12:22 -0700498void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
499 int color, SkXfermode::Mode mode) {
500 const int alpha = (color >> 24) & 0xFF;
Romain Guy026c5e162010-06-28 17:12:22 -0700501 const GLfloat a = alpha / 255.0f;
502 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
503 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
504 const GLfloat b = ((color ) & 0xFF) / 255.0f;
505
Romain Guy82ba8142010-07-09 13:25:56 -0700506 chooseBlending(alpha < 255, mode, true);
Romain Guy9d5316e2010-06-24 19:30:36 -0700507
Romain Guyc7d53492010-06-25 13:41:57 -0700508 mModelView.loadTranslate(left, top, 0.0f);
509 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700510
Romain Guy260e1022010-07-12 14:41:06 -0700511 useShader(mDrawColorShader);
512 mDrawColorShader->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy9d5316e2010-06-24 19:30:36 -0700513
Romain Guyc7d53492010-06-25 13:41:57 -0700514 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700515
Romain Guyc7d53492010-06-25 13:41:57 -0700516 glEnableVertexAttribArray(mDrawColorShader->position);
517 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
518 gDrawColorVertexStride, p);
Romain Guy16202fc2010-07-09 16:13:28 -0700519 glUniform4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700520
Romain Guyc7d53492010-06-25 13:41:57 -0700521 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700522
Romain Guyc7d53492010-06-25 13:41:57 -0700523 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy82ba8142010-07-09 13:25:56 -0700524}
Romain Guy026c5e162010-06-28 17:12:22 -0700525
Romain Guy82ba8142010-07-09 13:25:56 -0700526void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
527 const Texture* texture, const SkPaint* paint, bool isPremultiplied) {
528 int alpha;
529 SkXfermode::Mode mode;
530 getAlphaAndMode(paint, &alpha, &mode);
531
532 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
533 isPremultiplied, &mDrawTextureVertices[0].position[0],
534 &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700535}
536
Romain Guybd6b79b2010-06-26 00:13:53 -0700537void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700538 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guyf7f93552010-07-08 19:17:03 -0700539 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, isPremultiplied,
540 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
541}
542
543void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
544 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied,
545 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700546 mModelView.loadTranslate(left, top, 0.0f);
547 mModelView.scale(right - left, bottom - top, 1.0f);
548
Romain Guy260e1022010-07-12 14:41:06 -0700549 useShader(mDrawTextureShader);
550 mDrawTextureShader->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700551
Romain Guy82ba8142010-07-09 13:25:56 -0700552 chooseBlending(blend || alpha < 1.0f, mode, isPremultiplied);
Romain Guyd55a8612010-06-28 17:42:46 -0700553
Romain Guybd6b79b2010-06-26 00:13:53 -0700554 glBindTexture(GL_TEXTURE_2D, texture);
555
Romain Guyc1396e92010-06-30 17:56:19 -0700556 // TODO handle tiling and filtering here
557
Romain Guybd6b79b2010-06-26 00:13:53 -0700558 glActiveTexture(GL_TEXTURE0);
559 glUniform1i(mDrawTextureShader->sampler, 0);
Romain Guy16202fc2010-07-09 16:13:28 -0700560 glUniform4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700561
Romain Guybd6b79b2010-06-26 00:13:53 -0700562 glEnableVertexAttribArray(mDrawTextureShader->position);
563 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700564 gDrawTextureVertexStride, vertices);
Romain Guybd6b79b2010-06-26 00:13:53 -0700565
566 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
567 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700568 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700569
Romain Guyf7f93552010-07-08 19:17:03 -0700570 if (!indices) {
571 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
572 } else {
573 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
574 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700575
576 glDisableVertexAttribArray(mDrawTextureShader->position);
577 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
578
579 glBindTexture(GL_TEXTURE_2D, 0);
Romain Guy82ba8142010-07-09 13:25:56 -0700580}
581
582void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
583 // In theory we should not blend if the mode is Src, but it's rare enough
584 // that it's not worth it
585 blend = blend || mode != SkXfermode::kSrcOver_Mode;
586 if (blend) {
587 if (!mBlend) {
588 glEnable(GL_BLEND);
589 }
590
591 GLenum sourceMode = gBlends[mode].src;
592 GLenum destMode = gBlends[mode].dst;
593 if (!isPremultiplied && sourceMode == GL_ONE) {
594 sourceMode = GL_SRC_ALPHA;
595 }
596
597 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
598 glBlendFunc(sourceMode, destMode);
599 mLastSrcMode = sourceMode;
600 mLastDstMode = destMode;
601 }
602 } else if (mBlend) {
603 glDisable(GL_BLEND);
604 }
605 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700606}
607
Romain Guy260e1022010-07-12 14:41:06 -0700608void OpenGLRenderer::useShader(const sp<Program>& shader) {
609 if (!shader->isInUse()) {
610 mCurrentShader->remove();
611 shader->use();
612 mCurrentShader = shader;
613 }
614}
615
Romain Guy026c5e162010-06-28 17:12:22 -0700616void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700617 TextureVertex* v = &mDrawTextureVertices[0];
618 TextureVertex::setUV(v++, u1, v1);
619 TextureVertex::setUV(v++, u2, v1);
620 TextureVertex::setUV(v++, u1, v2);
621 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700622}
623
624void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
625 if (paint) {
626 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
627 if (!isMode) {
628 // Assume SRC_OVER
629 *mode = SkXfermode::kSrcOver_Mode;
630 }
631
632 // Skia draws using the color's alpha channel if < 255
633 // Otherwise, it uses the paint's alpha
634 int color = paint->getColor();
635 *alpha = (color >> 24) & 0xFF;
636 if (*alpha == 255) {
637 *alpha = paint->getAlpha();
638 }
639 } else {
640 *mode = SkXfermode::kSrcOver_Mode;
641 *alpha = 255;
642 }
Romain Guy026c5e162010-06-28 17:12:22 -0700643}
644
Romain Guy9d5316e2010-06-24 19:30:36 -0700645}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700646}; // namespace android