blob: 027ed79118109ae6ba7163bc419a25ae9a6a19ae [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
24
Romain Guy121e2242010-07-01 18:26:52 -070025#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy121e2242010-07-01 18:26:52 -070037// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
40
Romain Guydda57022010-07-06 11:39:32 -070041#define DEFAULT_TEXTURE_CACHE_SIZE 20
42#define DEFAULT_LAYER_CACHE_SIZE 10
43
Romain Guy121e2242010-07-01 18:26:52 -070044// Converts a number of mega-bytes into bytes
45#define MB(s) s * 1024 * 1024
46
Romain Guydda57022010-07-06 11:39:32 -070047// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070048#define SV(x, y) { { x, y } }
49#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070050
51///////////////////////////////////////////////////////////////////////////////
52// Globals
53///////////////////////////////////////////////////////////////////////////////
54
Romain Guy026c5e162010-06-28 17:12:22 -070055static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070056 SV(0.0f, 0.0f),
57 SV(1.0f, 0.0f),
58 SV(0.0f, 1.0f),
59 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070060};
Romain Guy026c5e162010-06-28 17:12:22 -070061static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
62static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070063
Romain Guy026c5e162010-06-28 17:12:22 -070064// This array is never used directly but used as a memcpy source in the
65// OpenGLRenderer constructor
66static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070067 FV(0.0f, 0.0f, 0.0f, 0.0f),
68 FV(1.0f, 0.0f, 1.0f, 0.0f),
69 FV(0.0f, 1.0f, 0.0f, 1.0f),
70 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070071};
Romain Guy026c5e162010-06-28 17:12:22 -070072static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
73static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070074
Romain Guy026c5e162010-06-28 17:12:22 -070075// In this array, the index of each Blender equals the value of the first
76// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
77static const Blender gBlends[] = {
78 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
79 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
80 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
81 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
83 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
84 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
85 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
86 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
89 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
90};
Romain Guye4d01122010-06-16 18:44:05 -070091
Romain Guyf6a11b82010-06-23 17:47:49 -070092///////////////////////////////////////////////////////////////////////////////
93// Constructors/destructor
94///////////////////////////////////////////////////////////////////////////////
95
Romain Guydda57022010-07-06 11:39:32 -070096OpenGLRenderer::OpenGLRenderer():
97 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
98 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)) {
Romain Guy85bf02f2010-06-22 13:11:24 -070099 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700100
Romain Guy121e2242010-07-01 18:26:52 -0700101 char property[PROPERTY_VALUE_MAX];
102 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700103 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e2242010-07-01 18:26:52 -0700104 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda57022010-07-06 11:39:32 -0700105 } else {
106 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE);
107 }
108
109 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
110 LOGD(" Setting layer cache size to %sMB", property);
111 mLayerCache.setMaxSize(MB(atoi(property)));
112 } else {
113 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700114 }
115
Romain Guy9d5316e2010-06-24 19:30:36 -0700116 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -0700117 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
119 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
123 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700124
125 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700126 mLayerCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guyf6a11b82010-06-23 17:47:49 -0700129///////////////////////////////////////////////////////////////////////////////
130// Setup
131///////////////////////////////////////////////////////////////////////////////
132
Romain Guy85bf02f2010-06-22 13:11:24 -0700133void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700134 glViewport(0, 0, width, height);
135
136 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700137 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700138 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700139
140 mWidth = width;
141 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700142 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700143}
144
Romain Guy85bf02f2010-06-22 13:11:24 -0700145void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700146 mSnapshot = &mFirstSnapshot;
147 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700148
Romain Guy08ae3172010-06-21 19:35:50 -0700149 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700150
Romain Guy08ae3172010-06-21 19:35:50 -0700151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
Romain Guy08ae3172010-06-21 19:35:50 -0700154 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700155 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700156
Romain Guybb9524b2010-06-22 18:56:38 -0700157 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
158}
159
Romain Guyf6a11b82010-06-23 17:47:49 -0700160///////////////////////////////////////////////////////////////////////////////
161// State management
162///////////////////////////////////////////////////////////////////////////////
163
Romain Guybb9524b2010-06-22 18:56:38 -0700164int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700165 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700166}
167
168int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700169 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700170}
171
172void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700173 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700174
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 if (restoreSnapshot()) {
176 setScissorFromClip();
177 }
Romain Guybb9524b2010-06-22 18:56:38 -0700178}
179
180void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700181 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700182
Romain Guy7ae7ac42010-06-25 13:46:18 -0700183 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 while (mSaveCount != saveCount - 1) {
186 restoreClip |= restoreSnapshot();
187 }
Romain Guybb9524b2010-06-22 18:56:38 -0700188
Romain Guy7ae7ac42010-06-25 13:46:18 -0700189 if (restoreClip) {
190 setScissorFromClip();
191 }
Romain Guybb9524b2010-06-22 18:56:38 -0700192}
193
194int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700195 mSnapshot = new Snapshot(mSnapshot);
196 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700197}
198
199bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700200 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700201 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700202 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700203
Romain Guybd6b79b2010-06-26 00:13:53 -0700204 sp<Snapshot> current = mSnapshot;
205 sp<Snapshot> previous = mSnapshot->previous;
206
Romain Guyf86ef572010-07-01 11:05:42 -0700207 if (restoreOrtho) {
208 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
209 }
210
Romain Guybd6b79b2010-06-26 00:13:53 -0700211 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700212 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700213 }
214
215 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700217
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700219}
220
Romain Guyd55a8612010-06-28 17:42:46 -0700221void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700222 if (!current->layer) {
223 LOGE("Attempting to compose a layer that does not exist");
224 return;
225 }
226
Romain Guyd55a8612010-06-28 17:42:46 -0700227 // Unbind current FBO and restore previous one
228 // Most of the time, previous->fbo will be 0 to bind the default buffer
229 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
230
231 // Restore the clip from the previous snapshot
232 const Rect& clip = previous->getMappedClip();
233 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
234
Romain Guydda57022010-07-06 11:39:32 -0700235 Layer* layer = current->layer;
236
Romain Guyd55a8612010-06-28 17:42:46 -0700237 // Compute the correct texture coordinates for the FBO texture
238 // The texture is currently as big as the window but drawn with
239 // a quad of the appropriate size
Romain Guydda57022010-07-06 11:39:32 -0700240 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700241
Romain Guydda57022010-07-06 11:39:32 -0700242 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
243 layer->texture, layer->alpha, layer->mode, layer->blend, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700244
Romain Guydda57022010-07-06 11:39:32 -0700245 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700246 // Failing to add the layer to the cache should happen only if the
247 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700248 if (!mLayerCache.put(size, layer)) {
249 LAYER_LOGD("Deleting layer");
250
251 glDeleteFramebuffers(1, &layer->fbo);
252 glDeleteTextures(1, &layer->texture);
253
254 delete layer;
255 }
Romain Guyd55a8612010-06-28 17:42:46 -0700256}
257
Romain Guyf6a11b82010-06-23 17:47:49 -0700258///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700259// Layers
260///////////////////////////////////////////////////////////////////////////////
261
262int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
263 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700264 int count = saveSnapshot();
265
266 int alpha = 255;
267 SkXfermode::Mode mode;
268
269 if (p) {
270 alpha = p->getAlpha();
271 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
272 if (!isMode) {
273 // Assume SRC_OVER
274 mode = SkXfermode::kSrcOver_Mode;
275 }
276 } else {
277 mode = SkXfermode::kSrcOver_Mode;
278 }
279
280 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
281
282 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700283}
284
285int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
286 int alpha, int flags) {
287 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700288 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
289 return count;
290}
Romain Guybd6b79b2010-06-26 00:13:53 -0700291
Romain Guyd55a8612010-06-28 17:42:46 -0700292bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
293 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700294
Romain Guydda57022010-07-06 11:39:32 -0700295 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height);
296 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700297
Romain Guyf18fd992010-07-08 11:45:51 -0700298 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
299 LayerSize size(right - left, bottom - top);
300
301 // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
302 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700303 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700304 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700305 }
306
Romain Guyf18fd992010-07-08 11:45:51 -0700307 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
308
Romain Guyf86ef572010-07-01 11:05:42 -0700309 // Clear the FBO
310 glDisable(GL_SCISSOR_TEST);
311 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
312 glClear(GL_COLOR_BUFFER_BIT);
313 glEnable(GL_SCISSOR_TEST);
314
Romain Guydda57022010-07-06 11:39:32 -0700315 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700316 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700317 layer->mode = mode;
318 layer->alpha = alpha / 255.0f;
319 layer->layer.set(left, top, right, bottom);
320
321 snapshot->layer = layer;
322 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700323
Romain Guyf86ef572010-07-01 11:05:42 -0700324 // Creates a new snapshot to draw into the FBO
325 saveSnapshot();
326 // TODO: This doesn't preserve other transformations (check Skia first)
327 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
328 mSnapshot->clipRect.set(left, top, right, bottom);
329 mSnapshot->height = bottom - top;
330 setScissorFromClip();
331
332 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
333 Snapshot::kFlagClipSet;
334 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
335
336 // Change the ortho projection
337 mat4 ortho;
338 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
339 ortho.copyTo(mOrthoMatrix);
340
Romain Guyd55a8612010-06-28 17:42:46 -0700341 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700342}
343
344///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700345// Transforms
346///////////////////////////////////////////////////////////////////////////////
347
348void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700349 mSnapshot->transform.translate(dx, dy, 0.0f);
350 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700351}
352
353void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700354 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
355 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700356}
357
358void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700359 mSnapshot->transform.scale(sx, sy, 1.0f);
360 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700361}
362
363void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700364 mSnapshot->transform.load(*matrix);
365 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700366}
367
368void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700370}
371
372void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700373 mat4 m(*matrix);
374 mSnapshot->transform.multiply(m);
375 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700376}
377
378///////////////////////////////////////////////////////////////////////////////
379// Clipping
380///////////////////////////////////////////////////////////////////////////////
381
Romain Guybb9524b2010-06-22 18:56:38 -0700382void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700383 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700384 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700385}
386
387const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700388 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700389}
390
Romain Guyc7d53492010-06-25 13:41:57 -0700391bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
392 /*
393 * The documentation of quickReject() indicates that the specified rect
394 * is transformed before being compared to the clip rect. However, the
395 * clip rect is not stored transformed in the snapshot and can thus be
396 * compared directly
397 *
398 * The following code can be used instead to performed a mapped comparison:
399 *
400 * mSnapshot->transform.mapRect(r);
401 * const Rect& clip = mSnapshot->getMappedClip();
402 * return !clip.intersects(r);
403 */
Romain Guyc7d53492010-06-25 13:41:57 -0700404 Rect r(left, top, right, bottom);
405 return !mSnapshot->clipRect.intersects(r);
406}
407
Romain Guybb9524b2010-06-22 18:56:38 -0700408bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700409 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
410 if (clipped) {
411 mSnapshot->flags |= Snapshot::kFlagClipSet;
412 setScissorFromClip();
413 }
414 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700415}
416
Romain Guyf6a11b82010-06-23 17:47:49 -0700417///////////////////////////////////////////////////////////////////////////////
418// Drawing
419///////////////////////////////////////////////////////////////////////////////
420
Romain Guyc1396e92010-06-30 17:56:19 -0700421void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700422 const Texture* texture = mTextureCache.get(bitmap);
Romain Guyc1396e92010-06-30 17:56:19 -0700423
Romain Guyc1396e92010-06-30 17:56:19 -0700424 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700425 SkXfermode::Mode mode;
426 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700427
428 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
429 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700430}
431
Romain Guyf86ef572010-07-01 11:05:42 -0700432void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
433 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
434 const mat4 transform(*matrix);
435 transform.mapRect(r);
436
437 const Texture* texture = mTextureCache.get(bitmap);
438
439 int alpha;
440 SkXfermode::Mode mode;
441 getAlphaAndMode(paint, &alpha, &mode);
442
443 drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
444 alpha / 255.0f, mode, texture->blend, true);
445}
446
Romain Guy8ba548f2010-06-30 19:21:21 -0700447void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
448 float srcLeft, float srcTop, float srcRight, float srcBottom,
449 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700450 const SkPaint* paint) {
451 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700452
453 int alpha;
454 SkXfermode::Mode mode;
455 getAlphaAndMode(paint, &alpha, &mode);
456
457 const float width = texture->width;
458 const float height = texture->height;
459
460 const float u1 = srcLeft / width;
461 const float v1 = srcTop / height;
462 const float u2 = srcRight / width;
463 const float v2 = srcBottom / height;
464
465 resetDrawTextureTexCoords(u1, v1, u2, v2);
466
Romain Guy8ba548f2010-06-30 19:21:21 -0700467 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
468 alpha / 255.0f, mode, texture->blend, true);
469
470 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
471}
472
Romain Guydeba7852010-07-07 17:54:48 -0700473void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
474 float left, float top, float right, float bottom, const SkPaint* paint) {
475 // TODO: Implement
476 LOGD("Draw 9patch, paddingLeft=%d", patch->paddingLeft);
477}
478
Romain Guy85bf02f2010-06-22 13:11:24 -0700479void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700480 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700481 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700482}
Romain Guy9d5316e2010-06-24 19:30:36 -0700483
Romain Guybd6b79b2010-06-26 00:13:53 -0700484void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700485 SkXfermode::Mode mode;
486
487 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
488 if (!isMode) {
489 // Assume SRC_OVER
490 mode = SkXfermode::kSrcOver_Mode;
491 }
492
493 // Skia draws using the color's alpha channel if < 255
494 // Otherwise, it uses the paint's alpha
495 int color = p->getColor();
496 if (((color >> 24) & 0xFF) == 255) {
497 color |= p->getAlpha() << 24;
498 }
499
500 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700501}
Romain Guy9d5316e2010-06-24 19:30:36 -0700502
Romain Guy026c5e162010-06-28 17:12:22 -0700503void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
504 int color, SkXfermode::Mode mode) {
505 const int alpha = (color >> 24) & 0xFF;
506 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
507
508 const GLfloat a = alpha / 255.0f;
509 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
510 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
511 const GLfloat b = ((color ) & 0xFF) / 255.0f;
512
513 if (blend) {
514 glEnable(GL_BLEND);
515 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
516 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700517
Romain Guyc7d53492010-06-25 13:41:57 -0700518 mModelView.loadTranslate(left, top, 0.0f);
519 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700520
Romain Guyc7d53492010-06-25 13:41:57 -0700521 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700522
Romain Guyc7d53492010-06-25 13:41:57 -0700523 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700524
Romain Guyc7d53492010-06-25 13:41:57 -0700525 glEnableVertexAttribArray(mDrawColorShader->position);
526 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
527 gDrawColorVertexStride, p);
528 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700529
Romain Guyc7d53492010-06-25 13:41:57 -0700530 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700531
Romain Guyc7d53492010-06-25 13:41:57 -0700532 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700533
534 if (blend) {
535 glDisable(GL_BLEND);
536 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700537}
538
Romain Guybd6b79b2010-06-26 00:13:53 -0700539void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700540 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700541 mModelView.loadTranslate(left, top, 0.0f);
542 mModelView.scale(right - left, bottom - top, 1.0f);
543
544 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
545
Romain Guyc1396e92010-06-30 17:56:19 -0700546 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
547 GLenum sourceMode = gBlends[mode].src;
548 if (!isPremultiplied && sourceMode == GL_ONE) {
549 sourceMode = GL_SRC_ALPHA;
550 }
551
552 glEnable(GL_BLEND);
553 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700554 }
555
Romain Guybd6b79b2010-06-26 00:13:53 -0700556 glBindTexture(GL_TEXTURE_2D, texture);
557
Romain Guyc1396e92010-06-30 17:56:19 -0700558 // TODO handle tiling and filtering here
559
Romain Guybd6b79b2010-06-26 00:13:53 -0700560 glActiveTexture(GL_TEXTURE0);
561 glUniform1i(mDrawTextureShader->sampler, 0);
562
Romain Guy026c5e162010-06-28 17:12:22 -0700563 const GLvoid* p = &mDrawTextureVertices[0].position[0];
564 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700565
566 glEnableVertexAttribArray(mDrawTextureShader->position);
567 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
568 gDrawTextureVertexStride, p);
569
570 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
571 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
572 gDrawTextureVertexStride, t);
573
574 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
575
576 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
577
578 glDisableVertexAttribArray(mDrawTextureShader->position);
579 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
580
581 glBindTexture(GL_TEXTURE_2D, 0);
582 glDisable(GL_BLEND);
583}
584
Romain Guy026c5e162010-06-28 17:12:22 -0700585void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
586 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700587 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700588 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700589 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700590 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700591 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700592 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700593 mDrawTextureVertices[3].texture[1] = v2;
594}
595
596void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
597 if (paint) {
598 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
599 if (!isMode) {
600 // Assume SRC_OVER
601 *mode = SkXfermode::kSrcOver_Mode;
602 }
603
604 // Skia draws using the color's alpha channel if < 255
605 // Otherwise, it uses the paint's alpha
606 int color = paint->getColor();
607 *alpha = (color >> 24) & 0xFF;
608 if (*alpha == 255) {
609 *alpha = paint->getAlpha();
610 }
611 } else {
612 *mode = SkXfermode::kSrcOver_Mode;
613 *alpha = 255;
614 }
Romain Guy026c5e162010-06-28 17:12:22 -0700615}
616
Romain Guy9d5316e2010-06-24 19:30:36 -0700617}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700618}; // namespace android