blob: 0bf0e8942a40b56cc7a9e4f6836fa644d18ac9b6 [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
Romain Guy1e793862010-07-16 15:07:42 -0700135 mLastTexture = 0;
136
Romain Guy026c5e162010-06-28 17:12:22 -0700137 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700138}
139
Romain Guy85bf02f2010-06-22 13:11:24 -0700140OpenGLRenderer::~OpenGLRenderer() {
141 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700142
143 mTextureCache.clear();
Romain Guydda570202010-07-06 11:39:32 -0700144 mLayerCache.clear();
Romain Guy6926c722010-07-12 20:20:03 -0700145 mPatchCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700146}
147
Romain Guyf6a11b82010-06-23 17:47:49 -0700148///////////////////////////////////////////////////////////////////////////////
149// Setup
150///////////////////////////////////////////////////////////////////////////////
151
Romain Guy85bf02f2010-06-22 13:11:24 -0700152void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700153 glViewport(0, 0, width, height);
154
Romain Guy260e1022010-07-12 14:41:06 -0700155 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700156
157 mWidth = width;
158 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700159 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700160}
161
Romain Guy85bf02f2010-06-22 13:11:24 -0700162void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700163 mSnapshot = &mFirstSnapshot;
164 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700165
Romain Guy08ae3172010-06-21 19:35:50 -0700166 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700167
Romain Guy08ae3172010-06-21 19:35:50 -0700168 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
169 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700170
Romain Guy08ae3172010-06-21 19:35:50 -0700171 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700172 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700173
Romain Guybb9524b2010-06-22 18:56:38 -0700174 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
175}
176
Romain Guyf6a11b82010-06-23 17:47:49 -0700177///////////////////////////////////////////////////////////////////////////////
178// State management
179///////////////////////////////////////////////////////////////////////////////
180
Romain Guybb9524b2010-06-22 18:56:38 -0700181int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700182 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700183}
184
185int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700186 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700187}
188
189void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700191
Romain Guy7ae7ac42010-06-25 13:46:18 -0700192 if (restoreSnapshot()) {
193 setScissorFromClip();
194 }
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
197void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700198 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700199
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700201
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 while (mSaveCount != saveCount - 1) {
203 restoreClip |= restoreSnapshot();
204 }
Romain Guybb9524b2010-06-22 18:56:38 -0700205
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 if (restoreClip) {
207 setScissorFromClip();
208 }
Romain Guybb9524b2010-06-22 18:56:38 -0700209}
210
211int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700212 mSnapshot = new Snapshot(mSnapshot);
213 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700214}
215
216bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700218 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700219 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700220
Romain Guybd6b79b2010-06-26 00:13:53 -0700221 sp<Snapshot> current = mSnapshot;
222 sp<Snapshot> previous = mSnapshot->previous;
223
Romain Guyf86ef572010-07-01 11:05:42 -0700224 if (restoreOrtho) {
Romain Guy260e1022010-07-12 14:41:06 -0700225 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700226 }
227
Romain Guybd6b79b2010-06-26 00:13:53 -0700228 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700229 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700230 }
231
232 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700233 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700234
Romain Guy7ae7ac42010-06-25 13:46:18 -0700235 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700236}
237
Romain Guyd55a8612010-06-28 17:42:46 -0700238void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda570202010-07-06 11:39:32 -0700239 if (!current->layer) {
240 LOGE("Attempting to compose a layer that does not exist");
241 return;
242 }
243
Romain Guyd55a8612010-06-28 17:42:46 -0700244 // Unbind current FBO and restore previous one
245 // Most of the time, previous->fbo will be 0 to bind the default buffer
246 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
247
248 // Restore the clip from the previous snapshot
Romain Guy079ba2c2010-07-16 14:12:24 -0700249 const Rect& clip = previous->clipRect;
Romain Guyd55a8612010-06-28 17:42:46 -0700250 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
251
Romain Guydda570202010-07-06 11:39:32 -0700252 Layer* layer = current->layer;
253
Romain Guyd55a8612010-06-28 17:42:46 -0700254 // Compute the correct texture coordinates for the FBO texture
255 // The texture is currently as big as the window but drawn with
256 // a quad of the appropriate size
Romain Guydda570202010-07-06 11:39:32 -0700257 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700258
Romain Guydda570202010-07-06 11:39:32 -0700259 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guya9794742010-07-13 11:37:54 -0700260 layer->texture, layer->alpha, layer->mode, layer->blend);
Romain Guyd55a8612010-06-28 17:42:46 -0700261
Romain Guydda570202010-07-06 11:39:32 -0700262 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700263 // Failing to add the layer to the cache should happen only if the
264 // layer is too large
Romain Guydda570202010-07-06 11:39:32 -0700265 if (!mLayerCache.put(size, layer)) {
266 LAYER_LOGD("Deleting layer");
267
268 glDeleteFramebuffers(1, &layer->fbo);
269 glDeleteTextures(1, &layer->texture);
270
271 delete layer;
272 }
Romain Guyd55a8612010-06-28 17:42:46 -0700273}
274
Romain Guyf6a11b82010-06-23 17:47:49 -0700275///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700276// Layers
277///////////////////////////////////////////////////////////////////////////////
278
279int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
280 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700281 int count = saveSnapshot();
282
283 int alpha = 255;
284 SkXfermode::Mode mode;
285
286 if (p) {
287 alpha = p->getAlpha();
288 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
289 if (!isMode) {
290 // Assume SRC_OVER
291 mode = SkXfermode::kSrcOver_Mode;
292 }
293 } else {
294 mode = SkXfermode::kSrcOver_Mode;
295 }
296
297 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
298
299 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700300}
301
302int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
303 int alpha, int flags) {
304 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700305 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
306 return count;
307}
Romain Guybd6b79b2010-06-26 00:13:53 -0700308
Romain Guyd55a8612010-06-28 17:42:46 -0700309bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
310 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700311
Romain Guyd27977d2010-07-14 19:18:51 -0700312 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guydda570202010-07-06 11:39:32 -0700313 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700314
Romain Guyf18fd992010-07-08 11:45:51 -0700315 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
316 LayerSize size(right - left, bottom - top);
317
Romain Guyf18fd992010-07-08 11:45:51 -0700318 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700319 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700320 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700321 }
322
Romain Guyf18fd992010-07-08 11:45:51 -0700323 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
324
Romain Guyf86ef572010-07-01 11:05:42 -0700325 // Clear the FBO
326 glDisable(GL_SCISSOR_TEST);
327 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
328 glClear(GL_COLOR_BUFFER_BIT);
329 glEnable(GL_SCISSOR_TEST);
330
Romain Guydda570202010-07-06 11:39:32 -0700331 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700332 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700333 layer->mode = mode;
334 layer->alpha = alpha / 255.0f;
335 layer->layer.set(left, top, right, bottom);
336
337 snapshot->layer = layer;
338 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700339
Romain Guyf86ef572010-07-01 11:05:42 -0700340 // Creates a new snapshot to draw into the FBO
341 saveSnapshot();
342 // TODO: This doesn't preserve other transformations (check Skia first)
343 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
Romain Guy079ba2c2010-07-16 14:12:24 -0700344 mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
Romain Guyf86ef572010-07-01 11:05:42 -0700345 mSnapshot->height = bottom - top;
346 setScissorFromClip();
347
Romain Guy079ba2c2010-07-16 14:12:24 -0700348 mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet;
Romain Guy260e1022010-07-12 14:41:06 -0700349 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700350
351 // Change the ortho projection
Romain Guy260e1022010-07-12 14:41:06 -0700352 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700353
Romain Guyd55a8612010-06-28 17:42:46 -0700354 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700355}
356
357///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700358// Transforms
359///////////////////////////////////////////////////////////////////////////////
360
361void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700362 mSnapshot->transform.translate(dx, dy, 0.0f);
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);
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700370 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 mat4 m(*matrix);
383 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700384}
385
386///////////////////////////////////////////////////////////////////////////////
387// Clipping
388///////////////////////////////////////////////////////////////////////////////
389
Romain Guybb9524b2010-06-22 18:56:38 -0700390void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700391 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700392 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700393}
394
395const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700396 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700397}
398
Romain Guyc7d53492010-06-25 13:41:57 -0700399bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyc7d53492010-06-25 13:41:57 -0700400 Rect r(left, top, right, bottom);
Romain Guy079ba2c2010-07-16 14:12:24 -0700401 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700402 return !mSnapshot->clipRect.intersects(r);
403}
404
Romain Guy079ba2c2010-07-16 14:12:24 -0700405bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
406 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700407 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700408 setScissorFromClip();
409 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700410 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700411}
412
Romain Guyf6a11b82010-06-23 17:47:49 -0700413///////////////////////////////////////////////////////////////////////////////
414// Drawing
415///////////////////////////////////////////////////////////////////////////////
416
Romain Guyc1396e92010-06-30 17:56:19 -0700417void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700418 const float right = left + bitmap->width();
419 const float bottom = top + bitmap->height();
420
421 if (quickReject(left, top, right, bottom)) {
422 return;
423 }
424
Romain Guyf86ef572010-07-01 11:05:42 -0700425 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy6926c722010-07-12 20:20:03 -0700426 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700427}
428
Romain Guyf86ef572010-07-01 11:05:42 -0700429void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
430 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
431 const mat4 transform(*matrix);
432 transform.mapRect(r);
433
Romain Guy6926c722010-07-12 20:20:03 -0700434 if (quickReject(r.left, r.top, r.right, r.bottom)) {
435 return;
436 }
437
Romain Guyf86ef572010-07-01 11:05:42 -0700438 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy82ba8142010-07-09 13:25:56 -0700439 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700440}
441
Romain Guy8ba548f2010-06-30 19:21:21 -0700442void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
443 float srcLeft, float srcTop, float srcRight, float srcBottom,
444 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700445 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700446 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
447 return;
448 }
449
Romain Guyf86ef572010-07-01 11:05:42 -0700450 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700451
Romain Guy8ba548f2010-06-30 19:21:21 -0700452 const float width = texture->width;
453 const float height = texture->height;
454
455 const float u1 = srcLeft / width;
456 const float v1 = srcTop / height;
457 const float u2 = srcRight / width;
458 const float v2 = srcBottom / height;
459
460 resetDrawTextureTexCoords(u1, v1, u2, v2);
461
Romain Guy82ba8142010-07-09 13:25:56 -0700462 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700463
464 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
465}
466
Romain Guydeba7852010-07-07 17:54:48 -0700467void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
468 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700469 if (quickReject(left, top, right, bottom)) {
470 return;
471 }
472
Romain Guyf7f93552010-07-08 19:17:03 -0700473 const Texture* texture = mTextureCache.get(bitmap);
474
475 int alpha;
476 SkXfermode::Mode mode;
477 getAlphaAndMode(paint, &alpha, &mode);
478
Romain Guyf7f93552010-07-08 19:17:03 -0700479 Patch* mesh = mPatchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700480 mesh->updateVertices(bitmap, left, top, right, bottom,
481 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700482
483 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
484 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700485 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
486 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700487 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700488}
489
Romain Guy85bf02f2010-06-22 13:11:24 -0700490void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700491 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700492 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700493}
Romain Guy9d5316e2010-06-24 19:30:36 -0700494
Romain Guybd6b79b2010-06-26 00:13:53 -0700495void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700496 if (quickReject(left, top, right, bottom)) {
497 return;
498 }
499
Romain Guy026c5e162010-06-28 17:12:22 -0700500 SkXfermode::Mode mode;
501
502 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
503 if (!isMode) {
504 // Assume SRC_OVER
505 mode = SkXfermode::kSrcOver_Mode;
506 }
507
508 // Skia draws using the color's alpha channel if < 255
509 // Otherwise, it uses the paint's alpha
510 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700511 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700512 color |= p->getAlpha() << 24;
513 }
514
515 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700516}
Romain Guy9d5316e2010-06-24 19:30:36 -0700517
Romain Guy6926c722010-07-12 20:20:03 -0700518///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700519// Shaders
520///////////////////////////////////////////////////////////////////////////////
521
522void OpenGLRenderer::resetShader() {
523 mShader = OpenGLRenderer::kShaderNone;
524 mShaderBlend = false;
525 mShaderTileX = SkShader::kClamp_TileMode;
526 mShaderTileY = SkShader::kClamp_TileMode;
527}
528
529void OpenGLRenderer::setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX,
530 SkShader::TileMode tileY, SkMatrix* matrix, bool hasAlpha) {
531 mShader = kShaderBitmap;
532 mShaderBlend = hasAlpha;
533 mShaderBitmap = bitmap;
534 mShaderTileX = tileX;
535 mShaderTileY = tileY;
536 mShaderMatrix = matrix;
537}
538
539///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700540// Drawing implementation
541///////////////////////////////////////////////////////////////////////////////
542
Romain Guy026c5e162010-06-28 17:12:22 -0700543void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700544 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700545 // If a shader is set, preserve only the alpha
546 if (mShader != kShaderNone) {
547 color |= 0x00ffffff;
548 }
549
550 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700551 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700552 const GLfloat a = alpha / 255.0f;
553 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
554 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
555 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
556
557 switch (mShader) {
558 case kShaderBitmap:
559 drawBitmapShader(left, top, right, bottom, a, mode);
560 return;
561 default:
562 break;
563 }
Romain Guy026c5e162010-06-28 17:12:22 -0700564
Romain Guy6926c722010-07-12 20:20:03 -0700565 // Pre-multiplication happens when setting the shader color
Romain Guyd27977d2010-07-14 19:18:51 -0700566 chooseBlending(alpha < 255 || mShaderBlend, mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700567
Romain Guyc7d53492010-06-25 13:41:57 -0700568 mModelView.loadTranslate(left, top, 0.0f);
569 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700570
Romain Guy079ba2c2010-07-16 14:12:24 -0700571 if (!useProgram(mDrawColorProgram)) {
Romain Guy6926c722010-07-12 20:20:03 -0700572 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy079ba2c2010-07-16 14:12:24 -0700573 glVertexAttribPointer(mDrawColorProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy6926c722010-07-12 20:20:03 -0700574 gDrawColorVertexStride, p);
575 }
Romain Guyd27977d2010-07-14 19:18:51 -0700576
577 if (!ignoreTransform) {
Romain Guy079ba2c2010-07-16 14:12:24 -0700578 mDrawColorProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700579 } else {
580 mat4 identity;
Romain Guy079ba2c2010-07-16 14:12:24 -0700581 mDrawColorProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700582 }
583
Romain Guy079ba2c2010-07-16 14:12:24 -0700584 glUniform4f(mDrawColorProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700585
Romain Guyc7d53492010-06-25 13:41:57 -0700586 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy82ba8142010-07-09 13:25:56 -0700587}
Romain Guy026c5e162010-06-28 17:12:22 -0700588
Romain Guyd27977d2010-07-14 19:18:51 -0700589void OpenGLRenderer::drawBitmapShader(float left, float top, float right, float bottom,
590 float alpha, SkXfermode::Mode mode) {
591 const Texture* texture = mTextureCache.get(mShaderBitmap);
592
593 const float width = texture->width;
594 const float height = texture->height;
595
596 // This could be done in the vertex shader but we have only 4 vertices
597 float u1 = 0.0f;
598 float v1 = 0.0f;
599 float u2 = right - left;
600 float v2 = bottom - top;
601
602 if (mShaderMatrix) {
603 SkMatrix inverse;
604 mShaderMatrix->invert(&inverse);
605 mat4 m(inverse);
606 Rect r(u1, v1, u2, v2);
607 m.mapRect(r);
608
609 u1 = r.left;
610 u2 = r.right;
611 v1 = r.top;
612 v2 = r.bottom;
613 }
614
615 u1 /= width;
616 u2 /= width;
617 v1 /= height;
618 v2 /= height;
619
620 resetDrawTextureTexCoords(u1, v1, u2, v2);
621
622 drawTextureMesh(left, top, right, bottom, texture->id, alpha, mode, texture->blend,
623 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
624
625 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
626}
627
Romain Guy82ba8142010-07-09 13:25:56 -0700628void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700629 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700630 int alpha;
631 SkXfermode::Mode mode;
632 getAlphaAndMode(paint, &alpha, &mode);
633
Romain Guya9794742010-07-13 11:37:54 -0700634 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
635 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700636}
637
Romain Guybd6b79b2010-06-26 00:13:53 -0700638void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700639 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
640 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700641 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
642}
643
644void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700645 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700646 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700647 mModelView.loadTranslate(left, top, 0.0f);
648 mModelView.scale(right - left, bottom - top, 1.0f);
649
Romain Guyd27977d2010-07-14 19:18:51 -0700650 useProgram(mDrawTextureProgram);
651 mDrawTextureProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700652
Romain Guya9794742010-07-13 11:37:54 -0700653 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guyd55a8612010-06-28 17:42:46 -0700654
Romain Guy1e793862010-07-16 15:07:42 -0700655 if (texture != mLastTexture) {
656 glBindTexture(GL_TEXTURE_2D, texture);
657 mLastTexture = texture;
658 }
659 // TODO: Don't set the texture parameters every time
Romain Guyd27977d2010-07-14 19:18:51 -0700660 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, gTileModes[mShaderTileX]);
661 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, gTileModes[mShaderTileY]);
Romain Guyc1396e92010-06-30 17:56:19 -0700662
Romain Guya9794742010-07-13 11:37:54 -0700663 // Always premultiplied
Romain Guyd27977d2010-07-14 19:18:51 -0700664 glUniform4f(mDrawTextureProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700665
Romain Guyd27977d2010-07-14 19:18:51 -0700666 glVertexAttribPointer(mDrawTextureProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700667 gDrawTextureVertexStride, vertices);
Romain Guyd27977d2010-07-14 19:18:51 -0700668 glVertexAttribPointer(mDrawTextureProgram->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700669 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700670
Romain Guyf7f93552010-07-08 19:17:03 -0700671 if (!indices) {
672 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
673 } else {
674 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
675 }
Romain Guy82ba8142010-07-09 13:25:56 -0700676}
677
678void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
679 // In theory we should not blend if the mode is Src, but it's rare enough
680 // that it's not worth it
681 blend = blend || mode != SkXfermode::kSrcOver_Mode;
682 if (blend) {
683 if (!mBlend) {
684 glEnable(GL_BLEND);
685 }
686
687 GLenum sourceMode = gBlends[mode].src;
688 GLenum destMode = gBlends[mode].dst;
689 if (!isPremultiplied && sourceMode == GL_ONE) {
690 sourceMode = GL_SRC_ALPHA;
691 }
692
693 if (sourceMode != mLastSrcMode || destMode != mLastDstMode) {
694 glBlendFunc(sourceMode, destMode);
695 mLastSrcMode = sourceMode;
696 mLastDstMode = destMode;
697 }
698 } else if (mBlend) {
699 glDisable(GL_BLEND);
700 }
701 mBlend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700702}
703
Romain Guyd27977d2010-07-14 19:18:51 -0700704bool OpenGLRenderer::useProgram(const sp<Program>& program) {
705 if (!program->isInUse()) {
706 mCurrentProgram->remove();
707 program->use();
708 mCurrentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700709 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700710 }
Romain Guy6926c722010-07-12 20:20:03 -0700711 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700712}
713
Romain Guy026c5e162010-06-28 17:12:22 -0700714void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guy82ba8142010-07-09 13:25:56 -0700715 TextureVertex* v = &mDrawTextureVertices[0];
716 TextureVertex::setUV(v++, u1, v1);
717 TextureVertex::setUV(v++, u2, v1);
718 TextureVertex::setUV(v++, u1, v2);
719 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700720}
721
722void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
723 if (paint) {
724 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
725 if (!isMode) {
726 // Assume SRC_OVER
727 *mode = SkXfermode::kSrcOver_Mode;
728 }
729
730 // Skia draws using the color's alpha channel if < 255
731 // Otherwise, it uses the paint's alpha
732 int color = paint->getColor();
733 *alpha = (color >> 24) & 0xFF;
734 if (*alpha == 255) {
735 *alpha = paint->getAlpha();
736 }
737 } else {
738 *mode = SkXfermode::kSrcOver_Mode;
739 *alpha = 255;
740 }
Romain Guy026c5e162010-06-28 17:12:22 -0700741}
742
Romain Guy9d5316e2010-06-24 19:30:36 -0700743}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700744}; // namespace android