blob: 34da572936f921d1eecf7dead464154865d5e18a [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
24
Romain Guy121e2242010-07-01 18:26:52 -070025#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy121e2242010-07-01 18:26:52 -070037// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
40
Romain Guydda57022010-07-06 11:39:32 -070041#define DEFAULT_TEXTURE_CACHE_SIZE 20
42#define DEFAULT_LAYER_CACHE_SIZE 10
Romain Guyf7f93552010-07-08 19:17:03 -070043#define DEFAULT_PATCH_CACHE_SIZE 100
Romain Guydda57022010-07-06 11:39:32 -070044
Romain Guy121e2242010-07-01 18:26:52 -070045// Converts a number of mega-bytes into bytes
46#define MB(s) s * 1024 * 1024
47
Romain Guydda57022010-07-06 11:39:32 -070048// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070049#define SV(x, y) { { x, y } }
50#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070051
52///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy026c5e162010-06-28 17:12:22 -070056static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070057 SV(0.0f, 0.0f),
58 SV(1.0f, 0.0f),
59 SV(0.0f, 1.0f),
60 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070061};
Romain Guy026c5e162010-06-28 17:12:22 -070062static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
63static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070064
Romain Guy026c5e162010-06-28 17:12:22 -070065// This array is never used directly but used as a memcpy source in the
66// OpenGLRenderer constructor
67static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070068 FV(0.0f, 0.0f, 0.0f, 0.0f),
69 FV(1.0f, 0.0f, 1.0f, 0.0f),
70 FV(0.0f, 1.0f, 0.0f, 1.0f),
71 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070072};
Romain Guy026c5e162010-06-28 17:12:22 -070073static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
74static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070075
Romain Guy026c5e162010-06-28 17:12:22 -070076// In this array, the index of each Blender equals the value of the first
77// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
78static const Blender gBlends[] = {
79 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
80 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
81 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
82 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
84 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
86 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
90 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
91};
Romain Guye4d01122010-06-16 18:44:05 -070092
Romain Guyf6a11b82010-06-23 17:47:49 -070093///////////////////////////////////////////////////////////////////////////////
94// Constructors/destructor
95///////////////////////////////////////////////////////////////////////////////
96
Romain Guydda57022010-07-06 11:39:32 -070097OpenGLRenderer::OpenGLRenderer():
98 mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)),
Romain Guyf7f93552010-07-08 19:17:03 -070099 mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)),
100 mPatchCache(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -0700101 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -0700102
Romain Guy121e2242010-07-01 18:26:52 -0700103 char property[PROPERTY_VALUE_MAX];
104 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
Romain Guydda57022010-07-06 11:39:32 -0700105 LOGD(" Setting texture cache size to %sMB", property);
Romain Guy121e2242010-07-01 18:26:52 -0700106 mTextureCache.setMaxSize(MB(atoi(property)));
Romain Guydda57022010-07-06 11:39:32 -0700107 } else {
108 LOGD(" Using default texture cache size of %dMB", DEFAULT_TEXTURE_CACHE_SIZE);
109 }
110
111 if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) {
112 LOGD(" Setting layer cache size to %sMB", property);
113 mLayerCache.setMaxSize(MB(atoi(property)));
114 } else {
115 LOGD(" Using default layer cache size of %dMB", DEFAULT_LAYER_CACHE_SIZE);
Romain Guy121e2242010-07-01 18:26:52 -0700116 }
117
Romain Guy9d5316e2010-06-24 19:30:36 -0700118 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -0700119 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700120
121 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700122}
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124OpenGLRenderer::~OpenGLRenderer() {
125 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700126
127 mTextureCache.clear();
Romain Guydda57022010-07-06 11:39:32 -0700128 mLayerCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guyf6a11b82010-06-23 17:47:49 -0700131///////////////////////////////////////////////////////////////////////////////
132// Setup
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guy85bf02f2010-06-22 13:11:24 -0700135void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700136 glViewport(0, 0, width, height);
137
138 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700139 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700140 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700141
142 mWidth = width;
143 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700144 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700148 mSnapshot = &mFirstSnapshot;
149 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700150
Romain Guy08ae3172010-06-21 19:35:50 -0700151 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
Romain Guy08ae3172010-06-21 19:35:50 -0700153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guy08ae3172010-06-21 19:35:50 -0700156 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700157 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700158
Romain Guybb9524b2010-06-22 18:56:38 -0700159 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
160}
161
Romain Guyf6a11b82010-06-23 17:47:49 -0700162///////////////////////////////////////////////////////////////////////////////
163// State management
164///////////////////////////////////////////////////////////////////////////////
165
Romain Guybb9524b2010-06-22 18:56:38 -0700166int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700167 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700168}
169
170int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700171 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700172}
173
174void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700176
Romain Guy7ae7ac42010-06-25 13:46:18 -0700177 if (restoreSnapshot()) {
178 setScissorFromClip();
179 }
Romain Guybb9524b2010-06-22 18:56:38 -0700180}
181
182void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700183 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700184
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700186
Romain Guy7ae7ac42010-06-25 13:46:18 -0700187 while (mSaveCount != saveCount - 1) {
188 restoreClip |= restoreSnapshot();
189 }
Romain Guybb9524b2010-06-22 18:56:38 -0700190
Romain Guy7ae7ac42010-06-25 13:46:18 -0700191 if (restoreClip) {
192 setScissorFromClip();
193 }
Romain Guybb9524b2010-06-22 18:56:38 -0700194}
195
196int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700197 mSnapshot = new Snapshot(mSnapshot);
198 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700203 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700204 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700205
Romain Guybd6b79b2010-06-26 00:13:53 -0700206 sp<Snapshot> current = mSnapshot;
207 sp<Snapshot> previous = mSnapshot->previous;
208
Romain Guyf86ef572010-07-01 11:05:42 -0700209 if (restoreOrtho) {
210 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
211 }
212
Romain Guybd6b79b2010-06-26 00:13:53 -0700213 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700214 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700215 }
216
217 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700219
Romain Guy7ae7ac42010-06-25 13:46:18 -0700220 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700221}
222
Romain Guyd55a8612010-06-28 17:42:46 -0700223void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
Romain Guydda57022010-07-06 11:39:32 -0700224 if (!current->layer) {
225 LOGE("Attempting to compose a layer that does not exist");
226 return;
227 }
228
Romain Guyd55a8612010-06-28 17:42:46 -0700229 // Unbind current FBO and restore previous one
230 // Most of the time, previous->fbo will be 0 to bind the default buffer
231 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
232
233 // Restore the clip from the previous snapshot
234 const Rect& clip = previous->getMappedClip();
235 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
236
Romain Guydda57022010-07-06 11:39:32 -0700237 Layer* layer = current->layer;
238
Romain Guyd55a8612010-06-28 17:42:46 -0700239 // Compute the correct texture coordinates for the FBO texture
240 // The texture is currently as big as the window but drawn with
241 // a quad of the appropriate size
Romain Guydda57022010-07-06 11:39:32 -0700242 const Rect& rect = layer->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700243
Romain Guydda57022010-07-06 11:39:32 -0700244 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
245 layer->texture, layer->alpha, layer->mode, layer->blend, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700246
Romain Guydda57022010-07-06 11:39:32 -0700247 LayerSize size(rect.getWidth(), rect.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700248 // Failing to add the layer to the cache should happen only if the
249 // layer is too large
Romain Guydda57022010-07-06 11:39:32 -0700250 if (!mLayerCache.put(size, layer)) {
251 LAYER_LOGD("Deleting layer");
252
253 glDeleteFramebuffers(1, &layer->fbo);
254 glDeleteTextures(1, &layer->texture);
255
256 delete layer;
257 }
Romain Guyd55a8612010-06-28 17:42:46 -0700258}
259
Romain Guyf6a11b82010-06-23 17:47:49 -0700260///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700261// Layers
262///////////////////////////////////////////////////////////////////////////////
263
264int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
265 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700266 int count = saveSnapshot();
267
268 int alpha = 255;
269 SkXfermode::Mode mode;
270
271 if (p) {
272 alpha = p->getAlpha();
273 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
274 if (!isMode) {
275 // Assume SRC_OVER
276 mode = SkXfermode::kSrcOver_Mode;
277 }
278 } else {
279 mode = SkXfermode::kSrcOver_Mode;
280 }
281
282 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
283
284 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700285}
286
287int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
288 int alpha, int flags) {
289 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700290 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
291 return count;
292}
Romain Guybd6b79b2010-06-26 00:13:53 -0700293
Romain Guyd55a8612010-06-28 17:42:46 -0700294bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
295 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700296
Romain Guydda57022010-07-06 11:39:32 -0700297 LAYER_LOGD("Requesting layer %dx%d", size.width, size.height);
298 LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700299
Romain Guyf18fd992010-07-08 11:45:51 -0700300 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
301 LayerSize size(right - left, bottom - top);
302
Romain Guyf18fd992010-07-08 11:45:51 -0700303 Layer* layer = mLayerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700304 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700305 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700306 }
307
Romain Guyf18fd992010-07-08 11:45:51 -0700308 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
309
Romain Guyf86ef572010-07-01 11:05:42 -0700310 // Clear the FBO
311 glDisable(GL_SCISSOR_TEST);
312 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
313 glClear(GL_COLOR_BUFFER_BIT);
314 glEnable(GL_SCISSOR_TEST);
315
Romain Guydda57022010-07-06 11:39:32 -0700316 // Save the layer in the snapshot
Romain Guyd55a8612010-06-28 17:42:46 -0700317 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700318 layer->mode = mode;
319 layer->alpha = alpha / 255.0f;
320 layer->layer.set(left, top, right, bottom);
321
322 snapshot->layer = layer;
323 snapshot->fbo = layer->fbo;
Romain Guyd55a8612010-06-28 17:42:46 -0700324
Romain Guyf86ef572010-07-01 11:05:42 -0700325 // Creates a new snapshot to draw into the FBO
326 saveSnapshot();
327 // TODO: This doesn't preserve other transformations (check Skia first)
328 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
329 mSnapshot->clipRect.set(left, top, right, bottom);
330 mSnapshot->height = bottom - top;
331 setScissorFromClip();
332
333 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
334 Snapshot::kFlagClipSet;
335 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
336
337 // Change the ortho projection
338 mat4 ortho;
339 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
340 ortho.copyTo(mOrthoMatrix);
341
Romain Guyd55a8612010-06-28 17:42:46 -0700342 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700343}
344
345///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700346// Transforms
347///////////////////////////////////////////////////////////////////////////////
348
349void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700350 mSnapshot->transform.translate(dx, dy, 0.0f);
351 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700352}
353
354void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700355 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
356 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700357}
358
359void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700360 mSnapshot->transform.scale(sx, sy, 1.0f);
361 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700362}
363
364void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700365 mSnapshot->transform.load(*matrix);
366 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700370 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mat4 m(*matrix);
375 mSnapshot->transform.multiply(m);
376 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700377}
378
379///////////////////////////////////////////////////////////////////////////////
380// Clipping
381///////////////////////////////////////////////////////////////////////////////
382
Romain Guybb9524b2010-06-22 18:56:38 -0700383void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700384 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700385 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700386}
387
388const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700389 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700390}
391
Romain Guyc7d53492010-06-25 13:41:57 -0700392bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
393 /*
394 * The documentation of quickReject() indicates that the specified rect
395 * is transformed before being compared to the clip rect. However, the
396 * clip rect is not stored transformed in the snapshot and can thus be
397 * compared directly
398 *
399 * The following code can be used instead to performed a mapped comparison:
400 *
401 * mSnapshot->transform.mapRect(r);
402 * const Rect& clip = mSnapshot->getMappedClip();
403 * return !clip.intersects(r);
404 */
Romain Guyc7d53492010-06-25 13:41:57 -0700405 Rect r(left, top, right, bottom);
406 return !mSnapshot->clipRect.intersects(r);
407}
408
Romain Guybb9524b2010-06-22 18:56:38 -0700409bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700410 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
411 if (clipped) {
412 mSnapshot->flags |= Snapshot::kFlagClipSet;
413 setScissorFromClip();
414 }
415 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700416}
417
Romain Guyf6a11b82010-06-23 17:47:49 -0700418///////////////////////////////////////////////////////////////////////////////
419// Drawing
420///////////////////////////////////////////////////////////////////////////////
421
Romain Guyc1396e92010-06-30 17:56:19 -0700422void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700423 const Texture* texture = mTextureCache.get(bitmap);
Romain Guyc1396e92010-06-30 17:56:19 -0700424
Romain Guyc1396e92010-06-30 17:56:19 -0700425 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700426 SkXfermode::Mode mode;
427 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700428
429 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
430 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700431}
432
Romain Guyf86ef572010-07-01 11:05:42 -0700433void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
434 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
435 const mat4 transform(*matrix);
436 transform.mapRect(r);
437
438 const Texture* texture = mTextureCache.get(bitmap);
439
440 int alpha;
441 SkXfermode::Mode mode;
442 getAlphaAndMode(paint, &alpha, &mode);
443
444 drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
445 alpha / 255.0f, mode, texture->blend, true);
446}
447
Romain Guy8ba548f2010-06-30 19:21:21 -0700448void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
449 float srcLeft, float srcTop, float srcRight, float srcBottom,
450 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700451 const SkPaint* paint) {
452 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700453
454 int alpha;
455 SkXfermode::Mode mode;
456 getAlphaAndMode(paint, &alpha, &mode);
457
458 const float width = texture->width;
459 const float height = texture->height;
460
461 const float u1 = srcLeft / width;
462 const float v1 = srcTop / height;
463 const float u2 = srcRight / width;
464 const float v2 = srcBottom / height;
465
466 resetDrawTextureTexCoords(u1, v1, u2, v2);
467
Romain Guy8ba548f2010-06-30 19:21:21 -0700468 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
469 alpha / 255.0f, mode, texture->blend, true);
470
471 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
472}
473
Romain Guydeba7852010-07-07 17:54:48 -0700474void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
475 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guyf7f93552010-07-08 19:17:03 -0700476 const Texture* texture = mTextureCache.get(bitmap);
477
478 int alpha;
479 SkXfermode::Mode mode;
480 getAlphaAndMode(paint, &alpha, &mode);
481
482 const uint32_t width = patch->numXDivs;
483 const uint32_t height = patch->numYDivs;
484
485 Patch* mesh = mPatchCache.get(patch);
486
487 const uint32_t xStretchCount = (width + 1) >> 1;
488 const uint32_t yStretchCount = (height + 1) >> 1;
489
490 const int32_t* xDivs = &patch->xDivs[0];
491 const int32_t* yDivs = &patch->yDivs[0];
492
493 float xStretch = 0;
494 float yStretch = 0;
495 float xStretchTex = 0;
496 float yStretchTex = 0;
497
498 const float meshWidth = right - left;
499
500 const float bitmapWidth = float(bitmap->width());
501 const float bitmapHeight = float(bitmap->height());
502
503 if (xStretchCount > 0) {
504 uint32_t stretchSize = 0;
505 for (uint32_t i = 1; i < width; i += 2) {
506 stretchSize += xDivs[i] - xDivs[i - 1];
507 }
508 xStretchTex = (stretchSize / bitmapWidth) / xStretchCount;
509 const float fixed = bitmapWidth - stretchSize;
510 xStretch = (right - left - fixed) / xStretchCount;
511 }
512
513 if (yStretchCount > 0) {
514 uint32_t stretchSize = 0;
515 for (uint32_t i = 1; i < height; i += 2) {
516 stretchSize += yDivs[i] - yDivs[i - 1];
517 }
518 yStretchTex = (stretchSize / bitmapHeight) / yStretchCount;
519 const float fixed = bitmapHeight - stretchSize;
520 yStretch = (bottom - top - fixed) / yStretchCount;
521 }
522
523 float vy = 0.0f;
524 float ty = 0.0f;
525 TextureVertex* vertex = mesh->vertices;
526
527 generateVertices(vertex, 0.0f, 0.0f, xDivs, width, xStretch, xStretchTex,
528 meshWidth, bitmapWidth);
529 vertex += width + 2;
530
531 for (uint32_t y = 0; y < height; y++) {
532 if (y & 1) {
533 vy += yStretch;
534 ty += yStretchTex;
535 } else {
536 const float step = float(yDivs[y]);
537 vy += step;
538 ty += step / bitmapHeight;
539 }
540 generateVertices(vertex, vy, ty, xDivs, width, xStretch, xStretchTex,
541 meshWidth, bitmapWidth);
542 vertex += width + 2;
543 }
544
545 generateVertices(vertex, bottom - top, 1.0f, xDivs, width, xStretch, xStretchTex,
546 meshWidth, bitmapWidth);
547
548 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
549 // patch mesh already defines the final size
550 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha, mode, texture->blend,
551 true, &mesh->vertices[0].position[0], &mesh->vertices[0].texture[0], mesh->indices,
552 mesh->indicesCount);
553}
554
555void OpenGLRenderer::generateVertices(TextureVertex* vertex, float y, float v,
556 const int32_t xDivs[], uint32_t xCount, float xStretch, float xStretchTex,
557 float width, float widthTex) {
558 float vx = 0.0f;
559 float tx = 0.0f;
560
561 TextureVertex::set(vertex, vx, y, tx, v);
562 vertex++;
563
564 for (uint32_t x = 0; x < xCount; x++) {
565 if (x & 1) {
566 vx += xStretch;
567 tx += xStretchTex;
568 } else {
569 const float step = float(xDivs[x]);
570 vx += step;
571 tx += step / widthTex;
572 }
573
574 TextureVertex::set(vertex, vx, y, tx, v);
575 vertex++;
576 }
577
578 TextureVertex::set(vertex, width, y, 1.0f, v);
579 vertex++;
Romain Guydeba7852010-07-07 17:54:48 -0700580}
581
Romain Guy85bf02f2010-06-22 13:11:24 -0700582void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700583 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700584 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700585}
Romain Guy9d5316e2010-06-24 19:30:36 -0700586
Romain Guybd6b79b2010-06-26 00:13:53 -0700587void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700588 SkXfermode::Mode mode;
589
590 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
591 if (!isMode) {
592 // Assume SRC_OVER
593 mode = SkXfermode::kSrcOver_Mode;
594 }
595
596 // Skia draws using the color's alpha channel if < 255
597 // Otherwise, it uses the paint's alpha
598 int color = p->getColor();
599 if (((color >> 24) & 0xFF) == 255) {
600 color |= p->getAlpha() << 24;
601 }
602
603 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700604}
Romain Guy9d5316e2010-06-24 19:30:36 -0700605
Romain Guy026c5e162010-06-28 17:12:22 -0700606void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
607 int color, SkXfermode::Mode mode) {
608 const int alpha = (color >> 24) & 0xFF;
609 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
610
611 const GLfloat a = alpha / 255.0f;
612 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
613 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
614 const GLfloat b = ((color ) & 0xFF) / 255.0f;
615
616 if (blend) {
617 glEnable(GL_BLEND);
618 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
619 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700620
Romain Guyc7d53492010-06-25 13:41:57 -0700621 mModelView.loadTranslate(left, top, 0.0f);
622 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700623
Romain Guyc7d53492010-06-25 13:41:57 -0700624 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700625
Romain Guyc7d53492010-06-25 13:41:57 -0700626 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700627
Romain Guyc7d53492010-06-25 13:41:57 -0700628 glEnableVertexAttribArray(mDrawColorShader->position);
629 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
630 gDrawColorVertexStride, p);
631 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700632
Romain Guyc7d53492010-06-25 13:41:57 -0700633 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700634
Romain Guyc7d53492010-06-25 13:41:57 -0700635 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700636
637 if (blend) {
638 glDisable(GL_BLEND);
639 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700640}
641
Romain Guybd6b79b2010-06-26 00:13:53 -0700642void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700643 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guyf7f93552010-07-08 19:17:03 -0700644 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, isPremultiplied,
645 &mDrawTextureVertices[0].position[0], &mDrawTextureVertices[0].texture[0], NULL);
646}
647
648void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
649 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied,
650 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700651 mModelView.loadTranslate(left, top, 0.0f);
652 mModelView.scale(right - left, bottom - top, 1.0f);
653
654 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
655
Romain Guyc1396e92010-06-30 17:56:19 -0700656 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
657 GLenum sourceMode = gBlends[mode].src;
658 if (!isPremultiplied && sourceMode == GL_ONE) {
659 sourceMode = GL_SRC_ALPHA;
660 }
661
662 glEnable(GL_BLEND);
663 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700664 }
665
Romain Guybd6b79b2010-06-26 00:13:53 -0700666 glBindTexture(GL_TEXTURE_2D, texture);
667
Romain Guyc1396e92010-06-30 17:56:19 -0700668 // TODO handle tiling and filtering here
669
Romain Guybd6b79b2010-06-26 00:13:53 -0700670 glActiveTexture(GL_TEXTURE0);
671 glUniform1i(mDrawTextureShader->sampler, 0);
672
Romain Guybd6b79b2010-06-26 00:13:53 -0700673 glEnableVertexAttribArray(mDrawTextureShader->position);
674 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700675 gDrawTextureVertexStride, vertices);
Romain Guybd6b79b2010-06-26 00:13:53 -0700676
677 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
678 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
Romain Guyf7f93552010-07-08 19:17:03 -0700679 gDrawTextureVertexStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700680
681 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
682
Romain Guyf7f93552010-07-08 19:17:03 -0700683 if (!indices) {
684 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
685 } else {
686 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
687 }
Romain Guybd6b79b2010-06-26 00:13:53 -0700688
689 glDisableVertexAttribArray(mDrawTextureShader->position);
690 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
691
692 glBindTexture(GL_TEXTURE_2D, 0);
693 glDisable(GL_BLEND);
694}
695
Romain Guy026c5e162010-06-28 17:12:22 -0700696void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
697 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700698 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700699 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700700 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700701 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700702 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700703 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700704 mDrawTextureVertices[3].texture[1] = v2;
705}
706
707void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
708 if (paint) {
709 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
710 if (!isMode) {
711 // Assume SRC_OVER
712 *mode = SkXfermode::kSrcOver_Mode;
713 }
714
715 // Skia draws using the color's alpha channel if < 255
716 // Otherwise, it uses the paint's alpha
717 int color = paint->getColor();
718 *alpha = (color >> 24) & 0xFF;
719 if (*alpha == 255) {
720 *alpha = paint->getAlpha();
721 }
722 } else {
723 *mode = SkXfermode::kSrcOver_Mode;
724 *alpha = 255;
725 }
Romain Guy026c5e162010-06-28 17:12:22 -0700726}
727
Romain Guy9d5316e2010-06-24 19:30:36 -0700728}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700729}; // namespace android