blob: f7b4455967adb5c4c9770494371ce4ab4c72cf0a [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 Guyf6a11b82010-06-23 17:47:49 -070093///////////////////////////////////////////////////////////////////////////////
94// Constructors/destructor
95///////////////////////////////////////////////////////////////////////////////
96
Romain Guydda57022010-07-06 11:39:32 -070097OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -070098 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda57022010-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 Guy121e2242010-07-01 18:26:52 -0700104 char property[PROPERTY_VALUE_MAX];
105 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700106 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e2242010-07-01 18:26:52 -0700107 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda57022010-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 Guy121e2242010-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 Guydda57022010-07-06 11:39:32 -0700130 mLayerCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700131 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700132}
133
Romain Guyf6a11b82010-06-23 17:47:49 -0700134///////////////////////////////////////////////////////////////////////////////
135// Setup
136///////////////////////////////////////////////////////////////////////////////
137
Romain Guy85bf02f2010-06-22 13:11:24 -0700138void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700139 glViewport(0, 0, width, height);
140
Romain Guy260e1022010-07-12 14:41:06 -0700141 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700142
143 mWidth = width;
144 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700145 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700146}
147
Romain Guy85bf02f2010-06-22 13:11:24 -0700148void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700149 mSnapshot = &mFirstSnapshot;
150 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700151
Romain Guy08ae3172010-06-21 19:35:50 -0700152 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
Romain Guy08ae3172010-06-21 19:35:50 -0700154 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
155 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700156
Romain Guy08ae3172010-06-21 19:35:50 -0700157 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700158 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700159
Romain Guybb9524b2010-06-22 18:56:38 -0700160 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
161}
162
Romain Guyf6a11b82010-06-23 17:47:49 -0700163///////////////////////////////////////////////////////////////////////////////
164// State management
165///////////////////////////////////////////////////////////////////////////////
166
Romain Guybb9524b2010-06-22 18:56:38 -0700167int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700168 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700169}
170
171int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700172 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700173}
174
175void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700176 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700177
Romain Guy7ae7ac42010-06-25 13:46:18 -0700178 if (restoreSnapshot()) {
179 setScissorFromClip();
180 }
Romain Guybb9524b2010-06-22 18:56:38 -0700181}
182
183void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700184 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700185
Romain Guy7ae7ac42010-06-25 13:46:18 -0700186 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700187
Romain Guy7ae7ac42010-06-25 13:46:18 -0700188 while (mSaveCount != saveCount - 1) {
189 restoreClip |= restoreSnapshot();
190 }
Romain Guybb9524b2010-06-22 18:56:38 -0700191
Romain Guy7ae7ac42010-06-25 13:46:18 -0700192 if (restoreClip) {
193 setScissorFromClip();
194 }
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 mSnapshot = new Snapshot(mSnapshot);
199 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
202bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700203 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700204 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700205 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700206
Romain Guybd6b79b2010-06-26 00:13:53 -0700207 sp<Snapshot> current = mSnapshot;
208 sp<Snapshot> previous = mSnapshot->previous;
209
Romain Guyf86ef572010-07-01 11:05:42 -0700210 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700211 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700212 }
213
Romain Guybd6b79b2010-06-26 00:13:53 -0700214 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700215 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700216 }
217
218 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700219 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700220
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700222}
223
Romain Guyd55a8612010-06-28 17:42:46 -0700224void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700225 if (!current->layer) {
226 LOGE("Attempting to compose a layer that does not exist");
227 return;
228 }
229
Romain Guyd55a8612010-06-28 17:42:46 -0700230 // Unbind current FBO and restore previous one
231 // Most of the time, previous->fbo will be 0 to bind the default buffer
232 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
233
234 // Restore the clip from the previous snapshot
235 const Rect& clip = previous->getMappedClip();
236 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
237
Romain Guydda57022010-07-06 11:39:32 -0700238 Layer* layer = current->layer;
239
Romain Guyd55a8612010-06-28 17:42:46 -0700240 // Compute the correct texture coordinates for the FBO texture
241 // The texture is currently as big as the window but drawn with
242 // a quad of the appropriate size
Romain Guydda57022010-07-06 11:39:32 -0700243 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700244
Romain Guydda57022010-07-06 11:39:32 -0700245 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy6926c722010-07-12 20:20:03 -0700246 layer->texture, layer->alpha, layer->mode, layer->blend, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700247
Romain Guydda57022010-07-06 11:39:32 -0700248 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700249 // Failing to add the layer to the cache should happen only if the
250 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700251 if (!mLayerCache.put(size, layer)) {
252 LAYER_LOGD("Deleting layer");
253
254 glDeleteFramebuffers(1, &layer->fbo);
255 glDeleteTextures(1, &layer->texture);
256
257 delete layer;
258 }
Romain Guyd55a8612010-06-28 17:42:46 -0700259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700262// Layers
263///////////////////////////////////////////////////////////////////////////////
264
265int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
266 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700267 int count = saveSnapshot();
268
269 int alpha = 255;
270 SkXfermode::Mode mode;
271
272 if (p) {
273 alpha = p->getAlpha();
274 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
275 if (!isMode) {
276 // Assume SRC_OVER
277 mode = SkXfermode::kSrcOver_Mode;
278 }
279 } else {
280 mode = SkXfermode::kSrcOver_Mode;
281 }
282
283 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
284
285 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700286}
287
288int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
289 int alpha, int flags) {
290 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700291 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
292 return count;
293}
Romain Guybd6b79b2010-06-26 00:13:53 -0700294
Romain Guyd55a8612010-06-28 17:42:46 -0700295bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
296 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700297
Romain Guydda57022010-07-06 11:39:32 -0700298 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height);
299 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700300
Romain Guyf18fd992010-07-08 11:45:51 -0700301 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
302 LayerSize size(right - left, bottom - top);
303
Romain Guyf18fd992010-07-08 11:45:51 -0700304 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700305 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700306 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700307 }
308
Romain Guyf18fd992010-07-08 11:45:51 -0700309 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
310
Romain Guyf86ef572010-07-01 11:05:42 -0700311 // Clear the FBO
312 glDisable(GL_SCISSOR_TEST);
313 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
314 glClear(GL_COLOR_BUFFER_BIT);
315 glEnable(GL_SCISSOR_TEST);
316
Romain Guydda57022010-07-06 11:39:32 -0700317 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700318 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700319 layer->mode = mode;
320 layer->alpha = alpha / 255.0f;
321 layer->layer.set(left, top, right, bottom);
322
323 snapshot->layer = layer;
324 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700325
Romain Guyf86ef572010-07-01 11:05:42 -0700326 // Creates a new snapshot to draw into the FBO
327 saveSnapshot();
328 // TODO: This doesn't preserve other transformations (check Skia first)
329 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
330 mSnapshot->clipRect.set(left, top, right, bottom);
331 mSnapshot->height = bottom - top;
332 setScissorFromClip();
333
334 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
335 Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700336 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700337
338 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700339 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700340
Romain Guyd55a8612010-06-28 17:42:46 -0700341 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700342}
343
344///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700345// Transforms
346///////////////////////////////////////////////////////////////////////////////
347
348void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700349 mSnapshot->transform.translate(dx, dy, 0.0f);
350 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700351}
352
353void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700354 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
355 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700356}
357
358void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700359 mSnapshot->transform.scale(sx, sy, 1.0f);
360 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700361}
362
363void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700364 mSnapshot->transform.load(*matrix);
365 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700366}
367
368void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700370}
371
372void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700373 mat4 m(*matrix);
374 mSnapshot->transform.multiply(m);
375 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700376}
377
378///////////////////////////////////////////////////////////////////////////////
379// Clipping
380///////////////////////////////////////////////////////////////////////////////
381
Romain Guybb9524b2010-06-22 18:56:38 -0700382void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700383 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700384 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700385}
386
387const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700389}
390
Romain Guyc7d53492010-06-25 13:41:57 -0700391bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
392 /*
393 * The documentation of quickReject() indicates that the specified rect
394 * is transformed before being compared to the clip rect. However, the
395 * clip rect is not stored transformed in the snapshot and can thus be
396 * compared directly
397 *
398 * The following code can be used instead to performed a mapped comparison:
399 *
400 * mSnapshot->transform.mapRect(r);
401 * const Rect& clip = mSnapshot->getMappedClip();
402 * return !clip.intersects(r);
403 */
Romain Guyc7d53492010-06-25 13:41:57 -0700404 Rect r(left, top, right, bottom);
405 return !mSnapshot->clipRect.intersects(r);
406}
407
Romain Guybb9524b2010-06-22 18:56:38 -0700408bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700409 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
410 if (clipped) {
411 mSnapshot->flags |= Snapshot::kFlagClipSet;
412 setScissorFromClip();
413 }
414 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700415}
416
Romain Guyf6a11b82010-06-23 17:47:49 -0700417///////////////////////////////////////////////////////////////////////////////
418// Drawing
419///////////////////////////////////////////////////////////////////////////////
420
Romain Guyc1396e92010-06-30 17:56:19 -0700421void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700422 const float right = left + bitmap->width();
423 const float bottom = top + bitmap->height();
424
425 if (quickReject(left, top, right, bottom)) {
426 return;
427 }
428
Romain Guyf86ef572010-07-01 11:05:42 -0700429 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700430 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700431}
432
Romain Guyf86ef572010-07-01 11:05:42 -0700433void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
434 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
435 const mat4 transform(*matrix);
436 transform.mapRect(r);
437
Romain Guy6926c722010-07-12 20:20:03 -0700438 if (quickReject(r.left, r.top, r.right, r.bottom)) {
439 return;
440 }
441
Romain Guyf86ef572010-07-01 11:05:42 -0700442 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700443 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700444}
445
Romain Guy8ba548f2010-06-30 19:21:21 -0700446void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
447 float srcLeft, float srcTop, float srcRight, float srcBottom,
448 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700449 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700450 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
451 return;
452 }
453
Romain Guyf86ef572010-07-01 11:05:42 -0700454 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700455
Romain Guy8ba548f2010-06-30 19:21:21 -0700456 const float width = texture->width;
457 const float height = texture->height;
458
459 const float u1 = srcLeft / width;
460 const float v1 = srcTop / height;
461 const float u2 = srcRight / width;
462 const float v2 = srcBottom / height;
463
464 resetDrawTextureTexCoords(u1, v1, u2, v2);
465
Romain Guy82ba8142010-07-09 13:25:56 -0700466 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700467
468 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
469}
470
Romain Guydeba7852010-07-07 17:54:48 -0700471void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
472 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700473 if (quickReject(left, top, right, bottom)) {
474 return;
475 }
476
Romain Guyf7f93552010-07-08 19:17:03 -0700477 const Texture* texture = mTextureCache.get(bitmap);
478
479 int alpha;
480 SkXfermode::Mode mode;
481 getAlphaAndMode(paint, &alpha, &mode);
482
Romain Guyf7f93552010-07-08 19:17:03 -0700483 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700484 mesh->updateVertices(bitmap, left, top, right, bottom,
485 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700486
487 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
488 // patch mesh already defines the final size
Romain Guy16202fc2010-07-09 16:13:28 -0700489 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, mode,
490 texture->blend, true, &mesh->vertices[0].position[0],
491 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700492}
493
Romain Guy85bf02f2010-06-22 13:11:24 -0700494void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700495 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700496 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700497}
Romain Guy9d5316e2010-06-24 19:30:36 -0700498
Romain Guybd6b79b2010-06-26 00:13:53 -0700499void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700500 if (quickReject(left, top, right, bottom)) {
501 return;
502 }
503
Romain Guy026c5e162010-06-28 17:12:22 -0700504 SkXfermode::Mode mode;
505
506 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
507 if (!isMode) {
508 // Assume SRC_OVER
509 mode = SkXfermode::kSrcOver_Mode;
510 }
511
512 // Skia draws using the color's alpha channel if < 255
513 // Otherwise, it uses the paint's alpha
514 int color = p->getColor();
515 if (((color >> 24) & 0xFF) == 255) {
516 color |= p->getAlpha() << 24;
517 }
518
519 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700520}
Romain Guy9d5316e2010-06-24 19:30:36 -0700521
Romain Guy6926c722010-07-12 20:20:03 -0700522///////////////////////////////////////////////////////////////////////////////
523// Drawing implementation
524///////////////////////////////////////////////////////////////////////////////
525
Romain Guy026c5e162010-06-28 17:12:22 -0700526void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
527 int color, SkXfermode::Mode mode) {
528 const int alpha = (color >> 24) & 0xFF;
Romain Guy026c5e162010-06-28 17:12:22 -0700529 const GLfloat a = alpha / 255.0f;
530 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
531 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
532 const GLfloat b = ((color ) & 0xFF) / 255.0f;
533
Romain Guy6926c722010-07-12 20:20:03 -0700534 // Pre-multiplication happens when setting the shader color
Romain Guy82ba8142010-07-09 13:25:56 -0700535 chooseBlending(alpha < 255, mode, true);
Romain Guy9d5316e2010-06-24 19:30:36 -0700536
Romain Guyc7d53492010-06-25 13:41:57 -0700537 mModelView.loadTranslate(left, top, 0.0f);
538 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700539
Romain Guy6926c722010-07-12 20:20:03 -0700540 const bool inUse = useShader(mDrawColorShader);
Romain Guy260e1022010-07-12 14:41:06 -0700541 mDrawColorShader->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guy9d5316e2010-06-24 19:30:36 -0700542
Romain Guy6926c722010-07-12 20:20:03 -0700543 if (!inUse) {
544 const GLvoid* p = &gDrawColorVertices[0].position[0];
545 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
546 gDrawColorVertexStride, p);
547 }
548 // Render using pre-multiplied alpha
549 glUniform4f(mDrawColorShader->color, r * a, g * a, b * a, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700550
Romain Guyc7d53492010-06-25 13:41:57 -0700551 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700552}
Romain Guy026c5e162010-06-28 17:12:22 -0700553
Romain Guy82ba8142010-07-09 13:25:56 -0700554void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
555 const Texture* texture, const SkPaint* paint, bool isPremultiplied) {
556 int alpha;
557 SkXfermode::Mode mode;
558 getAlphaAndMode(paint, &alpha, &mode);
559
Romain Guy6926c722010-07-12 20:20:03 -0700560 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
561 texture->blend, texture->blend, &mDrawTextureVertices[0].position[0],
Romain Guy82ba8142010-07-09 13:25:56 -0700562 &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700563}
564
Romain Guybd6b79b2010-06-26 00:13:53 -0700565void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700566 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guyf7f93552010-07-08 19:17:03 -0700567 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, isPremultiplied,
568 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
569}
570
571void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
572 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied,
573 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700574 mModelView.loadTranslate(left, top, 0.0f);
575 mModelView.scale(right - left, bottom - top, 1.0f);
576
Romain Guy260e1022010-07-12 14:41:06 -0700577 useShader(mDrawTextureShader);
578 mDrawTextureShader->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700579
Romain Guy82ba8142010-07-09 13:25:56 -0700580 chooseBlending(blend || alpha < 1.0f, mode, isPremultiplied);
Romain Guyd55a8612010-06-28 17:42:46 -0700581
Romain Guybd6b79b2010-06-26 00:13:53 -0700582 glBindTexture(GL_TEXTURE_2D, texture);
583
Romain Guyc1396e92010-06-30 17:56:19 -0700584 // TODO handle tiling and filtering here
585
Romain Guy6926c722010-07-12 20:20:03 -0700586 if (isPremultiplied) {
587 glUniform4f(mDrawTextureShader->color, alpha, alpha, alpha, alpha);
588 } else {
589 glUniform4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
590 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700591
Romain Guybd6b79b2010-06-26 00:13:53 -0700592 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700593 gDrawTextureVertexStride, vertices);
Romain Guybd6b79b2010-06-26 00:13:53 -0700594 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700595 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700596
Romain Guyf7f93552010-07-08 19:17:03 -0700597 if (!indices) {
598 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
599 } else {
Romain Guy6926c722010-07-12 20:20:03 -0700600 // TODO: Use triangle strip instead
Romain Guyf7f93552010-07-08 19:17:03 -0700601 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
602 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700603
Romain Guybd6b79b2010-06-26 00:13:53 -0700604 glBindTexture(GL_TEXTURE_2D, 0);
Romain Guy82ba8142010-07-09 13:25:56 -0700605}
606
607void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
608 // In theory we should not blend if the mode is Src, but it's rare enough
609 // that it's not worth it
610 blend = blend || mode != SkXfermode::kSrcOver_Mode;
611 if (blend) {
612 if (!mBlend) {
613 glEnable(GL_BLEND);
614 }
615
616 GLenum sourceMode = gBlends[mode].src;
617 GLenum destMode = gBlends[mode].dst;
618 if (!isPremultiplied && sourceMode == GL_ONE) {
619 sourceMode = GL_SRC_ALPHA;
620 }
621
622 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
623 glBlendFunc(sourceMode, destMode);
624 mLastSrcMode = sourceMode;
625 mLastDstMode = destMode;
626 }
627 } else if (mBlend) {
628 glDisable(GL_BLEND);
629 }
630 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700631}
632
Romain Guy6926c722010-07-12 20:20:03 -0700633bool OpenGLRenderer::useShader(const sp<Program>& shader) {
Romain Guy260e1022010-07-12 14:41:06 -0700634 if (!shader->isInUse()) {
635 mCurrentShader->remove();
636 shader->use();
637 mCurrentShader = shader;
Romain Guy6926c722010-07-12 20:20:03 -0700638 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700639 }
Romain Guy6926c722010-07-12 20:20:03 -0700640 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700641}
642
Romain Guy026c5e162010-06-28 17:12:22 -0700643void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700644 TextureVertex* v = &mDrawTextureVertices[0];
645 TextureVertex::setUV(v++, u1, v1);
646 TextureVertex::setUV(v++, u2, v1);
647 TextureVertex::setUV(v++, u1, v2);
648 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700649}
650
651void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
652 if (paint) {
653 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
654 if (!isMode) {
655 // Assume SRC_OVER
656 *mode = SkXfermode::kSrcOver_Mode;
657 }
658
659 // Skia draws using the color's alpha channel if < 255
660 // Otherwise, it uses the paint's alpha
661 int color = paint->getColor();
662 *alpha = (color >> 24) & 0xFF;
663 if (*alpha == 255) {
664 *alpha = paint->getAlpha();
665 }
666 } else {
667 *mode = SkXfermode::kSrcOver_Mode;
668 *alpha = 255;
669 }
Romain Guy026c5e162010-06-28 17:12:22 -0700670}
671
Romain Guy9d5316e2010-06-24 19:30:36 -0700672}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700673}; // namespace android