blob: d343a6f8aaf6d1bac87ef7a57aec3890e5193a4b [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>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy5b3b3522010-10-27 18:57:51 -070029#include <ui/Rect.h>
30
Romain Guy85bf02f2010-06-22 13:11:24 -070031#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080032#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080033#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070034
35namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070036namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Defines
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guy759ea802010-09-16 20:49:46 -070042#define RAD_TO_DEG (180.0f / 3.14159265f)
43#define MIN_ANGLE 0.001f
44
Romain Guydbc26d22010-10-11 17:58:29 -070045// TODO: This should be set in properties
46#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
47
Romain Guy9d5316e2010-06-24 19:30:36 -070048///////////////////////////////////////////////////////////////////////////////
49// Globals
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy889f8d12010-07-29 14:37:42 -070052/**
53 * Structure mapping Skia xfermodes to OpenGL blending factors.
54 */
55struct Blender {
56 SkXfermode::Mode mode;
57 GLenum src;
58 GLenum dst;
59}; // struct Blender
60
Romain Guy026c5e162010-06-28 17:12:22 -070061// In this array, the index of each Blender equals the value of the first
62// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
63static const Blender gBlends[] = {
Romain Guyb146b122010-12-15 17:06:45 -080064 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
65 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
66 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
67 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
68 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
69 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
70 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
71 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
75 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070076};
Romain Guye4d01122010-06-16 18:44:05 -070077
Romain Guy87a76572010-09-13 18:11:21 -070078// This array contains the swapped version of each SkXfermode. For instance
79// this array's SrcOver blending mode is actually DstOver. You can refer to
80// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070081static const Blender gBlendsSwap[] = {
Romain Guyb146b122010-12-15 17:06:45 -080082 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
83 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
85 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070094};
95
Romain Guy889f8d12010-07-29 14:37:42 -070096static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080097 GL_TEXTURE0,
98 GL_TEXTURE1,
99 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700100};
101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guyfb8b7632010-08-23 21:05:08 -0700106OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700107 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700108 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700109 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700110
Romain Guyac670c02010-07-27 17:39:27 -0700111 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
112
Romain Guyae5575b2010-07-29 18:48:04 -0700113 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700114}
115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700117 // The context has already been destroyed at this point, do not call
118 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guyf6a11b82010-06-23 17:47:49 -0700121///////////////////////////////////////////////////////////////////////////////
122// Setup
123///////////////////////////////////////////////////////////////////////////////
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700126 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700127 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700128
129 mWidth = width;
130 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700131
132 mFirstSnapshot->height = height;
133 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700134
135 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy6b7bd242010-10-06 19:49:23 -0700138void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800139 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
140}
141
142void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800143 mCaches.clearGarbage();
144
Romain Guy8aef54f2010-09-01 15:13:49 -0700145 mSnapshot = new Snapshot(mFirstSnapshot,
146 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700147 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700148
Romain Guyfb8b7632010-08-23 21:05:08 -0700149 glViewport(0, 0, mWidth, mHeight);
150
Romain Guyd90f23e2010-09-09 11:47:54 -0700151 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
Romain Guy7d7b5492011-01-24 16:33:45 -0800153 glEnable(GL_SCISSOR_TEST);
154 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
155 mSnapshot->setClip(left, top, right, bottom);
156
Romain Guy6b7bd242010-10-06 19:49:23 -0700157 if (!opaque) {
158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
159 glClear(GL_COLOR_BUFFER_BIT);
160 }
Romain Guybb9524b2010-06-22 18:56:38 -0700161}
162
Romain Guyb025b9c2010-09-16 14:16:48 -0700163void OpenGLRenderer::finish() {
164#if DEBUG_OPENGL
165 GLenum status = GL_NO_ERROR;
166 while ((status = glGetError()) != GL_NO_ERROR) {
167 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800168 switch (status) {
169 case GL_OUT_OF_MEMORY:
170 LOGE(" OpenGLRenderer is out of memory!");
171 break;
172 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700173 }
174#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800175#if DEBUG_MEMORY_USAGE
176 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800177#else
178 if (mCaches.getDebugLevel() & kDebugMemory) {
179 mCaches.dumpMemoryUsage();
180 }
Romain Guyc15008e2010-11-10 11:59:15 -0800181#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700182}
183
Romain Guy6c319ca2011-01-11 14:29:25 -0800184void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700185 if (mCaches.currentProgram) {
186 if (mCaches.currentProgram->isInUse()) {
187 mCaches.currentProgram->remove();
188 mCaches.currentProgram = NULL;
189 }
190 }
Romain Guy50c0f092010-10-19 11:42:22 -0700191 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700192}
193
Romain Guy6c319ca2011-01-11 14:29:25 -0800194void OpenGLRenderer::acquireContext() {
195 interrupt();
196}
197
198void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700199 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700200
201 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700202 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700203
Romain Guyf607bdc2010-09-10 19:20:06 -0700204 glDisable(GL_DITHER);
205
Romain Guy42f3a4b2011-01-19 13:42:26 -0800206 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700207 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700208
Romain Guy50c0f092010-10-19 11:42:22 -0700209 mCaches.blend = true;
210 glEnable(GL_BLEND);
211 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
212 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700213}
214
Romain Guy6c319ca2011-01-11 14:29:25 -0800215void OpenGLRenderer::releaseContext() {
216 resume();
217}
218
Chet Haasedaf98e92011-01-10 14:10:36 -0800219bool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
220 interrupt();
221 status_t result = (*functor)();
222 resume();
223 return (result == 0) ? false : true;
224}
225
Romain Guyf6a11b82010-06-23 17:47:49 -0700226///////////////////////////////////////////////////////////////////////////////
227// State management
228///////////////////////////////////////////////////////////////////////////////
229
Romain Guybb9524b2010-06-22 18:56:38 -0700230int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
234int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700235 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700236}
237
238void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700239 if (mSaveCount > 1) {
240 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700241 }
Romain Guybb9524b2010-06-22 18:56:38 -0700242}
243
244void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700245 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700246
Romain Guy8fb95422010-08-17 18:38:51 -0700247 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700248 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700249 }
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guy8aef54f2010-09-01 15:13:49 -0700252int OpenGLRenderer::saveSnapshot(int flags) {
253 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700254 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700255}
256
257bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700258 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700260 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700261
Romain Guybd6b79b2010-06-26 00:13:53 -0700262 sp<Snapshot> current = mSnapshot;
263 sp<Snapshot> previous = mSnapshot->previous;
264
Romain Guyeb993562010-10-05 18:14:38 -0700265 if (restoreOrtho) {
266 Rect& r = previous->viewport;
267 glViewport(r.left, r.top, r.right, r.bottom);
268 mOrthoMatrix.load(current->orthoMatrix);
269 }
270
Romain Guy8b55f372010-08-18 17:10:07 -0700271 mSaveCount--;
272 mSnapshot = previous;
273
Romain Guy2542d192010-08-18 11:47:12 -0700274 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700275 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700276 }
Romain Guy2542d192010-08-18 11:47:12 -0700277
Romain Guy5ec99242010-11-03 16:19:08 -0700278 if (restoreLayer) {
279 composeLayer(current, previous);
280 }
281
Romain Guy2542d192010-08-18 11:47:12 -0700282 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700283}
284
Romain Guyf6a11b82010-06-23 17:47:49 -0700285///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700286// Layers
287///////////////////////////////////////////////////////////////////////////////
288
289int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700290 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700291 const GLuint previousFbo = mSnapshot->fbo;
292 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700293
Romain Guyaf636eb2010-12-09 17:47:21 -0800294 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700295 int alpha = 255;
296 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700297
Romain Guye45362c2010-11-03 19:58:32 -0700298 if (p) {
299 alpha = p->getAlpha();
300 if (!mCaches.extensions.hasFramebufferFetch()) {
301 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
302 if (!isMode) {
303 // Assume SRC_OVER
304 mode = SkXfermode::kSrcOver_Mode;
305 }
306 } else {
307 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700308 }
309 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700310 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700311 }
Romain Guyd55a8612010-06-28 17:42:46 -0700312
Romain Guydbc26d22010-10-11 17:58:29 -0700313 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
314 }
Romain Guyd55a8612010-06-28 17:42:46 -0700315
316 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700317}
318
319int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
320 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700321 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700322 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700323 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700324 SkPaint paint;
325 paint.setAlpha(alpha);
326 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700327 }
Romain Guyd55a8612010-06-28 17:42:46 -0700328}
Romain Guybd6b79b2010-06-26 00:13:53 -0700329
Romain Guy1c740bc2010-09-13 18:00:09 -0700330/**
331 * Layers are viewed by Skia are slightly different than layers in image editing
332 * programs (for instance.) When a layer is created, previously created layers
333 * and the frame buffer still receive every drawing command. For instance, if a
334 * layer is created and a shape intersecting the bounds of the layers and the
335 * framebuffer is draw, the shape will be drawn on both (unless the layer was
336 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
337 *
338 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
339 * texture. Unfortunately, this is inefficient as it requires every primitive to
340 * be drawn n + 1 times, where n is the number of active layers. In practice this
341 * means, for every primitive:
342 * - Switch active frame buffer
343 * - Change viewport, clip and projection matrix
344 * - Issue the drawing
345 *
346 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700347 * To avoid this, layers are implemented in a different way here, at least in the
348 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
349 * is set. When this flag is set we can redirect all drawing operations into a
350 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700351 *
352 * This implementation relies on the frame buffer being at least RGBA 8888. When
353 * a layer is created, only a texture is created, not an FBO. The content of the
354 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700355 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700356 * buffer and drawing continues as normal. This technique therefore treats the
357 * frame buffer as a scratch buffer for the layers.
358 *
359 * To compose the layers back onto the frame buffer, each layer texture
360 * (containing the original frame buffer data) is drawn as a simple quad over
361 * the frame buffer. The trick is that the quad is set as the composition
362 * destination in the blending equation, and the frame buffer becomes the source
363 * of the composition.
364 *
365 * Drawing layers with an alpha value requires an extra step before composition.
366 * An empty quad is drawn over the layer's region in the frame buffer. This quad
367 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
368 * quad is used to multiply the colors in the frame buffer. This is achieved by
369 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
370 * GL_ZERO, GL_SRC_ALPHA.
371 *
372 * Because glCopyTexImage2D() can be slow, an alternative implementation might
373 * be use to draw a single clipped layer. The implementation described above
374 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700375 *
376 * (1) The frame buffer is actually not cleared right away. To allow the GPU
377 * to potentially optimize series of calls to glCopyTexImage2D, the frame
378 * buffer is left untouched until the first drawing operation. Only when
379 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700380 */
Romain Guyd55a8612010-06-28 17:42:46 -0700381bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700382 float right, float bottom, int alpha, SkXfermode::Mode mode,
383 int flags, GLuint previousFbo) {
384 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700385 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700386
Romain Guyeb993562010-10-05 18:14:38 -0700387 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
388
Romain Guyf607bdc2010-09-10 19:20:06 -0700389 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700390 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700391 if (fboLayer) {
392 // Clear the previous layer regions before we change the viewport
393 clearLayerRegions();
394 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700395 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700396
Romain Guyeb993562010-10-05 18:14:38 -0700397 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700398 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700399
Romain Guyae88e5e2010-10-22 17:49:18 -0700400 // We cannot work with sub-pixels in this case
401 bounds.snapToPixelBoundaries();
402
Romain Guyae517592010-10-22 10:40:27 -0700403 // When the layer is not an FBO, we may use glCopyTexImage so we
404 // need to make sure the layer does not extend outside the bounds
405 // of the framebuffer
406 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700407 }
Romain Guybf434112010-09-16 14:40:17 -0700408
Romain Guy746b7402010-10-26 16:27:31 -0700409 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
410 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800411 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700412 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700413 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700414 }
415
416 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800417 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700418 return false;
419 }
Romain Guyf18fd992010-07-08 11:45:51 -0700420
Romain Guy5b3b3522010-10-27 18:57:51 -0700421 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700422 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700423 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700424 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700425 }
426
Romain Guydda57022010-07-06 11:39:32 -0700427 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700428 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700429 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700430 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
431 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800432 layer->colorFilter = mColorFilter;
Romain Guydda57022010-07-06 11:39:32 -0700433
Romain Guy8fb95422010-08-17 18:38:51 -0700434 // Save the layer in the snapshot
435 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700436 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700437
Romain Guyeb993562010-10-05 18:14:38 -0700438 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700439 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700440 } else {
441 // Copy the framebuffer into the layer
442 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800443 if (!bounds.isEmpty()) {
444 if (layer->empty) {
445 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
446 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
447 layer->empty = false;
448 } else {
449 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
450 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
451 }
452 // Enqueue the buffer coordinates to clear the corresponding region later
453 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700454 }
Romain Guyeb993562010-10-05 18:14:38 -0700455 }
Romain Guyf86ef572010-07-01 11:05:42 -0700456
Romain Guyd55a8612010-06-28 17:42:46 -0700457 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700458}
459
Romain Guy5b3b3522010-10-27 18:57:51 -0700460bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
461 GLuint previousFbo) {
462 layer->fbo = mCaches.fboCache.get();
463
464#if RENDER_LAYERS_AS_REGIONS
465 snapshot->region = &snapshot->layer->region;
466 snapshot->flags |= Snapshot::kFlagFboTarget;
467#endif
468
469 Rect clip(bounds);
470 snapshot->transform->mapRect(clip);
471 clip.intersect(*snapshot->clipRect);
472 clip.snapToPixelBoundaries();
473 clip.intersect(snapshot->previous->viewport);
474
475 mat4 inverse;
476 inverse.loadInverse(*mSnapshot->transform);
477
478 inverse.mapRect(clip);
479 clip.snapToPixelBoundaries();
480 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700481 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700482
483 snapshot->flags |= Snapshot::kFlagIsFboLayer;
484 snapshot->fbo = layer->fbo;
485 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700486 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
487 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
488 snapshot->height = bounds.getHeight();
489 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
490 snapshot->orthoMatrix.load(mOrthoMatrix);
491
492 // Bind texture to FBO
493 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
494 glBindTexture(GL_TEXTURE_2D, layer->texture);
495
496 // Initialize the texture if needed
497 if (layer->empty) {
498 layer->empty = false;
499 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
500 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
501 }
502
503 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
504 layer->texture, 0);
505
506#if DEBUG_LAYERS_AS_REGIONS
507 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
508 if (status != GL_FRAMEBUFFER_COMPLETE) {
509 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
510
511 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
512 glDeleteTextures(1, &layer->texture);
513 mCaches.fboCache.put(layer->fbo);
514
515 delete layer;
516
517 return false;
518 }
519#endif
520
521 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
522 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
523 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
524 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
525 glClear(GL_COLOR_BUFFER_BIT);
526
527 dirtyClip();
528
529 // Change the ortho projection
530 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
531 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
532
533 return true;
534}
535
Romain Guy1c740bc2010-09-13 18:00:09 -0700536/**
537 * Read the documentation of createLayer() before doing anything in this method.
538 */
Romain Guy1d83e192010-08-17 11:37:00 -0700539void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
540 if (!current->layer) {
541 LOGE("Attempting to compose a layer that does not exist");
542 return;
543 }
544
Romain Guy5b3b3522010-10-27 18:57:51 -0700545 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700546
547 if (fboLayer) {
548 // Unbind current FBO and restore previous one
549 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
550 }
551
Romain Guy1d83e192010-08-17 11:37:00 -0700552 Layer* layer = current->layer;
553 const Rect& rect = layer->layer;
554
Romain Guyeb993562010-10-05 18:14:38 -0700555 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700556 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700557 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700558 // Required below, composeLayerRect() will divide by 255
559 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700560 }
561
Romain Guy03750a02010-10-18 14:06:08 -0700562 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700563
Romain Guy746b7402010-10-26 16:27:31 -0700564 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700565
Romain Guy5b3b3522010-10-27 18:57:51 -0700566 // When the layer is stored in an FBO, we can save a bit of fillrate by
567 // drawing only the dirty region
568 if (fboLayer) {
569 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800570 if (layer->colorFilter) {
571 setupColorFilter(layer->colorFilter);
572 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700573 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800574 if (layer->colorFilter) {
575 resetColorFilter();
576 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700577 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800578 if (!rect.isEmpty()) {
579 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
580 composeLayerRect(layer, rect, true);
581 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700582 }
Romain Guy8b55f372010-08-18 17:10:07 -0700583
Romain Guyeb993562010-10-05 18:14:38 -0700584 if (fboLayer) {
585 // Detach the texture from the FBO
586 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
587 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
588 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
589
590 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
591 mCaches.fboCache.put(current->fbo);
592 }
593
Romain Guy746b7402010-10-26 16:27:31 -0700594 dirtyClip();
595
Romain Guyeb993562010-10-05 18:14:38 -0700596 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700597 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700598 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700599 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700600 delete layer;
601 }
602}
603
Romain Guy5b3b3522010-10-27 18:57:51 -0700604void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
605 const Rect& texCoords = layer->texCoords;
606 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
607
608 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
609 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
610 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
611
612 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
613}
614
615void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
616#if RENDER_LAYERS_AS_REGIONS
617 if (layer->region.isRect()) {
618 composeLayerRect(layer, rect);
619 layer->region.clear();
620 return;
621 }
622
623 if (!layer->region.isEmpty()) {
624 size_t count;
625 const android::Rect* rects = layer->region.getArray(&count);
626
Romain Guy5b3b3522010-10-27 18:57:51 -0700627 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700628 const float texX = 1.0f / float(layer->width);
629 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800630 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700631
632 TextureVertex* mesh = mCaches.getRegionMesh();
633 GLsizei numQuads = 0;
634
Romain Guy7230a742011-01-10 22:26:16 -0800635 setupDraw();
636 setupDrawWithTexture();
637 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800638 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800639 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
640 setupDrawProgram();
641 setupDrawDirtyRegionsDisabled();
642 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800643 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800644 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800645 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800646 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700647
648 for (size_t i = 0; i < count; i++) {
649 const android::Rect* r = &rects[i];
650
651 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800652 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800654 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700655
656 // TODO: Reject quads outside of the clip
657 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
658 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
659 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
660 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
661
662 numQuads++;
663
664 if (numQuads >= REGION_MESH_QUAD_COUNT) {
665 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
666 numQuads = 0;
667 mesh = mCaches.getRegionMesh();
668 }
669 }
670
671 if (numQuads > 0) {
672 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
673 }
674
675 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800676 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700677
678#if DEBUG_LAYERS_AS_REGIONS
679 uint32_t colors[] = {
680 0x7fff0000, 0x7f00ff00,
681 0x7f0000ff, 0x7fff00ff,
682 };
683
684 int offset = 0;
685 int32_t top = rects[0].top;
686 int i = 0;
687
688 for (size_t i = 0; i < count; i++) {
689 if (top != rects[i].top) {
690 offset ^= 0x2;
691 top = rects[i].top;
692 }
693
694 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
695 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
696 SkXfermode::kSrcOver_Mode);
697 }
698#endif
699
700 layer->region.clear();
701 }
702#else
703 composeLayerRect(layer, rect);
704#endif
705}
706
707void OpenGLRenderer::dirtyLayer(const float left, const float top,
708 const float right, const float bottom, const mat4 transform) {
709#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800710 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 Rect bounds(left, top, right, bottom);
712 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800713 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700714 }
715#endif
716}
717
718void OpenGLRenderer::dirtyLayer(const float left, const float top,
719 const float right, const float bottom) {
720#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800721 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700722 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800723 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800724 }
725#endif
726}
727
728void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
729#if RENDER_LAYERS_AS_REGIONS
730 if (bounds.intersect(*mSnapshot->clipRect)) {
731 bounds.snapToPixelBoundaries();
732 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
733 if (!dirty.isEmpty()) {
734 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 }
736 }
737#endif
738}
739
Romain Guy86942302010-09-12 13:02:16 -0700740void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800741 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700742
Romain Guy5b3b3522010-10-27 18:57:51 -0700743 Rect clipRect(*mSnapshot->clipRect);
744 clipRect.snapToPixelBoundaries();
745
Romain Guy86942302010-09-12 13:02:16 -0700746 for (uint32_t i = 0; i < mLayers.size(); i++) {
747 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700748 if (clipRect.intersects(*bounds)) {
749 // Clear the framebuffer where the layer will draw
750 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
751 bounds->getWidth(), bounds->getHeight());
752 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
753 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700754
Romain Guy5b3b3522010-10-27 18:57:51 -0700755 // Restore the clip
756 dirtyClip();
757 }
Romain Guy86942302010-09-12 13:02:16 -0700758
759 delete bounds;
760 }
Romain Guy86942302010-09-12 13:02:16 -0700761
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700763}
764
Romain Guybd6b79b2010-06-26 00:13:53 -0700765///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700766// Transforms
767///////////////////////////////////////////////////////////////////////////////
768
769void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700771}
772
773void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700774 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700775}
776
777void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700778 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700779}
780
Romain Guy807daf72011-01-18 11:19:19 -0800781void OpenGLRenderer::skew(float sx, float sy) {
782 mSnapshot->transform->skew(sx, sy);
783}
784
Romain Guyf6a11b82010-06-23 17:47:49 -0700785void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700786 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700787}
788
Romain Guy41030da2010-10-13 13:40:37 -0700789const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700790 if (mSnapshot->fbo != 0) {
791 return &mSnapshot->transform->data[0];
792 }
793 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700794}
795
Romain Guyf6a11b82010-06-23 17:47:49 -0700796void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700797 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700798}
799
800void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700801 SkMatrix transform;
802 mSnapshot->transform->copyTo(transform);
803 transform.preConcat(*matrix);
804 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700805}
806
807///////////////////////////////////////////////////////////////////////////////
808// Clipping
809///////////////////////////////////////////////////////////////////////////////
810
Romain Guybb9524b2010-06-22 18:56:38 -0700811void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700812 Rect clip(*mSnapshot->clipRect);
813 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700814 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700815 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700816}
817
818const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700819 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700820}
821
Romain Guyc7d53492010-06-25 13:41:57 -0700822bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800823 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700824 return true;
825 }
826
Romain Guy1d83e192010-08-17 11:37:00 -0700827 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700828 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700829 r.snapToPixelBoundaries();
830
831 Rect clipRect(*mSnapshot->clipRect);
832 clipRect.snapToPixelBoundaries();
833
834 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700835}
836
Romain Guy079ba2c2010-07-16 14:12:24 -0700837bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
838 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700839 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700840 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700841 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700842 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700843}
844
Romain Guyf6a11b82010-06-23 17:47:49 -0700845///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800846// Drawing commands
847///////////////////////////////////////////////////////////////////////////////
848
849void OpenGLRenderer::setupDraw() {
850 clearLayerRegions();
851 if (mDirtyClip) {
852 setScissorFromClip();
853 }
854 mDescription.reset();
855 mSetShaderColor = false;
856 mColorSet = false;
857 mColorA = mColorR = mColorG = mColorB = 0.0f;
858 mTextureUnit = 0;
859 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800860 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800861}
862
863void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
864 mDescription.hasTexture = true;
865 mDescription.hasAlpha8Texture = isAlpha8;
866}
867
868void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800869 setupDrawColor(color, (color >> 24) & 0xFF);
870}
871
872void OpenGLRenderer::setupDrawColor(int color, int alpha) {
873 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800874 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800875 mColorR = a * ((color >> 16) & 0xFF);
876 mColorG = a * ((color >> 8) & 0xFF);
877 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800878 mColorSet = true;
879 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
880}
881
Romain Guy86568192010-12-14 15:55:39 -0800882void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
883 mColorA = alpha / 255.0f;
884 const float a = mColorA / 255.0f;
885 mColorR = a * ((color >> 16) & 0xFF);
886 mColorG = a * ((color >> 8) & 0xFF);
887 mColorB = a * ((color ) & 0xFF);
888 mColorSet = true;
889 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
890}
891
Romain Guy70ca14e2010-12-13 18:24:33 -0800892void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
893 mColorA = a;
894 mColorR = r;
895 mColorG = g;
896 mColorB = b;
897 mColorSet = true;
898 mSetShaderColor = mDescription.setColor(r, g, b, a);
899}
900
Romain Guy86568192010-12-14 15:55:39 -0800901void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
902 mColorA = a;
903 mColorR = r;
904 mColorG = g;
905 mColorB = b;
906 mColorSet = true;
907 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
908}
909
Romain Guy70ca14e2010-12-13 18:24:33 -0800910void OpenGLRenderer::setupDrawShader() {
911 if (mShader) {
912 mShader->describe(mDescription, mCaches.extensions);
913 }
914}
915
916void OpenGLRenderer::setupDrawColorFilter() {
917 if (mColorFilter) {
918 mColorFilter->describe(mDescription, mCaches.extensions);
919 }
920}
921
922void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
923 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
924 mDescription, swapSrcDst);
925}
926
927void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
928 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
929 mDescription, swapSrcDst);
930}
931
932void OpenGLRenderer::setupDrawProgram() {
933 useProgram(mCaches.programCache.get(mDescription));
934}
935
936void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
937 mTrackDirtyRegions = false;
938}
939
940void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
941 bool ignoreTransform) {
942 mModelView.loadTranslate(left, top, 0.0f);
943 if (!ignoreTransform) {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
945 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
946 } else {
947 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
948 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
949 }
950}
951
Romain Guy8d0d4782010-12-14 20:13:35 -0800952void OpenGLRenderer::setupDrawModelViewIdentity() {
953 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
954}
955
Romain Guy70ca14e2010-12-13 18:24:33 -0800956void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
957 bool ignoreTransform, bool ignoreModelView) {
958 if (!ignoreModelView) {
959 mModelView.loadTranslate(left, top, 0.0f);
960 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800961 } else {
962 mModelView.loadIdentity();
963 }
Romain Guy86568192010-12-14 15:55:39 -0800964 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
965 if (!ignoreTransform) {
966 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
967 if (mTrackDirtyRegions && dirty) {
968 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
969 }
970 } else {
971 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
972 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
973 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800974}
975
976void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800977 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800978 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
979 }
980}
981
Romain Guy86568192010-12-14 15:55:39 -0800982void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800983 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800984 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800985 }
986}
987
Romain Guy70ca14e2010-12-13 18:24:33 -0800988void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
989 if (mShader) {
990 if (ignoreTransform) {
991 mModelView.loadInverse(*mSnapshot->transform);
992 }
993 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
994 }
995}
996
Romain Guy8d0d4782010-12-14 20:13:35 -0800997void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
998 if (mShader) {
999 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1000 }
1001}
1002
Romain Guy70ca14e2010-12-13 18:24:33 -08001003void OpenGLRenderer::setupDrawColorFilterUniforms() {
1004 if (mColorFilter) {
1005 mColorFilter->setupProgram(mCaches.currentProgram);
1006 }
1007}
1008
1009void OpenGLRenderer::setupDrawSimpleMesh() {
1010 mCaches.bindMeshBuffer();
1011 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1012 gMeshStride, 0);
1013}
1014
1015void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1016 bindTexture(texture);
1017 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1018
1019 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1020 glEnableVertexAttribArray(mTexCoordsSlot);
1021}
1022
1023void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1024 if (!vertices) {
1025 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1026 } else {
1027 mCaches.unbindMeshBuffer();
1028 }
1029 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1030 gMeshStride, vertices);
Romain Guy8d0d4782010-12-14 20:13:35 -08001031 if (mTexCoordsSlot > 0) {
1032 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1033 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001034}
1035
1036void OpenGLRenderer::finishDrawTexture() {
1037 glDisableVertexAttribArray(mTexCoordsSlot);
1038}
1039
1040///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001041// Drawing
1042///////////////////////////////////////////////////////////////////////////////
1043
Chet Haasedaf98e92011-01-10 14:10:36 -08001044bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001045 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1046 // will be performed by the display list itself
1047 if (displayList) {
Chet Haasedaf98e92011-01-10 14:10:36 -08001048 return displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001049 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001050 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001051}
1052
Chet Haase5c13d892010-10-08 08:37:55 -07001053void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001054 const float right = left + bitmap->width();
1055 const float bottom = top + bitmap->height();
1056
1057 if (quickReject(left, top, right, bottom)) {
1058 return;
1059 }
1060
Romain Guy86568192010-12-14 15:55:39 -08001061 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001062 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001063 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001064 const AutoTexture autoCleanup(texture);
1065
Romain Guy6926c722010-07-12 20:20:03 -07001066 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001067}
1068
Chet Haase5c13d892010-10-08 08:37:55 -07001069void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001070 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1071 const mat4 transform(*matrix);
1072 transform.mapRect(r);
1073
Romain Guy6926c722010-07-12 20:20:03 -07001074 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1075 return;
1076 }
1077
Romain Guy86568192010-12-14 15:55:39 -08001078 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001079 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001080 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001081 const AutoTexture autoCleanup(texture);
1082
Romain Guy5b3b3522010-10-27 18:57:51 -07001083 // This could be done in a cheaper way, all we need is pass the matrix
1084 // to the vertex shader. The save/restore is a bit overkill.
1085 save(SkCanvas::kMatrix_SaveFlag);
1086 concatMatrix(matrix);
1087 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1088 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001089}
1090
Romain Guy5a7b4662011-01-20 19:09:30 -08001091void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1092 float* vertices, int* colors, SkPaint* paint) {
1093 // TODO: Do a quickReject
1094 if (!vertices || mSnapshot->isIgnored()) {
1095 return;
1096 }
1097
1098 glActiveTexture(gTextureUnits[0]);
1099 Texture* texture = mCaches.textureCache.get(bitmap);
1100 if (!texture) return;
1101 const AutoTexture autoCleanup(texture);
1102 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1103
1104 int alpha;
1105 SkXfermode::Mode mode;
1106 getAlphaAndMode(paint, &alpha, &mode);
1107
Romain Guy5a7b4662011-01-20 19:09:30 -08001108 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001109
Romain Guya566b7c2011-01-23 16:36:11 -08001110 // TODO: Support the colors array
1111 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001112 TextureVertex* vertex = mesh;
1113 for (int32_t y = 0; y < meshHeight; y++) {
1114 for (int32_t x = 0; x < meshWidth; x++) {
1115 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1116
1117 float u1 = float(x) / meshWidth;
1118 float u2 = float(x + 1) / meshWidth;
1119 float v1 = float(y) / meshHeight;
1120 float v2 = float(y + 1) / meshHeight;
1121
1122 int ax = i + (meshWidth + 1) * 2;
1123 int ay = ax + 1;
1124 int bx = i;
1125 int by = bx + 1;
1126 int cx = i + 2;
1127 int cy = cx + 1;
1128 int dx = i + (meshWidth + 1) * 2 + 2;
1129 int dy = dx + 1;
1130
1131 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1132 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1133 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1134
1135 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1136 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1137 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
1138 }
1139 }
1140
1141 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1142 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
1143 GL_TRIANGLES, count);
1144}
1145
Romain Guy8ba548f2010-06-30 19:21:21 -07001146void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1147 float srcLeft, float srcTop, float srcRight, float srcBottom,
1148 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001149 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001150 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1151 return;
1152 }
1153
Romain Guy746b7402010-10-26 16:27:31 -07001154 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001155 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001156 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001157 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001158 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001159
Romain Guy8ba548f2010-06-30 19:21:21 -07001160 const float width = texture->width;
1161 const float height = texture->height;
1162
1163 const float u1 = srcLeft / width;
1164 const float v1 = srcTop / height;
1165 const float u2 = srcRight / width;
1166 const float v2 = srcBottom / height;
1167
Romain Guy03750a02010-10-18 14:06:08 -07001168 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001169 resetDrawTextureTexCoords(u1, v1, u2, v2);
1170
Romain Guy03750a02010-10-18 14:06:08 -07001171 int alpha;
1172 SkXfermode::Mode mode;
1173 getAlphaAndMode(paint, &alpha, &mode);
1174
Romain Guy6620c6d2010-12-06 18:07:02 -08001175 if (mSnapshot->transform->isPureTranslate()) {
1176 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1177 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1178
1179 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1180 texture->id, alpha / 255.0f, mode, texture->blend,
1181 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1182 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1183 } else {
1184 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1185 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1186 GL_TRIANGLE_STRIP, gMeshCount);
1187 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001188
1189 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1190}
1191
Romain Guy4aa90572010-09-26 18:40:37 -07001192void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001193 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001194 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001195 if (quickReject(left, top, right, bottom)) {
1196 return;
1197 }
1198
Romain Guy746b7402010-10-26 16:27:31 -07001199 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001200 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001201 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001202 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001203 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001204
1205 int alpha;
1206 SkXfermode::Mode mode;
1207 getAlphaAndMode(paint, &alpha, &mode);
1208
Romain Guy2728f962010-10-08 18:36:15 -07001209 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001210 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001211
Romain Guya5ef39a2010-12-03 16:48:20 -08001212 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001213 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001214#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001215 // Mark the current layer dirty where we are going to draw the patch
1216 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
1217 mSnapshot->region && mesh->hasEmptyQuads) {
1218 const size_t count = mesh->quads.size();
1219 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001220 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001221 if (pureTranslate) {
1222 const float x = (int) floorf(bounds.left + 0.5f);
1223 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001224 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001225 *mSnapshot->transform);
1226 } else {
1227 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1228 *mSnapshot->transform);
1229 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001230 }
1231 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001232#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001233
Romain Guy6620c6d2010-12-06 18:07:02 -08001234 if (pureTranslate) {
1235 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1236 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1237
1238 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1239 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1240 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1241 true, !mesh->hasEmptyQuads);
1242 } else {
1243 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1244 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1245 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1246 true, !mesh->hasEmptyQuads);
1247 }
Romain Guy054dc182010-10-15 17:55:25 -07001248 }
Romain Guyf7f93552010-07-08 19:17:03 -07001249}
1250
Chet Haase5c13d892010-10-08 08:37:55 -07001251void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001252 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001253
Romain Guya957eea2010-12-08 18:34:42 -08001254 const bool isAA = paint->isAntiAlias();
1255 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1256 // A stroke width of 0 has a special meaningin Skia:
1257 // it draws an unscaled 1px wide line
1258 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1259
Romain Guy759ea802010-09-16 20:49:46 -07001260 int alpha;
1261 SkXfermode::Mode mode;
1262 getAlphaAndMode(paint, &alpha, &mode);
1263
Romain Guya957eea2010-12-08 18:34:42 -08001264 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001265 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001266 if (!isHairLine) {
1267 // TODO: AA needs more vertices
1268 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001269 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001270 // TODO: AA will be different
1271 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001272 }
1273
Romain Guya957eea2010-12-08 18:34:42 -08001274 TextureVertex lines[verticesCount];
1275 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001276
Romain Guy8d0d4782010-12-14 20:13:35 -08001277 setupDraw();
1278 setupDrawColor(paint->getColor(), alpha);
1279 setupDrawColorFilter();
1280 setupDrawShader();
1281 setupDrawBlending(mode);
1282 setupDrawProgram();
1283 setupDrawModelViewIdentity();
1284 setupDrawColorUniforms();
1285 setupDrawColorFilterUniforms();
1286 setupDrawShaderIdentityUniforms();
1287 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001288
1289 if (!isHairLine) {
1290 // TODO: Handle the AA case
1291 for (int i = 0; i < count; i += 4) {
1292 // a = start point, b = end point
1293 vec2 a(points[i], points[i + 1]);
1294 vec2 b(points[i + 2], points[i + 3]);
1295
1296 // Bias to snap to the same pixels as Skia
1297 a += 0.375;
1298 b += 0.375;
1299
1300 // Find the normal to the line
1301 vec2 n = (b - a).copyNormalized() * strokeWidth;
1302 float x = n.x;
1303 n.x = -n.y;
1304 n.y = x;
1305
1306 // Four corners of the rectangle defining a thick line
1307 vec2 p1 = a - n;
1308 vec2 p2 = a + n;
1309 vec2 p3 = b + n;
1310 vec2 p4 = b - n;
1311
Romain Guy7230a742011-01-10 22:26:16 -08001312 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1313 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1314 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1315 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001316
Romain Guy7230a742011-01-10 22:26:16 -08001317 if (!quickReject(left, top, right, bottom)) {
1318 // Draw the line as 2 triangles, could be optimized
1319 // by using only 4 vertices and the correct indices
1320 // Also we should probably used non textured vertices
1321 // when line AA is disabled to save on bandwidth
1322 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1323 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1324 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1325 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1326 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1327 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1328
1329 generatedVerticesCount += 6;
1330
1331 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1332 }
Romain Guya957eea2010-12-08 18:34:42 -08001333 }
1334
Romain Guy7230a742011-01-10 22:26:16 -08001335 if (generatedVerticesCount > 0) {
1336 // GL_LINE does not give the result we want to match Skia
1337 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1338 }
Romain Guya957eea2010-12-08 18:34:42 -08001339 } else {
1340 // TODO: Handle the AA case
1341 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001342 const float left = fmin(points[i], points[i + 1]);
1343 const float right = fmax(points[i], points[i + 1]);
1344 const float top = fmin(points[i + 2], points[i + 3]);
1345 const float bottom = fmax(points[i + 2], points[i + 3]);
1346
1347 if (!quickReject(left, top, right, bottom)) {
1348 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1349 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1350
1351 generatedVerticesCount += 2;
1352
1353 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1354 }
Romain Guya957eea2010-12-08 18:34:42 -08001355 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001356
Romain Guy7230a742011-01-10 22:26:16 -08001357 if (generatedVerticesCount > 0) {
1358 glLineWidth(1.0f);
1359 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1360 }
Romain Guy29d89972010-09-22 16:10:57 -07001361 }
Romain Guy759ea802010-09-16 20:49:46 -07001362}
1363
Romain Guy85bf02f2010-06-22 13:11:24 -07001364void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001365 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001366 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001367
Romain Guyae88e5e2010-10-22 17:49:18 -07001368 Rect& clip(*mSnapshot->clipRect);
1369 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001370
Romain Guy3d58c032010-07-14 16:34:53 -07001371 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001372}
Romain Guy9d5316e2010-06-24 19:30:36 -07001373
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001374void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001375 if (!texture) return;
1376 const AutoTexture autoCleanup(texture);
1377
1378 const float x = left + texture->left - texture->offset;
1379 const float y = top + texture->top - texture->offset;
1380
1381 drawPathTexture(texture, x, y, paint);
1382}
1383
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001384void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1385 float rx, float ry, SkPaint* paint) {
1386 if (mSnapshot->isIgnored()) return;
1387
1388 glActiveTexture(gTextureUnits[0]);
1389 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1390 right - left, bottom - top, rx, ry, paint);
1391 drawShape(left, top, texture, paint);
1392}
1393
Romain Guy01d58e42011-01-19 21:54:02 -08001394void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1395 if (mSnapshot->isIgnored()) return;
1396
1397 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001398 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001399 drawShape(x - radius, y - radius, texture, paint);
1400}
Romain Guy01d58e42011-01-19 21:54:02 -08001401
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001402void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1403 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001404
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001405 glActiveTexture(gTextureUnits[0]);
1406 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1407 drawShape(left, top, texture, paint);
1408}
1409
Romain Guy8b2f5262011-01-23 16:15:02 -08001410void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1411 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1412 if (mSnapshot->isIgnored()) return;
1413
1414 if (fabs(sweepAngle) >= 360.0f) {
1415 drawOval(left, top, right, bottom, paint);
1416 return;
1417 }
1418
1419 glActiveTexture(gTextureUnits[0]);
1420 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1421 startAngle, sweepAngle, useCenter, paint);
1422 drawShape(left, top, texture, paint);
1423}
1424
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001425void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1426 SkPaint* paint) {
1427 if (mSnapshot->isIgnored()) return;
1428
1429 glActiveTexture(gTextureUnits[0]);
1430 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1431 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001432}
1433
Chet Haase5c13d892010-10-08 08:37:55 -07001434void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001435 if (p->getStyle() != SkPaint::kFill_Style) {
1436 drawRectAsShape(left, top, right, bottom, p);
1437 return;
1438 }
1439
Romain Guy6926c722010-07-12 20:20:03 -07001440 if (quickReject(left, top, right, bottom)) {
1441 return;
1442 }
1443
Romain Guy026c5e162010-06-28 17:12:22 -07001444 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001445 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001446 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1447 if (!isMode) {
1448 // Assume SRC_OVER
1449 mode = SkXfermode::kSrcOver_Mode;
1450 }
1451 } else {
1452 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001453 }
1454
1455 // Skia draws using the color's alpha channel if < 255
1456 // Otherwise, it uses the paint's alpha
1457 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001458 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001459 color |= p->getAlpha() << 24;
1460 }
1461
1462 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001463}
Romain Guy9d5316e2010-06-24 19:30:36 -07001464
Romain Guye8e62a42010-07-23 18:55:21 -07001465void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1466 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001467 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001468 return;
1469 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001470 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001471
Romain Guyf607bdc2010-09-10 19:20:06 -07001472 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001473
Romain Guya674ab72010-08-10 17:26:42 -07001474 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001475 switch (paint->getTextAlign()) {
1476 case SkPaint::kCenter_Align:
1477 length = paint->measureText(text, bytesCount);
1478 x -= length / 2.0f;
1479 break;
1480 case SkPaint::kRight_Align:
1481 length = paint->measureText(text, bytesCount);
1482 x -= length;
1483 break;
1484 default:
1485 break;
1486 }
1487
Romain Guy6620c6d2010-12-06 18:07:02 -08001488 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001489 const float oldX = x;
1490 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001491 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1492 if (pureTranslate) {
1493 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1494 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1495 }
1496
Romain Guyb45c0c92010-08-26 20:35:23 -07001497 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1498 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001499 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001500
Romain Guy86568192010-12-14 15:55:39 -08001501 int alpha;
1502 SkXfermode::Mode mode;
1503 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001504
Romain Guy1e45aae2010-08-13 19:39:53 -07001505 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001506 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001507 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001508 count, mShadowRadius);
1509 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001510
Romain Guy86568192010-12-14 15:55:39 -08001511 const float sx = x - shadow->left + mShadowDx;
1512 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001513
Romain Guy86568192010-12-14 15:55:39 -08001514 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1515
1516 glActiveTexture(gTextureUnits[0]);
1517 setupDraw();
1518 setupDrawWithTexture(true);
1519 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1520 setupDrawBlending(true, mode);
1521 setupDrawProgram();
1522 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1523 setupDrawTexture(shadow->id);
1524 setupDrawPureColorUniforms();
1525 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1526
Romain Guy0a417492010-08-16 20:26:20 -07001527 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001528 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001529 }
1530
Romain Guyb146b122010-12-15 17:06:45 -08001531 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1532 return;
1533 }
1534
Romain Guy6620c6d2010-12-06 18:07:02 -08001535 // Pick the appropriate texture filtering
1536 bool linearFilter = mSnapshot->transform->changesBounds();
1537 if (pureTranslate && !linearFilter) {
1538 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1539 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001540
Romain Guy86568192010-12-14 15:55:39 -08001541 glActiveTexture(gTextureUnits[0]);
1542 setupDraw();
1543 setupDrawDirtyRegionsDisabled();
1544 setupDrawWithTexture(true);
1545 setupDrawAlpha8Color(paint->getColor(), alpha);
1546 setupDrawColorFilter();
1547 setupDrawShader();
1548 setupDrawBlending(true, mode);
1549 setupDrawProgram();
1550 setupDrawModelView(x, y, x, y, pureTranslate, true);
1551 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1552 setupDrawPureColorUniforms();
1553 setupDrawColorFilterUniforms();
1554 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001555
Romain Guy6620c6d2010-12-06 18:07:02 -08001556 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001557 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1558
1559#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001560 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001561#else
Romain Guyf219da52011-01-16 12:54:25 -08001562 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001563#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001564
Romain Guy03750a02010-10-18 14:06:08 -07001565 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001566 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001567 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001568#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001569 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001570 if (!pureTranslate) {
1571 mSnapshot->transform->mapRect(bounds);
1572 }
Romain Guyf219da52011-01-16 12:54:25 -08001573 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001574 }
1575#endif
1576 }
Romain Guy694b5192010-07-21 21:33:20 -07001577
1578 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001579 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001580
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001581 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001582}
1583
Romain Guy7fbcc042010-08-04 15:40:07 -07001584void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001585 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001586
Romain Guy01d58e42011-01-19 21:54:02 -08001587 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001588
Romain Guyfb8b7632010-08-23 21:05:08 -07001589 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001590 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001591 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001592
Romain Guy8b55f372010-08-18 17:10:07 -07001593 const float x = texture->left - texture->offset;
1594 const float y = texture->top - texture->offset;
1595
Romain Guy01d58e42011-01-19 21:54:02 -08001596 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001597}
1598
Romain Guyada830f2011-01-13 12:13:20 -08001599void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1600 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001601 return;
1602 }
1603
1604 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001605
1606 int alpha;
1607 SkXfermode::Mode mode;
1608 getAlphaAndMode(paint, &alpha, &mode);
1609
Romain Guyada830f2011-01-13 12:13:20 -08001610 layer->alpha = alpha;
1611 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001612
Romain Guyf219da52011-01-16 12:54:25 -08001613
1614#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001615 if (!layer->region.isEmpty()) {
1616 if (layer->region.isRect()) {
1617 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1618 composeLayerRect(layer, r);
1619 } else if (layer->mesh) {
1620 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001621
Romain Guyc88e3572011-01-22 00:32:12 -08001622 setupDraw();
1623 setupDrawWithTexture();
1624 setupDrawColor(alpha, alpha, alpha, alpha);
1625 setupDrawColorFilter();
1626 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1627 setupDrawProgram();
1628 setupDrawDirtyRegionsDisabled();
1629 setupDrawPureColorUniforms();
1630 setupDrawColorFilterUniforms();
1631 setupDrawTexture(layer->texture);
1632 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1633 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001634
Romain Guyc88e3572011-01-22 00:32:12 -08001635 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1636 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001637
Romain Guyc88e3572011-01-22 00:32:12 -08001638 finishDrawTexture();
1639 }
Romain Guyf219da52011-01-16 12:54:25 -08001640 }
1641#else
Romain Guyada830f2011-01-13 12:13:20 -08001642 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1643 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001644#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001645}
1646
Romain Guy6926c722010-07-12 20:20:03 -07001647///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001648// Shaders
1649///////////////////////////////////////////////////////////////////////////////
1650
1651void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001652 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001653}
1654
Romain Guy06f96e22010-07-30 19:18:16 -07001655void OpenGLRenderer::setupShader(SkiaShader* shader) {
1656 mShader = shader;
1657 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001658 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001659 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001660}
1661
Romain Guyd27977d2010-07-14 19:18:51 -07001662///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001663// Color filters
1664///////////////////////////////////////////////////////////////////////////////
1665
1666void OpenGLRenderer::resetColorFilter() {
1667 mColorFilter = NULL;
1668}
1669
1670void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1671 mColorFilter = filter;
1672}
1673
1674///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001675// Drop shadow
1676///////////////////////////////////////////////////////////////////////////////
1677
1678void OpenGLRenderer::resetShadow() {
1679 mHasShadow = false;
1680}
1681
1682void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1683 mHasShadow = true;
1684 mShadowRadius = radius;
1685 mShadowDx = dx;
1686 mShadowDy = dy;
1687 mShadowColor = color;
1688}
1689
1690///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001691// Drawing implementation
1692///////////////////////////////////////////////////////////////////////////////
1693
Romain Guy01d58e42011-01-19 21:54:02 -08001694void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1695 float x, float y, SkPaint* paint) {
1696 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1697 return;
1698 }
1699
1700 int alpha;
1701 SkXfermode::Mode mode;
1702 getAlphaAndMode(paint, &alpha, &mode);
1703
1704 setupDraw();
1705 setupDrawWithTexture(true);
1706 setupDrawAlpha8Color(paint->getColor(), alpha);
1707 setupDrawColorFilter();
1708 setupDrawShader();
1709 setupDrawBlending(true, mode);
1710 setupDrawProgram();
1711 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1712 setupDrawTexture(texture->id);
1713 setupDrawPureColorUniforms();
1714 setupDrawColorFilterUniforms();
1715 setupDrawShaderUniforms();
1716 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1717
1718 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1719
1720 finishDrawTexture();
1721}
1722
Romain Guyf607bdc2010-09-10 19:20:06 -07001723// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001724#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1725#define kStdUnderline_Offset (1.0f / 9.0f)
1726#define kStdUnderline_Thickness (1.0f / 18.0f)
1727
1728void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1729 float x, float y, SkPaint* paint) {
1730 // Handle underline and strike-through
1731 uint32_t flags = paint->getFlags();
1732 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1733 float underlineWidth = length;
1734 // If length is > 0.0f, we already measured the text for the text alignment
1735 if (length <= 0.0f) {
1736 underlineWidth = paint->measureText(text, bytesCount);
1737 }
1738
1739 float offsetX = 0;
1740 switch (paint->getTextAlign()) {
1741 case SkPaint::kCenter_Align:
1742 offsetX = underlineWidth * 0.5f;
1743 break;
1744 case SkPaint::kRight_Align:
1745 offsetX = underlineWidth;
1746 break;
1747 default:
1748 break;
1749 }
1750
1751 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001752 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001753 // TODO: Support stroke width < 1.0f when we have AA lines
1754 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001755
Romain Guye20ecbd2010-09-22 19:49:04 -07001756 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001757 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001758
Romain Guyf6834472011-01-23 13:32:12 -08001759 int linesCount = 0;
1760 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1761 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1762
1763 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001764 float points[pointsCount];
1765 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001766
1767 if (flags & SkPaint::kUnderlineText_Flag) {
1768 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001769 points[currentPoint++] = left;
1770 points[currentPoint++] = top;
1771 points[currentPoint++] = left + underlineWidth;
1772 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001773 }
1774
1775 if (flags & SkPaint::kStrikeThruText_Flag) {
1776 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001777 points[currentPoint++] = left;
1778 points[currentPoint++] = top;
1779 points[currentPoint++] = left + underlineWidth;
1780 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001781 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001782
1783 SkPaint linesPaint(*paint);
1784 linesPaint.setStrokeWidth(strokeWidth);
1785
1786 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001787 }
1788 }
1789}
1790
Romain Guy026c5e162010-06-28 17:12:22 -07001791void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001792 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001793 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001794 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001795 color |= 0x00ffffff;
1796 }
1797
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 setupDraw();
1799 setupDrawColor(color);
1800 setupDrawShader();
1801 setupDrawColorFilter();
1802 setupDrawBlending(mode);
1803 setupDrawProgram();
1804 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1805 setupDrawColorUniforms();
1806 setupDrawShaderUniforms(ignoreTransform);
1807 setupDrawColorFilterUniforms();
1808 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001809
Romain Guyc95c8d62010-09-17 15:31:32 -07001810 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1811}
1812
Romain Guy82ba8142010-07-09 13:25:56 -07001813void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001814 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001815 int alpha;
1816 SkXfermode::Mode mode;
1817 getAlphaAndMode(paint, &alpha, &mode);
1818
Romain Guy8164c2d2010-10-25 18:03:28 -07001819 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1820
Romain Guy6620c6d2010-12-06 18:07:02 -08001821 if (mSnapshot->transform->isPureTranslate()) {
1822 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1823 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1824
1825 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1826 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1827 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1828 } else {
1829 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1830 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1831 GL_TRIANGLE_STRIP, gMeshCount);
1832 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001833}
1834
Romain Guybd6b79b2010-06-26 00:13:53 -07001835void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001836 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1837 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001838 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001839}
1840
1841void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001842 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001843 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001844 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001845
Romain Guy746b7402010-10-26 16:27:31 -07001846 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001847 setupDrawWithTexture();
1848 setupDrawColor(alpha, alpha, alpha, alpha);
1849 setupDrawColorFilter();
1850 setupDrawBlending(blend, mode, swapSrcDst);
1851 setupDrawProgram();
1852 if (!dirty) {
1853 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001854 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001855 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001856 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001857 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001858 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001859 }
Romain Guy86568192010-12-14 15:55:39 -08001860 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001861 setupDrawColorFilterUniforms();
1862 setupDrawTexture(texture);
1863 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001864
Romain Guy6820ac82010-09-15 18:11:50 -07001865 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001866
1867 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001868}
1869
Romain Guya5aed0d2010-09-09 14:42:43 -07001870void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001871 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001872 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1873 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001874 if (mode < SkXfermode::kPlus_Mode) {
1875 if (!mCaches.blend) {
1876 glEnable(GL_BLEND);
1877 }
Romain Guy82ba8142010-07-09 13:25:56 -07001878
Romain Guyf607bdc2010-09-10 19:20:06 -07001879 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1880 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001881
Romain Guya5aed0d2010-09-09 14:42:43 -07001882 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1883 glBlendFunc(sourceMode, destMode);
1884 mCaches.lastSrcMode = sourceMode;
1885 mCaches.lastDstMode = destMode;
1886 }
1887 } else {
1888 // These blend modes are not supported by OpenGL directly and have
1889 // to be implemented using shaders. Since the shader will perform
1890 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001891 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001892 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001893 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001894 }
1895
1896 if (mCaches.blend) {
1897 glDisable(GL_BLEND);
1898 }
1899 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001900 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001901 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001902 glDisable(GL_BLEND);
1903 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001904 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001905}
1906
Romain Guy889f8d12010-07-29 14:37:42 -07001907bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001908 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001909 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001910 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001911 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001912 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001913 }
Romain Guy6926c722010-07-12 20:20:03 -07001914 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001915}
1916
Romain Guy026c5e162010-06-28 17:12:22 -07001917void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001918 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001919 TextureVertex::setUV(v++, u1, v1);
1920 TextureVertex::setUV(v++, u2, v1);
1921 TextureVertex::setUV(v++, u1, v2);
1922 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001923}
1924
Chet Haase5c13d892010-10-08 08:37:55 -07001925void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001926 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001927 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001928 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1929 if (!isMode) {
1930 // Assume SRC_OVER
1931 *mode = SkXfermode::kSrcOver_Mode;
1932 }
1933 } else {
1934 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001935 }
1936
1937 // Skia draws using the color's alpha channel if < 255
1938 // Otherwise, it uses the paint's alpha
1939 int color = paint->getColor();
1940 *alpha = (color >> 24) & 0xFF;
1941 if (*alpha == 255) {
1942 *alpha = paint->getAlpha();
1943 }
1944 } else {
1945 *mode = SkXfermode::kSrcOver_Mode;
1946 *alpha = 255;
1947 }
Romain Guy026c5e162010-06-28 17:12:22 -07001948}
1949
Romain Guya5aed0d2010-09-09 14:42:43 -07001950SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1951 if (mode == NULL) {
1952 return SkXfermode::kSrcOver_Mode;
1953 }
1954 return mode->fMode;
1955}
1956
Romain Guy746b7402010-10-26 16:27:31 -07001957void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001958 bool bound = false;
1959 if (wrapS != texture->wrapS) {
1960 glBindTexture(GL_TEXTURE_2D, texture->id);
1961 bound = true;
1962 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1963 texture->wrapS = wrapS;
1964 }
1965 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001966 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001967 glBindTexture(GL_TEXTURE_2D, texture->id);
1968 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001969 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1970 texture->wrapT = wrapT;
1971 }
Romain Guya1db5742010-07-20 13:09:13 -07001972}
1973
Romain Guy9d5316e2010-06-24 19:30:36 -07001974}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001975}; // namespace android