blob: decfecfcbe4e9a76816581a9fcdee2e45bf96689 [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 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 Guydda570202010-07-06 11:39:32 -0700103OpenGLRenderer::OpenGLRenderer():
Romain Guy82ba8142010-07-09 13:25:56 -0700104 mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO),
Romain Guydda570202010-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 Guy121e22422010-07-01 18:26:52 -0700110 char property[PROPERTY_VALUE_MAX];
111 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda570202010-07-06 11:39:32 -0700112 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e22422010-07-01 18:26:52 -0700113 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda570202010-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 Guy121e22422010-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 Guydda570202010-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 Guydda570202010-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
247 const Rect& clip = previous->getMappedClip();
248 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
249
Romain Guydda570202010-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 Guydda570202010-07-06 11:39:32 -0700255 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700256
Romain Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guydda570202010-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 Guyd27977d2010-07-14 19:18:51 -0700342 mSnapshot->setClip(left, top, right, bottom);
Romain Guyf86ef572010-07-01 11:05:42 -0700343 mSnapshot->height = bottom - top;
344 setScissorFromClip();
345
346 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
347 Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700348 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700349
350 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700351 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700352
Romain Guyd55a8612010-06-28 17:42:46 -0700353 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700354}
355
356///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700357// Transforms
358///////////////////////////////////////////////////////////////////////////////
359
360void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700361 mSnapshot->transform.translate(dx, dy, 0.0f);
362 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700363}
364
365void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700366 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
367 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700368}
369
370void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700371 mSnapshot->transform.scale(sx, sy, 1.0f);
372 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700373}
374
375void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700376 mSnapshot->transform.load(*matrix);
377 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700381 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mat4 m(*matrix);
386 mSnapshot->transform.multiply(m);
387 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390///////////////////////////////////////////////////////////////////////////////
391// Clipping
392///////////////////////////////////////////////////////////////////////////////
393
Romain Guybb9524b2010-06-22 18:56:38 -0700394void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700395 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700396 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700397}
398
399const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700400 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700401}
402
Romain Guyc7d53492010-06-25 13:41:57 -0700403bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
404 /*
405 * The documentation of quickReject() indicates that the specified rect
406 * is transformed before being compared to the clip rect. However, the
407 * clip rect is not stored transformed in the snapshot and can thus be
408 * compared directly
409 *
410 * The following code can be used instead to performed a mapped comparison:
411 *
412 * mSnapshot->transform.mapRect(r);
413 * const Rect& clip = mSnapshot->getMappedClip();
414 * return !clip.intersects(r);
415 */
Romain Guyc7d53492010-06-25 13:41:57 -0700416 Rect r(left, top, right, bottom);
417 return !mSnapshot->clipRect.intersects(r);
418}
419
Romain Guybb9524b2010-06-22 18:56:38 -0700420bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy3d58c032010-07-14 16:34:53 -0700421 bool clipped = mSnapshot->clip(left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700422 if (clipped) {
423 mSnapshot->flags |= Snapshot::kFlagClipSet;
424 setScissorFromClip();
425 }
426 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700427}
428
Romain Guyf6a11b82010-06-23 17:47:49 -0700429///////////////////////////////////////////////////////////////////////////////
430// Drawing
431///////////////////////////////////////////////////////////////////////////////
432
Romain Guyc1396e92010-06-30 17:56:19 -0700433void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700434 const float right = left + bitmap->width();
435 const float bottom = top + bitmap->height();
436
437 if (quickReject(left, top, right, bottom)) {
438 return;
439 }
440
Romain Guyf86ef572010-07-01 11:05:42 -0700441 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700442 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700443}
444
Romain Guyf86ef572010-07-01 11:05:42 -0700445void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
446 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
447 const mat4 transform(*matrix);
448 transform.mapRect(r);
449
Romain Guy6926c722010-07-12 20:20:03 -0700450 if (quickReject(r.left, r.top, r.right, r.bottom)) {
451 return;
452 }
453
Romain Guyf86ef572010-07-01 11:05:42 -0700454 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700455 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700456}
457
Romain Guy8ba548f2010-06-30 19:21:21 -0700458void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
459 float srcLeft, float srcTop, float srcRight, float srcBottom,
460 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700461 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700462 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
463 return;
464 }
465
Romain Guyf86ef572010-07-01 11:05:42 -0700466 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700467
Romain Guy8ba548f2010-06-30 19:21:21 -0700468 const float width = texture->width;
469 const float height = texture->height;
470
471 const float u1 = srcLeft / width;
472 const float v1 = srcTop / height;
473 const float u2 = srcRight / width;
474 const float v2 = srcBottom / height;
475
476 resetDrawTextureTexCoords(u1, v1, u2, v2);
477
Romain Guy82ba8142010-07-09 13:25:56 -0700478 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700479
480 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
481}
482
Romain Guydeba7852010-07-07 17:54:48 -0700483void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
484 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700485 if (quickReject(left, top, right, bottom)) {
486 return;
487 }
488
Romain Guyf7f93552010-07-08 19:17:03 -0700489 const Texture* texture = mTextureCache.get(bitmap);
490
491 int alpha;
492 SkXfermode::Mode mode;
493 getAlphaAndMode(paint, &alpha, &mode);
494
Romain Guyf7f93552010-07-08 19:17:03 -0700495 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700496 mesh->updateVertices(bitmap, left, top, right, bottom,
497 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700498
499 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
500 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700501 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
502 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700503 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700504}
505
Romain Guy85bf02f2010-06-22 13:11:24 -0700506void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy3d58c032010-07-14 16:34:53 -0700507 const Rect& clip = mSnapshot->getMappedClip();
508 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700509}
Romain Guy9d5316e2010-06-24 19:30:36 -0700510
Romain Guybd6b79b2010-06-26 00:13:53 -0700511void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700512 if (quickReject(left, top, right, bottom)) {
513 return;
514 }
515
Romain Guy026c5e162010-06-28 17:12:22 -0700516 SkXfermode::Mode mode;
517
518 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
519 if (!isMode) {
520 // Assume SRC_OVER
521 mode = SkXfermode::kSrcOver_Mode;
522 }
523
524 // Skia draws using the color's alpha channel if < 255
525 // Otherwise, it uses the paint's alpha
526 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700527 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700528 color |= p->getAlpha() << 24;
529 }
530
531 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700532}
Romain Guy9d5316e2010-06-24 19:30:36 -0700533
Romain Guy6926c722010-07-12 20:20:03 -0700534///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700535// Shaders
536///////////////////////////////////////////////////////////////////////////////
537
538void OpenGLRenderer::resetShader() {
539 mShader = OpenGLRenderer::kShaderNone;
540 mShaderBlend = false;
541 mShaderTileX = SkShader::kClamp_TileMode;
542 mShaderTileY = SkShader::kClamp_TileMode;
543}
544
545void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
546 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
547 mShader = kShaderBitmap;
548 mShaderBlend = hasAlpha;
549 mShaderBitmap = bitmap;
550 mShaderTileX = tileX;
551 mShaderTileY = tileY;
552 mShaderMatrix = matrix;
553}
554
555///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700556// Drawing implementation
557///////////////////////////////////////////////////////////////////////////////
558
Romain Guy026c5e162010-06-28 17:12:22 -0700559void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700560 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700561 // If a shader is set, preserve only the alpha
562 if (mShader != kShaderNone) {
563 color |= 0x00ffffff;
564 }
565
566 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700567 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700568 const GLfloat a = alpha / 255.0f;
569 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
570 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
571 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
572
573 switch (mShader) {
574 case kShaderBitmap:
575 drawBitmapShader(left, top, right, bottom, a, mode);
576 return;
577 default:
578 break;
579 }
Romain Guy026c5e162010-06-28 17:12:22 -0700580
Romain Guy6926c722010-07-12 20:20:03 -0700581 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700582 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700583
Romain Guyc7d53492010-06-25 13:41:57 -0700584 mModelView.loadTranslate(left, top, 0.0f);
585 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700586
Romain Guyd27977d2010-07-14 19:18:51 -0700587 // TODO: Pick the program matching the current shader
588 sp<DrawColorProgram> program = mDrawColorProgram;
589 if (!useProgram(program)) {
Romain Guy6926c722010-07-12 20:20:03 -0700590 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guyd27977d2010-07-14 19:18:51 -0700591 glVertexAttribPointer(program->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700592 gDrawColorVertexStride, p);
593 }
Romain Guyd27977d2010-07-14 19:18:51 -0700594
595 if (!ignoreTransform) {
596 program->set(mOrthoMatrix, mModelView, mSnapshot->transform);
597 } else {
598 mat4 identity;
599 program->set(mOrthoMatrix, mModelView, identity);
600 }
601
602 glUniform4f(program->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700603
Romain Guyc7d53492010-06-25 13:41:57 -0700604 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700605}
Romain Guy026c5e162010-06-28 17:12:22 -0700606
Romain Guyd27977d2010-07-14 19:18:51 -0700607void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
608 float alpha, SkXfermode::Mode mode) {
609 const Texture* texture = mTextureCache.get(mShaderBitmap);
610
611 const float width = texture->width;
612 const float height = texture->height;
613
614 // This could be done in the vertex shader but we have only 4 vertices
615 float u1 = 0.0f;
616 float v1 = 0.0f;
617 float u2 = right - left;
618 float v2 = bottom - top;
619
620 if (mShaderMatrix) {
621 SkMatrix inverse;
622 mShaderMatrix->invert(&inverse);
623 mat4 m(inverse);
624 Rect r(u1, v1, u2, v2);
625 m.mapRect(r);
626
627 u1 = r.left;
628 u2 = r.right;
629 v1 = r.top;
630 v2 = r.bottom;
631 }
632
633 u1 /= width;
634 u2 /= width;
635 v1 /= height;
636 v2 /= height;
637
638 resetDrawTextureTexCoords(u1, v1, u2, v2);
639
640 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
641 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
642
643 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
644}
645
Romain Guy82ba8142010-07-09 13:25:56 -0700646void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700647 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700648 int alpha;
649 SkXfermode::Mode mode;
650 getAlphaAndMode(paint, &alpha, &mode);
651
Romain Guya9794742010-07-13 11:37:54 -0700652 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
653 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700654}
655
Romain Guybd6b79b2010-06-26 00:13:53 -0700656void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700657 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
658 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700659 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
660}
661
662void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700663 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700664 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700665 mModelView.loadTranslate(left, top, 0.0f);
666 mModelView.scale(right - left, bottom - top, 1.0f);
667
Romain Guyd27977d2010-07-14 19:18:51 -0700668 useProgram(mDrawTextureProgram);
669 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700670
Romain Guya9794742010-07-13 11:37:54 -0700671 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guyd55a8612010-06-28 17:42:46 -0700672
Romain Guyd27977d2010-07-14 19:18:51 -0700673 // TODO: Only bind/set parameters when needed
Romain Guybd6b79b2010-06-26 00:13:53 -0700674 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guyd27977d2010-07-14 19:18:51 -0700675 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gTileModes[mShaderTileX]);
676 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gTileModes[mShaderTileY]);
Romain Guyc1396e92010-06-30 17:56:19 -0700677
Romain Guya9794742010-07-13 11:37:54 -0700678 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700679 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700680
Romain Guyd27977d2010-07-14 19:18:51 -0700681 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700682 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700683 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700684 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700685
Romain Guyf7f93552010-07-08 19:17:03 -0700686 if (!indices) {
687 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
688 } else {
689 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
690 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700691
Romain Guybd6b79b2010-06-26 00:13:53 -0700692 glBindTexture(GL_TEXTURE_2D, 0);
Romain Guy82ba8142010-07-09 13:25:56 -0700693}
694
695void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
696 // In theory we should not blend if the mode is Src, but it's rare enough
697 // that it's not worth it
698 blend = blend || mode != SkXfermode::kSrcOver_Mode;
699 if (blend) {
700 if (!mBlend) {
701 glEnable(GL_BLEND);
702 }
703
704 GLenum sourceMode = gBlends[mode].src;
705 GLenum destMode = gBlends[mode].dst;
706 if (!isPremultiplied && sourceMode == GL_ONE) {
707 sourceMode = GL_SRC_ALPHA;
708 }
709
710 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
711 glBlendFunc(sourceMode, destMode);
712 mLastSrcMode = sourceMode;
713 mLastDstMode = destMode;
714 }
715 } else if (mBlend) {
716 glDisable(GL_BLEND);
717 }
718 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700719}
720
Romain Guyd27977d2010-07-14 19:18:51 -0700721bool OpenGLRenderer::useProgram(const sp<Program>& program) {
722 if (!program->isInUse()) {
723 mCurrentProgram->remove();
724 program->use();
725 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700726 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700727 }
Romain Guy6926c722010-07-12 20:20:03 -0700728 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700729}
730
Romain Guy026c5e162010-06-28 17:12:22 -0700731void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700732 TextureVertex* v = &mDrawTextureVertices[0];
733 TextureVertex::setUV(v++, u1, v1);
734 TextureVertex::setUV(v++, u2, v1);
735 TextureVertex::setUV(v++, u1, v2);
736 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700737}
738
739void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
740 if (paint) {
741 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
742 if (!isMode) {
743 // Assume SRC_OVER
744 *mode = SkXfermode::kSrcOver_Mode;
745 }
746
747 // Skia draws using the color's alpha channel if < 255
748 // Otherwise, it uses the paint's alpha
749 int color = paint->getColor();
750 *alpha = (color >> 24) & 0xFF;
751 if (*alpha == 255) {
752 *alpha = paint->getAlpha();
753 }
754 } else {
755 *mode = SkXfermode::kSrcOver_Mode;
756 *alpha = 255;
757 }
Romain Guy026c5e162010-06-28 17:12:22 -0700758}
759
Romain Guy9d5316e2010-06-24 19:30:36 -0700760}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700761}; // namespace android