blob: 9f2cc24035054f1b61b4a6de9c6feffe048a39dd [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 Guydda57022010-07-06 11:39:32 -070037// Debug
38#define DEBUG_LAYERS 0
39
Romain Guy121e2242010-07-01 18:26:52 -070040// These properties are defined in mega-bytes
41#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
42#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
43
Romain Guydda57022010-07-06 11:39:32 -070044#define DEFAULT_TEXTURE_CACHE_SIZE 20
45#define DEFAULT_LAYER_CACHE_SIZE 10
46
Romain Guy121e2242010-07-01 18:26:52 -070047// Converts a number of mega-bytes into bytes
48#define MB(s) s * 1024 * 1024
49
Romain Guydda57022010-07-06 11:39:32 -070050// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070051#define SV(x, y) { { x, y } }
52#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070053
Romain Guydda57022010-07-06 11:39:32 -070054// Debug
55#ifdef DEBUG_LAYERS
56 #define LAYER_LOGD(...) LOGD(__VA_ARGS__)
57#else
58 #define LAYER_LOGD(...)
59#endif
60
Romain Guy9d5316e2010-06-24 19:30:36 -070061///////////////////////////////////////////////////////////////////////////////
62// Globals
63///////////////////////////////////////////////////////////////////////////////
64
Romain Guy026c5e162010-06-28 17:12:22 -070065static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070066 SV(0.0f, 0.0f),
67 SV(1.0f, 0.0f),
68 SV(0.0f, 1.0f),
69 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070070};
Romain Guy026c5e162010-06-28 17:12:22 -070071static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
72static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070073
Romain Guy026c5e162010-06-28 17:12:22 -070074// This array is never used directly but used as a memcpy source in the
75// OpenGLRenderer constructor
76static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070077 FV(0.0f, 0.0f, 0.0f, 0.0f),
78 FV(1.0f, 0.0f, 1.0f, 0.0f),
79 FV(0.0f, 1.0f, 0.0f, 1.0f),
80 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070081};
Romain Guy026c5e162010-06-28 17:12:22 -070082static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
83static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070084
Romain Guy026c5e162010-06-28 17:12:22 -070085// In this array, the index of each Blender equals the value of the first
86// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
87static const Blender gBlends[] = {
88 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
89 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
90 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
94 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
100};
Romain Guye4d01122010-06-16 18:44:05 -0700101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guydda57022010-07-06 11:39:32 -0700106OpenGLRenderer::OpenGLRenderer():
107 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
108 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700109 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700110
Romain Guy121e2242010-07-01 18:26:52 -0700111 char property[PROPERTY_VALUE_MAX];
112 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700113 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e2242010-07-01 18:26:52 -0700114 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda57022010-07-06 11:39:32 -0700115 } else {
116 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE);
117 }
118
119 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
120 LOGD(" Setting layer cache size to %sMB", property);
121 mLayerCache.setMaxSize(MB(atoi(property)));
122 } else {
123 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700124 }
125
Romain Guy9d5316e2010-06-24 19:30:36 -0700126 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -0700127 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700128
129 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700130}
131
Romain Guy85bf02f2010-06-22 13:11:24 -0700132OpenGLRenderer::~OpenGLRenderer() {
133 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700134
135 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700136 mLayerCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700137}
138
Romain Guyf6a11b82010-06-23 17:47:49 -0700139///////////////////////////////////////////////////////////////////////////////
140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy85bf02f2010-06-22 13:11:24 -0700143void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700144 glViewport(0, 0, width, height);
145
146 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700147 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700148 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700149
150 mWidth = width;
151 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700152 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700153}
154
Romain Guy85bf02f2010-06-22 13:11:24 -0700155void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700156 mSnapshot = &mFirstSnapshot;
157 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700158
Romain Guy08ae3172010-06-21 19:35:50 -0700159 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700160
Romain Guy08ae3172010-06-21 19:35:50 -0700161 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
162 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700163
Romain Guy08ae3172010-06-21 19:35:50 -0700164 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700165 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700166
Romain Guybb9524b2010-06-22 18:56:38 -0700167 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
168}
169
Romain Guyf6a11b82010-06-23 17:47:49 -0700170///////////////////////////////////////////////////////////////////////////////
171// State management
172///////////////////////////////////////////////////////////////////////////////
173
Romain Guybb9524b2010-06-22 18:56:38 -0700174int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700176}
177
178int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700179 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700180}
181
182void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700183 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 if (restoreSnapshot()) {
186 setScissorFromClip();
187 }
Romain Guybb9524b2010-06-22 18:56:38 -0700188}
189
190void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700191 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700192
Romain Guy7ae7ac42010-06-25 13:46:18 -0700193 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700194
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 while (mSaveCount != saveCount - 1) {
196 restoreClip |= restoreSnapshot();
197 }
Romain Guybb9524b2010-06-22 18:56:38 -0700198
Romain Guy7ae7ac42010-06-25 13:46:18 -0700199 if (restoreClip) {
200 setScissorFromClip();
201 }
Romain Guybb9524b2010-06-22 18:56:38 -0700202}
203
204int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700205 mSnapshot = new Snapshot(mSnapshot);
206 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700207}
208
209bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700210 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700211 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700212 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700213
Romain Guybd6b79b2010-06-26 00:13:53 -0700214 sp<Snapshot> current = mSnapshot;
215 sp<Snapshot> previous = mSnapshot->previous;
216
Romain Guyf86ef572010-07-01 11:05:42 -0700217 if (restoreOrtho) {
218 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
219 }
220
Romain Guybd6b79b2010-06-26 00:13:53 -0700221 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700222 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700223 }
224
225 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700226 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700227
Romain Guy7ae7ac42010-06-25 13:46:18 -0700228 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700229}
230
Romain Guyd55a8612010-06-28 17:42:46 -0700231void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700232 if (!current->layer) {
233 LOGE("Attempting to compose a layer that does not exist");
234 return;
235 }
236
Romain Guyd55a8612010-06-28 17:42:46 -0700237 // Unbind current FBO and restore previous one
238 // Most of the time, previous->fbo will be 0 to bind the default buffer
239 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
240
241 // Restore the clip from the previous snapshot
242 const Rect& clip = previous->getMappedClip();
243 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
244
Romain Guydda57022010-07-06 11:39:32 -0700245 Layer* layer = current->layer;
246
Romain Guyd55a8612010-06-28 17:42:46 -0700247 // Compute the correct texture coordinates for the FBO texture
248 // The texture is currently as big as the window but drawn with
249 // a quad of the appropriate size
Romain Guydda57022010-07-06 11:39:32 -0700250 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700251
Romain Guydda57022010-07-06 11:39:32 -0700252 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
253 layer->texture, layer->alpha, layer->mode, layer->blend, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700254
Romain Guydda57022010-07-06 11:39:32 -0700255 LayerSize size(rect.getWidth(), rect.getHeight());
256 if (!mLayerCache.put(size, layer)) {
257 LAYER_LOGD("Deleting layer");
258
259 glDeleteFramebuffers(1, &layer->fbo);
260 glDeleteTextures(1, &layer->texture);
261
262 delete layer;
263 }
Romain Guyd55a8612010-06-28 17:42:46 -0700264}
265
Romain Guyf6a11b82010-06-23 17:47:49 -0700266///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700267// Layers
268///////////////////////////////////////////////////////////////////////////////
269
270int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
271 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700272 int count = saveSnapshot();
273
274 int alpha = 255;
275 SkXfermode::Mode mode;
276
277 if (p) {
278 alpha = p->getAlpha();
279 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
280 if (!isMode) {
281 // Assume SRC_OVER
282 mode = SkXfermode::kSrcOver_Mode;
283 }
284 } else {
285 mode = SkXfermode::kSrcOver_Mode;
286 }
287
288 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
289
290 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700291}
292
293int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
294 int alpha, int flags) {
295 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700296 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
297 return count;
298}
Romain Guybd6b79b2010-06-26 00:13:53 -0700299
Romain Guyd55a8612010-06-28 17:42:46 -0700300bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
301 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700302
Romain Guydda57022010-07-06 11:39:32 -0700303 LayerSize size(right - left, bottom - top);
304 Layer* layer = mLayerCache.get(size);
Romain Guybd6b79b2010-06-26 00:13:53 -0700305
Romain Guydda57022010-07-06 11:39:32 -0700306 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height);
307 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700308
Romain Guydda57022010-07-06 11:39:32 -0700309 if (!layer) {
310 LAYER_LOGD("Creating new layer");
Romain Guybd6b79b2010-06-26 00:13:53 -0700311
Romain Guydda57022010-07-06 11:39:32 -0700312 layer = new Layer;
313 layer->blend = true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700314
Romain Guydda57022010-07-06 11:39:32 -0700315 // Generate the FBO and attach the texture
316 glGenFramebuffers(1, &layer->fbo);
317 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guybd6b79b2010-06-26 00:13:53 -0700318
Romain Guydda57022010-07-06 11:39:32 -0700319 // Generate the texture in which the FBO will draw
320 glGenTextures(1, &layer->texture);
321 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guybd6b79b2010-06-26 00:13:53 -0700322
Romain Guydda57022010-07-06 11:39:32 -0700323 // The FBO will not be scaled, so we can use lower quality filtering
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guybd6b79b2010-06-26 00:13:53 -0700326
Romain Guydda57022010-07-06 11:39:32 -0700327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700329
Romain Guydda57022010-07-06 11:39:32 -0700330 // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
Romain Guyd55a8612010-06-28 17:42:46 -0700331
Romain Guydda57022010-07-06 11:39:32 -0700332 const GLsizei width = right - left;
333 const GLsizei height = bottom - top;
334
335 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
336 glBindTexture(GL_TEXTURE_2D, 0);
337
338 // Bind texture to FBO
339 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
340 layer->texture, 0);
341
342 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
343 if (status != GL_FRAMEBUFFER_COMPLETE) {
344 LOGD("Framebuffer incomplete (GL error code 0x%x)", status);
345
346 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
347 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
348
349 glDeleteFramebuffers(1, &layer->fbo);
350 glDeleteTextures(1, &layer->texture);
351 delete layer;
352
353 return false;
354 }
355 } else {
356 LAYER_LOGD("Reusing layer");
357 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
Romain Guybd6b79b2010-06-26 00:13:53 -0700358 }
359
Romain Guyf86ef572010-07-01 11:05:42 -0700360 // Clear the FBO
361 glDisable(GL_SCISSOR_TEST);
362 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
363 glClear(GL_COLOR_BUFFER_BIT);
364 glEnable(GL_SCISSOR_TEST);
365
Romain Guydda57022010-07-06 11:39:32 -0700366 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700367 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700368 layer->mode = mode;
369 layer->alpha = alpha / 255.0f;
370 layer->layer.set(left, top, right, bottom);
371
372 snapshot->layer = layer;
373 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700374
Romain Guyf86ef572010-07-01 11:05:42 -0700375 // Creates a new snapshot to draw into the FBO
376 saveSnapshot();
377 // TODO: This doesn't preserve other transformations (check Skia first)
378 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
379 mSnapshot->clipRect.set(left, top, right, bottom);
380 mSnapshot->height = bottom - top;
381 setScissorFromClip();
382
383 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
384 Snapshot::kFlagClipSet;
385 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
386
387 // Change the ortho projection
388 mat4 ortho;
389 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
390 ortho.copyTo(mOrthoMatrix);
391
Romain Guyd55a8612010-06-28 17:42:46 -0700392 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700393}
394
395///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700396// Transforms
397///////////////////////////////////////////////////////////////////////////////
398
399void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700400 mSnapshot->transform.translate(dx, dy, 0.0f);
401 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700402}
403
404void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700405 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
406 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700407}
408
409void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700410 mSnapshot->transform.scale(sx, sy, 1.0f);
411 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700412}
413
414void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700415 mSnapshot->transform.load(*matrix);
416 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700417}
418
419void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700420 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700421}
422
423void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700424 mat4 m(*matrix);
425 mSnapshot->transform.multiply(m);
426 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700427}
428
429///////////////////////////////////////////////////////////////////////////////
430// Clipping
431///////////////////////////////////////////////////////////////////////////////
432
Romain Guybb9524b2010-06-22 18:56:38 -0700433void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700434 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700435 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700436}
437
438const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700439 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700440}
441
Romain Guyc7d53492010-06-25 13:41:57 -0700442bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
443 /*
444 * The documentation of quickReject() indicates that the specified rect
445 * is transformed before being compared to the clip rect. However, the
446 * clip rect is not stored transformed in the snapshot and can thus be
447 * compared directly
448 *
449 * The following code can be used instead to performed a mapped comparison:
450 *
451 * mSnapshot->transform.mapRect(r);
452 * const Rect& clip = mSnapshot->getMappedClip();
453 * return !clip.intersects(r);
454 */
Romain Guyc7d53492010-06-25 13:41:57 -0700455 Rect r(left, top, right, bottom);
456 return !mSnapshot->clipRect.intersects(r);
457}
458
Romain Guybb9524b2010-06-22 18:56:38 -0700459bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700460 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
461 if (clipped) {
462 mSnapshot->flags |= Snapshot::kFlagClipSet;
463 setScissorFromClip();
464 }
465 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700466}
467
Romain Guyf6a11b82010-06-23 17:47:49 -0700468///////////////////////////////////////////////////////////////////////////////
469// Drawing
470///////////////////////////////////////////////////////////////////////////////
471
Romain Guyc1396e92010-06-30 17:56:19 -0700472void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700473 const Texture* texture = mTextureCache.get(bitmap);
Romain Guyc1396e92010-06-30 17:56:19 -0700474
Romain Guyc1396e92010-06-30 17:56:19 -0700475 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700476 SkXfermode::Mode mode;
477 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700478
479 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
480 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700481}
482
Romain Guyf86ef572010-07-01 11:05:42 -0700483void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
484 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
485 const mat4 transform(*matrix);
486 transform.mapRect(r);
487
488 const Texture* texture = mTextureCache.get(bitmap);
489
490 int alpha;
491 SkXfermode::Mode mode;
492 getAlphaAndMode(paint, &alpha, &mode);
493
494 drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
495 alpha / 255.0f, mode, texture->blend, true);
496}
497
Romain Guy8ba548f2010-06-30 19:21:21 -0700498void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
499 float srcLeft, float srcTop, float srcRight, float srcBottom,
500 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700501 const SkPaint* paint) {
502 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700503
504 int alpha;
505 SkXfermode::Mode mode;
506 getAlphaAndMode(paint, &alpha, &mode);
507
508 const float width = texture->width;
509 const float height = texture->height;
510
511 const float u1 = srcLeft / width;
512 const float v1 = srcTop / height;
513 const float u2 = srcRight / width;
514 const float v2 = srcBottom / height;
515
516 resetDrawTextureTexCoords(u1, v1, u2, v2);
517
Romain Guy8ba548f2010-06-30 19:21:21 -0700518 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
519 alpha / 255.0f, mode, texture->blend, true);
520
521 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
522}
523
Romain Guy85bf02f2010-06-22 13:11:24 -0700524void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700525 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700526 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700527}
Romain Guy9d5316e2010-06-24 19:30:36 -0700528
Romain Guybd6b79b2010-06-26 00:13:53 -0700529void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700530 SkXfermode::Mode mode;
531
532 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
533 if (!isMode) {
534 // Assume SRC_OVER
535 mode = SkXfermode::kSrcOver_Mode;
536 }
537
538 // Skia draws using the color's alpha channel if < 255
539 // Otherwise, it uses the paint's alpha
540 int color = p->getColor();
541 if (((color >> 24) & 0xFF) == 255) {
542 color |= p->getAlpha() << 24;
543 }
544
545 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700546}
Romain Guy9d5316e2010-06-24 19:30:36 -0700547
Romain Guy026c5e162010-06-28 17:12:22 -0700548void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
549 int color, SkXfermode::Mode mode) {
550 const int alpha = (color >> 24) & 0xFF;
551 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
552
553 const GLfloat a = alpha / 255.0f;
554 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
555 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
556 const GLfloat b = ((color ) & 0xFF) / 255.0f;
557
558 if (blend) {
559 glEnable(GL_BLEND);
560 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
561 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700562
Romain Guyc7d53492010-06-25 13:41:57 -0700563 mModelView.loadTranslate(left, top, 0.0f);
564 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700565
Romain Guyc7d53492010-06-25 13:41:57 -0700566 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700567
Romain Guyc7d53492010-06-25 13:41:57 -0700568 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700569
Romain Guyc7d53492010-06-25 13:41:57 -0700570 glEnableVertexAttribArray(mDrawColorShader->position);
571 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
572 gDrawColorVertexStride, p);
573 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700574
Romain Guyc7d53492010-06-25 13:41:57 -0700575 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700576
Romain Guyc7d53492010-06-25 13:41:57 -0700577 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700578
579 if (blend) {
580 glDisable(GL_BLEND);
581 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700582}
583
Romain Guybd6b79b2010-06-26 00:13:53 -0700584void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700585 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700586 mModelView.loadTranslate(left, top, 0.0f);
587 mModelView.scale(right - left, bottom - top, 1.0f);
588
589 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
590
Romain Guyc1396e92010-06-30 17:56:19 -0700591 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
592 GLenum sourceMode = gBlends[mode].src;
593 if (!isPremultiplied && sourceMode == GL_ONE) {
594 sourceMode = GL_SRC_ALPHA;
595 }
596
597 glEnable(GL_BLEND);
598 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700599 }
600
Romain Guybd6b79b2010-06-26 00:13:53 -0700601 glBindTexture(GL_TEXTURE_2D, texture);
602
Romain Guyc1396e92010-06-30 17:56:19 -0700603 // TODO handle tiling and filtering here
604
Romain Guybd6b79b2010-06-26 00:13:53 -0700605 glActiveTexture(GL_TEXTURE0);
606 glUniform1i(mDrawTextureShader->sampler, 0);
607
Romain Guy026c5e162010-06-28 17:12:22 -0700608 const GLvoid* p = &mDrawTextureVertices[0].position[0];
609 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700610
611 glEnableVertexAttribArray(mDrawTextureShader->position);
612 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
613 gDrawTextureVertexStride, p);
614
615 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
616 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
617 gDrawTextureVertexStride, t);
618
619 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
620
621 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
622
623 glDisableVertexAttribArray(mDrawTextureShader->position);
624 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
625
626 glBindTexture(GL_TEXTURE_2D, 0);
627 glDisable(GL_BLEND);
628}
629
Romain Guy026c5e162010-06-28 17:12:22 -0700630void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
631 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700632 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700633 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700634 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700635 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700636 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700637 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700638 mDrawTextureVertices[3].texture[1] = v2;
639}
640
641void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
642 if (paint) {
643 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
644 if (!isMode) {
645 // Assume SRC_OVER
646 *mode = SkXfermode::kSrcOver_Mode;
647 }
648
649 // Skia draws using the color's alpha channel if < 255
650 // Otherwise, it uses the paint's alpha
651 int color = paint->getColor();
652 *alpha = (color >> 24) & 0xFF;
653 if (*alpha == 255) {
654 *alpha = paint->getAlpha();
655 }
656 } else {
657 *mode = SkXfermode::kSrcOver_Mode;
658 *alpha = 255;
659 }
Romain Guy026c5e162010-06-28 17:12:22 -0700660}
661
Romain Guy9d5316e2010-06-24 19:30:36 -0700662}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700663}; // namespace android