blob: 98f8fc5e73f6e79f1426b8a8ae1c740f9470f74c [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 Guyfe48f652010-11-11 15:36:56 -0800139 mCaches.clearGarbage();
140
Romain Guy8aef54f2010-09-01 15:13:49 -0700141 mSnapshot = new Snapshot(mFirstSnapshot,
142 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700143 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700144
Romain Guyfb8b7632010-08-23 21:05:08 -0700145 glViewport(0, 0, mWidth, mHeight);
146
Romain Guyd90f23e2010-09-09 11:47:54 -0700147 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
Romain Guy6b7bd242010-10-06 19:49:23 -0700149 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700150 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152 glClear(GL_COLOR_BUFFER_BIT);
153 }
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Chet Haase0d200832010-11-05 15:36:16 -0700155 glEnable(GL_SCISSOR_TEST);
156 glScissor(0, 0, mWidth, mHeight);
Romain Guyb82da652010-07-30 11:36:12 -0700157 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700158}
159
Romain Guyb025b9c2010-09-16 14:16:48 -0700160void OpenGLRenderer::finish() {
161#if DEBUG_OPENGL
162 GLenum status = GL_NO_ERROR;
163 while ((status = glGetError()) != GL_NO_ERROR) {
164 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800165 switch (status) {
166 case GL_OUT_OF_MEMORY:
167 LOGE(" OpenGLRenderer is out of memory!");
168 break;
169 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700170 }
171#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800172#if DEBUG_MEMORY_USAGE
173 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800174#else
175 if (mCaches.getDebugLevel() & kDebugMemory) {
176 mCaches.dumpMemoryUsage();
177 }
Romain Guyc15008e2010-11-10 11:59:15 -0800178#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700179}
180
Romain Guy6c319ca2011-01-11 14:29:25 -0800181void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700182 if (mCaches.currentProgram) {
183 if (mCaches.currentProgram->isInUse()) {
184 mCaches.currentProgram->remove();
185 mCaches.currentProgram = NULL;
186 }
187 }
Romain Guy50c0f092010-10-19 11:42:22 -0700188 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700189}
190
Romain Guy6c319ca2011-01-11 14:29:25 -0800191void OpenGLRenderer::acquireContext() {
192 interrupt();
193}
194
195void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700196 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700197
198 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700199 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700200
Romain Guyf607bdc2010-09-10 19:20:06 -0700201 glDisable(GL_DITHER);
202
Romain Guy42f3a4b2011-01-19 13:42:26 -0800203 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700204 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700205
Romain Guy50c0f092010-10-19 11:42:22 -0700206 mCaches.blend = true;
207 glEnable(GL_BLEND);
208 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
209 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700210}
211
Romain Guy6c319ca2011-01-11 14:29:25 -0800212void OpenGLRenderer::releaseContext() {
213 resume();
214}
215
Chet Haasedaf98e92011-01-10 14:10:36 -0800216bool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
217 interrupt();
218 status_t result = (*functor)();
219 resume();
220 return (result == 0) ? false : true;
221}
222
Romain Guyf6a11b82010-06-23 17:47:49 -0700223///////////////////////////////////////////////////////////////////////////////
224// State management
225///////////////////////////////////////////////////////////////////////////////
226
Romain Guybb9524b2010-06-22 18:56:38 -0700227int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700228 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700229}
230
231int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700232 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700233}
234
235void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700236 if (mSaveCount > 1) {
237 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700238 }
Romain Guybb9524b2010-06-22 18:56:38 -0700239}
240
241void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700242 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700243
Romain Guy8fb95422010-08-17 18:38:51 -0700244 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700245 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700246 }
Romain Guybb9524b2010-06-22 18:56:38 -0700247}
248
Romain Guy8aef54f2010-09-01 15:13:49 -0700249int OpenGLRenderer::saveSnapshot(int flags) {
250 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700251 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700252}
253
254bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700255 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700256 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700257 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700258
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 sp<Snapshot> current = mSnapshot;
260 sp<Snapshot> previous = mSnapshot->previous;
261
Romain Guyeb993562010-10-05 18:14:38 -0700262 if (restoreOrtho) {
263 Rect& r = previous->viewport;
264 glViewport(r.left, r.top, r.right, r.bottom);
265 mOrthoMatrix.load(current->orthoMatrix);
266 }
267
Romain Guy8b55f372010-08-18 17:10:07 -0700268 mSaveCount--;
269 mSnapshot = previous;
270
Romain Guy2542d192010-08-18 11:47:12 -0700271 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700272 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700273 }
Romain Guy2542d192010-08-18 11:47:12 -0700274
Romain Guy5ec99242010-11-03 16:19:08 -0700275 if (restoreLayer) {
276 composeLayer(current, previous);
277 }
278
Romain Guy2542d192010-08-18 11:47:12 -0700279 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
Romain Guyf6a11b82010-06-23 17:47:49 -0700282///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700283// Layers
284///////////////////////////////////////////////////////////////////////////////
285
286int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700287 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700288 const GLuint previousFbo = mSnapshot->fbo;
289 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700290
Romain Guyaf636eb2010-12-09 17:47:21 -0800291 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700292 int alpha = 255;
293 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700294
Romain Guye45362c2010-11-03 19:58:32 -0700295 if (p) {
296 alpha = p->getAlpha();
297 if (!mCaches.extensions.hasFramebufferFetch()) {
298 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
299 if (!isMode) {
300 // Assume SRC_OVER
301 mode = SkXfermode::kSrcOver_Mode;
302 }
303 } else {
304 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700305 }
306 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700307 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700308 }
Romain Guyd55a8612010-06-28 17:42:46 -0700309
Romain Guydbc26d22010-10-11 17:58:29 -0700310 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
311 }
Romain Guyd55a8612010-06-28 17:42:46 -0700312
313 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700314}
315
316int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
317 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700318 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700319 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700320 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700321 SkPaint paint;
322 paint.setAlpha(alpha);
323 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700324 }
Romain Guyd55a8612010-06-28 17:42:46 -0700325}
Romain Guybd6b79b2010-06-26 00:13:53 -0700326
Romain Guy1c740bc2010-09-13 18:00:09 -0700327/**
328 * Layers are viewed by Skia are slightly different than layers in image editing
329 * programs (for instance.) When a layer is created, previously created layers
330 * and the frame buffer still receive every drawing command. For instance, if a
331 * layer is created and a shape intersecting the bounds of the layers and the
332 * framebuffer is draw, the shape will be drawn on both (unless the layer was
333 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
334 *
335 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
336 * texture. Unfortunately, this is inefficient as it requires every primitive to
337 * be drawn n + 1 times, where n is the number of active layers. In practice this
338 * means, for every primitive:
339 * - Switch active frame buffer
340 * - Change viewport, clip and projection matrix
341 * - Issue the drawing
342 *
343 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700344 * To avoid this, layers are implemented in a different way here, at least in the
345 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
346 * is set. When this flag is set we can redirect all drawing operations into a
347 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700348 *
349 * This implementation relies on the frame buffer being at least RGBA 8888. When
350 * a layer is created, only a texture is created, not an FBO. The content of the
351 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700352 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700353 * buffer and drawing continues as normal. This technique therefore treats the
354 * frame buffer as a scratch buffer for the layers.
355 *
356 * To compose the layers back onto the frame buffer, each layer texture
357 * (containing the original frame buffer data) is drawn as a simple quad over
358 * the frame buffer. The trick is that the quad is set as the composition
359 * destination in the blending equation, and the frame buffer becomes the source
360 * of the composition.
361 *
362 * Drawing layers with an alpha value requires an extra step before composition.
363 * An empty quad is drawn over the layer's region in the frame buffer. This quad
364 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
365 * quad is used to multiply the colors in the frame buffer. This is achieved by
366 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
367 * GL_ZERO, GL_SRC_ALPHA.
368 *
369 * Because glCopyTexImage2D() can be slow, an alternative implementation might
370 * be use to draw a single clipped layer. The implementation described above
371 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700372 *
373 * (1) The frame buffer is actually not cleared right away. To allow the GPU
374 * to potentially optimize series of calls to glCopyTexImage2D, the frame
375 * buffer is left untouched until the first drawing operation. Only when
376 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700377 */
Romain Guyd55a8612010-06-28 17:42:46 -0700378bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700379 float right, float bottom, int alpha, SkXfermode::Mode mode,
380 int flags, GLuint previousFbo) {
381 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700382 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700383
Romain Guyeb993562010-10-05 18:14:38 -0700384 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
385
Romain Guyf607bdc2010-09-10 19:20:06 -0700386 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700387 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700388 if (fboLayer) {
389 // Clear the previous layer regions before we change the viewport
390 clearLayerRegions();
391 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700392 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700393
Romain Guyeb993562010-10-05 18:14:38 -0700394 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700395 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700396
Romain Guyae88e5e2010-10-22 17:49:18 -0700397 // We cannot work with sub-pixels in this case
398 bounds.snapToPixelBoundaries();
399
Romain Guyae517592010-10-22 10:40:27 -0700400 // When the layer is not an FBO, we may use glCopyTexImage so we
401 // need to make sure the layer does not extend outside the bounds
402 // of the framebuffer
403 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700404 }
Romain Guybf434112010-09-16 14:40:17 -0700405
Romain Guy746b7402010-10-26 16:27:31 -0700406 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
407 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800408 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700409 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700410 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700411 }
412
413 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800414 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700415 return false;
416 }
Romain Guyf18fd992010-07-08 11:45:51 -0700417
Romain Guy5b3b3522010-10-27 18:57:51 -0700418 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700419 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700420 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700421 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700422 }
423
Romain Guydda570202010-07-06 11:39:32 -0700424 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700425 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700426 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700427 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
428 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800429 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700430
Romain Guy8fb95422010-08-17 18:38:51 -0700431 // Save the layer in the snapshot
432 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700433 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700434
Romain Guyeb993562010-10-05 18:14:38 -0700435 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700436 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700437 } else {
438 // Copy the framebuffer into the layer
439 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800440 if (!bounds.isEmpty()) {
441 if (layer->empty) {
442 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
443 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
444 layer->empty = false;
445 } else {
446 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
447 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
448 }
449 // Enqueue the buffer coordinates to clear the corresponding region later
450 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700451 }
Romain Guyeb993562010-10-05 18:14:38 -0700452 }
Romain Guyf86ef572010-07-01 11:05:42 -0700453
Romain Guyd55a8612010-06-28 17:42:46 -0700454 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700455}
456
Romain Guy5b3b3522010-10-27 18:57:51 -0700457bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
458 GLuint previousFbo) {
459 layer->fbo = mCaches.fboCache.get();
460
461#if RENDER_LAYERS_AS_REGIONS
462 snapshot->region = &snapshot->layer->region;
463 snapshot->flags |= Snapshot::kFlagFboTarget;
464#endif
465
466 Rect clip(bounds);
467 snapshot->transform->mapRect(clip);
468 clip.intersect(*snapshot->clipRect);
469 clip.snapToPixelBoundaries();
470 clip.intersect(snapshot->previous->viewport);
471
472 mat4 inverse;
473 inverse.loadInverse(*mSnapshot->transform);
474
475 inverse.mapRect(clip);
476 clip.snapToPixelBoundaries();
477 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700478 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700479
480 snapshot->flags |= Snapshot::kFlagIsFboLayer;
481 snapshot->fbo = layer->fbo;
482 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700483 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
484 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
485 snapshot->height = bounds.getHeight();
486 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
487 snapshot->orthoMatrix.load(mOrthoMatrix);
488
489 // Bind texture to FBO
490 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
491 glBindTexture(GL_TEXTURE_2D, layer->texture);
492
493 // Initialize the texture if needed
494 if (layer->empty) {
495 layer->empty = false;
496 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
497 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
498 }
499
500 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
501 layer->texture, 0);
502
503#if DEBUG_LAYERS_AS_REGIONS
504 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
505 if (status != GL_FRAMEBUFFER_COMPLETE) {
506 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
507
508 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
509 glDeleteTextures(1, &layer->texture);
510 mCaches.fboCache.put(layer->fbo);
511
512 delete layer;
513
514 return false;
515 }
516#endif
517
518 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
519 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
520 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
521 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
522 glClear(GL_COLOR_BUFFER_BIT);
523
524 dirtyClip();
525
526 // Change the ortho projection
527 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
528 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
529
530 return true;
531}
532
Romain Guy1c740bc2010-09-13 18:00:09 -0700533/**
534 * Read the documentation of createLayer() before doing anything in this method.
535 */
Romain Guy1d83e192010-08-17 11:37:00 -0700536void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
537 if (!current->layer) {
538 LOGE("Attempting to compose a layer that does not exist");
539 return;
540 }
541
Romain Guy5b3b3522010-10-27 18:57:51 -0700542 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700543
544 if (fboLayer) {
545 // Unbind current FBO and restore previous one
546 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
547 }
548
Romain Guy1d83e192010-08-17 11:37:00 -0700549 Layer* layer = current->layer;
550 const Rect& rect = layer->layer;
551
Romain Guyeb993562010-10-05 18:14:38 -0700552 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700553 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700554 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700555 // Required below, composeLayerRect() will divide by 255
556 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700557 }
558
Romain Guy03750a02010-10-18 14:06:08 -0700559 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700560
Romain Guy746b7402010-10-26 16:27:31 -0700561 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700562
Romain Guy5b3b3522010-10-27 18:57:51 -0700563 // When the layer is stored in an FBO, we can save a bit of fillrate by
564 // drawing only the dirty region
565 if (fboLayer) {
566 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800567 if (layer->colorFilter) {
568 setupColorFilter(layer->colorFilter);
569 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700570 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800571 if (layer->colorFilter) {
572 resetColorFilter();
573 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700574 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800575 if (!rect.isEmpty()) {
576 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
577 composeLayerRect(layer, rect, true);
578 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700579 }
Romain Guy8b55f372010-08-18 17:10:07 -0700580
Romain Guyeb993562010-10-05 18:14:38 -0700581 if (fboLayer) {
582 // Detach the texture from the FBO
583 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
584 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
585 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
586
587 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
588 mCaches.fboCache.put(current->fbo);
589 }
590
Romain Guy746b7402010-10-26 16:27:31 -0700591 dirtyClip();
592
Romain Guyeb993562010-10-05 18:14:38 -0700593 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700594 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700595 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700596 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700597 delete layer;
598 }
599}
600
Romain Guy5b3b3522010-10-27 18:57:51 -0700601void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
602 const Rect& texCoords = layer->texCoords;
603 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
604
605 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
606 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
607 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
608
609 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
610}
611
612void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
613#if RENDER_LAYERS_AS_REGIONS
614 if (layer->region.isRect()) {
615 composeLayerRect(layer, rect);
616 layer->region.clear();
617 return;
618 }
619
620 if (!layer->region.isEmpty()) {
621 size_t count;
622 const android::Rect* rects = layer->region.getArray(&count);
623
Romain Guy5b3b3522010-10-27 18:57:51 -0700624 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700625 const float texX = 1.0f / float(layer->width);
626 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800627 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700628
629 TextureVertex* mesh = mCaches.getRegionMesh();
630 GLsizei numQuads = 0;
631
Romain Guy7230a742011-01-10 22:26:16 -0800632 setupDraw();
633 setupDrawWithTexture();
634 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800635 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800636 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
637 setupDrawProgram();
638 setupDrawDirtyRegionsDisabled();
639 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800640 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800641 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800642 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800643 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700644
645 for (size_t i = 0; i < count; i++) {
646 const android::Rect* r = &rects[i];
647
648 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800649 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700650 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800651 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700652
653 // TODO: Reject quads outside of the clip
654 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
655 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
656 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
657 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
658
659 numQuads++;
660
661 if (numQuads >= REGION_MESH_QUAD_COUNT) {
662 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
663 numQuads = 0;
664 mesh = mCaches.getRegionMesh();
665 }
666 }
667
668 if (numQuads > 0) {
669 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
670 }
671
672 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800673 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700674
675#if DEBUG_LAYERS_AS_REGIONS
676 uint32_t colors[] = {
677 0x7fff0000, 0x7f00ff00,
678 0x7f0000ff, 0x7fff00ff,
679 };
680
681 int offset = 0;
682 int32_t top = rects[0].top;
683 int i = 0;
684
685 for (size_t i = 0; i < count; i++) {
686 if (top != rects[i].top) {
687 offset ^= 0x2;
688 top = rects[i].top;
689 }
690
691 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
692 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
693 SkXfermode::kSrcOver_Mode);
694 }
695#endif
696
697 layer->region.clear();
698 }
699#else
700 composeLayerRect(layer, rect);
701#endif
702}
703
704void OpenGLRenderer::dirtyLayer(const float left, const float top,
705 const float right, const float bottom, const mat4 transform) {
706#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800707 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700708 Rect bounds(left, top, right, bottom);
709 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800710 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 }
712#endif
713}
714
715void OpenGLRenderer::dirtyLayer(const float left, const float top,
716 const float right, const float bottom) {
717#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800718 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700719 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800720 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800721 }
722#endif
723}
724
725void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
726#if RENDER_LAYERS_AS_REGIONS
727 if (bounds.intersect(*mSnapshot->clipRect)) {
728 bounds.snapToPixelBoundaries();
729 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
730 if (!dirty.isEmpty()) {
731 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700732 }
733 }
734#endif
735}
736
Romain Guy86942302010-09-12 13:02:16 -0700737void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800738 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700739
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 Rect clipRect(*mSnapshot->clipRect);
741 clipRect.snapToPixelBoundaries();
742
Romain Guy86942302010-09-12 13:02:16 -0700743 for (uint32_t i = 0; i < mLayers.size(); i++) {
744 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 if (clipRect.intersects(*bounds)) {
746 // Clear the framebuffer where the layer will draw
747 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
748 bounds->getWidth(), bounds->getHeight());
749 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
750 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700751
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 // Restore the clip
753 dirtyClip();
754 }
Romain Guy86942302010-09-12 13:02:16 -0700755
756 delete bounds;
757 }
Romain Guy86942302010-09-12 13:02:16 -0700758
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700760}
761
Romain Guybd6b79b2010-06-26 00:13:53 -0700762///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700763// Transforms
764///////////////////////////////////////////////////////////////////////////////
765
766void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700767 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700768}
769
770void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700771 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700772}
773
774void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700775 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700776}
777
Romain Guy807daf72011-01-18 11:19:19 -0800778void OpenGLRenderer::skew(float sx, float sy) {
779 mSnapshot->transform->skew(sx, sy);
780}
781
Romain Guyf6a11b82010-06-23 17:47:49 -0700782void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700783 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700784}
785
Romain Guy41030da2010-10-13 13:40:37 -0700786const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700787 if (mSnapshot->fbo != 0) {
788 return &mSnapshot->transform->data[0];
789 }
790 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700791}
792
Romain Guyf6a11b82010-06-23 17:47:49 -0700793void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700794 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700795}
796
797void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700798 SkMatrix transform;
799 mSnapshot->transform->copyTo(transform);
800 transform.preConcat(*matrix);
801 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700802}
803
804///////////////////////////////////////////////////////////////////////////////
805// Clipping
806///////////////////////////////////////////////////////////////////////////////
807
Romain Guybb9524b2010-06-22 18:56:38 -0700808void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700809 Rect clip(*mSnapshot->clipRect);
810 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700811 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700812 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700813}
814
815const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700816 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700817}
818
Romain Guyc7d53492010-06-25 13:41:57 -0700819bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800820 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700821 return true;
822 }
823
Romain Guy1d83e192010-08-17 11:37:00 -0700824 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700825 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700826 r.snapToPixelBoundaries();
827
828 Rect clipRect(*mSnapshot->clipRect);
829 clipRect.snapToPixelBoundaries();
830
831 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700832}
833
Romain Guy079ba2c2010-07-16 14:12:24 -0700834bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
835 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700836 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700837 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700838 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700839 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700840}
841
Romain Guyf6a11b82010-06-23 17:47:49 -0700842///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800843// Drawing commands
844///////////////////////////////////////////////////////////////////////////////
845
846void OpenGLRenderer::setupDraw() {
847 clearLayerRegions();
848 if (mDirtyClip) {
849 setScissorFromClip();
850 }
851 mDescription.reset();
852 mSetShaderColor = false;
853 mColorSet = false;
854 mColorA = mColorR = mColorG = mColorB = 0.0f;
855 mTextureUnit = 0;
856 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800857 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800858}
859
860void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
861 mDescription.hasTexture = true;
862 mDescription.hasAlpha8Texture = isAlpha8;
863}
864
865void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800866 setupDrawColor(color, (color >> 24) & 0xFF);
867}
868
869void OpenGLRenderer::setupDrawColor(int color, int alpha) {
870 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800871 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800872 mColorR = a * ((color >> 16) & 0xFF);
873 mColorG = a * ((color >> 8) & 0xFF);
874 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800875 mColorSet = true;
876 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
877}
878
Romain Guy86568192010-12-14 15:55:39 -0800879void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
880 mColorA = alpha / 255.0f;
881 const float a = mColorA / 255.0f;
882 mColorR = a * ((color >> 16) & 0xFF);
883 mColorG = a * ((color >> 8) & 0xFF);
884 mColorB = a * ((color ) & 0xFF);
885 mColorSet = true;
886 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
887}
888
Romain Guy70ca14e2010-12-13 18:24:33 -0800889void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
890 mColorA = a;
891 mColorR = r;
892 mColorG = g;
893 mColorB = b;
894 mColorSet = true;
895 mSetShaderColor = mDescription.setColor(r, g, b, a);
896}
897
Romain Guy86568192010-12-14 15:55:39 -0800898void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
899 mColorA = a;
900 mColorR = r;
901 mColorG = g;
902 mColorB = b;
903 mColorSet = true;
904 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
905}
906
Romain Guy70ca14e2010-12-13 18:24:33 -0800907void OpenGLRenderer::setupDrawShader() {
908 if (mShader) {
909 mShader->describe(mDescription, mCaches.extensions);
910 }
911}
912
913void OpenGLRenderer::setupDrawColorFilter() {
914 if (mColorFilter) {
915 mColorFilter->describe(mDescription, mCaches.extensions);
916 }
917}
918
919void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
920 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
921 mDescription, swapSrcDst);
922}
923
924void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
925 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
926 mDescription, swapSrcDst);
927}
928
929void OpenGLRenderer::setupDrawProgram() {
930 useProgram(mCaches.programCache.get(mDescription));
931}
932
933void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
934 mTrackDirtyRegions = false;
935}
936
937void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
938 bool ignoreTransform) {
939 mModelView.loadTranslate(left, top, 0.0f);
940 if (!ignoreTransform) {
941 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
942 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
943 } else {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
945 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
946 }
947}
948
Romain Guy8d0d4782010-12-14 20:13:35 -0800949void OpenGLRenderer::setupDrawModelViewIdentity() {
950 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
951}
952
Romain Guy70ca14e2010-12-13 18:24:33 -0800953void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
954 bool ignoreTransform, bool ignoreModelView) {
955 if (!ignoreModelView) {
956 mModelView.loadTranslate(left, top, 0.0f);
957 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800958 } else {
959 mModelView.loadIdentity();
960 }
Romain Guy86568192010-12-14 15:55:39 -0800961 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
962 if (!ignoreTransform) {
963 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
964 if (mTrackDirtyRegions && dirty) {
965 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
966 }
967 } else {
968 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
969 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
970 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800971}
972
973void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800974 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800975 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
976 }
977}
978
Romain Guy86568192010-12-14 15:55:39 -0800979void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800980 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800981 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800982 }
983}
984
Romain Guy70ca14e2010-12-13 18:24:33 -0800985void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
986 if (mShader) {
987 if (ignoreTransform) {
988 mModelView.loadInverse(*mSnapshot->transform);
989 }
990 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
991 }
992}
993
Romain Guy8d0d4782010-12-14 20:13:35 -0800994void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
995 if (mShader) {
996 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
997 }
998}
999
Romain Guy70ca14e2010-12-13 18:24:33 -08001000void OpenGLRenderer::setupDrawColorFilterUniforms() {
1001 if (mColorFilter) {
1002 mColorFilter->setupProgram(mCaches.currentProgram);
1003 }
1004}
1005
1006void OpenGLRenderer::setupDrawSimpleMesh() {
1007 mCaches.bindMeshBuffer();
1008 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1009 gMeshStride, 0);
1010}
1011
1012void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1013 bindTexture(texture);
1014 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1015
1016 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1017 glEnableVertexAttribArray(mTexCoordsSlot);
1018}
1019
1020void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1021 if (!vertices) {
1022 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1023 } else {
1024 mCaches.unbindMeshBuffer();
1025 }
1026 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1027 gMeshStride, vertices);
Romain Guy8d0d4782010-12-14 20:13:35 -08001028 if (mTexCoordsSlot > 0) {
1029 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1030 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001031}
1032
1033void OpenGLRenderer::finishDrawTexture() {
1034 glDisableVertexAttribArray(mTexCoordsSlot);
1035}
1036
1037///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001038// Drawing
1039///////////////////////////////////////////////////////////////////////////////
1040
Chet Haasedaf98e92011-01-10 14:10:36 -08001041bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001042 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1043 // will be performed by the display list itself
1044 if (displayList) {
Chet Haasedaf98e92011-01-10 14:10:36 -08001045 return displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001046 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001047 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001048}
1049
Chet Haase5c13d892010-10-08 08:37:55 -07001050void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001051 const float right = left + bitmap->width();
1052 const float bottom = top + bitmap->height();
1053
1054 if (quickReject(left, top, right, bottom)) {
1055 return;
1056 }
1057
Romain Guy86568192010-12-14 15:55:39 -08001058 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001059 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001060 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001061 const AutoTexture autoCleanup(texture);
1062
Romain Guy6926c722010-07-12 20:20:03 -07001063 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001064}
1065
Chet Haase5c13d892010-10-08 08:37:55 -07001066void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001067 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1068 const mat4 transform(*matrix);
1069 transform.mapRect(r);
1070
Romain Guy6926c722010-07-12 20:20:03 -07001071 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1072 return;
1073 }
1074
Romain Guy86568192010-12-14 15:55:39 -08001075 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001076 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001077 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001078 const AutoTexture autoCleanup(texture);
1079
Romain Guy5b3b3522010-10-27 18:57:51 -07001080 // This could be done in a cheaper way, all we need is pass the matrix
1081 // to the vertex shader. The save/restore is a bit overkill.
1082 save(SkCanvas::kMatrix_SaveFlag);
1083 concatMatrix(matrix);
1084 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1085 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001086}
1087
Romain Guy5a7b4662011-01-20 19:09:30 -08001088void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1089 float* vertices, int* colors, SkPaint* paint) {
1090 // TODO: Do a quickReject
1091 if (!vertices || mSnapshot->isIgnored()) {
1092 return;
1093 }
1094
1095 glActiveTexture(gTextureUnits[0]);
1096 Texture* texture = mCaches.textureCache.get(bitmap);
1097 if (!texture) return;
1098 const AutoTexture autoCleanup(texture);
1099 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1100
1101 int alpha;
1102 SkXfermode::Mode mode;
1103 getAlphaAndMode(paint, &alpha, &mode);
1104
Romain Guy5a7b4662011-01-20 19:09:30 -08001105 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001106
Romain Guya566b7c2011-01-23 16:36:11 -08001107 // TODO: Support the colors array
1108 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001109 TextureVertex* vertex = mesh;
1110 for (int32_t y = 0; y < meshHeight; y++) {
1111 for (int32_t x = 0; x < meshWidth; x++) {
1112 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1113
1114 float u1 = float(x) / meshWidth;
1115 float u2 = float(x + 1) / meshWidth;
1116 float v1 = float(y) / meshHeight;
1117 float v2 = float(y + 1) / meshHeight;
1118
1119 int ax = i + (meshWidth + 1) * 2;
1120 int ay = ax + 1;
1121 int bx = i;
1122 int by = bx + 1;
1123 int cx = i + 2;
1124 int cy = cx + 1;
1125 int dx = i + (meshWidth + 1) * 2 + 2;
1126 int dy = dx + 1;
1127
1128 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1129 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1130 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1131
1132 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1133 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1134 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
1135 }
1136 }
1137
1138 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1139 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
1140 GL_TRIANGLES, count);
1141}
1142
Romain Guy8ba548f2010-06-30 19:21:21 -07001143void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1144 float srcLeft, float srcTop, float srcRight, float srcBottom,
1145 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001146 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001147 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1148 return;
1149 }
1150
Romain Guy746b7402010-10-26 16:27:31 -07001151 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001152 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001153 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001154 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001155 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001156
Romain Guy8ba548f2010-06-30 19:21:21 -07001157 const float width = texture->width;
1158 const float height = texture->height;
1159
1160 const float u1 = srcLeft / width;
1161 const float v1 = srcTop / height;
1162 const float u2 = srcRight / width;
1163 const float v2 = srcBottom / height;
1164
Romain Guy03750a02010-10-18 14:06:08 -07001165 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001166 resetDrawTextureTexCoords(u1, v1, u2, v2);
1167
Romain Guy03750a02010-10-18 14:06:08 -07001168 int alpha;
1169 SkXfermode::Mode mode;
1170 getAlphaAndMode(paint, &alpha, &mode);
1171
Romain Guy6620c6d2010-12-06 18:07:02 -08001172 if (mSnapshot->transform->isPureTranslate()) {
1173 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1174 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1175
1176 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1177 texture->id, alpha / 255.0f, mode, texture->blend,
1178 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1179 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1180 } else {
1181 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1182 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1183 GL_TRIANGLE_STRIP, gMeshCount);
1184 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001185
1186 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1187}
1188
Romain Guy4aa90572010-09-26 18:40:37 -07001189void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001190 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001191 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001192 if (quickReject(left, top, right, bottom)) {
1193 return;
1194 }
1195
Romain Guy746b7402010-10-26 16:27:31 -07001196 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001197 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001198 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001199 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001200 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001201
1202 int alpha;
1203 SkXfermode::Mode mode;
1204 getAlphaAndMode(paint, &alpha, &mode);
1205
Romain Guy2728f962010-10-08 18:36:15 -07001206 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001207 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001208
Romain Guya5ef39a2010-12-03 16:48:20 -08001209 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001210 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001211#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001212 // Mark the current layer dirty where we are going to draw the patch
1213 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
1214 mSnapshot->region && mesh->hasEmptyQuads) {
1215 const size_t count = mesh->quads.size();
1216 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001217 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001218 if (pureTranslate) {
1219 const float x = (int) floorf(bounds.left + 0.5f);
1220 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001221 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001222 *mSnapshot->transform);
1223 } else {
1224 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1225 *mSnapshot->transform);
1226 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001227 }
1228 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001229#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001230
Romain Guy6620c6d2010-12-06 18:07:02 -08001231 if (pureTranslate) {
1232 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1233 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1234
1235 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1236 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1237 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1238 true, !mesh->hasEmptyQuads);
1239 } else {
1240 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1241 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1242 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1243 true, !mesh->hasEmptyQuads);
1244 }
Romain Guy054dc182010-10-15 17:55:25 -07001245 }
Romain Guyf7f93552010-07-08 19:17:03 -07001246}
1247
Chet Haase5c13d892010-10-08 08:37:55 -07001248void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001249 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001250
Romain Guya957eea2010-12-08 18:34:42 -08001251 const bool isAA = paint->isAntiAlias();
1252 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1253 // A stroke width of 0 has a special meaningin Skia:
1254 // it draws an unscaled 1px wide line
1255 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1256
Romain Guy759ea802010-09-16 20:49:46 -07001257 int alpha;
1258 SkXfermode::Mode mode;
1259 getAlphaAndMode(paint, &alpha, &mode);
1260
Romain Guya957eea2010-12-08 18:34:42 -08001261 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001262 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001263 if (!isHairLine) {
1264 // TODO: AA needs more vertices
1265 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001266 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001267 // TODO: AA will be different
1268 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001269 }
1270
Romain Guya957eea2010-12-08 18:34:42 -08001271 TextureVertex lines[verticesCount];
1272 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001273
Romain Guy8d0d4782010-12-14 20:13:35 -08001274 setupDraw();
1275 setupDrawColor(paint->getColor(), alpha);
1276 setupDrawColorFilter();
1277 setupDrawShader();
1278 setupDrawBlending(mode);
1279 setupDrawProgram();
1280 setupDrawModelViewIdentity();
1281 setupDrawColorUniforms();
1282 setupDrawColorFilterUniforms();
1283 setupDrawShaderIdentityUniforms();
1284 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001285
1286 if (!isHairLine) {
1287 // TODO: Handle the AA case
1288 for (int i = 0; i < count; i += 4) {
1289 // a = start point, b = end point
1290 vec2 a(points[i], points[i + 1]);
1291 vec2 b(points[i + 2], points[i + 3]);
1292
1293 // Bias to snap to the same pixels as Skia
1294 a += 0.375;
1295 b += 0.375;
1296
1297 // Find the normal to the line
1298 vec2 n = (b - a).copyNormalized() * strokeWidth;
1299 float x = n.x;
1300 n.x = -n.y;
1301 n.y = x;
1302
1303 // Four corners of the rectangle defining a thick line
1304 vec2 p1 = a - n;
1305 vec2 p2 = a + n;
1306 vec2 p3 = b + n;
1307 vec2 p4 = b - n;
1308
Romain Guy7230a742011-01-10 22:26:16 -08001309 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1310 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1311 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1312 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001313
Romain Guy7230a742011-01-10 22:26:16 -08001314 if (!quickReject(left, top, right, bottom)) {
1315 // Draw the line as 2 triangles, could be optimized
1316 // by using only 4 vertices and the correct indices
1317 // Also we should probably used non textured vertices
1318 // when line AA is disabled to save on bandwidth
1319 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1320 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1321 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1322 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1323 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1324 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1325
1326 generatedVerticesCount += 6;
1327
1328 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1329 }
Romain Guya957eea2010-12-08 18:34:42 -08001330 }
1331
Romain Guy7230a742011-01-10 22:26:16 -08001332 if (generatedVerticesCount > 0) {
1333 // GL_LINE does not give the result we want to match Skia
1334 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1335 }
Romain Guya957eea2010-12-08 18:34:42 -08001336 } else {
1337 // TODO: Handle the AA case
1338 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001339 const float left = fmin(points[i], points[i + 1]);
1340 const float right = fmax(points[i], points[i + 1]);
1341 const float top = fmin(points[i + 2], points[i + 3]);
1342 const float bottom = fmax(points[i + 2], points[i + 3]);
1343
1344 if (!quickReject(left, top, right, bottom)) {
1345 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1346 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1347
1348 generatedVerticesCount += 2;
1349
1350 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1351 }
Romain Guya957eea2010-12-08 18:34:42 -08001352 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001353
Romain Guy7230a742011-01-10 22:26:16 -08001354 if (generatedVerticesCount > 0) {
1355 glLineWidth(1.0f);
1356 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1357 }
Romain Guy29d89972010-09-22 16:10:57 -07001358 }
Romain Guy759ea802010-09-16 20:49:46 -07001359}
1360
Romain Guy85bf02f2010-06-22 13:11:24 -07001361void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001362 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001363 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001364
Romain Guyae88e5e2010-10-22 17:49:18 -07001365 Rect& clip(*mSnapshot->clipRect);
1366 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001367
Romain Guy3d58c032010-07-14 16:34:53 -07001368 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001369}
Romain Guy9d5316e2010-06-24 19:30:36 -07001370
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001371void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001372 if (!texture) return;
1373 const AutoTexture autoCleanup(texture);
1374
1375 const float x = left + texture->left - texture->offset;
1376 const float y = top + texture->top - texture->offset;
1377
1378 drawPathTexture(texture, x, y, paint);
1379}
1380
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001381void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1382 float rx, float ry, SkPaint* paint) {
1383 if (mSnapshot->isIgnored()) return;
1384
1385 glActiveTexture(gTextureUnits[0]);
1386 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1387 right - left, bottom - top, rx, ry, paint);
1388 drawShape(left, top, texture, paint);
1389}
1390
Romain Guy01d58e42011-01-19 21:54:02 -08001391void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1392 if (mSnapshot->isIgnored()) return;
1393
1394 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001395 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001396 drawShape(x - radius, y - radius, texture, paint);
1397}
Romain Guy01d58e42011-01-19 21:54:02 -08001398
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001399void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1400 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001401
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001402 glActiveTexture(gTextureUnits[0]);
1403 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1404 drawShape(left, top, texture, paint);
1405}
1406
Romain Guy8b2f5262011-01-23 16:15:02 -08001407void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1408 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1409 if (mSnapshot->isIgnored()) return;
1410
1411 if (fabs(sweepAngle) >= 360.0f) {
1412 drawOval(left, top, right, bottom, paint);
1413 return;
1414 }
1415
1416 glActiveTexture(gTextureUnits[0]);
1417 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1418 startAngle, sweepAngle, useCenter, paint);
1419 drawShape(left, top, texture, paint);
1420}
1421
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001422void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1423 SkPaint* paint) {
1424 if (mSnapshot->isIgnored()) return;
1425
1426 glActiveTexture(gTextureUnits[0]);
1427 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1428 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001429}
1430
Chet Haase5c13d892010-10-08 08:37:55 -07001431void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001432 if (p->getStyle() != SkPaint::kFill_Style) {
1433 drawRectAsShape(left, top, right, bottom, p);
1434 return;
1435 }
1436
Romain Guy6926c722010-07-12 20:20:03 -07001437 if (quickReject(left, top, right, bottom)) {
1438 return;
1439 }
1440
Romain Guy026c5e162010-06-28 17:12:22 -07001441 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001442 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001443 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1444 if (!isMode) {
1445 // Assume SRC_OVER
1446 mode = SkXfermode::kSrcOver_Mode;
1447 }
1448 } else {
1449 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001450 }
1451
1452 // Skia draws using the color's alpha channel if < 255
1453 // Otherwise, it uses the paint's alpha
1454 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001455 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001456 color |= p->getAlpha() << 24;
1457 }
1458
1459 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001460}
Romain Guy9d5316e2010-06-24 19:30:36 -07001461
Romain Guye8e62a42010-07-23 18:55:21 -07001462void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1463 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001464 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001465 return;
1466 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001467 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001468
Romain Guyf607bdc2010-09-10 19:20:06 -07001469 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001470
Romain Guya674ab72010-08-10 17:26:42 -07001471 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001472 switch (paint->getTextAlign()) {
1473 case SkPaint::kCenter_Align:
1474 length = paint->measureText(text, bytesCount);
1475 x -= length / 2.0f;
1476 break;
1477 case SkPaint::kRight_Align:
1478 length = paint->measureText(text, bytesCount);
1479 x -= length;
1480 break;
1481 default:
1482 break;
1483 }
1484
Romain Guy6620c6d2010-12-06 18:07:02 -08001485 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001486 const float oldX = x;
1487 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001488 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1489 if (pureTranslate) {
1490 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1491 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1492 }
1493
Romain Guyb45c0c92010-08-26 20:35:23 -07001494 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1495 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001496 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001497
Romain Guy86568192010-12-14 15:55:39 -08001498 int alpha;
1499 SkXfermode::Mode mode;
1500 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001501
Romain Guy1e45aae2010-08-13 19:39:53 -07001502 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001503 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001504 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001505 count, mShadowRadius);
1506 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001507
Romain Guy86568192010-12-14 15:55:39 -08001508 const float sx = x - shadow->left + mShadowDx;
1509 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001510
Romain Guy86568192010-12-14 15:55:39 -08001511 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1512
1513 glActiveTexture(gTextureUnits[0]);
1514 setupDraw();
1515 setupDrawWithTexture(true);
1516 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1517 setupDrawBlending(true, mode);
1518 setupDrawProgram();
1519 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1520 setupDrawTexture(shadow->id);
1521 setupDrawPureColorUniforms();
1522 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1523
Romain Guy0a417492010-08-16 20:26:20 -07001524 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001525 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001526 }
1527
Romain Guyb146b122010-12-15 17:06:45 -08001528 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1529 return;
1530 }
1531
Romain Guy6620c6d2010-12-06 18:07:02 -08001532 // Pick the appropriate texture filtering
1533 bool linearFilter = mSnapshot->transform->changesBounds();
1534 if (pureTranslate && !linearFilter) {
1535 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1536 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001537
Romain Guy86568192010-12-14 15:55:39 -08001538 glActiveTexture(gTextureUnits[0]);
1539 setupDraw();
1540 setupDrawDirtyRegionsDisabled();
1541 setupDrawWithTexture(true);
1542 setupDrawAlpha8Color(paint->getColor(), alpha);
1543 setupDrawColorFilter();
1544 setupDrawShader();
1545 setupDrawBlending(true, mode);
1546 setupDrawProgram();
1547 setupDrawModelView(x, y, x, y, pureTranslate, true);
1548 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1549 setupDrawPureColorUniforms();
1550 setupDrawColorFilterUniforms();
1551 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001552
Romain Guy6620c6d2010-12-06 18:07:02 -08001553 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001554 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1555
1556#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001557 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001558#else
Romain Guyf219da52011-01-16 12:54:25 -08001559 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001560#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001561
Romain Guy03750a02010-10-18 14:06:08 -07001562 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001563 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001564 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001565#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001566 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001567 if (!pureTranslate) {
1568 mSnapshot->transform->mapRect(bounds);
1569 }
Romain Guyf219da52011-01-16 12:54:25 -08001570 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001571 }
1572#endif
1573 }
Romain Guy694b5192010-07-21 21:33:20 -07001574
1575 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001576 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001577
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001578 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001579}
1580
Romain Guy7fbcc042010-08-04 15:40:07 -07001581void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001582 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001583
Romain Guy01d58e42011-01-19 21:54:02 -08001584 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001585
Romain Guyfb8b7632010-08-23 21:05:08 -07001586 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001587 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001588 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001589
Romain Guy8b55f372010-08-18 17:10:07 -07001590 const float x = texture->left - texture->offset;
1591 const float y = texture->top - texture->offset;
1592
Romain Guy01d58e42011-01-19 21:54:02 -08001593 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001594}
1595
Romain Guyada830f2011-01-13 12:13:20 -08001596void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1597 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001598 return;
1599 }
1600
1601 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001602
1603 int alpha;
1604 SkXfermode::Mode mode;
1605 getAlphaAndMode(paint, &alpha, &mode);
1606
Romain Guyada830f2011-01-13 12:13:20 -08001607 layer->alpha = alpha;
1608 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001609
Romain Guyf219da52011-01-16 12:54:25 -08001610
1611#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001612 if (!layer->region.isEmpty()) {
1613 if (layer->region.isRect()) {
1614 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1615 composeLayerRect(layer, r);
1616 } else if (layer->mesh) {
1617 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001618
Romain Guyc88e3572011-01-22 00:32:12 -08001619 setupDraw();
1620 setupDrawWithTexture();
1621 setupDrawColor(alpha, alpha, alpha, alpha);
1622 setupDrawColorFilter();
1623 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1624 setupDrawProgram();
1625 setupDrawDirtyRegionsDisabled();
1626 setupDrawPureColorUniforms();
1627 setupDrawColorFilterUniforms();
1628 setupDrawTexture(layer->texture);
1629 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1630 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001631
Romain Guyc88e3572011-01-22 00:32:12 -08001632 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1633 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001634
Romain Guyc88e3572011-01-22 00:32:12 -08001635 finishDrawTexture();
1636 }
Romain Guyf219da52011-01-16 12:54:25 -08001637 }
1638#else
Romain Guyada830f2011-01-13 12:13:20 -08001639 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1640 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001641#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001642}
1643
Romain Guy6926c722010-07-12 20:20:03 -07001644///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001645// Shaders
1646///////////////////////////////////////////////////////////////////////////////
1647
1648void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001649 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001650}
1651
Romain Guy06f96e22010-07-30 19:18:16 -07001652void OpenGLRenderer::setupShader(SkiaShader* shader) {
1653 mShader = shader;
1654 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001655 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001656 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001657}
1658
Romain Guyd27977d2010-07-14 19:18:51 -07001659///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001660// Color filters
1661///////////////////////////////////////////////////////////////////////////////
1662
1663void OpenGLRenderer::resetColorFilter() {
1664 mColorFilter = NULL;
1665}
1666
1667void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1668 mColorFilter = filter;
1669}
1670
1671///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001672// Drop shadow
1673///////////////////////////////////////////////////////////////////////////////
1674
1675void OpenGLRenderer::resetShadow() {
1676 mHasShadow = false;
1677}
1678
1679void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1680 mHasShadow = true;
1681 mShadowRadius = radius;
1682 mShadowDx = dx;
1683 mShadowDy = dy;
1684 mShadowColor = color;
1685}
1686
1687///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001688// Drawing implementation
1689///////////////////////////////////////////////////////////////////////////////
1690
Romain Guy01d58e42011-01-19 21:54:02 -08001691void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1692 float x, float y, SkPaint* paint) {
1693 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1694 return;
1695 }
1696
1697 int alpha;
1698 SkXfermode::Mode mode;
1699 getAlphaAndMode(paint, &alpha, &mode);
1700
1701 setupDraw();
1702 setupDrawWithTexture(true);
1703 setupDrawAlpha8Color(paint->getColor(), alpha);
1704 setupDrawColorFilter();
1705 setupDrawShader();
1706 setupDrawBlending(true, mode);
1707 setupDrawProgram();
1708 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1709 setupDrawTexture(texture->id);
1710 setupDrawPureColorUniforms();
1711 setupDrawColorFilterUniforms();
1712 setupDrawShaderUniforms();
1713 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1714
1715 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1716
1717 finishDrawTexture();
1718}
1719
Romain Guyf607bdc2010-09-10 19:20:06 -07001720// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001721#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1722#define kStdUnderline_Offset (1.0f / 9.0f)
1723#define kStdUnderline_Thickness (1.0f / 18.0f)
1724
1725void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1726 float x, float y, SkPaint* paint) {
1727 // Handle underline and strike-through
1728 uint32_t flags = paint->getFlags();
1729 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1730 float underlineWidth = length;
1731 // If length is > 0.0f, we already measured the text for the text alignment
1732 if (length <= 0.0f) {
1733 underlineWidth = paint->measureText(text, bytesCount);
1734 }
1735
1736 float offsetX = 0;
1737 switch (paint->getTextAlign()) {
1738 case SkPaint::kCenter_Align:
1739 offsetX = underlineWidth * 0.5f;
1740 break;
1741 case SkPaint::kRight_Align:
1742 offsetX = underlineWidth;
1743 break;
1744 default:
1745 break;
1746 }
1747
1748 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001749 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001750 // TODO: Support stroke width < 1.0f when we have AA lines
1751 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001752
Romain Guye20ecbd2010-09-22 19:49:04 -07001753 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001754 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001755
Romain Guyf6834472011-01-23 13:32:12 -08001756 int linesCount = 0;
1757 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1758 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1759
1760 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001761 float points[pointsCount];
1762 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001763
1764 if (flags & SkPaint::kUnderlineText_Flag) {
1765 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001766 points[currentPoint++] = left;
1767 points[currentPoint++] = top;
1768 points[currentPoint++] = left + underlineWidth;
1769 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001770 }
1771
1772 if (flags & SkPaint::kStrikeThruText_Flag) {
1773 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001774 points[currentPoint++] = left;
1775 points[currentPoint++] = top;
1776 points[currentPoint++] = left + underlineWidth;
1777 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001778 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001779
1780 SkPaint linesPaint(*paint);
1781 linesPaint.setStrokeWidth(strokeWidth);
1782
1783 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001784 }
1785 }
1786}
1787
Romain Guy026c5e162010-06-28 17:12:22 -07001788void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001789 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001790 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001791 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001792 color |= 0x00ffffff;
1793 }
1794
Romain Guy70ca14e2010-12-13 18:24:33 -08001795 setupDraw();
1796 setupDrawColor(color);
1797 setupDrawShader();
1798 setupDrawColorFilter();
1799 setupDrawBlending(mode);
1800 setupDrawProgram();
1801 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1802 setupDrawColorUniforms();
1803 setupDrawShaderUniforms(ignoreTransform);
1804 setupDrawColorFilterUniforms();
1805 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001806
Romain Guyc95c8d62010-09-17 15:31:32 -07001807 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1808}
1809
Romain Guy82ba8142010-07-09 13:25:56 -07001810void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001811 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001812 int alpha;
1813 SkXfermode::Mode mode;
1814 getAlphaAndMode(paint, &alpha, &mode);
1815
Romain Guy8164c2d2010-10-25 18:03:28 -07001816 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1817
Romain Guy6620c6d2010-12-06 18:07:02 -08001818 if (mSnapshot->transform->isPureTranslate()) {
1819 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1820 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1821
1822 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1823 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1824 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1825 } else {
1826 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1827 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1828 GL_TRIANGLE_STRIP, gMeshCount);
1829 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001830}
1831
Romain Guybd6b79b2010-06-26 00:13:53 -07001832void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001833 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1834 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001835 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001836}
1837
1838void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001839 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001840 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001841 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001842
Romain Guy746b7402010-10-26 16:27:31 -07001843 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001844 setupDrawWithTexture();
1845 setupDrawColor(alpha, alpha, alpha, alpha);
1846 setupDrawColorFilter();
1847 setupDrawBlending(blend, mode, swapSrcDst);
1848 setupDrawProgram();
1849 if (!dirty) {
1850 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001851 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001852 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001853 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001854 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001855 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001856 }
Romain Guy86568192010-12-14 15:55:39 -08001857 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001858 setupDrawColorFilterUniforms();
1859 setupDrawTexture(texture);
1860 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001861
Romain Guy6820ac82010-09-15 18:11:50 -07001862 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001863
1864 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001865}
1866
Romain Guya5aed0d2010-09-09 14:42:43 -07001867void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001868 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001869 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1870 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001871 if (mode < SkXfermode::kPlus_Mode) {
1872 if (!mCaches.blend) {
1873 glEnable(GL_BLEND);
1874 }
Romain Guy82ba8142010-07-09 13:25:56 -07001875
Romain Guyf607bdc2010-09-10 19:20:06 -07001876 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1877 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001878
Romain Guya5aed0d2010-09-09 14:42:43 -07001879 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1880 glBlendFunc(sourceMode, destMode);
1881 mCaches.lastSrcMode = sourceMode;
1882 mCaches.lastDstMode = destMode;
1883 }
1884 } else {
1885 // These blend modes are not supported by OpenGL directly and have
1886 // to be implemented using shaders. Since the shader will perform
1887 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001888 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001889 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001890 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001891 }
1892
1893 if (mCaches.blend) {
1894 glDisable(GL_BLEND);
1895 }
1896 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001897 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001898 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001899 glDisable(GL_BLEND);
1900 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001901 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001902}
1903
Romain Guy889f8d12010-07-29 14:37:42 -07001904bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001905 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001906 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001907 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001908 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001909 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001910 }
Romain Guy6926c722010-07-12 20:20:03 -07001911 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001912}
1913
Romain Guy026c5e162010-06-28 17:12:22 -07001914void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001915 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001916 TextureVertex::setUV(v++, u1, v1);
1917 TextureVertex::setUV(v++, u2, v1);
1918 TextureVertex::setUV(v++, u1, v2);
1919 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001920}
1921
Chet Haase5c13d892010-10-08 08:37:55 -07001922void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001923 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001924 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001925 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1926 if (!isMode) {
1927 // Assume SRC_OVER
1928 *mode = SkXfermode::kSrcOver_Mode;
1929 }
1930 } else {
1931 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001932 }
1933
1934 // Skia draws using the color's alpha channel if < 255
1935 // Otherwise, it uses the paint's alpha
1936 int color = paint->getColor();
1937 *alpha = (color >> 24) & 0xFF;
1938 if (*alpha == 255) {
1939 *alpha = paint->getAlpha();
1940 }
1941 } else {
1942 *mode = SkXfermode::kSrcOver_Mode;
1943 *alpha = 255;
1944 }
Romain Guy026c5e162010-06-28 17:12:22 -07001945}
1946
Romain Guya5aed0d2010-09-09 14:42:43 -07001947SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1948 if (mode == NULL) {
1949 return SkXfermode::kSrcOver_Mode;
1950 }
1951 return mode->fMode;
1952}
1953
Romain Guy746b7402010-10-26 16:27:31 -07001954void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001955 bool bound = false;
1956 if (wrapS != texture->wrapS) {
1957 glBindTexture(GL_TEXTURE_2D, texture->id);
1958 bound = true;
1959 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1960 texture->wrapS = wrapS;
1961 }
1962 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001963 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001964 glBindTexture(GL_TEXTURE_2D, texture->id);
1965 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001966 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1967 texture->wrapT = wrapT;
1968 }
Romain Guya1db5742010-07-20 13:09:13 -07001969}
1970
Romain Guy9d5316e2010-06-24 19:30:36 -07001971}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001972}; // namespace android