blob: dfca7ebc44b0fe19921cedc4151cdadc49f09455 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy5b3b3522010-10-27 18:57:51 -070029#include <ui/Rect.h>
30
Romain Guy85bf02f2010-06-22 13:11:24 -070031#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080032#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080033#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070034
35namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070036namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Defines
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guy759ea802010-09-16 20:49:46 -070042#define RAD_TO_DEG (180.0f / 3.14159265f)
43#define MIN_ANGLE 0.001f
44
Romain Guydbc26d22010-10-11 17:58:29 -070045// TODO: This should be set in properties
46#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
47
Romain Guy9d5316e2010-06-24 19:30:36 -070048///////////////////////////////////////////////////////////////////////////////
49// Globals
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy889f8d12010-07-29 14:37:42 -070052/**
53 * Structure mapping Skia xfermodes to OpenGL blending factors.
54 */
55struct Blender {
56 SkXfermode::Mode mode;
57 GLenum src;
58 GLenum dst;
59}; // struct Blender
60
Romain Guy026c5e162010-06-28 17:12:22 -070061// In this array, the index of each Blender equals the value of the first
62// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
63static const Blender gBlends[] = {
Romain Guyb146b122010-12-15 17:06:45 -080064 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
65 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
66 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
67 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
68 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
69 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
70 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
71 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
75 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070076};
Romain Guye4d01122010-06-16 18:44:05 -070077
Romain Guy87a76572010-09-13 18:11:21 -070078// This array contains the swapped version of each SkXfermode. For instance
79// this array's SrcOver blending mode is actually DstOver. You can refer to
80// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070081static const Blender gBlendsSwap[] = {
Romain Guyb146b122010-12-15 17:06:45 -080082 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
83 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
85 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070094};
95
Romain Guy889f8d12010-07-29 14:37:42 -070096static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080097 GL_TEXTURE0,
98 GL_TEXTURE1,
99 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700100};
101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guyfb8b7632010-08-23 21:05:08 -0700106OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700107 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700108 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700109 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700110
Romain Guyac670c02010-07-27 17:39:27 -0700111 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
112
Romain Guyae5575b2010-07-29 18:48:04 -0700113 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700114}
115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700117 // The context has already been destroyed at this point, do not call
118 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guyf6a11b82010-06-23 17:47:49 -0700121///////////////////////////////////////////////////////////////////////////////
122// Setup
123///////////////////////////////////////////////////////////////////////////////
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700126 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700127 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700128
129 mWidth = width;
130 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700131
132 mFirstSnapshot->height = height;
133 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700134
135 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy6b7bd242010-10-06 19:49:23 -0700138void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800139 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
140}
141
142void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800143 mCaches.clearGarbage();
144
Romain Guy8aef54f2010-09-01 15:13:49 -0700145 mSnapshot = new Snapshot(mFirstSnapshot,
146 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800147 mSnapshot->fbo = getTargetFbo();
148
Romain Guy8fb95422010-08-17 18:38:51 -0700149 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700150
Romain Guyfb8b7632010-08-23 21:05:08 -0700151 glViewport(0, 0, mWidth, mHeight);
152
Romain Guyd90f23e2010-09-09 11:47:54 -0700153 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Romain Guy7d7b5492011-01-24 16:33:45 -0800155 glEnable(GL_SCISSOR_TEST);
156 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
157 mSnapshot->setClip(left, top, right, bottom);
158
Romain Guy6b7bd242010-10-06 19:49:23 -0700159 if (!opaque) {
160 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
161 glClear(GL_COLOR_BUFFER_BIT);
162 }
Romain Guybb9524b2010-06-22 18:56:38 -0700163}
164
Romain Guyb025b9c2010-09-16 14:16:48 -0700165void OpenGLRenderer::finish() {
166#if DEBUG_OPENGL
167 GLenum status = GL_NO_ERROR;
168 while ((status = glGetError()) != GL_NO_ERROR) {
169 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800170 switch (status) {
171 case GL_OUT_OF_MEMORY:
172 LOGE(" OpenGLRenderer is out of memory!");
173 break;
174 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700175 }
176#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800177#if DEBUG_MEMORY_USAGE
178 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800179#else
180 if (mCaches.getDebugLevel() & kDebugMemory) {
181 mCaches.dumpMemoryUsage();
182 }
Romain Guyc15008e2010-11-10 11:59:15 -0800183#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700184}
185
Romain Guy6c319ca2011-01-11 14:29:25 -0800186void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700187 if (mCaches.currentProgram) {
188 if (mCaches.currentProgram->isInUse()) {
189 mCaches.currentProgram->remove();
190 mCaches.currentProgram = NULL;
191 }
192 }
Romain Guy50c0f092010-10-19 11:42:22 -0700193 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700194}
195
Romain Guy6c319ca2011-01-11 14:29:25 -0800196void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700197 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700198
199 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700200 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700201
Romain Guyf607bdc2010-09-10 19:20:06 -0700202 glDisable(GL_DITHER);
203
Romain Guy42f3a4b2011-01-19 13:42:26 -0800204 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700205 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700206
Romain Guy50c0f092010-10-19 11:42:22 -0700207 mCaches.blend = true;
208 glEnable(GL_BLEND);
209 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
210 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700211}
212
Chet Haasedaf98e92011-01-10 14:10:36 -0800213bool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
214 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800215 if (mDirtyClip) {
216 setScissorFromClip();
217 }
Romain Guyd643bb52011-03-01 14:55:21 -0800218
219#if RENDER_LAYERS_AS_REGIONS
220 // Since we don't know what the functor will draw, let's dirty
221 // tne entire clip region
222 if (hasLayer()) {
223 Rect clip(*mSnapshot->clipRect);
224 clip.snapToPixelBoundaries();
225 dirtyLayerUnchecked(clip, getRegion());
226 }
227#endif
228
Chet Haasedaf98e92011-01-10 14:10:36 -0800229 status_t result = (*functor)();
230 resume();
231 return (result == 0) ? false : true;
232}
233
Romain Guyf6a11b82010-06-23 17:47:49 -0700234///////////////////////////////////////////////////////////////////////////////
235// State management
236///////////////////////////////////////////////////////////////////////////////
237
Romain Guybb9524b2010-06-22 18:56:38 -0700238int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700239 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700240}
241
242int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700243 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700244}
245
246void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700247 if (mSaveCount > 1) {
248 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700249 }
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
252void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700253 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700254
Romain Guy8fb95422010-08-17 18:38:51 -0700255 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700256 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700257 }
Romain Guybb9524b2010-06-22 18:56:38 -0700258}
259
Romain Guy8aef54f2010-09-01 15:13:49 -0700260int OpenGLRenderer::saveSnapshot(int flags) {
261 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700262 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700263}
264
265bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700266 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700267 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700268 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700269
Romain Guybd6b79b2010-06-26 00:13:53 -0700270 sp<Snapshot> current = mSnapshot;
271 sp<Snapshot> previous = mSnapshot->previous;
272
Romain Guyeb993562010-10-05 18:14:38 -0700273 if (restoreOrtho) {
274 Rect& r = previous->viewport;
275 glViewport(r.left, r.top, r.right, r.bottom);
276 mOrthoMatrix.load(current->orthoMatrix);
277 }
278
Romain Guy8b55f372010-08-18 17:10:07 -0700279 mSaveCount--;
280 mSnapshot = previous;
281
Romain Guy2542d192010-08-18 11:47:12 -0700282 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700283 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700284 }
Romain Guy2542d192010-08-18 11:47:12 -0700285
Romain Guy5ec99242010-11-03 16:19:08 -0700286 if (restoreLayer) {
287 composeLayer(current, previous);
288 }
289
Romain Guy2542d192010-08-18 11:47:12 -0700290 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700291}
292
Romain Guyf6a11b82010-06-23 17:47:49 -0700293///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700294// Layers
295///////////////////////////////////////////////////////////////////////////////
296
297int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700298 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700299 const GLuint previousFbo = mSnapshot->fbo;
300 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700301
Romain Guyaf636eb2010-12-09 17:47:21 -0800302 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700303 int alpha = 255;
304 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700305
Romain Guye45362c2010-11-03 19:58:32 -0700306 if (p) {
307 alpha = p->getAlpha();
308 if (!mCaches.extensions.hasFramebufferFetch()) {
309 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
310 if (!isMode) {
311 // Assume SRC_OVER
312 mode = SkXfermode::kSrcOver_Mode;
313 }
314 } else {
315 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700316 }
317 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700318 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700319 }
Romain Guyd55a8612010-06-28 17:42:46 -0700320
Romain Guydbc26d22010-10-11 17:58:29 -0700321 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
322 }
Romain Guyd55a8612010-06-28 17:42:46 -0700323
324 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700325}
326
327int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
328 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700329 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700330 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700331 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700332 SkPaint paint;
333 paint.setAlpha(alpha);
334 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700335 }
Romain Guyd55a8612010-06-28 17:42:46 -0700336}
Romain Guybd6b79b2010-06-26 00:13:53 -0700337
Romain Guy1c740bc2010-09-13 18:00:09 -0700338/**
339 * Layers are viewed by Skia are slightly different than layers in image editing
340 * programs (for instance.) When a layer is created, previously created layers
341 * and the frame buffer still receive every drawing command. For instance, if a
342 * layer is created and a shape intersecting the bounds of the layers and the
343 * framebuffer is draw, the shape will be drawn on both (unless the layer was
344 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
345 *
346 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
347 * texture. Unfortunately, this is inefficient as it requires every primitive to
348 * be drawn n + 1 times, where n is the number of active layers. In practice this
349 * means, for every primitive:
350 * - Switch active frame buffer
351 * - Change viewport, clip and projection matrix
352 * - Issue the drawing
353 *
354 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700355 * To avoid this, layers are implemented in a different way here, at least in the
356 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
357 * is set. When this flag is set we can redirect all drawing operations into a
358 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700359 *
360 * This implementation relies on the frame buffer being at least RGBA 8888. When
361 * a layer is created, only a texture is created, not an FBO. The content of the
362 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700363 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700364 * buffer and drawing continues as normal. This technique therefore treats the
365 * frame buffer as a scratch buffer for the layers.
366 *
367 * To compose the layers back onto the frame buffer, each layer texture
368 * (containing the original frame buffer data) is drawn as a simple quad over
369 * the frame buffer. The trick is that the quad is set as the composition
370 * destination in the blending equation, and the frame buffer becomes the source
371 * of the composition.
372 *
373 * Drawing layers with an alpha value requires an extra step before composition.
374 * An empty quad is drawn over the layer's region in the frame buffer. This quad
375 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
376 * quad is used to multiply the colors in the frame buffer. This is achieved by
377 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
378 * GL_ZERO, GL_SRC_ALPHA.
379 *
380 * Because glCopyTexImage2D() can be slow, an alternative implementation might
381 * be use to draw a single clipped layer. The implementation described above
382 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700383 *
384 * (1) The frame buffer is actually not cleared right away. To allow the GPU
385 * to potentially optimize series of calls to glCopyTexImage2D, the frame
386 * buffer is left untouched until the first drawing operation. Only when
387 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700388 */
Romain Guyd55a8612010-06-28 17:42:46 -0700389bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700390 float right, float bottom, int alpha, SkXfermode::Mode mode,
391 int flags, GLuint previousFbo) {
392 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700393 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700394
Romain Guyeb993562010-10-05 18:14:38 -0700395 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
396
Romain Guyf607bdc2010-09-10 19:20:06 -0700397 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700398 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700399 if (fboLayer) {
400 // Clear the previous layer regions before we change the viewport
401 clearLayerRegions();
402 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700403 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700404
Romain Guyeb993562010-10-05 18:14:38 -0700405 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700406 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700407
Romain Guyae88e5e2010-10-22 17:49:18 -0700408 // We cannot work with sub-pixels in this case
409 bounds.snapToPixelBoundaries();
410
Romain Guyae517592010-10-22 10:40:27 -0700411 // When the layer is not an FBO, we may use glCopyTexImage so we
412 // need to make sure the layer does not extend outside the bounds
413 // of the framebuffer
414 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700415 }
Romain Guybf434112010-09-16 14:40:17 -0700416
Romain Guy746b7402010-10-26 16:27:31 -0700417 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
418 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800419 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700420 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700421 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700422 }
423
424 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800425 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700426 return false;
427 }
Romain Guyf18fd992010-07-08 11:45:51 -0700428
Romain Guy5b3b3522010-10-27 18:57:51 -0700429 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700430 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700431 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700432 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700433 }
434
Romain Guydda570202010-07-06 11:39:32 -0700435 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700436 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700437 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700438 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
439 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800440 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700441
Romain Guy8fb95422010-08-17 18:38:51 -0700442 // Save the layer in the snapshot
443 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700444 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700445
Romain Guyeb993562010-10-05 18:14:38 -0700446 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700447 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700448 } else {
449 // Copy the framebuffer into the layer
450 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800451 if (!bounds.isEmpty()) {
452 if (layer->empty) {
453 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
454 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
455 layer->empty = false;
456 } else {
457 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
458 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
459 }
460 // Enqueue the buffer coordinates to clear the corresponding region later
461 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700462 }
Romain Guyeb993562010-10-05 18:14:38 -0700463 }
Romain Guyf86ef572010-07-01 11:05:42 -0700464
Romain Guyd55a8612010-06-28 17:42:46 -0700465 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700466}
467
Romain Guy5b3b3522010-10-27 18:57:51 -0700468bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
469 GLuint previousFbo) {
470 layer->fbo = mCaches.fboCache.get();
471
472#if RENDER_LAYERS_AS_REGIONS
473 snapshot->region = &snapshot->layer->region;
474 snapshot->flags |= Snapshot::kFlagFboTarget;
475#endif
476
477 Rect clip(bounds);
478 snapshot->transform->mapRect(clip);
479 clip.intersect(*snapshot->clipRect);
480 clip.snapToPixelBoundaries();
481 clip.intersect(snapshot->previous->viewport);
482
483 mat4 inverse;
484 inverse.loadInverse(*mSnapshot->transform);
485
486 inverse.mapRect(clip);
487 clip.snapToPixelBoundaries();
488 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700489 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700490
491 snapshot->flags |= Snapshot::kFlagIsFboLayer;
492 snapshot->fbo = layer->fbo;
493 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700494 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
495 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
496 snapshot->height = bounds.getHeight();
497 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
498 snapshot->orthoMatrix.load(mOrthoMatrix);
499
500 // Bind texture to FBO
501 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
502 glBindTexture(GL_TEXTURE_2D, layer->texture);
503
504 // Initialize the texture if needed
505 if (layer->empty) {
506 layer->empty = false;
507 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
508 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
509 }
510
511 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
512 layer->texture, 0);
513
514#if DEBUG_LAYERS_AS_REGIONS
515 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
516 if (status != GL_FRAMEBUFFER_COMPLETE) {
517 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
518
519 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
520 glDeleteTextures(1, &layer->texture);
521 mCaches.fboCache.put(layer->fbo);
522
523 delete layer;
524
525 return false;
526 }
527#endif
528
529 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
530 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
531 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
532 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
533 glClear(GL_COLOR_BUFFER_BIT);
534
535 dirtyClip();
536
537 // Change the ortho projection
538 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
539 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
540
541 return true;
542}
543
Romain Guy1c740bc2010-09-13 18:00:09 -0700544/**
545 * Read the documentation of createLayer() before doing anything in this method.
546 */
Romain Guy1d83e192010-08-17 11:37:00 -0700547void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
548 if (!current->layer) {
549 LOGE("Attempting to compose a layer that does not exist");
550 return;
551 }
552
Romain Guy5b3b3522010-10-27 18:57:51 -0700553 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700554
555 if (fboLayer) {
556 // Unbind current FBO and restore previous one
557 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
558 }
559
Romain Guy1d83e192010-08-17 11:37:00 -0700560 Layer* layer = current->layer;
561 const Rect& rect = layer->layer;
562
Romain Guyeb993562010-10-05 18:14:38 -0700563 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700564 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700565 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700566 // Required below, composeLayerRect() will divide by 255
567 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700568 }
569
Romain Guy03750a02010-10-18 14:06:08 -0700570 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700571
Romain Guy746b7402010-10-26 16:27:31 -0700572 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700573
Romain Guy5b3b3522010-10-27 18:57:51 -0700574 // When the layer is stored in an FBO, we can save a bit of fillrate by
575 // drawing only the dirty region
576 if (fboLayer) {
577 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800578 if (layer->colorFilter) {
579 setupColorFilter(layer->colorFilter);
580 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700581 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800582 if (layer->colorFilter) {
583 resetColorFilter();
584 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700585 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800586 if (!rect.isEmpty()) {
587 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
588 composeLayerRect(layer, rect, true);
589 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700590 }
Romain Guy8b55f372010-08-18 17:10:07 -0700591
Romain Guyeb993562010-10-05 18:14:38 -0700592 if (fboLayer) {
593 // Detach the texture from the FBO
594 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
595 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
596 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
597
598 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
599 mCaches.fboCache.put(current->fbo);
600 }
601
Romain Guy746b7402010-10-26 16:27:31 -0700602 dirtyClip();
603
Romain Guyeb993562010-10-05 18:14:38 -0700604 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700605 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700606 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700607 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700608 delete layer;
609 }
610}
611
Romain Guy5b3b3522010-10-27 18:57:51 -0700612void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
613 const Rect& texCoords = layer->texCoords;
614 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
615
616 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
617 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
618 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
619
620 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
621}
622
623void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
624#if RENDER_LAYERS_AS_REGIONS
625 if (layer->region.isRect()) {
626 composeLayerRect(layer, rect);
627 layer->region.clear();
628 return;
629 }
630
631 if (!layer->region.isEmpty()) {
632 size_t count;
633 const android::Rect* rects = layer->region.getArray(&count);
634
Romain Guy5b3b3522010-10-27 18:57:51 -0700635 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700636 const float texX = 1.0f / float(layer->width);
637 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800638 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700639
640 TextureVertex* mesh = mCaches.getRegionMesh();
641 GLsizei numQuads = 0;
642
Romain Guy7230a742011-01-10 22:26:16 -0800643 setupDraw();
644 setupDrawWithTexture();
645 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800646 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800647 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
648 setupDrawProgram();
649 setupDrawDirtyRegionsDisabled();
650 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800651 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800652 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800653 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800654 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700655
656 for (size_t i = 0; i < count; i++) {
657 const android::Rect* r = &rects[i];
658
659 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800660 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700661 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800662 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700663
664 // TODO: Reject quads outside of the clip
665 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
666 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
667 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
668 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
669
670 numQuads++;
671
672 if (numQuads >= REGION_MESH_QUAD_COUNT) {
673 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
674 numQuads = 0;
675 mesh = mCaches.getRegionMesh();
676 }
677 }
678
679 if (numQuads > 0) {
680 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
681 }
682
683 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800684 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700685
686#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800687 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700688#endif
689
690 layer->region.clear();
691 }
692#else
693 composeLayerRect(layer, rect);
694#endif
695}
696
Romain Guy3a3133d2011-02-01 22:59:58 -0800697void OpenGLRenderer::drawRegionRects(const Region& region) {
698#if DEBUG_LAYERS_AS_REGIONS
699 size_t count;
700 const android::Rect* rects = region.getArray(&count);
701
702 uint32_t colors[] = {
703 0x7fff0000, 0x7f00ff00,
704 0x7f0000ff, 0x7fff00ff,
705 };
706
707 int offset = 0;
708 int32_t top = rects[0].top;
709
710 for (size_t i = 0; i < count; i++) {
711 if (top != rects[i].top) {
712 offset ^= 0x2;
713 top = rects[i].top;
714 }
715
716 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
717 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
718 SkXfermode::kSrcOver_Mode);
719 }
720#endif
721}
722
Romain Guy5b3b3522010-10-27 18:57:51 -0700723void OpenGLRenderer::dirtyLayer(const float left, const float top,
724 const float right, const float bottom, const mat4 transform) {
725#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800726 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700727 Rect bounds(left, top, right, bottom);
728 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800729 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700730 }
731#endif
732}
733
734void OpenGLRenderer::dirtyLayer(const float left, const float top,
735 const float right, const float bottom) {
736#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800737 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700738 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800739 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800740 }
741#endif
742}
743
744void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
745#if RENDER_LAYERS_AS_REGIONS
746 if (bounds.intersect(*mSnapshot->clipRect)) {
747 bounds.snapToPixelBoundaries();
748 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
749 if (!dirty.isEmpty()) {
750 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700751 }
752 }
753#endif
754}
755
Romain Guy86942302010-09-12 13:02:16 -0700756void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800757 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700758
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 Rect clipRect(*mSnapshot->clipRect);
760 clipRect.snapToPixelBoundaries();
761
Romain Guy86942302010-09-12 13:02:16 -0700762 for (uint32_t i = 0; i < mLayers.size(); i++) {
763 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700764 if (clipRect.intersects(*bounds)) {
765 // Clear the framebuffer where the layer will draw
766 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
767 bounds->getWidth(), bounds->getHeight());
768 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
769 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700770
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 // Restore the clip
772 dirtyClip();
773 }
Romain Guy86942302010-09-12 13:02:16 -0700774
775 delete bounds;
776 }
Romain Guy86942302010-09-12 13:02:16 -0700777
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700779}
780
Romain Guybd6b79b2010-06-26 00:13:53 -0700781///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700782// Transforms
783///////////////////////////////////////////////////////////////////////////////
784
785void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700786 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700787}
788
789void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700790 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700791}
792
793void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700794 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700795}
796
Romain Guy807daf72011-01-18 11:19:19 -0800797void OpenGLRenderer::skew(float sx, float sy) {
798 mSnapshot->transform->skew(sx, sy);
799}
800
Romain Guyf6a11b82010-06-23 17:47:49 -0700801void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700802 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700803}
804
Romain Guy41030da2010-10-13 13:40:37 -0700805const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700806 if (mSnapshot->fbo != 0) {
807 return &mSnapshot->transform->data[0];
808 }
809 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700810}
811
Romain Guyf6a11b82010-06-23 17:47:49 -0700812void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700813 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700814}
815
816void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700817 SkMatrix transform;
818 mSnapshot->transform->copyTo(transform);
819 transform.preConcat(*matrix);
820 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700821}
822
823///////////////////////////////////////////////////////////////////////////////
824// Clipping
825///////////////////////////////////////////////////////////////////////////////
826
Romain Guybb9524b2010-06-22 18:56:38 -0700827void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700828 Rect clip(*mSnapshot->clipRect);
829 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700830 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700831 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700832}
833
834const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700835 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700836}
837
Romain Guyc7d53492010-06-25 13:41:57 -0700838bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800839 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700840 return true;
841 }
842
Romain Guy1d83e192010-08-17 11:37:00 -0700843 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700844 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700845 r.snapToPixelBoundaries();
846
847 Rect clipRect(*mSnapshot->clipRect);
848 clipRect.snapToPixelBoundaries();
849
850 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700851}
852
Romain Guy079ba2c2010-07-16 14:12:24 -0700853bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
854 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700855 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700856 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700857 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700858 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700859}
860
Romain Guyf6a11b82010-06-23 17:47:49 -0700861///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800862// Drawing commands
863///////////////////////////////////////////////////////////////////////////////
864
865void OpenGLRenderer::setupDraw() {
866 clearLayerRegions();
867 if (mDirtyClip) {
868 setScissorFromClip();
869 }
870 mDescription.reset();
871 mSetShaderColor = false;
872 mColorSet = false;
873 mColorA = mColorR = mColorG = mColorB = 0.0f;
874 mTextureUnit = 0;
875 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800876 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800877}
878
879void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
880 mDescription.hasTexture = true;
881 mDescription.hasAlpha8Texture = isAlpha8;
882}
883
884void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800885 setupDrawColor(color, (color >> 24) & 0xFF);
886}
887
888void OpenGLRenderer::setupDrawColor(int color, int alpha) {
889 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800890 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800891 mColorR = a * ((color >> 16) & 0xFF);
892 mColorG = a * ((color >> 8) & 0xFF);
893 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800894 mColorSet = true;
895 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
896}
897
Romain Guy86568192010-12-14 15:55:39 -0800898void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
899 mColorA = alpha / 255.0f;
900 const float a = mColorA / 255.0f;
901 mColorR = a * ((color >> 16) & 0xFF);
902 mColorG = a * ((color >> 8) & 0xFF);
903 mColorB = a * ((color ) & 0xFF);
904 mColorSet = true;
905 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
906}
907
Romain Guy70ca14e2010-12-13 18:24:33 -0800908void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
909 mColorA = a;
910 mColorR = r;
911 mColorG = g;
912 mColorB = b;
913 mColorSet = true;
914 mSetShaderColor = mDescription.setColor(r, g, b, a);
915}
916
Romain Guy86568192010-12-14 15:55:39 -0800917void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
918 mColorA = a;
919 mColorR = r;
920 mColorG = g;
921 mColorB = b;
922 mColorSet = true;
923 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
924}
925
Romain Guy70ca14e2010-12-13 18:24:33 -0800926void OpenGLRenderer::setupDrawShader() {
927 if (mShader) {
928 mShader->describe(mDescription, mCaches.extensions);
929 }
930}
931
932void OpenGLRenderer::setupDrawColorFilter() {
933 if (mColorFilter) {
934 mColorFilter->describe(mDescription, mCaches.extensions);
935 }
936}
937
938void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
939 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
940 mDescription, swapSrcDst);
941}
942
943void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
944 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
945 mDescription, swapSrcDst);
946}
947
948void OpenGLRenderer::setupDrawProgram() {
949 useProgram(mCaches.programCache.get(mDescription));
950}
951
952void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
953 mTrackDirtyRegions = false;
954}
955
956void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
957 bool ignoreTransform) {
958 mModelView.loadTranslate(left, top, 0.0f);
959 if (!ignoreTransform) {
960 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
961 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
962 } else {
963 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
964 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
965 }
966}
967
Romain Guy8d0d4782010-12-14 20:13:35 -0800968void OpenGLRenderer::setupDrawModelViewIdentity() {
969 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
970}
971
Romain Guy70ca14e2010-12-13 18:24:33 -0800972void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
973 bool ignoreTransform, bool ignoreModelView) {
974 if (!ignoreModelView) {
975 mModelView.loadTranslate(left, top, 0.0f);
976 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800977 } else {
978 mModelView.loadIdentity();
979 }
Romain Guy86568192010-12-14 15:55:39 -0800980 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
981 if (!ignoreTransform) {
982 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
983 if (mTrackDirtyRegions && dirty) {
984 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
985 }
986 } else {
987 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
988 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
989 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800990}
991
992void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800993 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800994 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
995 }
996}
997
Romain Guy86568192010-12-14 15:55:39 -0800998void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800999 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001000 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001001 }
1002}
1003
Romain Guy70ca14e2010-12-13 18:24:33 -08001004void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1005 if (mShader) {
1006 if (ignoreTransform) {
1007 mModelView.loadInverse(*mSnapshot->transform);
1008 }
1009 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1010 }
1011}
1012
Romain Guy8d0d4782010-12-14 20:13:35 -08001013void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1014 if (mShader) {
1015 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1016 }
1017}
1018
Romain Guy70ca14e2010-12-13 18:24:33 -08001019void OpenGLRenderer::setupDrawColorFilterUniforms() {
1020 if (mColorFilter) {
1021 mColorFilter->setupProgram(mCaches.currentProgram);
1022 }
1023}
1024
1025void OpenGLRenderer::setupDrawSimpleMesh() {
1026 mCaches.bindMeshBuffer();
1027 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1028 gMeshStride, 0);
1029}
1030
1031void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1032 bindTexture(texture);
1033 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1034
1035 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1036 glEnableVertexAttribArray(mTexCoordsSlot);
1037}
1038
1039void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1040 if (!vertices) {
1041 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1042 } else {
1043 mCaches.unbindMeshBuffer();
1044 }
1045 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1046 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001047 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001048 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1049 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001050}
1051
1052void OpenGLRenderer::finishDrawTexture() {
1053 glDisableVertexAttribArray(mTexCoordsSlot);
1054}
1055
1056///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001057// Drawing
1058///////////////////////////////////////////////////////////////////////////////
1059
Chet Haasedaf98e92011-01-10 14:10:36 -08001060bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001061 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1062 // will be performed by the display list itself
1063 if (displayList) {
Chet Haasedaf98e92011-01-10 14:10:36 -08001064 return displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001065 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001066 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001067}
1068
Chet Haase5c13d892010-10-08 08:37:55 -07001069void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001070 const float right = left + bitmap->width();
1071 const float bottom = top + bitmap->height();
1072
1073 if (quickReject(left, top, right, bottom)) {
1074 return;
1075 }
1076
Romain Guy86568192010-12-14 15:55:39 -08001077 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001078 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001079 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001080 const AutoTexture autoCleanup(texture);
1081
Romain Guy6926c722010-07-12 20:20:03 -07001082 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001083}
1084
Chet Haase5c13d892010-10-08 08:37:55 -07001085void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001086 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1087 const mat4 transform(*matrix);
1088 transform.mapRect(r);
1089
Romain Guy6926c722010-07-12 20:20:03 -07001090 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1091 return;
1092 }
1093
Romain Guy86568192010-12-14 15:55:39 -08001094 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001095 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001096 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001097 const AutoTexture autoCleanup(texture);
1098
Romain Guy5b3b3522010-10-27 18:57:51 -07001099 // This could be done in a cheaper way, all we need is pass the matrix
1100 // to the vertex shader. The save/restore is a bit overkill.
1101 save(SkCanvas::kMatrix_SaveFlag);
1102 concatMatrix(matrix);
1103 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1104 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001105}
1106
Romain Guy5a7b4662011-01-20 19:09:30 -08001107void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1108 float* vertices, int* colors, SkPaint* paint) {
1109 // TODO: Do a quickReject
1110 if (!vertices || mSnapshot->isIgnored()) {
1111 return;
1112 }
1113
1114 glActiveTexture(gTextureUnits[0]);
1115 Texture* texture = mCaches.textureCache.get(bitmap);
1116 if (!texture) return;
1117 const AutoTexture autoCleanup(texture);
1118 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1119
1120 int alpha;
1121 SkXfermode::Mode mode;
1122 getAlphaAndMode(paint, &alpha, &mode);
1123
Romain Guy5a7b4662011-01-20 19:09:30 -08001124 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001125
Romain Guyb18d2d02011-02-10 15:52:54 -08001126 float left = FLT_MAX;
1127 float top = FLT_MAX;
1128 float right = FLT_MIN;
1129 float bottom = FLT_MIN;
1130
1131#if RENDER_LAYERS_AS_REGIONS
1132 bool hasActiveLayer = hasLayer();
1133#else
1134 bool hasActiveLayer = false;
1135#endif
1136
Romain Guya566b7c2011-01-23 16:36:11 -08001137 // TODO: Support the colors array
1138 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001139 TextureVertex* vertex = mesh;
1140 for (int32_t y = 0; y < meshHeight; y++) {
1141 for (int32_t x = 0; x < meshWidth; x++) {
1142 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1143
1144 float u1 = float(x) / meshWidth;
1145 float u2 = float(x + 1) / meshWidth;
1146 float v1 = float(y) / meshHeight;
1147 float v2 = float(y + 1) / meshHeight;
1148
1149 int ax = i + (meshWidth + 1) * 2;
1150 int ay = ax + 1;
1151 int bx = i;
1152 int by = bx + 1;
1153 int cx = i + 2;
1154 int cy = cx + 1;
1155 int dx = i + (meshWidth + 1) * 2 + 2;
1156 int dy = dx + 1;
1157
1158 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1159 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1160 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1161
1162 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1163 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1164 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001165
1166#if RENDER_LAYERS_AS_REGIONS
1167 if (hasActiveLayer) {
1168 // TODO: This could be optimized to avoid unnecessary ops
1169 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1170 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1171 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1172 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1173 }
1174#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001175 }
1176 }
1177
Romain Guyb18d2d02011-02-10 15:52:54 -08001178#if RENDER_LAYERS_AS_REGIONS
1179 if (hasActiveLayer) {
1180 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1181 }
1182#endif
1183
Romain Guy5a7b4662011-01-20 19:09:30 -08001184 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1185 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001186 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001187}
1188
Romain Guy8ba548f2010-06-30 19:21:21 -07001189void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1190 float srcLeft, float srcTop, float srcRight, float srcBottom,
1191 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001192 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001193 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1194 return;
1195 }
1196
Romain Guy746b7402010-10-26 16:27:31 -07001197 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001198 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001199 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001200 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001201 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001202
Romain Guy8ba548f2010-06-30 19:21:21 -07001203 const float width = texture->width;
1204 const float height = texture->height;
1205
1206 const float u1 = srcLeft / width;
1207 const float v1 = srcTop / height;
1208 const float u2 = srcRight / width;
1209 const float v2 = srcBottom / height;
1210
Romain Guy03750a02010-10-18 14:06:08 -07001211 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001212 resetDrawTextureTexCoords(u1, v1, u2, v2);
1213
Romain Guy03750a02010-10-18 14:06:08 -07001214 int alpha;
1215 SkXfermode::Mode mode;
1216 getAlphaAndMode(paint, &alpha, &mode);
1217
Romain Guy6620c6d2010-12-06 18:07:02 -08001218 if (mSnapshot->transform->isPureTranslate()) {
1219 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1220 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1221
1222 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1223 texture->id, alpha / 255.0f, mode, texture->blend,
1224 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1225 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1226 } else {
1227 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1228 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1229 GL_TRIANGLE_STRIP, gMeshCount);
1230 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001231
1232 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1233}
1234
Romain Guy4aa90572010-09-26 18:40:37 -07001235void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001236 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001237 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001238 if (quickReject(left, top, right, bottom)) {
1239 return;
1240 }
1241
Romain Guy746b7402010-10-26 16:27:31 -07001242 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001243 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001244 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001245 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001246 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001247
1248 int alpha;
1249 SkXfermode::Mode mode;
1250 getAlphaAndMode(paint, &alpha, &mode);
1251
Romain Guy2728f962010-10-08 18:36:15 -07001252 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001253 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001254
Romain Guya5ef39a2010-12-03 16:48:20 -08001255 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001256 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001257#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001258 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001259 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001260 const float offsetX = left + mSnapshot->transform->getTranslateX();
1261 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001262 const size_t count = mesh->quads.size();
1263 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001264 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001265 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001266 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1267 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1268 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001269 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001270 dirtyLayer(left + bounds.left, top + bounds.top,
1271 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001272 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001273 }
1274 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001275#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001276
Romain Guy6620c6d2010-12-06 18:07:02 -08001277 if (pureTranslate) {
1278 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1279 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1280
1281 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1282 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1283 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1284 true, !mesh->hasEmptyQuads);
1285 } else {
1286 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1287 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1288 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1289 true, !mesh->hasEmptyQuads);
1290 }
Romain Guy054dc182010-10-15 17:55:25 -07001291 }
Romain Guyf7f93552010-07-08 19:17:03 -07001292}
1293
Chet Haase5c13d892010-10-08 08:37:55 -07001294void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001295 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001296
Romain Guya957eea2010-12-08 18:34:42 -08001297 const bool isAA = paint->isAntiAlias();
1298 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1299 // A stroke width of 0 has a special meaningin Skia:
1300 // it draws an unscaled 1px wide line
1301 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1302
Romain Guy759ea802010-09-16 20:49:46 -07001303 int alpha;
1304 SkXfermode::Mode mode;
1305 getAlphaAndMode(paint, &alpha, &mode);
1306
Romain Guya957eea2010-12-08 18:34:42 -08001307 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001308 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001309 if (!isHairLine) {
1310 // TODO: AA needs more vertices
1311 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001312 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001313 // TODO: AA will be different
1314 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001315 }
1316
Romain Guya957eea2010-12-08 18:34:42 -08001317 TextureVertex lines[verticesCount];
1318 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001319
Romain Guy8d0d4782010-12-14 20:13:35 -08001320 setupDraw();
1321 setupDrawColor(paint->getColor(), alpha);
1322 setupDrawColorFilter();
1323 setupDrawShader();
1324 setupDrawBlending(mode);
1325 setupDrawProgram();
1326 setupDrawModelViewIdentity();
1327 setupDrawColorUniforms();
1328 setupDrawColorFilterUniforms();
1329 setupDrawShaderIdentityUniforms();
1330 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001331
1332 if (!isHairLine) {
1333 // TODO: Handle the AA case
1334 for (int i = 0; i < count; i += 4) {
1335 // a = start point, b = end point
1336 vec2 a(points[i], points[i + 1]);
1337 vec2 b(points[i + 2], points[i + 3]);
1338
1339 // Bias to snap to the same pixels as Skia
1340 a += 0.375;
1341 b += 0.375;
1342
1343 // Find the normal to the line
1344 vec2 n = (b - a).copyNormalized() * strokeWidth;
1345 float x = n.x;
1346 n.x = -n.y;
1347 n.y = x;
1348
1349 // Four corners of the rectangle defining a thick line
1350 vec2 p1 = a - n;
1351 vec2 p2 = a + n;
1352 vec2 p3 = b + n;
1353 vec2 p4 = b - n;
1354
Romain Guy7230a742011-01-10 22:26:16 -08001355 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1356 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1357 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1358 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001359
Romain Guy7230a742011-01-10 22:26:16 -08001360 if (!quickReject(left, top, right, bottom)) {
1361 // Draw the line as 2 triangles, could be optimized
1362 // by using only 4 vertices and the correct indices
1363 // Also we should probably used non textured vertices
1364 // when line AA is disabled to save on bandwidth
1365 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1366 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1367 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1368 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1369 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1370 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1371
1372 generatedVerticesCount += 6;
1373
1374 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1375 }
Romain Guya957eea2010-12-08 18:34:42 -08001376 }
1377
Romain Guy7230a742011-01-10 22:26:16 -08001378 if (generatedVerticesCount > 0) {
1379 // GL_LINE does not give the result we want to match Skia
1380 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1381 }
Romain Guya957eea2010-12-08 18:34:42 -08001382 } else {
1383 // TODO: Handle the AA case
1384 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001385 const float left = fmin(points[i], points[i + 1]);
1386 const float right = fmax(points[i], points[i + 1]);
1387 const float top = fmin(points[i + 2], points[i + 3]);
1388 const float bottom = fmax(points[i + 2], points[i + 3]);
1389
1390 if (!quickReject(left, top, right, bottom)) {
1391 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1392 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1393
1394 generatedVerticesCount += 2;
1395
1396 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1397 }
Romain Guya957eea2010-12-08 18:34:42 -08001398 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001399
Romain Guy7230a742011-01-10 22:26:16 -08001400 if (generatedVerticesCount > 0) {
1401 glLineWidth(1.0f);
1402 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1403 }
Romain Guy29d89972010-09-22 16:10:57 -07001404 }
Romain Guy759ea802010-09-16 20:49:46 -07001405}
1406
Romain Guy85bf02f2010-06-22 13:11:24 -07001407void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001408 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001409 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001410
Romain Guyae88e5e2010-10-22 17:49:18 -07001411 Rect& clip(*mSnapshot->clipRect);
1412 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001413
Romain Guy3d58c032010-07-14 16:34:53 -07001414 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001415}
Romain Guy9d5316e2010-06-24 19:30:36 -07001416
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001417void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001418 if (!texture) return;
1419 const AutoTexture autoCleanup(texture);
1420
1421 const float x = left + texture->left - texture->offset;
1422 const float y = top + texture->top - texture->offset;
1423
1424 drawPathTexture(texture, x, y, paint);
1425}
1426
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001427void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1428 float rx, float ry, SkPaint* paint) {
1429 if (mSnapshot->isIgnored()) return;
1430
1431 glActiveTexture(gTextureUnits[0]);
1432 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1433 right - left, bottom - top, rx, ry, paint);
1434 drawShape(left, top, texture, paint);
1435}
1436
Romain Guy01d58e42011-01-19 21:54:02 -08001437void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1438 if (mSnapshot->isIgnored()) return;
1439
1440 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001441 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001442 drawShape(x - radius, y - radius, texture, paint);
1443}
Romain Guy01d58e42011-01-19 21:54:02 -08001444
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001445void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1446 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001447
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001448 glActiveTexture(gTextureUnits[0]);
1449 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1450 drawShape(left, top, texture, paint);
1451}
1452
Romain Guy8b2f5262011-01-23 16:15:02 -08001453void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1454 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1455 if (mSnapshot->isIgnored()) return;
1456
1457 if (fabs(sweepAngle) >= 360.0f) {
1458 drawOval(left, top, right, bottom, paint);
1459 return;
1460 }
1461
1462 glActiveTexture(gTextureUnits[0]);
1463 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1464 startAngle, sweepAngle, useCenter, paint);
1465 drawShape(left, top, texture, paint);
1466}
1467
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001468void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1469 SkPaint* paint) {
1470 if (mSnapshot->isIgnored()) return;
1471
1472 glActiveTexture(gTextureUnits[0]);
1473 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1474 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001475}
1476
Chet Haase5c13d892010-10-08 08:37:55 -07001477void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001478 if (p->getStyle() != SkPaint::kFill_Style) {
1479 drawRectAsShape(left, top, right, bottom, p);
1480 return;
1481 }
1482
Romain Guy6926c722010-07-12 20:20:03 -07001483 if (quickReject(left, top, right, bottom)) {
1484 return;
1485 }
1486
Romain Guy026c5e162010-06-28 17:12:22 -07001487 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001488 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001489 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1490 if (!isMode) {
1491 // Assume SRC_OVER
1492 mode = SkXfermode::kSrcOver_Mode;
1493 }
1494 } else {
1495 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001496 }
1497
Romain Guy026c5e162010-06-28 17:12:22 -07001498 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001499 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001500}
Romain Guy9d5316e2010-06-24 19:30:36 -07001501
Romain Guye8e62a42010-07-23 18:55:21 -07001502void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1503 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001504 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001505 return;
1506 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001507 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001508
Romain Guyf607bdc2010-09-10 19:20:06 -07001509 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001510
Romain Guya674ab72010-08-10 17:26:42 -07001511 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001512 switch (paint->getTextAlign()) {
1513 case SkPaint::kCenter_Align:
1514 length = paint->measureText(text, bytesCount);
1515 x -= length / 2.0f;
1516 break;
1517 case SkPaint::kRight_Align:
1518 length = paint->measureText(text, bytesCount);
1519 x -= length;
1520 break;
1521 default:
1522 break;
1523 }
1524
Romain Guy6620c6d2010-12-06 18:07:02 -08001525 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001526 const float oldX = x;
1527 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001528 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1529 if (pureTranslate) {
1530 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1531 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1532 }
1533
Romain Guyb45c0c92010-08-26 20:35:23 -07001534 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1535 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001536 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001537
Romain Guy86568192010-12-14 15:55:39 -08001538 int alpha;
1539 SkXfermode::Mode mode;
1540 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001541
Romain Guy1e45aae2010-08-13 19:39:53 -07001542 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001543 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001544 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001545 count, mShadowRadius);
1546 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001547
Romain Guy86568192010-12-14 15:55:39 -08001548 const float sx = x - shadow->left + mShadowDx;
1549 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001550
Romain Guy86568192010-12-14 15:55:39 -08001551 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1552
1553 glActiveTexture(gTextureUnits[0]);
1554 setupDraw();
1555 setupDrawWithTexture(true);
1556 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1557 setupDrawBlending(true, mode);
1558 setupDrawProgram();
1559 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1560 setupDrawTexture(shadow->id);
1561 setupDrawPureColorUniforms();
1562 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1563
Romain Guy0a417492010-08-16 20:26:20 -07001564 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001565 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001566 }
1567
Romain Guyb146b122010-12-15 17:06:45 -08001568 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1569 return;
1570 }
1571
Romain Guy6620c6d2010-12-06 18:07:02 -08001572 // Pick the appropriate texture filtering
1573 bool linearFilter = mSnapshot->transform->changesBounds();
1574 if (pureTranslate && !linearFilter) {
1575 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1576 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001577
Romain Guy86568192010-12-14 15:55:39 -08001578 glActiveTexture(gTextureUnits[0]);
1579 setupDraw();
1580 setupDrawDirtyRegionsDisabled();
1581 setupDrawWithTexture(true);
1582 setupDrawAlpha8Color(paint->getColor(), alpha);
1583 setupDrawColorFilter();
1584 setupDrawShader();
1585 setupDrawBlending(true, mode);
1586 setupDrawProgram();
1587 setupDrawModelView(x, y, x, y, pureTranslate, true);
1588 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1589 setupDrawPureColorUniforms();
1590 setupDrawColorFilterUniforms();
1591 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001592
Romain Guy6620c6d2010-12-06 18:07:02 -08001593 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001594 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1595
1596#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001597 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001598#else
Romain Guyf219da52011-01-16 12:54:25 -08001599 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001600#endif
Romain Guy03750a02010-10-18 14:06:08 -07001601 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001602
1603 // Tell font renderer the locations of position and texture coord
1604 // attributes so it can bind its data properly
1605 int positionSlot = mCaches.currentProgram->position;
1606 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001607 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001608 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001609#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001610 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001611 if (!pureTranslate) {
1612 mSnapshot->transform->mapRect(bounds);
1613 }
Romain Guyf219da52011-01-16 12:54:25 -08001614 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001615 }
1616#endif
1617 }
Romain Guy694b5192010-07-21 21:33:20 -07001618
1619 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001620 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001621
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001622 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001623}
1624
Romain Guy7fbcc042010-08-04 15:40:07 -07001625void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001626 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001627
Romain Guy01d58e42011-01-19 21:54:02 -08001628 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001629
Romain Guyfb8b7632010-08-23 21:05:08 -07001630 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001631 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001632 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001633
Romain Guy8b55f372010-08-18 17:10:07 -07001634 const float x = texture->left - texture->offset;
1635 const float y = texture->top - texture->offset;
1636
Romain Guy01d58e42011-01-19 21:54:02 -08001637 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001638}
1639
Romain Guyada830f2011-01-13 12:13:20 -08001640void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1641 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001642 return;
1643 }
1644
1645 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001646
1647 int alpha;
1648 SkXfermode::Mode mode;
1649 getAlphaAndMode(paint, &alpha, &mode);
1650
Romain Guyada830f2011-01-13 12:13:20 -08001651 layer->alpha = alpha;
1652 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001653
Romain Guyf219da52011-01-16 12:54:25 -08001654#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001655 if (!layer->region.isEmpty()) {
1656 if (layer->region.isRect()) {
1657 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1658 composeLayerRect(layer, r);
1659 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001660 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001661 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001662
Romain Guyc88e3572011-01-22 00:32:12 -08001663 setupDraw();
1664 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001665 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001666 setupDrawColorFilter();
1667 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1668 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001669 setupDrawPureColorUniforms();
1670 setupDrawColorFilterUniforms();
1671 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001672 // TODO: The current layer, if any, will be dirtied with the bounding box
1673 // of the layer we are drawing. Since the layer we are drawing has
1674 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001675 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1676 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001677
Romain Guyc88e3572011-01-22 00:32:12 -08001678 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1679 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001680
Romain Guyc88e3572011-01-22 00:32:12 -08001681 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001682
1683#if DEBUG_LAYERS_AS_REGIONS
1684 drawRegionRects(layer->region);
1685#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001686 }
Romain Guyf219da52011-01-16 12:54:25 -08001687 }
1688#else
Romain Guyada830f2011-01-13 12:13:20 -08001689 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1690 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001691#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001692}
1693
Romain Guy6926c722010-07-12 20:20:03 -07001694///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001695// Shaders
1696///////////////////////////////////////////////////////////////////////////////
1697
1698void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001699 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001700}
1701
Romain Guy06f96e22010-07-30 19:18:16 -07001702void OpenGLRenderer::setupShader(SkiaShader* shader) {
1703 mShader = shader;
1704 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001705 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001706 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001707}
1708
Romain Guyd27977d2010-07-14 19:18:51 -07001709///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001710// Color filters
1711///////////////////////////////////////////////////////////////////////////////
1712
1713void OpenGLRenderer::resetColorFilter() {
1714 mColorFilter = NULL;
1715}
1716
1717void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1718 mColorFilter = filter;
1719}
1720
1721///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001722// Drop shadow
1723///////////////////////////////////////////////////////////////////////////////
1724
1725void OpenGLRenderer::resetShadow() {
1726 mHasShadow = false;
1727}
1728
1729void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1730 mHasShadow = true;
1731 mShadowRadius = radius;
1732 mShadowDx = dx;
1733 mShadowDy = dy;
1734 mShadowColor = color;
1735}
1736
1737///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001738// Drawing implementation
1739///////////////////////////////////////////////////////////////////////////////
1740
Romain Guy01d58e42011-01-19 21:54:02 -08001741void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1742 float x, float y, SkPaint* paint) {
1743 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1744 return;
1745 }
1746
1747 int alpha;
1748 SkXfermode::Mode mode;
1749 getAlphaAndMode(paint, &alpha, &mode);
1750
1751 setupDraw();
1752 setupDrawWithTexture(true);
1753 setupDrawAlpha8Color(paint->getColor(), alpha);
1754 setupDrawColorFilter();
1755 setupDrawShader();
1756 setupDrawBlending(true, mode);
1757 setupDrawProgram();
1758 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1759 setupDrawTexture(texture->id);
1760 setupDrawPureColorUniforms();
1761 setupDrawColorFilterUniforms();
1762 setupDrawShaderUniforms();
1763 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1764
1765 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1766
1767 finishDrawTexture();
1768}
1769
Romain Guyf607bdc2010-09-10 19:20:06 -07001770// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001771#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1772#define kStdUnderline_Offset (1.0f / 9.0f)
1773#define kStdUnderline_Thickness (1.0f / 18.0f)
1774
1775void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1776 float x, float y, SkPaint* paint) {
1777 // Handle underline and strike-through
1778 uint32_t flags = paint->getFlags();
1779 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1780 float underlineWidth = length;
1781 // If length is > 0.0f, we already measured the text for the text alignment
1782 if (length <= 0.0f) {
1783 underlineWidth = paint->measureText(text, bytesCount);
1784 }
1785
1786 float offsetX = 0;
1787 switch (paint->getTextAlign()) {
1788 case SkPaint::kCenter_Align:
1789 offsetX = underlineWidth * 0.5f;
1790 break;
1791 case SkPaint::kRight_Align:
1792 offsetX = underlineWidth;
1793 break;
1794 default:
1795 break;
1796 }
1797
1798 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001799 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001800 // TODO: Support stroke width < 1.0f when we have AA lines
1801 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001802
Romain Guye20ecbd2010-09-22 19:49:04 -07001803 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001804 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001805
Romain Guyf6834472011-01-23 13:32:12 -08001806 int linesCount = 0;
1807 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1808 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1809
1810 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001811 float points[pointsCount];
1812 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001813
1814 if (flags & SkPaint::kUnderlineText_Flag) {
1815 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001816 points[currentPoint++] = left;
1817 points[currentPoint++] = top;
1818 points[currentPoint++] = left + underlineWidth;
1819 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001820 }
1821
1822 if (flags & SkPaint::kStrikeThruText_Flag) {
1823 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001824 points[currentPoint++] = left;
1825 points[currentPoint++] = top;
1826 points[currentPoint++] = left + underlineWidth;
1827 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001828 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001829
1830 SkPaint linesPaint(*paint);
1831 linesPaint.setStrokeWidth(strokeWidth);
1832
1833 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001834 }
1835 }
1836}
1837
Romain Guy026c5e162010-06-28 17:12:22 -07001838void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001839 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001840 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001841 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001842 color |= 0x00ffffff;
1843 }
1844
Romain Guy70ca14e2010-12-13 18:24:33 -08001845 setupDraw();
1846 setupDrawColor(color);
1847 setupDrawShader();
1848 setupDrawColorFilter();
1849 setupDrawBlending(mode);
1850 setupDrawProgram();
1851 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1852 setupDrawColorUniforms();
1853 setupDrawShaderUniforms(ignoreTransform);
1854 setupDrawColorFilterUniforms();
1855 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001856
Romain Guyc95c8d62010-09-17 15:31:32 -07001857 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1858}
1859
Romain Guy82ba8142010-07-09 13:25:56 -07001860void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001861 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001862 int alpha;
1863 SkXfermode::Mode mode;
1864 getAlphaAndMode(paint, &alpha, &mode);
1865
Romain Guy8164c2d2010-10-25 18:03:28 -07001866 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1867
Romain Guy6620c6d2010-12-06 18:07:02 -08001868 if (mSnapshot->transform->isPureTranslate()) {
1869 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1870 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1871
1872 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1873 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1874 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1875 } else {
1876 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1877 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1878 GL_TRIANGLE_STRIP, gMeshCount);
1879 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001880}
1881
Romain Guybd6b79b2010-06-26 00:13:53 -07001882void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001883 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1884 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001885 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001886}
1887
1888void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001889 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001890 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001891 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001892
Romain Guy746b7402010-10-26 16:27:31 -07001893 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001894 setupDrawWithTexture();
1895 setupDrawColor(alpha, alpha, alpha, alpha);
1896 setupDrawColorFilter();
1897 setupDrawBlending(blend, mode, swapSrcDst);
1898 setupDrawProgram();
1899 if (!dirty) {
1900 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001901 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001902 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001903 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001904 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001905 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001906 }
Romain Guy86568192010-12-14 15:55:39 -08001907 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001908 setupDrawColorFilterUniforms();
1909 setupDrawTexture(texture);
1910 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001911
Romain Guy6820ac82010-09-15 18:11:50 -07001912 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001913
1914 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001915}
1916
Romain Guya5aed0d2010-09-09 14:42:43 -07001917void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001918 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001919 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1920 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001921 if (mode < SkXfermode::kPlus_Mode) {
1922 if (!mCaches.blend) {
1923 glEnable(GL_BLEND);
1924 }
Romain Guy82ba8142010-07-09 13:25:56 -07001925
Romain Guyf607bdc2010-09-10 19:20:06 -07001926 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1927 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001928
Romain Guya5aed0d2010-09-09 14:42:43 -07001929 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1930 glBlendFunc(sourceMode, destMode);
1931 mCaches.lastSrcMode = sourceMode;
1932 mCaches.lastDstMode = destMode;
1933 }
1934 } else {
1935 // These blend modes are not supported by OpenGL directly and have
1936 // to be implemented using shaders. Since the shader will perform
1937 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001938 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001939 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001940 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001941 }
1942
1943 if (mCaches.blend) {
1944 glDisable(GL_BLEND);
1945 }
1946 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001947 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001948 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001949 glDisable(GL_BLEND);
1950 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001951 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001952}
1953
Romain Guy889f8d12010-07-29 14:37:42 -07001954bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001955 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001956 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001957 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001958 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001959 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001960 }
Romain Guy6926c722010-07-12 20:20:03 -07001961 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001962}
1963
Romain Guy026c5e162010-06-28 17:12:22 -07001964void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001965 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001966 TextureVertex::setUV(v++, u1, v1);
1967 TextureVertex::setUV(v++, u2, v1);
1968 TextureVertex::setUV(v++, u1, v2);
1969 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001970}
1971
Chet Haase5c13d892010-10-08 08:37:55 -07001972void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001973 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001974 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001975 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1976 if (!isMode) {
1977 // Assume SRC_OVER
1978 *mode = SkXfermode::kSrcOver_Mode;
1979 }
1980 } else {
1981 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001982 }
1983
1984 // Skia draws using the color's alpha channel if < 255
1985 // Otherwise, it uses the paint's alpha
1986 int color = paint->getColor();
1987 *alpha = (color >> 24) & 0xFF;
1988 if (*alpha == 255) {
1989 *alpha = paint->getAlpha();
1990 }
1991 } else {
1992 *mode = SkXfermode::kSrcOver_Mode;
1993 *alpha = 255;
1994 }
Romain Guy026c5e162010-06-28 17:12:22 -07001995}
1996
Romain Guya5aed0d2010-09-09 14:42:43 -07001997SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08001998 // In the future we should look at unifying the Porter-Duff modes and
1999 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002000 if (mode == NULL) {
2001 return SkXfermode::kSrcOver_Mode;
2002 }
2003 return mode->fMode;
2004}
2005
Romain Guy746b7402010-10-26 16:27:31 -07002006void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002007 bool bound = false;
2008 if (wrapS != texture->wrapS) {
2009 glBindTexture(GL_TEXTURE_2D, texture->id);
2010 bound = true;
2011 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2012 texture->wrapS = wrapS;
2013 }
2014 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002015 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002016 glBindTexture(GL_TEXTURE_2D, texture->id);
2017 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002018 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2019 texture->wrapT = wrapT;
2020 }
Romain Guya1db5742010-07-20 13:09:13 -07002021}
2022
Romain Guy9d5316e2010-06-24 19:30:36 -07002023}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002024}; // namespace android