blob: b8bd7d6758d27c6cd05043d7f52dddcce95dc2a4 [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
Romain Guycabfcc12011-03-07 18:06:46 -0800213bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800214 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
Romain Guycabfcc12011-03-07 18:06:46 -0800229 float bounds[4];
230 status_t result = (*functor)(&bounds[0], 4);
231
232 if (result != 0) {
233 Rect localDirty(bounds[0], bounds[1], bounds[2], bounds[3]);
234 dirty.unionWith(localDirty);
235 }
236
Chet Haasedaf98e92011-01-10 14:10:36 -0800237 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800238 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800239}
240
Romain Guyf6a11b82010-06-23 17:47:49 -0700241///////////////////////////////////////////////////////////////////////////////
242// State management
243///////////////////////////////////////////////////////////////////////////////
244
Romain Guybb9524b2010-06-22 18:56:38 -0700245int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700246 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700247}
248
249int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700250 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700251}
252
253void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700254 if (mSaveCount > 1) {
255 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700256 }
Romain Guybb9524b2010-06-22 18:56:38 -0700257}
258
259void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700260 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700261
Romain Guy8fb95422010-08-17 18:38:51 -0700262 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700263 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700264 }
Romain Guybb9524b2010-06-22 18:56:38 -0700265}
266
Romain Guy8aef54f2010-09-01 15:13:49 -0700267int OpenGLRenderer::saveSnapshot(int flags) {
268 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700269 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700270}
271
272bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700273 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700274 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700275 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700276
Romain Guybd6b79b2010-06-26 00:13:53 -0700277 sp<Snapshot> current = mSnapshot;
278 sp<Snapshot> previous = mSnapshot->previous;
279
Romain Guyeb993562010-10-05 18:14:38 -0700280 if (restoreOrtho) {
281 Rect& r = previous->viewport;
282 glViewport(r.left, r.top, r.right, r.bottom);
283 mOrthoMatrix.load(current->orthoMatrix);
284 }
285
Romain Guy8b55f372010-08-18 17:10:07 -0700286 mSaveCount--;
287 mSnapshot = previous;
288
Romain Guy2542d192010-08-18 11:47:12 -0700289 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700290 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700291 }
Romain Guy2542d192010-08-18 11:47:12 -0700292
Romain Guy5ec99242010-11-03 16:19:08 -0700293 if (restoreLayer) {
294 composeLayer(current, previous);
295 }
296
Romain Guy2542d192010-08-18 11:47:12 -0700297 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700298}
299
Romain Guyf6a11b82010-06-23 17:47:49 -0700300///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700301// Layers
302///////////////////////////////////////////////////////////////////////////////
303
304int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700305 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700306 const GLuint previousFbo = mSnapshot->fbo;
307 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700308
Romain Guyaf636eb2010-12-09 17:47:21 -0800309 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700310 int alpha = 255;
311 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700312
Romain Guye45362c2010-11-03 19:58:32 -0700313 if (p) {
314 alpha = p->getAlpha();
315 if (!mCaches.extensions.hasFramebufferFetch()) {
316 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
317 if (!isMode) {
318 // Assume SRC_OVER
319 mode = SkXfermode::kSrcOver_Mode;
320 }
321 } else {
322 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700323 }
324 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700325 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700326 }
Romain Guyd55a8612010-06-28 17:42:46 -0700327
Romain Guydbc26d22010-10-11 17:58:29 -0700328 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
329 }
Romain Guyd55a8612010-06-28 17:42:46 -0700330
331 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700332}
333
334int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
335 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700336 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700337 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700338 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700339 SkPaint paint;
340 paint.setAlpha(alpha);
341 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700342 }
Romain Guyd55a8612010-06-28 17:42:46 -0700343}
Romain Guybd6b79b2010-06-26 00:13:53 -0700344
Romain Guy1c740bc2010-09-13 18:00:09 -0700345/**
346 * Layers are viewed by Skia are slightly different than layers in image editing
347 * programs (for instance.) When a layer is created, previously created layers
348 * and the frame buffer still receive every drawing command. For instance, if a
349 * layer is created and a shape intersecting the bounds of the layers and the
350 * framebuffer is draw, the shape will be drawn on both (unless the layer was
351 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
352 *
353 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
354 * texture. Unfortunately, this is inefficient as it requires every primitive to
355 * be drawn n + 1 times, where n is the number of active layers. In practice this
356 * means, for every primitive:
357 * - Switch active frame buffer
358 * - Change viewport, clip and projection matrix
359 * - Issue the drawing
360 *
361 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700362 * To avoid this, layers are implemented in a different way here, at least in the
363 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
364 * is set. When this flag is set we can redirect all drawing operations into a
365 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700366 *
367 * This implementation relies on the frame buffer being at least RGBA 8888. When
368 * a layer is created, only a texture is created, not an FBO. The content of the
369 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700370 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700371 * buffer and drawing continues as normal. This technique therefore treats the
372 * frame buffer as a scratch buffer for the layers.
373 *
374 * To compose the layers back onto the frame buffer, each layer texture
375 * (containing the original frame buffer data) is drawn as a simple quad over
376 * the frame buffer. The trick is that the quad is set as the composition
377 * destination in the blending equation, and the frame buffer becomes the source
378 * of the composition.
379 *
380 * Drawing layers with an alpha value requires an extra step before composition.
381 * An empty quad is drawn over the layer's region in the frame buffer. This quad
382 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
383 * quad is used to multiply the colors in the frame buffer. This is achieved by
384 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
385 * GL_ZERO, GL_SRC_ALPHA.
386 *
387 * Because glCopyTexImage2D() can be slow, an alternative implementation might
388 * be use to draw a single clipped layer. The implementation described above
389 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700390 *
391 * (1) The frame buffer is actually not cleared right away. To allow the GPU
392 * to potentially optimize series of calls to glCopyTexImage2D, the frame
393 * buffer is left untouched until the first drawing operation. Only when
394 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700395 */
Romain Guyd55a8612010-06-28 17:42:46 -0700396bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700397 float right, float bottom, int alpha, SkXfermode::Mode mode,
398 int flags, GLuint previousFbo) {
399 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700400 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700401
Romain Guyeb993562010-10-05 18:14:38 -0700402 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
403
Romain Guyf607bdc2010-09-10 19:20:06 -0700404 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700405 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700406 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700407 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700408
Romain Guyeb993562010-10-05 18:14:38 -0700409 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700410 if (bounds.intersect(*snapshot->clipRect)) {
411 // We cannot work with sub-pixels in this case
412 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700413
Romain Guyad37cd32011-03-15 11:12:25 -0700414 // When the layer is not an FBO, we may use glCopyTexImage so we
415 // need to make sure the layer does not extend outside the bounds
416 // of the framebuffer
417 if (!bounds.intersect(snapshot->previous->viewport)) {
418 bounds.setEmpty();
419 }
420 } else {
421 bounds.setEmpty();
422 }
Romain Guyeb993562010-10-05 18:14:38 -0700423 }
Romain Guybf434112010-09-16 14:40:17 -0700424
Romain Guy746b7402010-10-26 16:27:31 -0700425 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
426 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800427 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700428 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700429 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700430 }
431
432 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800433 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700434 return false;
435 }
Romain Guyf18fd992010-07-08 11:45:51 -0700436
Romain Guy5b3b3522010-10-27 18:57:51 -0700437 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700438 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700439 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700440 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700441 }
442
Romain Guydda570202010-07-06 11:39:32 -0700443 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700444 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700445 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700446 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
447 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800448 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700449
Romain Guy8fb95422010-08-17 18:38:51 -0700450 // Save the layer in the snapshot
451 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700452 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700453
Romain Guyeb993562010-10-05 18:14:38 -0700454 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700455 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700456 } else {
457 // Copy the framebuffer into the layer
458 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800459 if (!bounds.isEmpty()) {
460 if (layer->empty) {
461 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
462 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
463 layer->empty = false;
464 } else {
465 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
466 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
467 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700468
469 // Clear the framebuffer where the layer will draw
470 glScissor(bounds.left, mSnapshot->height - bounds.bottom,
471 bounds.getWidth(), bounds.getHeight());
472 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
473 glClear(GL_COLOR_BUFFER_BIT);
474
475 dirtyClip();
Romain Guyae88e5e2010-10-22 17:49:18 -0700476 }
Romain Guyeb993562010-10-05 18:14:38 -0700477 }
Romain Guyf86ef572010-07-01 11:05:42 -0700478
Romain Guyd55a8612010-06-28 17:42:46 -0700479 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700480}
481
Romain Guy5b3b3522010-10-27 18:57:51 -0700482bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
483 GLuint previousFbo) {
484 layer->fbo = mCaches.fboCache.get();
485
486#if RENDER_LAYERS_AS_REGIONS
487 snapshot->region = &snapshot->layer->region;
488 snapshot->flags |= Snapshot::kFlagFboTarget;
489#endif
490
491 Rect clip(bounds);
492 snapshot->transform->mapRect(clip);
493 clip.intersect(*snapshot->clipRect);
494 clip.snapToPixelBoundaries();
495 clip.intersect(snapshot->previous->viewport);
496
497 mat4 inverse;
498 inverse.loadInverse(*mSnapshot->transform);
499
500 inverse.mapRect(clip);
501 clip.snapToPixelBoundaries();
502 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700503 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700504
505 snapshot->flags |= Snapshot::kFlagIsFboLayer;
506 snapshot->fbo = layer->fbo;
507 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700508 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
509 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
510 snapshot->height = bounds.getHeight();
511 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
512 snapshot->orthoMatrix.load(mOrthoMatrix);
513
514 // Bind texture to FBO
515 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
516 glBindTexture(GL_TEXTURE_2D, layer->texture);
517
518 // Initialize the texture if needed
519 if (layer->empty) {
520 layer->empty = false;
521 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
522 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
523 }
524
525 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
526 layer->texture, 0);
527
528#if DEBUG_LAYERS_AS_REGIONS
529 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
530 if (status != GL_FRAMEBUFFER_COMPLETE) {
531 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
532
533 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
534 glDeleteTextures(1, &layer->texture);
535 mCaches.fboCache.put(layer->fbo);
536
537 delete layer;
538
539 return false;
540 }
541#endif
542
543 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
544 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
545 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
546 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
547 glClear(GL_COLOR_BUFFER_BIT);
548
549 dirtyClip();
550
551 // Change the ortho projection
552 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
553 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
554
555 return true;
556}
557
Romain Guy1c740bc2010-09-13 18:00:09 -0700558/**
559 * Read the documentation of createLayer() before doing anything in this method.
560 */
Romain Guy1d83e192010-08-17 11:37:00 -0700561void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
562 if (!current->layer) {
563 LOGE("Attempting to compose a layer that does not exist");
564 return;
565 }
566
Romain Guy5b3b3522010-10-27 18:57:51 -0700567 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700568
569 if (fboLayer) {
570 // Unbind current FBO and restore previous one
571 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
572 }
573
Romain Guy1d83e192010-08-17 11:37:00 -0700574 Layer* layer = current->layer;
575 const Rect& rect = layer->layer;
576
Romain Guyeb993562010-10-05 18:14:38 -0700577 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700578 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700579 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700580 // Required below, composeLayerRect() will divide by 255
581 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700582 }
583
Romain Guy03750a02010-10-18 14:06:08 -0700584 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700585
Romain Guy746b7402010-10-26 16:27:31 -0700586 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700587
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 // When the layer is stored in an FBO, we can save a bit of fillrate by
589 // drawing only the dirty region
590 if (fboLayer) {
591 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800592 if (layer->colorFilter) {
593 setupColorFilter(layer->colorFilter);
594 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700595 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800596 if (layer->colorFilter) {
597 resetColorFilter();
598 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700599 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800600 if (!rect.isEmpty()) {
601 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
602 composeLayerRect(layer, rect, true);
603 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 }
Romain Guy8b55f372010-08-18 17:10:07 -0700605
Romain Guyeb993562010-10-05 18:14:38 -0700606 if (fboLayer) {
607 // Detach the texture from the FBO
608 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
609 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
610 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
611
612 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
613 mCaches.fboCache.put(current->fbo);
614 }
615
Romain Guy746b7402010-10-26 16:27:31 -0700616 dirtyClip();
617
Romain Guyeb993562010-10-05 18:14:38 -0700618 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700619 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700620 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700621 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700622 delete layer;
623 }
624}
625
Romain Guy5b3b3522010-10-27 18:57:51 -0700626void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
627 const Rect& texCoords = layer->texCoords;
628 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
629
630 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
631 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
632 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
633
634 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
635}
636
637void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
638#if RENDER_LAYERS_AS_REGIONS
639 if (layer->region.isRect()) {
640 composeLayerRect(layer, rect);
641 layer->region.clear();
642 return;
643 }
644
645 if (!layer->region.isEmpty()) {
646 size_t count;
647 const android::Rect* rects = layer->region.getArray(&count);
648
Romain Guy5b3b3522010-10-27 18:57:51 -0700649 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700650 const float texX = 1.0f / float(layer->width);
651 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800652 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700653
654 TextureVertex* mesh = mCaches.getRegionMesh();
655 GLsizei numQuads = 0;
656
Romain Guy7230a742011-01-10 22:26:16 -0800657 setupDraw();
658 setupDrawWithTexture();
659 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800660 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800661 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
662 setupDrawProgram();
663 setupDrawDirtyRegionsDisabled();
664 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800665 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800666 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800667 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800668 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700669
670 for (size_t i = 0; i < count; i++) {
671 const android::Rect* r = &rects[i];
672
673 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800674 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700675 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800676 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700677
678 // TODO: Reject quads outside of the clip
679 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
680 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
681 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
682 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
683
684 numQuads++;
685
686 if (numQuads >= REGION_MESH_QUAD_COUNT) {
687 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
688 numQuads = 0;
689 mesh = mCaches.getRegionMesh();
690 }
691 }
692
693 if (numQuads > 0) {
694 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
695 }
696
697 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800698 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700699
700#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800701 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700702#endif
703
704 layer->region.clear();
705 }
706#else
707 composeLayerRect(layer, rect);
708#endif
709}
710
Romain Guy3a3133d2011-02-01 22:59:58 -0800711void OpenGLRenderer::drawRegionRects(const Region& region) {
712#if DEBUG_LAYERS_AS_REGIONS
713 size_t count;
714 const android::Rect* rects = region.getArray(&count);
715
716 uint32_t colors[] = {
717 0x7fff0000, 0x7f00ff00,
718 0x7f0000ff, 0x7fff00ff,
719 };
720
721 int offset = 0;
722 int32_t top = rects[0].top;
723
724 for (size_t i = 0; i < count; i++) {
725 if (top != rects[i].top) {
726 offset ^= 0x2;
727 top = rects[i].top;
728 }
729
730 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
731 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
732 SkXfermode::kSrcOver_Mode);
733 }
734#endif
735}
736
Romain Guy5b3b3522010-10-27 18:57:51 -0700737void OpenGLRenderer::dirtyLayer(const float left, const float top,
738 const float right, const float bottom, const mat4 transform) {
739#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800740 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700741 Rect bounds(left, top, right, bottom);
742 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800743 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700744 }
745#endif
746}
747
748void OpenGLRenderer::dirtyLayer(const float left, const float top,
749 const float right, const float bottom) {
750#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800751 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800753 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800754 }
755#endif
756}
757
758void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
759#if RENDER_LAYERS_AS_REGIONS
760 if (bounds.intersect(*mSnapshot->clipRect)) {
761 bounds.snapToPixelBoundaries();
762 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
763 if (!dirty.isEmpty()) {
764 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700765 }
766 }
767#endif
768}
769
Romain Guybd6b79b2010-06-26 00:13:53 -0700770///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700771// Transforms
772///////////////////////////////////////////////////////////////////////////////
773
774void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700775 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700776}
777
778void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700779 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700780}
781
782void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700783 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700784}
785
Romain Guy807daf72011-01-18 11:19:19 -0800786void OpenGLRenderer::skew(float sx, float sy) {
787 mSnapshot->transform->skew(sx, sy);
788}
789
Romain Guyf6a11b82010-06-23 17:47:49 -0700790void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700791 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700792}
793
Romain Guy41030da2010-10-13 13:40:37 -0700794const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700795 if (mSnapshot->fbo != 0) {
796 return &mSnapshot->transform->data[0];
797 }
798 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700799}
800
Romain Guyf6a11b82010-06-23 17:47:49 -0700801void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700802 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700803}
804
805void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700806 SkMatrix transform;
807 mSnapshot->transform->copyTo(transform);
808 transform.preConcat(*matrix);
809 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700810}
811
812///////////////////////////////////////////////////////////////////////////////
813// Clipping
814///////////////////////////////////////////////////////////////////////////////
815
Romain Guybb9524b2010-06-22 18:56:38 -0700816void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700817 Rect clip(*mSnapshot->clipRect);
818 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700819 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700820 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700821}
822
823const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700824 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700825}
826
Romain Guyc7d53492010-06-25 13:41:57 -0700827bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800828 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700829 return true;
830 }
831
Romain Guy1d83e192010-08-17 11:37:00 -0700832 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700833 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700834 r.snapToPixelBoundaries();
835
836 Rect clipRect(*mSnapshot->clipRect);
837 clipRect.snapToPixelBoundaries();
838
839 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700840}
841
Romain Guy079ba2c2010-07-16 14:12:24 -0700842bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
843 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700844 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700845 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700846 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700847 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700848}
849
Romain Guyf6a11b82010-06-23 17:47:49 -0700850///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800851// Drawing commands
852///////////////////////////////////////////////////////////////////////////////
853
854void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800855 if (mDirtyClip) {
856 setScissorFromClip();
857 }
858 mDescription.reset();
859 mSetShaderColor = false;
860 mColorSet = false;
861 mColorA = mColorR = mColorG = mColorB = 0.0f;
862 mTextureUnit = 0;
863 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800864 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800865}
866
867void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
868 mDescription.hasTexture = true;
869 mDescription.hasAlpha8Texture = isAlpha8;
870}
871
872void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800873 setupDrawColor(color, (color >> 24) & 0xFF);
874}
875
876void OpenGLRenderer::setupDrawColor(int color, int alpha) {
877 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800878 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800879 mColorR = a * ((color >> 16) & 0xFF);
880 mColorG = a * ((color >> 8) & 0xFF);
881 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800882 mColorSet = true;
883 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
884}
885
Romain Guy86568192010-12-14 15:55:39 -0800886void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
887 mColorA = alpha / 255.0f;
888 const float a = mColorA / 255.0f;
889 mColorR = a * ((color >> 16) & 0xFF);
890 mColorG = a * ((color >> 8) & 0xFF);
891 mColorB = a * ((color ) & 0xFF);
892 mColorSet = true;
893 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
894}
895
Romain Guy70ca14e2010-12-13 18:24:33 -0800896void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
897 mColorA = a;
898 mColorR = r;
899 mColorG = g;
900 mColorB = b;
901 mColorSet = true;
902 mSetShaderColor = mDescription.setColor(r, g, b, a);
903}
904
Romain Guy86568192010-12-14 15:55:39 -0800905void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
906 mColorA = a;
907 mColorR = r;
908 mColorG = g;
909 mColorB = b;
910 mColorSet = true;
911 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
912}
913
Romain Guy70ca14e2010-12-13 18:24:33 -0800914void OpenGLRenderer::setupDrawShader() {
915 if (mShader) {
916 mShader->describe(mDescription, mCaches.extensions);
917 }
918}
919
920void OpenGLRenderer::setupDrawColorFilter() {
921 if (mColorFilter) {
922 mColorFilter->describe(mDescription, mCaches.extensions);
923 }
924}
925
926void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
927 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
928 mDescription, swapSrcDst);
929}
930
931void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
932 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
933 mDescription, swapSrcDst);
934}
935
936void OpenGLRenderer::setupDrawProgram() {
937 useProgram(mCaches.programCache.get(mDescription));
938}
939
940void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
941 mTrackDirtyRegions = false;
942}
943
944void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
945 bool ignoreTransform) {
946 mModelView.loadTranslate(left, top, 0.0f);
947 if (!ignoreTransform) {
948 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
949 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
950 } else {
951 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
952 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
953 }
954}
955
Romain Guy8d0d4782010-12-14 20:13:35 -0800956void OpenGLRenderer::setupDrawModelViewIdentity() {
957 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
958}
959
Romain Guy70ca14e2010-12-13 18:24:33 -0800960void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
961 bool ignoreTransform, bool ignoreModelView) {
962 if (!ignoreModelView) {
963 mModelView.loadTranslate(left, top, 0.0f);
964 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800965 } else {
966 mModelView.loadIdentity();
967 }
Romain Guy86568192010-12-14 15:55:39 -0800968 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
969 if (!ignoreTransform) {
970 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
971 if (mTrackDirtyRegions && dirty) {
972 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
973 }
974 } else {
975 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
976 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
977 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800978}
979
980void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800981 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800982 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
983 }
984}
985
Romain Guy86568192010-12-14 15:55:39 -0800986void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800987 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800988 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800989 }
990}
991
Romain Guy70ca14e2010-12-13 18:24:33 -0800992void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
993 if (mShader) {
994 if (ignoreTransform) {
995 mModelView.loadInverse(*mSnapshot->transform);
996 }
997 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
998 }
999}
1000
Romain Guy8d0d4782010-12-14 20:13:35 -08001001void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1002 if (mShader) {
1003 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1004 }
1005}
1006
Romain Guy70ca14e2010-12-13 18:24:33 -08001007void OpenGLRenderer::setupDrawColorFilterUniforms() {
1008 if (mColorFilter) {
1009 mColorFilter->setupProgram(mCaches.currentProgram);
1010 }
1011}
1012
1013void OpenGLRenderer::setupDrawSimpleMesh() {
1014 mCaches.bindMeshBuffer();
1015 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1016 gMeshStride, 0);
1017}
1018
1019void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1020 bindTexture(texture);
1021 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1022
1023 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1024 glEnableVertexAttribArray(mTexCoordsSlot);
1025}
1026
1027void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1028 if (!vertices) {
1029 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1030 } else {
1031 mCaches.unbindMeshBuffer();
1032 }
1033 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1034 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001035 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001036 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1037 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001038}
1039
1040void OpenGLRenderer::finishDrawTexture() {
1041 glDisableVertexAttribArray(mTexCoordsSlot);
1042}
1043
1044///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001045// Drawing
1046///////////////////////////////////////////////////////////////////////////////
1047
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001048bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1049 Rect& dirty, uint32_t level) {
1050 if (quickReject(0.0f, 0.0f, width, height)) {
1051 return false;
1052 }
1053
Romain Guy0fe478e2010-11-08 12:08:41 -08001054 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1055 // will be performed by the display list itself
1056 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001057 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001058 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001059
Chet Haasedaf98e92011-01-10 14:10:36 -08001060 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001061}
1062
Chet Haase5c13d892010-10-08 08:37:55 -07001063void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001064 const float right = left + bitmap->width();
1065 const float bottom = top + bitmap->height();
1066
1067 if (quickReject(left, top, right, bottom)) {
1068 return;
1069 }
1070
Romain Guy86568192010-12-14 15:55:39 -08001071 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001072 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001073 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001074 const AutoTexture autoCleanup(texture);
1075
Romain Guy6926c722010-07-12 20:20:03 -07001076 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001077}
1078
Chet Haase5c13d892010-10-08 08:37:55 -07001079void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001080 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1081 const mat4 transform(*matrix);
1082 transform.mapRect(r);
1083
Romain Guy6926c722010-07-12 20:20:03 -07001084 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1085 return;
1086 }
1087
Romain Guy86568192010-12-14 15:55:39 -08001088 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001089 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001090 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001091 const AutoTexture autoCleanup(texture);
1092
Romain Guy5b3b3522010-10-27 18:57:51 -07001093 // This could be done in a cheaper way, all we need is pass the matrix
1094 // to the vertex shader. The save/restore is a bit overkill.
1095 save(SkCanvas::kMatrix_SaveFlag);
1096 concatMatrix(matrix);
1097 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1098 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001099}
1100
Romain Guy5a7b4662011-01-20 19:09:30 -08001101void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1102 float* vertices, int* colors, SkPaint* paint) {
1103 // TODO: Do a quickReject
1104 if (!vertices || mSnapshot->isIgnored()) {
1105 return;
1106 }
1107
1108 glActiveTexture(gTextureUnits[0]);
1109 Texture* texture = mCaches.textureCache.get(bitmap);
1110 if (!texture) return;
1111 const AutoTexture autoCleanup(texture);
1112 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1113
1114 int alpha;
1115 SkXfermode::Mode mode;
1116 getAlphaAndMode(paint, &alpha, &mode);
1117
Romain Guy5a7b4662011-01-20 19:09:30 -08001118 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001119
Romain Guyb18d2d02011-02-10 15:52:54 -08001120 float left = FLT_MAX;
1121 float top = FLT_MAX;
1122 float right = FLT_MIN;
1123 float bottom = FLT_MIN;
1124
1125#if RENDER_LAYERS_AS_REGIONS
1126 bool hasActiveLayer = hasLayer();
1127#else
1128 bool hasActiveLayer = false;
1129#endif
1130
Romain Guya566b7c2011-01-23 16:36:11 -08001131 // TODO: Support the colors array
1132 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001133 TextureVertex* vertex = mesh;
1134 for (int32_t y = 0; y < meshHeight; y++) {
1135 for (int32_t x = 0; x < meshWidth; x++) {
1136 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1137
1138 float u1 = float(x) / meshWidth;
1139 float u2 = float(x + 1) / meshWidth;
1140 float v1 = float(y) / meshHeight;
1141 float v2 = float(y + 1) / meshHeight;
1142
1143 int ax = i + (meshWidth + 1) * 2;
1144 int ay = ax + 1;
1145 int bx = i;
1146 int by = bx + 1;
1147 int cx = i + 2;
1148 int cy = cx + 1;
1149 int dx = i + (meshWidth + 1) * 2 + 2;
1150 int dy = dx + 1;
1151
1152 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1153 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1154 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1155
1156 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1157 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1158 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001159
1160#if RENDER_LAYERS_AS_REGIONS
1161 if (hasActiveLayer) {
1162 // TODO: This could be optimized to avoid unnecessary ops
1163 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1164 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1165 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1166 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1167 }
1168#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001169 }
1170 }
1171
Romain Guyb18d2d02011-02-10 15:52:54 -08001172#if RENDER_LAYERS_AS_REGIONS
1173 if (hasActiveLayer) {
1174 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1175 }
1176#endif
1177
Romain Guy5a7b4662011-01-20 19:09:30 -08001178 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1179 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001180 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001181}
1182
Romain Guy8ba548f2010-06-30 19:21:21 -07001183void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1184 float srcLeft, float srcTop, float srcRight, float srcBottom,
1185 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001186 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001187 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1188 return;
1189 }
1190
Romain Guy746b7402010-10-26 16:27:31 -07001191 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001192 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001193 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001194 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001195 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001196
Romain Guy8ba548f2010-06-30 19:21:21 -07001197 const float width = texture->width;
1198 const float height = texture->height;
1199
1200 const float u1 = srcLeft / width;
1201 const float v1 = srcTop / height;
1202 const float u2 = srcRight / width;
1203 const float v2 = srcBottom / height;
1204
Romain Guy03750a02010-10-18 14:06:08 -07001205 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001206 resetDrawTextureTexCoords(u1, v1, u2, v2);
1207
Romain Guy03750a02010-10-18 14:06:08 -07001208 int alpha;
1209 SkXfermode::Mode mode;
1210 getAlphaAndMode(paint, &alpha, &mode);
1211
Romain Guy6620c6d2010-12-06 18:07:02 -08001212 if (mSnapshot->transform->isPureTranslate()) {
1213 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1214 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1215
1216 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1217 texture->id, alpha / 255.0f, mode, texture->blend,
1218 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1219 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1220 } else {
1221 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1222 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1223 GL_TRIANGLE_STRIP, gMeshCount);
1224 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001225
1226 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1227}
1228
Romain Guy4aa90572010-09-26 18:40:37 -07001229void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001230 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001231 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001232 if (quickReject(left, top, right, bottom)) {
1233 return;
1234 }
1235
Romain Guy746b7402010-10-26 16:27:31 -07001236 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001237 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001238 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001239 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001240 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001241
1242 int alpha;
1243 SkXfermode::Mode mode;
1244 getAlphaAndMode(paint, &alpha, &mode);
1245
Romain Guy2728f962010-10-08 18:36:15 -07001246 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001247 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001248
Romain Guya5ef39a2010-12-03 16:48:20 -08001249 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001250 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001251#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001252 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001253 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001254 const float offsetX = left + mSnapshot->transform->getTranslateX();
1255 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 const size_t count = mesh->quads.size();
1257 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001258 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001259 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001260 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1261 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1262 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001263 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001264 dirtyLayer(left + bounds.left, top + bounds.top,
1265 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001266 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001267 }
1268 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001269#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001270
Romain Guy6620c6d2010-12-06 18:07:02 -08001271 if (pureTranslate) {
1272 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1273 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1274
1275 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1276 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1277 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1278 true, !mesh->hasEmptyQuads);
1279 } else {
1280 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1281 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1282 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1283 true, !mesh->hasEmptyQuads);
1284 }
Romain Guy054dc182010-10-15 17:55:25 -07001285 }
Romain Guyf7f93552010-07-08 19:17:03 -07001286}
1287
Chet Haase5c13d892010-10-08 08:37:55 -07001288void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001289 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001290
Romain Guya957eea2010-12-08 18:34:42 -08001291 const bool isAA = paint->isAntiAlias();
1292 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1293 // A stroke width of 0 has a special meaningin Skia:
1294 // it draws an unscaled 1px wide line
1295 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1296
Romain Guy759ea802010-09-16 20:49:46 -07001297 int alpha;
1298 SkXfermode::Mode mode;
1299 getAlphaAndMode(paint, &alpha, &mode);
1300
Romain Guya957eea2010-12-08 18:34:42 -08001301 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001302 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001303 if (!isHairLine) {
1304 // TODO: AA needs more vertices
1305 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001306 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001307 // TODO: AA will be different
1308 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001309 }
1310
Romain Guya957eea2010-12-08 18:34:42 -08001311 TextureVertex lines[verticesCount];
1312 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001313
Romain Guy8d0d4782010-12-14 20:13:35 -08001314 setupDraw();
1315 setupDrawColor(paint->getColor(), alpha);
1316 setupDrawColorFilter();
1317 setupDrawShader();
1318 setupDrawBlending(mode);
1319 setupDrawProgram();
1320 setupDrawModelViewIdentity();
1321 setupDrawColorUniforms();
1322 setupDrawColorFilterUniforms();
1323 setupDrawShaderIdentityUniforms();
1324 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001325
1326 if (!isHairLine) {
1327 // TODO: Handle the AA case
1328 for (int i = 0; i < count; i += 4) {
1329 // a = start point, b = end point
1330 vec2 a(points[i], points[i + 1]);
1331 vec2 b(points[i + 2], points[i + 3]);
1332
1333 // Bias to snap to the same pixels as Skia
1334 a += 0.375;
1335 b += 0.375;
1336
1337 // Find the normal to the line
1338 vec2 n = (b - a).copyNormalized() * strokeWidth;
1339 float x = n.x;
1340 n.x = -n.y;
1341 n.y = x;
1342
1343 // Four corners of the rectangle defining a thick line
1344 vec2 p1 = a - n;
1345 vec2 p2 = a + n;
1346 vec2 p3 = b + n;
1347 vec2 p4 = b - n;
1348
Romain Guy7230a742011-01-10 22:26:16 -08001349 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1350 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1351 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1352 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001353
Romain Guy7230a742011-01-10 22:26:16 -08001354 if (!quickReject(left, top, right, bottom)) {
1355 // Draw the line as 2 triangles, could be optimized
1356 // by using only 4 vertices and the correct indices
1357 // Also we should probably used non textured vertices
1358 // when line AA is disabled to save on bandwidth
1359 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1360 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1361 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1362 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1363 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1364 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1365
1366 generatedVerticesCount += 6;
1367
1368 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1369 }
Romain Guya957eea2010-12-08 18:34:42 -08001370 }
1371
Romain Guy7230a742011-01-10 22:26:16 -08001372 if (generatedVerticesCount > 0) {
1373 // GL_LINE does not give the result we want to match Skia
1374 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1375 }
Romain Guya957eea2010-12-08 18:34:42 -08001376 } else {
1377 // TODO: Handle the AA case
1378 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001379 const float left = fmin(points[i], points[i + 1]);
1380 const float right = fmax(points[i], points[i + 1]);
1381 const float top = fmin(points[i + 2], points[i + 3]);
1382 const float bottom = fmax(points[i + 2], points[i + 3]);
1383
1384 if (!quickReject(left, top, right, bottom)) {
1385 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1386 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1387
1388 generatedVerticesCount += 2;
1389
1390 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1391 }
Romain Guya957eea2010-12-08 18:34:42 -08001392 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001393
Romain Guy7230a742011-01-10 22:26:16 -08001394 if (generatedVerticesCount > 0) {
1395 glLineWidth(1.0f);
1396 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1397 }
Romain Guy29d89972010-09-22 16:10:57 -07001398 }
Romain Guy759ea802010-09-16 20:49:46 -07001399}
1400
Romain Guy85bf02f2010-06-22 13:11:24 -07001401void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001402 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001403 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001404
Romain Guyae88e5e2010-10-22 17:49:18 -07001405 Rect& clip(*mSnapshot->clipRect);
1406 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001407
Romain Guy3d58c032010-07-14 16:34:53 -07001408 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001409}
Romain Guy9d5316e2010-06-24 19:30:36 -07001410
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001411void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001412 if (!texture) return;
1413 const AutoTexture autoCleanup(texture);
1414
1415 const float x = left + texture->left - texture->offset;
1416 const float y = top + texture->top - texture->offset;
1417
1418 drawPathTexture(texture, x, y, paint);
1419}
1420
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001421void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1422 float rx, float ry, SkPaint* paint) {
1423 if (mSnapshot->isIgnored()) return;
1424
1425 glActiveTexture(gTextureUnits[0]);
1426 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1427 right - left, bottom - top, rx, ry, paint);
1428 drawShape(left, top, texture, paint);
1429}
1430
Romain Guy01d58e42011-01-19 21:54:02 -08001431void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1432 if (mSnapshot->isIgnored()) return;
1433
1434 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001435 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001436 drawShape(x - radius, y - radius, texture, paint);
1437}
Romain Guy01d58e42011-01-19 21:54:02 -08001438
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001439void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1440 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001441
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001442 glActiveTexture(gTextureUnits[0]);
1443 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1444 drawShape(left, top, texture, paint);
1445}
1446
Romain Guy8b2f5262011-01-23 16:15:02 -08001447void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1448 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1449 if (mSnapshot->isIgnored()) return;
1450
1451 if (fabs(sweepAngle) >= 360.0f) {
1452 drawOval(left, top, right, bottom, paint);
1453 return;
1454 }
1455
1456 glActiveTexture(gTextureUnits[0]);
1457 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1458 startAngle, sweepAngle, useCenter, paint);
1459 drawShape(left, top, texture, paint);
1460}
1461
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001462void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1463 SkPaint* paint) {
1464 if (mSnapshot->isIgnored()) return;
1465
1466 glActiveTexture(gTextureUnits[0]);
1467 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1468 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001469}
1470
Chet Haase5c13d892010-10-08 08:37:55 -07001471void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001472 if (p->getStyle() != SkPaint::kFill_Style) {
1473 drawRectAsShape(left, top, right, bottom, p);
1474 return;
1475 }
1476
Romain Guy6926c722010-07-12 20:20:03 -07001477 if (quickReject(left, top, right, bottom)) {
1478 return;
1479 }
1480
Romain Guy026c5e162010-06-28 17:12:22 -07001481 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001482 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001483 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1484 if (!isMode) {
1485 // Assume SRC_OVER
1486 mode = SkXfermode::kSrcOver_Mode;
1487 }
1488 } else {
1489 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001490 }
1491
Romain Guy026c5e162010-06-28 17:12:22 -07001492 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001493 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001494}
Romain Guy9d5316e2010-06-24 19:30:36 -07001495
Romain Guye8e62a42010-07-23 18:55:21 -07001496void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1497 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001498 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001499 return;
1500 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001501 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001502
Romain Guyf607bdc2010-09-10 19:20:06 -07001503 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001504
Romain Guya674ab72010-08-10 17:26:42 -07001505 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001506 switch (paint->getTextAlign()) {
1507 case SkPaint::kCenter_Align:
1508 length = paint->measureText(text, bytesCount);
1509 x -= length / 2.0f;
1510 break;
1511 case SkPaint::kRight_Align:
1512 length = paint->measureText(text, bytesCount);
1513 x -= length;
1514 break;
1515 default:
1516 break;
1517 }
1518
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001519 const float oldX = x;
1520 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001521 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1522 if (pureTranslate) {
1523 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1524 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1525 }
1526
Romain Guyb45c0c92010-08-26 20:35:23 -07001527 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1528 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001529 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001530
Romain Guy86568192010-12-14 15:55:39 -08001531 int alpha;
1532 SkXfermode::Mode mode;
1533 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001534
Romain Guy1e45aae2010-08-13 19:39:53 -07001535 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001536 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001537 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001538 count, mShadowRadius);
1539 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001540
Romain Guy86568192010-12-14 15:55:39 -08001541 const float sx = x - shadow->left + mShadowDx;
1542 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001543
Romain Guy86568192010-12-14 15:55:39 -08001544 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1545
1546 glActiveTexture(gTextureUnits[0]);
1547 setupDraw();
1548 setupDrawWithTexture(true);
1549 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1550 setupDrawBlending(true, mode);
1551 setupDrawProgram();
1552 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1553 setupDrawTexture(shadow->id);
1554 setupDrawPureColorUniforms();
1555 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1556
Romain Guy0a417492010-08-16 20:26:20 -07001557 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001558 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001559 }
1560
Romain Guyb146b122010-12-15 17:06:45 -08001561 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1562 return;
1563 }
1564
Romain Guy6620c6d2010-12-06 18:07:02 -08001565 // Pick the appropriate texture filtering
1566 bool linearFilter = mSnapshot->transform->changesBounds();
1567 if (pureTranslate && !linearFilter) {
1568 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1569 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001570
Romain Guy86568192010-12-14 15:55:39 -08001571 glActiveTexture(gTextureUnits[0]);
1572 setupDraw();
1573 setupDrawDirtyRegionsDisabled();
1574 setupDrawWithTexture(true);
1575 setupDrawAlpha8Color(paint->getColor(), alpha);
1576 setupDrawColorFilter();
1577 setupDrawShader();
1578 setupDrawBlending(true, mode);
1579 setupDrawProgram();
1580 setupDrawModelView(x, y, x, y, pureTranslate, true);
1581 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1582 setupDrawPureColorUniforms();
1583 setupDrawColorFilterUniforms();
1584 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001585
Romain Guy6620c6d2010-12-06 18:07:02 -08001586 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001587 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1588
1589#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001590 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001591#else
Romain Guyf219da52011-01-16 12:54:25 -08001592 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001593#endif
Romain Guy03750a02010-10-18 14:06:08 -07001594 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001595
1596 // Tell font renderer the locations of position and texture coord
1597 // attributes so it can bind its data properly
1598 int positionSlot = mCaches.currentProgram->position;
1599 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001600 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001601 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001602#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001603 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001604 if (!pureTranslate) {
1605 mSnapshot->transform->mapRect(bounds);
1606 }
Romain Guyf219da52011-01-16 12:54:25 -08001607 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001608 }
1609#endif
1610 }
Romain Guy694b5192010-07-21 21:33:20 -07001611
1612 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001613 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001614
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001615 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001616}
1617
Romain Guy7fbcc042010-08-04 15:40:07 -07001618void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001619 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001620
Romain Guy01d58e42011-01-19 21:54:02 -08001621 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001622
Romain Guyfb8b7632010-08-23 21:05:08 -07001623 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001624 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001625 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001626
Romain Guy8b55f372010-08-18 17:10:07 -07001627 const float x = texture->left - texture->offset;
1628 const float y = texture->top - texture->offset;
1629
Romain Guy01d58e42011-01-19 21:54:02 -08001630 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001631}
1632
Romain Guyada830f2011-01-13 12:13:20 -08001633void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1634 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001635 return;
1636 }
1637
1638 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001639
1640 int alpha;
1641 SkXfermode::Mode mode;
1642 getAlphaAndMode(paint, &alpha, &mode);
1643
Romain Guyada830f2011-01-13 12:13:20 -08001644 layer->alpha = alpha;
1645 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001646
Romain Guyf219da52011-01-16 12:54:25 -08001647#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001648 if (!layer->region.isEmpty()) {
1649 if (layer->region.isRect()) {
1650 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1651 composeLayerRect(layer, r);
1652 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001653 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001654 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001655
Romain Guyc88e3572011-01-22 00:32:12 -08001656 setupDraw();
1657 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001658 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001659 setupDrawColorFilter();
1660 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1661 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001662 setupDrawPureColorUniforms();
1663 setupDrawColorFilterUniforms();
1664 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001665 // TODO: The current layer, if any, will be dirtied with the bounding box
1666 // of the layer we are drawing. Since the layer we are drawing has
1667 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001668 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1669 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001670
Romain Guyc88e3572011-01-22 00:32:12 -08001671 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1672 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001673
Romain Guyc88e3572011-01-22 00:32:12 -08001674 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001675
1676#if DEBUG_LAYERS_AS_REGIONS
1677 drawRegionRects(layer->region);
1678#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001679 }
Romain Guyf219da52011-01-16 12:54:25 -08001680 }
1681#else
Romain Guyada830f2011-01-13 12:13:20 -08001682 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1683 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001684#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001685}
1686
Romain Guy6926c722010-07-12 20:20:03 -07001687///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001688// Shaders
1689///////////////////////////////////////////////////////////////////////////////
1690
1691void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001692 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001693}
1694
Romain Guy06f96e22010-07-30 19:18:16 -07001695void OpenGLRenderer::setupShader(SkiaShader* shader) {
1696 mShader = shader;
1697 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001698 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001699 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001700}
1701
Romain Guyd27977d2010-07-14 19:18:51 -07001702///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001703// Color filters
1704///////////////////////////////////////////////////////////////////////////////
1705
1706void OpenGLRenderer::resetColorFilter() {
1707 mColorFilter = NULL;
1708}
1709
1710void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1711 mColorFilter = filter;
1712}
1713
1714///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001715// Drop shadow
1716///////////////////////////////////////////////////////////////////////////////
1717
1718void OpenGLRenderer::resetShadow() {
1719 mHasShadow = false;
1720}
1721
1722void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1723 mHasShadow = true;
1724 mShadowRadius = radius;
1725 mShadowDx = dx;
1726 mShadowDy = dy;
1727 mShadowColor = color;
1728}
1729
1730///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001731// Drawing implementation
1732///////////////////////////////////////////////////////////////////////////////
1733
Romain Guy01d58e42011-01-19 21:54:02 -08001734void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1735 float x, float y, SkPaint* paint) {
1736 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1737 return;
1738 }
1739
1740 int alpha;
1741 SkXfermode::Mode mode;
1742 getAlphaAndMode(paint, &alpha, &mode);
1743
1744 setupDraw();
1745 setupDrawWithTexture(true);
1746 setupDrawAlpha8Color(paint->getColor(), alpha);
1747 setupDrawColorFilter();
1748 setupDrawShader();
1749 setupDrawBlending(true, mode);
1750 setupDrawProgram();
1751 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1752 setupDrawTexture(texture->id);
1753 setupDrawPureColorUniforms();
1754 setupDrawColorFilterUniforms();
1755 setupDrawShaderUniforms();
1756 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1757
1758 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1759
1760 finishDrawTexture();
1761}
1762
Romain Guyf607bdc2010-09-10 19:20:06 -07001763// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001764#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1765#define kStdUnderline_Offset (1.0f / 9.0f)
1766#define kStdUnderline_Thickness (1.0f / 18.0f)
1767
1768void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1769 float x, float y, SkPaint* paint) {
1770 // Handle underline and strike-through
1771 uint32_t flags = paint->getFlags();
1772 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1773 float underlineWidth = length;
1774 // If length is > 0.0f, we already measured the text for the text alignment
1775 if (length <= 0.0f) {
1776 underlineWidth = paint->measureText(text, bytesCount);
1777 }
1778
1779 float offsetX = 0;
1780 switch (paint->getTextAlign()) {
1781 case SkPaint::kCenter_Align:
1782 offsetX = underlineWidth * 0.5f;
1783 break;
1784 case SkPaint::kRight_Align:
1785 offsetX = underlineWidth;
1786 break;
1787 default:
1788 break;
1789 }
1790
1791 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001792 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001793 // TODO: Support stroke width < 1.0f when we have AA lines
1794 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001795
Romain Guye20ecbd2010-09-22 19:49:04 -07001796 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001797 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001798
Romain Guyf6834472011-01-23 13:32:12 -08001799 int linesCount = 0;
1800 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1801 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1802
1803 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001804 float points[pointsCount];
1805 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001806
1807 if (flags & SkPaint::kUnderlineText_Flag) {
1808 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001809 points[currentPoint++] = left;
1810 points[currentPoint++] = top;
1811 points[currentPoint++] = left + underlineWidth;
1812 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001813 }
1814
1815 if (flags & SkPaint::kStrikeThruText_Flag) {
1816 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001817 points[currentPoint++] = left;
1818 points[currentPoint++] = top;
1819 points[currentPoint++] = left + underlineWidth;
1820 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001821 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001822
1823 SkPaint linesPaint(*paint);
1824 linesPaint.setStrokeWidth(strokeWidth);
1825
1826 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001827 }
1828 }
1829}
1830
Romain Guy026c5e162010-06-28 17:12:22 -07001831void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001832 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001833 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001834 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001835 color |= 0x00ffffff;
1836 }
1837
Romain Guy70ca14e2010-12-13 18:24:33 -08001838 setupDraw();
1839 setupDrawColor(color);
1840 setupDrawShader();
1841 setupDrawColorFilter();
1842 setupDrawBlending(mode);
1843 setupDrawProgram();
1844 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1845 setupDrawColorUniforms();
1846 setupDrawShaderUniforms(ignoreTransform);
1847 setupDrawColorFilterUniforms();
1848 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001849
Romain Guyc95c8d62010-09-17 15:31:32 -07001850 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1851}
1852
Romain Guy82ba8142010-07-09 13:25:56 -07001853void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001854 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001855 int alpha;
1856 SkXfermode::Mode mode;
1857 getAlphaAndMode(paint, &alpha, &mode);
1858
Romain Guy8164c2d2010-10-25 18:03:28 -07001859 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1860
Romain Guy6620c6d2010-12-06 18:07:02 -08001861 if (mSnapshot->transform->isPureTranslate()) {
1862 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1863 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1864
1865 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1866 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1867 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1868 } else {
1869 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1870 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1871 GL_TRIANGLE_STRIP, gMeshCount);
1872 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001873}
1874
Romain Guybd6b79b2010-06-26 00:13:53 -07001875void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001876 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1877 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001878 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001879}
1880
1881void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001882 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001883 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001884 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001885
Romain Guy746b7402010-10-26 16:27:31 -07001886 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001887 setupDrawWithTexture();
1888 setupDrawColor(alpha, alpha, alpha, alpha);
1889 setupDrawColorFilter();
1890 setupDrawBlending(blend, mode, swapSrcDst);
1891 setupDrawProgram();
1892 if (!dirty) {
1893 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001894 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001895 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001896 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001897 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001898 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001899 }
Romain Guy86568192010-12-14 15:55:39 -08001900 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001901 setupDrawColorFilterUniforms();
1902 setupDrawTexture(texture);
1903 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001904
Romain Guy6820ac82010-09-15 18:11:50 -07001905 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001906
1907 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001908}
1909
Romain Guya5aed0d2010-09-09 14:42:43 -07001910void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001911 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001912 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1913 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001914 if (mode < SkXfermode::kPlus_Mode) {
1915 if (!mCaches.blend) {
1916 glEnable(GL_BLEND);
1917 }
Romain Guy82ba8142010-07-09 13:25:56 -07001918
Romain Guyf607bdc2010-09-10 19:20:06 -07001919 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1920 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001921
Romain Guya5aed0d2010-09-09 14:42:43 -07001922 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1923 glBlendFunc(sourceMode, destMode);
1924 mCaches.lastSrcMode = sourceMode;
1925 mCaches.lastDstMode = destMode;
1926 }
1927 } else {
1928 // These blend modes are not supported by OpenGL directly and have
1929 // to be implemented using shaders. Since the shader will perform
1930 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001931 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001932 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001933 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001934 }
1935
1936 if (mCaches.blend) {
1937 glDisable(GL_BLEND);
1938 }
1939 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001940 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001941 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001942 glDisable(GL_BLEND);
1943 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001944 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001945}
1946
Romain Guy889f8d12010-07-29 14:37:42 -07001947bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001948 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001949 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001950 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001951 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001952 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001953 }
Romain Guy6926c722010-07-12 20:20:03 -07001954 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001955}
1956
Romain Guy026c5e162010-06-28 17:12:22 -07001957void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001958 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001959 TextureVertex::setUV(v++, u1, v1);
1960 TextureVertex::setUV(v++, u2, v1);
1961 TextureVertex::setUV(v++, u1, v2);
1962 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001963}
1964
Chet Haase5c13d892010-10-08 08:37:55 -07001965void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001966 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001967 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001968 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1969 if (!isMode) {
1970 // Assume SRC_OVER
1971 *mode = SkXfermode::kSrcOver_Mode;
1972 }
1973 } else {
1974 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001975 }
1976
1977 // Skia draws using the color's alpha channel if < 255
1978 // Otherwise, it uses the paint's alpha
1979 int color = paint->getColor();
1980 *alpha = (color >> 24) & 0xFF;
1981 if (*alpha == 255) {
1982 *alpha = paint->getAlpha();
1983 }
1984 } else {
1985 *mode = SkXfermode::kSrcOver_Mode;
1986 *alpha = 255;
1987 }
Romain Guy026c5e162010-06-28 17:12:22 -07001988}
1989
Romain Guya5aed0d2010-09-09 14:42:43 -07001990SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08001991 // In the future we should look at unifying the Porter-Duff modes and
1992 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07001993 if (mode == NULL) {
1994 return SkXfermode::kSrcOver_Mode;
1995 }
1996 return mode->fMode;
1997}
1998
Romain Guy746b7402010-10-26 16:27:31 -07001999void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002000 bool bound = false;
2001 if (wrapS != texture->wrapS) {
2002 glBindTexture(GL_TEXTURE_2D, texture->id);
2003 bound = true;
2004 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2005 texture->wrapS = wrapS;
2006 }
2007 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002008 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002009 glBindTexture(GL_TEXTURE_2D, texture->id);
2010 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002011 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2012 texture->wrapT = wrapT;
2013 }
Romain Guya1db5742010-07-20 13:09:13 -07002014}
2015
Romain Guy9d5316e2010-06-24 19:30:36 -07002016}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002017}; // namespace android