blob: e01e072ee5e32e3686782fb2e6c065afd21d390a [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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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 Guydda57022010-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
Romain Guy6217a712011-03-15 16:32:28 -0700639#if RENDER_LAYERS_RECT_AS_RECT
Romain Guy5b3b3522010-10-27 18:57:51 -0700640 if (layer->region.isRect()) {
641 composeLayerRect(layer, rect);
642 layer->region.clear();
643 return;
644 }
Romain Guy6217a712011-03-15 16:32:28 -0700645#endif
Romain Guy5b3b3522010-10-27 18:57:51 -0700646
647 if (!layer->region.isEmpty()) {
648 size_t count;
649 const android::Rect* rects = layer->region.getArray(&count);
650
Romain Guy5b3b3522010-10-27 18:57:51 -0700651 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700652 const float texX = 1.0f / float(layer->width);
653 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800654 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700655
656 TextureVertex* mesh = mCaches.getRegionMesh();
657 GLsizei numQuads = 0;
658
Romain Guy7230a742011-01-10 22:26:16 -0800659 setupDraw();
660 setupDrawWithTexture();
661 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800662 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800663 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
664 setupDrawProgram();
665 setupDrawDirtyRegionsDisabled();
666 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800667 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800668 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800669 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800670 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700671
672 for (size_t i = 0; i < count; i++) {
673 const android::Rect* r = &rects[i];
674
675 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800676 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700677 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800678 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700679
680 // TODO: Reject quads outside of the clip
681 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
682 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
683 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
684 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
685
686 numQuads++;
687
688 if (numQuads >= REGION_MESH_QUAD_COUNT) {
689 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
690 numQuads = 0;
691 mesh = mCaches.getRegionMesh();
692 }
693 }
694
695 if (numQuads > 0) {
696 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
697 }
698
699 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800700 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700701
702#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800703 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700704#endif
705
706 layer->region.clear();
707 }
708#else
709 composeLayerRect(layer, rect);
710#endif
711}
712
Romain Guy3a3133d2011-02-01 22:59:58 -0800713void OpenGLRenderer::drawRegionRects(const Region& region) {
714#if DEBUG_LAYERS_AS_REGIONS
715 size_t count;
716 const android::Rect* rects = region.getArray(&count);
717
718 uint32_t colors[] = {
719 0x7fff0000, 0x7f00ff00,
720 0x7f0000ff, 0x7fff00ff,
721 };
722
723 int offset = 0;
724 int32_t top = rects[0].top;
725
726 for (size_t i = 0; i < count; i++) {
727 if (top != rects[i].top) {
728 offset ^= 0x2;
729 top = rects[i].top;
730 }
731
732 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
733 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
734 SkXfermode::kSrcOver_Mode);
735 }
736#endif
737}
738
Romain Guy5b3b3522010-10-27 18:57:51 -0700739void OpenGLRenderer::dirtyLayer(const float left, const float top,
740 const float right, const float bottom, const mat4 transform) {
741#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800742 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700743 Rect bounds(left, top, right, bottom);
744 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800745 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700746 }
747#endif
748}
749
750void OpenGLRenderer::dirtyLayer(const float left, const float top,
751 const float right, const float bottom) {
752#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800753 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700754 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800755 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800756 }
757#endif
758}
759
760void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
761#if RENDER_LAYERS_AS_REGIONS
762 if (bounds.intersect(*mSnapshot->clipRect)) {
763 bounds.snapToPixelBoundaries();
764 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
765 if (!dirty.isEmpty()) {
766 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700767 }
768 }
769#endif
770}
771
Romain Guybd6b79b2010-06-26 00:13:53 -0700772///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700773// Transforms
774///////////////////////////////////////////////////////////////////////////////
775
776void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700777 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700778}
779
780void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700781 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700782}
783
784void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700785 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700786}
787
Romain Guy807daf72011-01-18 11:19:19 -0800788void OpenGLRenderer::skew(float sx, float sy) {
789 mSnapshot->transform->skew(sx, sy);
790}
791
Romain Guyf6a11b82010-06-23 17:47:49 -0700792void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700793 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700794}
795
Romain Guy41030da2010-10-13 13:40:37 -0700796const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700797 if (mSnapshot->fbo != 0) {
798 return &mSnapshot->transform->data[0];
799 }
800 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700801}
802
Romain Guyf6a11b82010-06-23 17:47:49 -0700803void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700804 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700805}
806
807void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700808 SkMatrix transform;
809 mSnapshot->transform->copyTo(transform);
810 transform.preConcat(*matrix);
811 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700812}
813
814///////////////////////////////////////////////////////////////////////////////
815// Clipping
816///////////////////////////////////////////////////////////////////////////////
817
Romain Guybb9524b2010-06-22 18:56:38 -0700818void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700819 Rect clip(*mSnapshot->clipRect);
820 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700821 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700822 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700823}
824
825const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700826 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700827}
828
Romain Guyc7d53492010-06-25 13:41:57 -0700829bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800830 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700831 return true;
832 }
833
Romain Guy1d83e192010-08-17 11:37:00 -0700834 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700835 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700836 r.snapToPixelBoundaries();
837
838 Rect clipRect(*mSnapshot->clipRect);
839 clipRect.snapToPixelBoundaries();
840
841 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700842}
843
Romain Guy079ba2c2010-07-16 14:12:24 -0700844bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
845 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700846 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700847 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700848 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700849 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700850}
851
Romain Guyf6a11b82010-06-23 17:47:49 -0700852///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800853// Drawing commands
854///////////////////////////////////////////////////////////////////////////////
855
856void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800857 if (mDirtyClip) {
858 setScissorFromClip();
859 }
860 mDescription.reset();
861 mSetShaderColor = false;
862 mColorSet = false;
863 mColorA = mColorR = mColorG = mColorB = 0.0f;
864 mTextureUnit = 0;
865 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800866 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800867}
868
869void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
870 mDescription.hasTexture = true;
871 mDescription.hasAlpha8Texture = isAlpha8;
872}
873
874void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800875 setupDrawColor(color, (color >> 24) & 0xFF);
876}
877
878void OpenGLRenderer::setupDrawColor(int color, int alpha) {
879 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800880 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800881 mColorR = a * ((color >> 16) & 0xFF);
882 mColorG = a * ((color >> 8) & 0xFF);
883 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800884 mColorSet = true;
885 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
886}
887
Romain Guy86568192010-12-14 15:55:39 -0800888void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
889 mColorA = alpha / 255.0f;
890 const float a = mColorA / 255.0f;
891 mColorR = a * ((color >> 16) & 0xFF);
892 mColorG = a * ((color >> 8) & 0xFF);
893 mColorB = a * ((color ) & 0xFF);
894 mColorSet = true;
895 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
896}
897
Romain Guy70ca14e2010-12-13 18:24:33 -0800898void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
899 mColorA = a;
900 mColorR = r;
901 mColorG = g;
902 mColorB = b;
903 mColorSet = true;
904 mSetShaderColor = mDescription.setColor(r, g, b, a);
905}
906
Romain Guy86568192010-12-14 15:55:39 -0800907void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
908 mColorA = a;
909 mColorR = r;
910 mColorG = g;
911 mColorB = b;
912 mColorSet = true;
913 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
914}
915
Romain Guy70ca14e2010-12-13 18:24:33 -0800916void OpenGLRenderer::setupDrawShader() {
917 if (mShader) {
918 mShader->describe(mDescription, mCaches.extensions);
919 }
920}
921
922void OpenGLRenderer::setupDrawColorFilter() {
923 if (mColorFilter) {
924 mColorFilter->describe(mDescription, mCaches.extensions);
925 }
926}
927
928void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
929 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
930 mDescription, swapSrcDst);
931}
932
933void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
934 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
935 mDescription, swapSrcDst);
936}
937
938void OpenGLRenderer::setupDrawProgram() {
939 useProgram(mCaches.programCache.get(mDescription));
940}
941
942void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
943 mTrackDirtyRegions = false;
944}
945
946void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
947 bool ignoreTransform) {
948 mModelView.loadTranslate(left, top, 0.0f);
949 if (!ignoreTransform) {
950 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
951 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
952 } else {
953 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
954 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
955 }
956}
957
Romain Guy8d0d4782010-12-14 20:13:35 -0800958void OpenGLRenderer::setupDrawModelViewIdentity() {
959 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
960}
961
Romain Guy70ca14e2010-12-13 18:24:33 -0800962void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
963 bool ignoreTransform, bool ignoreModelView) {
964 if (!ignoreModelView) {
965 mModelView.loadTranslate(left, top, 0.0f);
966 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800967 } else {
968 mModelView.loadIdentity();
969 }
Romain Guy86568192010-12-14 15:55:39 -0800970 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
971 if (!ignoreTransform) {
972 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
973 if (mTrackDirtyRegions && dirty) {
974 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
975 }
976 } else {
977 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
978 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
979 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800980}
981
982void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800983 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800984 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
985 }
986}
987
Romain Guy86568192010-12-14 15:55:39 -0800988void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800989 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800990 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800991 }
992}
993
Romain Guy70ca14e2010-12-13 18:24:33 -0800994void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
995 if (mShader) {
996 if (ignoreTransform) {
997 mModelView.loadInverse(*mSnapshot->transform);
998 }
999 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1000 }
1001}
1002
Romain Guy8d0d4782010-12-14 20:13:35 -08001003void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1004 if (mShader) {
1005 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1006 }
1007}
1008
Romain Guy70ca14e2010-12-13 18:24:33 -08001009void OpenGLRenderer::setupDrawColorFilterUniforms() {
1010 if (mColorFilter) {
1011 mColorFilter->setupProgram(mCaches.currentProgram);
1012 }
1013}
1014
1015void OpenGLRenderer::setupDrawSimpleMesh() {
1016 mCaches.bindMeshBuffer();
1017 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1018 gMeshStride, 0);
1019}
1020
1021void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1022 bindTexture(texture);
1023 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1024
1025 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1026 glEnableVertexAttribArray(mTexCoordsSlot);
1027}
1028
1029void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1030 if (!vertices) {
1031 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1032 } else {
1033 mCaches.unbindMeshBuffer();
1034 }
1035 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1036 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001037 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001038 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1039 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001040}
1041
1042void OpenGLRenderer::finishDrawTexture() {
1043 glDisableVertexAttribArray(mTexCoordsSlot);
1044}
1045
1046///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001047// Drawing
1048///////////////////////////////////////////////////////////////////////////////
1049
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001050bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1051 Rect& dirty, uint32_t level) {
1052 if (quickReject(0.0f, 0.0f, width, height)) {
1053 return false;
1054 }
1055
Romain Guy0fe478e2010-11-08 12:08:41 -08001056 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1057 // will be performed by the display list itself
1058 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001059 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001060 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001061
Chet Haasedaf98e92011-01-10 14:10:36 -08001062 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001063}
1064
Chet Haase5c13d892010-10-08 08:37:55 -07001065void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001066 const float right = left + bitmap->width();
1067 const float bottom = top + bitmap->height();
1068
1069 if (quickReject(left, top, right, bottom)) {
1070 return;
1071 }
1072
Romain Guy86568192010-12-14 15:55:39 -08001073 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001074 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001075 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001076 const AutoTexture autoCleanup(texture);
1077
Romain Guy6926c722010-07-12 20:20:03 -07001078 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001079}
1080
Chet Haase5c13d892010-10-08 08:37:55 -07001081void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001082 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1083 const mat4 transform(*matrix);
1084 transform.mapRect(r);
1085
Romain Guy6926c722010-07-12 20:20:03 -07001086 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1087 return;
1088 }
1089
Romain Guy86568192010-12-14 15:55:39 -08001090 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001091 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001092 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001093 const AutoTexture autoCleanup(texture);
1094
Romain Guy5b3b3522010-10-27 18:57:51 -07001095 // This could be done in a cheaper way, all we need is pass the matrix
1096 // to the vertex shader. The save/restore is a bit overkill.
1097 save(SkCanvas::kMatrix_SaveFlag);
1098 concatMatrix(matrix);
1099 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1100 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001101}
1102
Romain Guy5a7b4662011-01-20 19:09:30 -08001103void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1104 float* vertices, int* colors, SkPaint* paint) {
1105 // TODO: Do a quickReject
1106 if (!vertices || mSnapshot->isIgnored()) {
1107 return;
1108 }
1109
1110 glActiveTexture(gTextureUnits[0]);
1111 Texture* texture = mCaches.textureCache.get(bitmap);
1112 if (!texture) return;
1113 const AutoTexture autoCleanup(texture);
1114 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1115
1116 int alpha;
1117 SkXfermode::Mode mode;
1118 getAlphaAndMode(paint, &alpha, &mode);
1119
Romain Guy5a7b4662011-01-20 19:09:30 -08001120 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001121
Romain Guyb18d2d02011-02-10 15:52:54 -08001122 float left = FLT_MAX;
1123 float top = FLT_MAX;
1124 float right = FLT_MIN;
1125 float bottom = FLT_MIN;
1126
1127#if RENDER_LAYERS_AS_REGIONS
1128 bool hasActiveLayer = hasLayer();
1129#else
1130 bool hasActiveLayer = false;
1131#endif
1132
Romain Guya566b7c2011-01-23 16:36:11 -08001133 // TODO: Support the colors array
1134 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001135 TextureVertex* vertex = mesh;
1136 for (int32_t y = 0; y < meshHeight; y++) {
1137 for (int32_t x = 0; x < meshWidth; x++) {
1138 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1139
1140 float u1 = float(x) / meshWidth;
1141 float u2 = float(x + 1) / meshWidth;
1142 float v1 = float(y) / meshHeight;
1143 float v2 = float(y + 1) / meshHeight;
1144
1145 int ax = i + (meshWidth + 1) * 2;
1146 int ay = ax + 1;
1147 int bx = i;
1148 int by = bx + 1;
1149 int cx = i + 2;
1150 int cy = cx + 1;
1151 int dx = i + (meshWidth + 1) * 2 + 2;
1152 int dy = dx + 1;
1153
1154 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1155 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1156 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1157
1158 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1159 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1160 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001161
1162#if RENDER_LAYERS_AS_REGIONS
1163 if (hasActiveLayer) {
1164 // TODO: This could be optimized to avoid unnecessary ops
1165 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1166 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1167 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1168 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1169 }
1170#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001171 }
1172 }
1173
Romain Guyb18d2d02011-02-10 15:52:54 -08001174#if RENDER_LAYERS_AS_REGIONS
1175 if (hasActiveLayer) {
1176 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1177 }
1178#endif
1179
Romain Guy5a7b4662011-01-20 19:09:30 -08001180 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1181 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001182 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001183}
1184
Romain Guy8ba548f2010-06-30 19:21:21 -07001185void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1186 float srcLeft, float srcTop, float srcRight, float srcBottom,
1187 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001188 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001189 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1190 return;
1191 }
1192
Romain Guy746b7402010-10-26 16:27:31 -07001193 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001194 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001195 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001196 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001197 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001198
Romain Guy8ba548f2010-06-30 19:21:21 -07001199 const float width = texture->width;
1200 const float height = texture->height;
1201
1202 const float u1 = srcLeft / width;
1203 const float v1 = srcTop / height;
1204 const float u2 = srcRight / width;
1205 const float v2 = srcBottom / height;
1206
Romain Guy03750a02010-10-18 14:06:08 -07001207 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001208 resetDrawTextureTexCoords(u1, v1, u2, v2);
1209
Romain Guy03750a02010-10-18 14:06:08 -07001210 int alpha;
1211 SkXfermode::Mode mode;
1212 getAlphaAndMode(paint, &alpha, &mode);
1213
Romain Guy6620c6d2010-12-06 18:07:02 -08001214 if (mSnapshot->transform->isPureTranslate()) {
1215 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1216 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1217
1218 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1219 texture->id, alpha / 255.0f, mode, texture->blend,
1220 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1221 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1222 } else {
1223 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1224 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1225 GL_TRIANGLE_STRIP, gMeshCount);
1226 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001227
1228 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1229}
1230
Romain Guy4aa90572010-09-26 18:40:37 -07001231void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001232 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001233 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001234 if (quickReject(left, top, right, bottom)) {
1235 return;
1236 }
1237
Romain Guy746b7402010-10-26 16:27:31 -07001238 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001239 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001240 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001241 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001242 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001243
1244 int alpha;
1245 SkXfermode::Mode mode;
1246 getAlphaAndMode(paint, &alpha, &mode);
1247
Romain Guy2728f962010-10-08 18:36:15 -07001248 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001249 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001250
Romain Guya5ef39a2010-12-03 16:48:20 -08001251 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001252 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001253#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001254 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001255 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001256 const float offsetX = left + mSnapshot->transform->getTranslateX();
1257 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001258 const size_t count = mesh->quads.size();
1259 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001260 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001261 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001262 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1263 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1264 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001265 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001266 dirtyLayer(left + bounds.left, top + bounds.top,
1267 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001268 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001269 }
1270 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001271#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001272
Romain Guy6620c6d2010-12-06 18:07:02 -08001273 if (pureTranslate) {
1274 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1275 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1276
1277 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1278 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1279 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1280 true, !mesh->hasEmptyQuads);
1281 } else {
1282 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1283 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1284 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1285 true, !mesh->hasEmptyQuads);
1286 }
Romain Guy054dc182010-10-15 17:55:25 -07001287 }
Romain Guyf7f93552010-07-08 19:17:03 -07001288}
1289
Chet Haase5c13d892010-10-08 08:37:55 -07001290void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001291 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001292
Romain Guya957eea2010-12-08 18:34:42 -08001293 const bool isAA = paint->isAntiAlias();
1294 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1295 // A stroke width of 0 has a special meaningin Skia:
1296 // it draws an unscaled 1px wide line
1297 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1298
Romain Guy759ea802010-09-16 20:49:46 -07001299 int alpha;
1300 SkXfermode::Mode mode;
1301 getAlphaAndMode(paint, &alpha, &mode);
1302
Romain Guya957eea2010-12-08 18:34:42 -08001303 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001304 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001305 if (!isHairLine) {
1306 // TODO: AA needs more vertices
1307 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001308 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001309 // TODO: AA will be different
1310 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001311 }
1312
Romain Guya957eea2010-12-08 18:34:42 -08001313 TextureVertex lines[verticesCount];
1314 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001315
Romain Guy8d0d4782010-12-14 20:13:35 -08001316 setupDraw();
1317 setupDrawColor(paint->getColor(), alpha);
1318 setupDrawColorFilter();
1319 setupDrawShader();
1320 setupDrawBlending(mode);
1321 setupDrawProgram();
1322 setupDrawModelViewIdentity();
1323 setupDrawColorUniforms();
1324 setupDrawColorFilterUniforms();
1325 setupDrawShaderIdentityUniforms();
1326 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001327
1328 if (!isHairLine) {
1329 // TODO: Handle the AA case
1330 for (int i = 0; i < count; i += 4) {
1331 // a = start point, b = end point
1332 vec2 a(points[i], points[i + 1]);
1333 vec2 b(points[i + 2], points[i + 3]);
1334
1335 // Bias to snap to the same pixels as Skia
1336 a += 0.375;
1337 b += 0.375;
1338
1339 // Find the normal to the line
1340 vec2 n = (b - a).copyNormalized() * strokeWidth;
1341 float x = n.x;
1342 n.x = -n.y;
1343 n.y = x;
1344
1345 // Four corners of the rectangle defining a thick line
1346 vec2 p1 = a - n;
1347 vec2 p2 = a + n;
1348 vec2 p3 = b + n;
1349 vec2 p4 = b - n;
1350
Romain Guy7230a742011-01-10 22:26:16 -08001351 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1352 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1353 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1354 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001355
Romain Guy7230a742011-01-10 22:26:16 -08001356 if (!quickReject(left, top, right, bottom)) {
1357 // Draw the line as 2 triangles, could be optimized
1358 // by using only 4 vertices and the correct indices
1359 // Also we should probably used non textured vertices
1360 // when line AA is disabled to save on bandwidth
1361 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1362 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1363 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1364 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1365 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1366 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1367
1368 generatedVerticesCount += 6;
1369
1370 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1371 }
Romain Guya957eea2010-12-08 18:34:42 -08001372 }
1373
Romain Guy7230a742011-01-10 22:26:16 -08001374 if (generatedVerticesCount > 0) {
1375 // GL_LINE does not give the result we want to match Skia
1376 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1377 }
Romain Guya957eea2010-12-08 18:34:42 -08001378 } else {
1379 // TODO: Handle the AA case
1380 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001381 const float left = fmin(points[i], points[i + 1]);
1382 const float right = fmax(points[i], points[i + 1]);
1383 const float top = fmin(points[i + 2], points[i + 3]);
1384 const float bottom = fmax(points[i + 2], points[i + 3]);
1385
1386 if (!quickReject(left, top, right, bottom)) {
1387 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1388 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1389
1390 generatedVerticesCount += 2;
1391
1392 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1393 }
Romain Guya957eea2010-12-08 18:34:42 -08001394 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001395
Romain Guy7230a742011-01-10 22:26:16 -08001396 if (generatedVerticesCount > 0) {
1397 glLineWidth(1.0f);
1398 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1399 }
Romain Guy29d89972010-09-22 16:10:57 -07001400 }
Romain Guy759ea802010-09-16 20:49:46 -07001401}
1402
Romain Guy85bf02f2010-06-22 13:11:24 -07001403void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001404 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001405 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001406
Romain Guyae88e5e2010-10-22 17:49:18 -07001407 Rect& clip(*mSnapshot->clipRect);
1408 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001409
Romain Guy3d58c032010-07-14 16:34:53 -07001410 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001411}
Romain Guy9d5316e2010-06-24 19:30:36 -07001412
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001413void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001414 if (!texture) return;
1415 const AutoTexture autoCleanup(texture);
1416
1417 const float x = left + texture->left - texture->offset;
1418 const float y = top + texture->top - texture->offset;
1419
1420 drawPathTexture(texture, x, y, paint);
1421}
1422
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001423void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1424 float rx, float ry, SkPaint* paint) {
1425 if (mSnapshot->isIgnored()) return;
1426
1427 glActiveTexture(gTextureUnits[0]);
1428 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1429 right - left, bottom - top, rx, ry, paint);
1430 drawShape(left, top, texture, paint);
1431}
1432
Romain Guy01d58e42011-01-19 21:54:02 -08001433void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1434 if (mSnapshot->isIgnored()) return;
1435
1436 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001437 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001438 drawShape(x - radius, y - radius, texture, paint);
1439}
Romain Guy01d58e42011-01-19 21:54:02 -08001440
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001441void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1442 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001443
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001444 glActiveTexture(gTextureUnits[0]);
1445 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1446 drawShape(left, top, texture, paint);
1447}
1448
Romain Guy8b2f5262011-01-23 16:15:02 -08001449void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1450 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1451 if (mSnapshot->isIgnored()) return;
1452
1453 if (fabs(sweepAngle) >= 360.0f) {
1454 drawOval(left, top, right, bottom, paint);
1455 return;
1456 }
1457
1458 glActiveTexture(gTextureUnits[0]);
1459 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1460 startAngle, sweepAngle, useCenter, paint);
1461 drawShape(left, top, texture, paint);
1462}
1463
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001464void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1465 SkPaint* paint) {
1466 if (mSnapshot->isIgnored()) return;
1467
1468 glActiveTexture(gTextureUnits[0]);
1469 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1470 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001471}
1472
Chet Haase5c13d892010-10-08 08:37:55 -07001473void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001474 if (p->getStyle() != SkPaint::kFill_Style) {
1475 drawRectAsShape(left, top, right, bottom, p);
1476 return;
1477 }
1478
Romain Guy6926c722010-07-12 20:20:03 -07001479 if (quickReject(left, top, right, bottom)) {
1480 return;
1481 }
1482
Romain Guy026c5e162010-06-28 17:12:22 -07001483 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001484 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001485 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1486 if (!isMode) {
1487 // Assume SRC_OVER
1488 mode = SkXfermode::kSrcOver_Mode;
1489 }
1490 } else {
1491 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001492 }
1493
Romain Guy026c5e162010-06-28 17:12:22 -07001494 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001495 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001496}
Romain Guy9d5316e2010-06-24 19:30:36 -07001497
Romain Guye8e62a42010-07-23 18:55:21 -07001498void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1499 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001500 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001501 return;
1502 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001503 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001504
Romain Guyf607bdc2010-09-10 19:20:06 -07001505 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001506
Romain Guya674ab72010-08-10 17:26:42 -07001507 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001508 switch (paint->getTextAlign()) {
1509 case SkPaint::kCenter_Align:
1510 length = paint->measureText(text, bytesCount);
1511 x -= length / 2.0f;
1512 break;
1513 case SkPaint::kRight_Align:
1514 length = paint->measureText(text, bytesCount);
1515 x -= length;
1516 break;
1517 default:
1518 break;
1519 }
1520
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001521 const float oldX = x;
1522 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001523 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1524 if (pureTranslate) {
1525 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1526 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1527 }
1528
Romain Guyb45c0c92010-08-26 20:35:23 -07001529 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1530 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001531 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001532
Romain Guy86568192010-12-14 15:55:39 -08001533 int alpha;
1534 SkXfermode::Mode mode;
1535 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001536
Romain Guy1e45aae2010-08-13 19:39:53 -07001537 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001538 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001539 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001540 count, mShadowRadius);
1541 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001542
Romain Guy86568192010-12-14 15:55:39 -08001543 const float sx = x - shadow->left + mShadowDx;
1544 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001545
Romain Guy86568192010-12-14 15:55:39 -08001546 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1547
1548 glActiveTexture(gTextureUnits[0]);
1549 setupDraw();
1550 setupDrawWithTexture(true);
1551 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1552 setupDrawBlending(true, mode);
1553 setupDrawProgram();
1554 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1555 setupDrawTexture(shadow->id);
1556 setupDrawPureColorUniforms();
1557 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1558
Romain Guy0a417492010-08-16 20:26:20 -07001559 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001560 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001561 }
1562
Romain Guyb146b122010-12-15 17:06:45 -08001563 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1564 return;
1565 }
1566
Romain Guy6620c6d2010-12-06 18:07:02 -08001567 // Pick the appropriate texture filtering
1568 bool linearFilter = mSnapshot->transform->changesBounds();
1569 if (pureTranslate && !linearFilter) {
1570 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1571 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001572
Romain Guy86568192010-12-14 15:55:39 -08001573 glActiveTexture(gTextureUnits[0]);
1574 setupDraw();
1575 setupDrawDirtyRegionsDisabled();
1576 setupDrawWithTexture(true);
1577 setupDrawAlpha8Color(paint->getColor(), alpha);
1578 setupDrawColorFilter();
1579 setupDrawShader();
1580 setupDrawBlending(true, mode);
1581 setupDrawProgram();
1582 setupDrawModelView(x, y, x, y, pureTranslate, true);
1583 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1584 setupDrawPureColorUniforms();
1585 setupDrawColorFilterUniforms();
1586 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001587
Romain Guy6620c6d2010-12-06 18:07:02 -08001588 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001589 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1590
1591#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001592 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001593#else
Romain Guyf219da52011-01-16 12:54:25 -08001594 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001595#endif
Romain Guy03750a02010-10-18 14:06:08 -07001596 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001597
1598 // Tell font renderer the locations of position and texture coord
1599 // attributes so it can bind its data properly
1600 int positionSlot = mCaches.currentProgram->position;
1601 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001602 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001603 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001604#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001605 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001606 if (!pureTranslate) {
1607 mSnapshot->transform->mapRect(bounds);
1608 }
Romain Guyf219da52011-01-16 12:54:25 -08001609 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001610 }
1611#endif
1612 }
Romain Guy694b5192010-07-21 21:33:20 -07001613
1614 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001615 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001616
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001617 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001618}
1619
Romain Guy7fbcc042010-08-04 15:40:07 -07001620void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001621 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001622
Romain Guy01d58e42011-01-19 21:54:02 -08001623 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001624
Romain Guyfb8b7632010-08-23 21:05:08 -07001625 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001626 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001627 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001628
Romain Guy8b55f372010-08-18 17:10:07 -07001629 const float x = texture->left - texture->offset;
1630 const float y = texture->top - texture->offset;
1631
Romain Guy01d58e42011-01-19 21:54:02 -08001632 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001633}
1634
Romain Guyada830f2011-01-13 12:13:20 -08001635void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1636 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001637 return;
1638 }
1639
1640 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001641
1642 int alpha;
1643 SkXfermode::Mode mode;
1644 getAlphaAndMode(paint, &alpha, &mode);
1645
Romain Guyada830f2011-01-13 12:13:20 -08001646 layer->alpha = alpha;
1647 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001648
Romain Guyf219da52011-01-16 12:54:25 -08001649#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001650 if (!layer->region.isEmpty()) {
Romain Guy6217a712011-03-15 16:32:28 -07001651#if RENDER_LAYERS_RECT_AS_RECT
Romain Guyc88e3572011-01-22 00:32:12 -08001652 if (layer->region.isRect()) {
1653 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1654 composeLayerRect(layer, r);
1655 } else if (layer->mesh) {
Romain Guy6217a712011-03-15 16:32:28 -07001656#else
1657 if (layer->mesh) {
1658#endif
Romain Guy81683962011-01-24 20:40:18 -08001659 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001660 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001661
Romain Guyc88e3572011-01-22 00:32:12 -08001662 setupDraw();
1663 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001664 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001665 setupDrawColorFilter();
1666 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1667 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001668 setupDrawPureColorUniforms();
1669 setupDrawColorFilterUniforms();
1670 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001671 // TODO: The current layer, if any, will be dirtied with the bounding box
1672 // of the layer we are drawing. Since the layer we are drawing has
1673 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001674 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1675 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001676
Romain Guyc88e3572011-01-22 00:32:12 -08001677 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1678 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001679
Romain Guyc88e3572011-01-22 00:32:12 -08001680 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001681
1682#if DEBUG_LAYERS_AS_REGIONS
1683 drawRegionRects(layer->region);
1684#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001685 }
Romain Guyf219da52011-01-16 12:54:25 -08001686 }
1687#else
Romain Guyada830f2011-01-13 12:13:20 -08001688 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1689 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001690#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001691}
1692
Romain Guy6926c722010-07-12 20:20:03 -07001693///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001694// Shaders
1695///////////////////////////////////////////////////////////////////////////////
1696
1697void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001698 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001699}
1700
Romain Guy06f96e22010-07-30 19:18:16 -07001701void OpenGLRenderer::setupShader(SkiaShader* shader) {
1702 mShader = shader;
1703 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001704 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001705 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001706}
1707
Romain Guyd27977d2010-07-14 19:18:51 -07001708///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001709// Color filters
1710///////////////////////////////////////////////////////////////////////////////
1711
1712void OpenGLRenderer::resetColorFilter() {
1713 mColorFilter = NULL;
1714}
1715
1716void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1717 mColorFilter = filter;
1718}
1719
1720///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001721// Drop shadow
1722///////////////////////////////////////////////////////////////////////////////
1723
1724void OpenGLRenderer::resetShadow() {
1725 mHasShadow = false;
1726}
1727
1728void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1729 mHasShadow = true;
1730 mShadowRadius = radius;
1731 mShadowDx = dx;
1732 mShadowDy = dy;
1733 mShadowColor = color;
1734}
1735
1736///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001737// Drawing implementation
1738///////////////////////////////////////////////////////////////////////////////
1739
Romain Guy01d58e42011-01-19 21:54:02 -08001740void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1741 float x, float y, SkPaint* paint) {
1742 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1743 return;
1744 }
1745
1746 int alpha;
1747 SkXfermode::Mode mode;
1748 getAlphaAndMode(paint, &alpha, &mode);
1749
1750 setupDraw();
1751 setupDrawWithTexture(true);
1752 setupDrawAlpha8Color(paint->getColor(), alpha);
1753 setupDrawColorFilter();
1754 setupDrawShader();
1755 setupDrawBlending(true, mode);
1756 setupDrawProgram();
1757 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1758 setupDrawTexture(texture->id);
1759 setupDrawPureColorUniforms();
1760 setupDrawColorFilterUniforms();
1761 setupDrawShaderUniforms();
1762 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1763
1764 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1765
1766 finishDrawTexture();
1767}
1768
Romain Guyf607bdc2010-09-10 19:20:06 -07001769// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001770#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1771#define kStdUnderline_Offset (1.0f / 9.0f)
1772#define kStdUnderline_Thickness (1.0f / 18.0f)
1773
1774void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1775 float x, float y, SkPaint* paint) {
1776 // Handle underline and strike-through
1777 uint32_t flags = paint->getFlags();
1778 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1779 float underlineWidth = length;
1780 // If length is > 0.0f, we already measured the text for the text alignment
1781 if (length <= 0.0f) {
1782 underlineWidth = paint->measureText(text, bytesCount);
1783 }
1784
1785 float offsetX = 0;
1786 switch (paint->getTextAlign()) {
1787 case SkPaint::kCenter_Align:
1788 offsetX = underlineWidth * 0.5f;
1789 break;
1790 case SkPaint::kRight_Align:
1791 offsetX = underlineWidth;
1792 break;
1793 default:
1794 break;
1795 }
1796
1797 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001798 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001799 // TODO: Support stroke width < 1.0f when we have AA lines
1800 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001801
Romain Guye20ecbd2010-09-22 19:49:04 -07001802 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001803 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001804
Romain Guyf6834472011-01-23 13:32:12 -08001805 int linesCount = 0;
1806 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1807 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1808
1809 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001810 float points[pointsCount];
1811 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001812
1813 if (flags & SkPaint::kUnderlineText_Flag) {
1814 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001815 points[currentPoint++] = left;
1816 points[currentPoint++] = top;
1817 points[currentPoint++] = left + underlineWidth;
1818 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001819 }
1820
1821 if (flags & SkPaint::kStrikeThruText_Flag) {
1822 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001823 points[currentPoint++] = left;
1824 points[currentPoint++] = top;
1825 points[currentPoint++] = left + underlineWidth;
1826 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001827 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001828
1829 SkPaint linesPaint(*paint);
1830 linesPaint.setStrokeWidth(strokeWidth);
1831
1832 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001833 }
1834 }
1835}
1836
Romain Guy026c5e162010-06-28 17:12:22 -07001837void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001838 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001839 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001840 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001841 color |= 0x00ffffff;
1842 }
1843
Romain Guy70ca14e2010-12-13 18:24:33 -08001844 setupDraw();
1845 setupDrawColor(color);
1846 setupDrawShader();
1847 setupDrawColorFilter();
1848 setupDrawBlending(mode);
1849 setupDrawProgram();
1850 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1851 setupDrawColorUniforms();
1852 setupDrawShaderUniforms(ignoreTransform);
1853 setupDrawColorFilterUniforms();
1854 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001855
Romain Guyc95c8d62010-09-17 15:31:32 -07001856 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1857}
1858
Romain Guy82ba8142010-07-09 13:25:56 -07001859void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001860 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001861 int alpha;
1862 SkXfermode::Mode mode;
1863 getAlphaAndMode(paint, &alpha, &mode);
1864
Romain Guy8164c2d2010-10-25 18:03:28 -07001865 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1866
Romain Guy6620c6d2010-12-06 18:07:02 -08001867 if (mSnapshot->transform->isPureTranslate()) {
1868 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1869 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1870
1871 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1872 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1873 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1874 } else {
1875 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1876 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1877 GL_TRIANGLE_STRIP, gMeshCount);
1878 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001879}
1880
Romain Guybd6b79b2010-06-26 00:13:53 -07001881void OpenGLRenderer::drawTextureRect(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) {
1883 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001884 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001885}
1886
1887void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001888 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001889 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001890 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001891
Romain Guy746b7402010-10-26 16:27:31 -07001892 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001893 setupDrawWithTexture();
1894 setupDrawColor(alpha, alpha, alpha, alpha);
1895 setupDrawColorFilter();
1896 setupDrawBlending(blend, mode, swapSrcDst);
1897 setupDrawProgram();
1898 if (!dirty) {
1899 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001900 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001901 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001902 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001903 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001904 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001905 }
Romain Guy86568192010-12-14 15:55:39 -08001906 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001907 setupDrawColorFilterUniforms();
1908 setupDrawTexture(texture);
1909 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001910
Romain Guy6820ac82010-09-15 18:11:50 -07001911 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001912
1913 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001914}
1915
Romain Guya5aed0d2010-09-09 14:42:43 -07001916void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001917 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001918 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1919 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001920 if (mode < SkXfermode::kPlus_Mode) {
1921 if (!mCaches.blend) {
1922 glEnable(GL_BLEND);
1923 }
Romain Guy82ba8142010-07-09 13:25:56 -07001924
Romain Guyf607bdc2010-09-10 19:20:06 -07001925 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1926 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001927
Romain Guya5aed0d2010-09-09 14:42:43 -07001928 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1929 glBlendFunc(sourceMode, destMode);
1930 mCaches.lastSrcMode = sourceMode;
1931 mCaches.lastDstMode = destMode;
1932 }
1933 } else {
1934 // These blend modes are not supported by OpenGL directly and have
1935 // to be implemented using shaders. Since the shader will perform
1936 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001937 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001938 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001939 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001940 }
1941
1942 if (mCaches.blend) {
1943 glDisable(GL_BLEND);
1944 }
1945 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001946 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001947 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001948 glDisable(GL_BLEND);
1949 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001950 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001951}
1952
Romain Guy889f8d12010-07-29 14:37:42 -07001953bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001954 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001955 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001956 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001957 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001958 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001959 }
Romain Guy6926c722010-07-12 20:20:03 -07001960 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001961}
1962
Romain Guy026c5e162010-06-28 17:12:22 -07001963void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001964 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001965 TextureVertex::setUV(v++, u1, v1);
1966 TextureVertex::setUV(v++, u2, v1);
1967 TextureVertex::setUV(v++, u1, v2);
1968 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001969}
1970
Chet Haase5c13d892010-10-08 08:37:55 -07001971void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001972 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001973 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001974 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1975 if (!isMode) {
1976 // Assume SRC_OVER
1977 *mode = SkXfermode::kSrcOver_Mode;
1978 }
1979 } else {
1980 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001981 }
1982
1983 // Skia draws using the color's alpha channel if < 255
1984 // Otherwise, it uses the paint's alpha
1985 int color = paint->getColor();
1986 *alpha = (color >> 24) & 0xFF;
1987 if (*alpha == 255) {
1988 *alpha = paint->getAlpha();
1989 }
1990 } else {
1991 *mode = SkXfermode::kSrcOver_Mode;
1992 *alpha = 255;
1993 }
Romain Guy026c5e162010-06-28 17:12:22 -07001994}
1995
Romain Guya5aed0d2010-09-09 14:42:43 -07001996SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08001997 // In the future we should look at unifying the Porter-Duff modes and
1998 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07001999 if (mode == NULL) {
2000 return SkXfermode::kSrcOver_Mode;
2001 }
2002 return mode->fMode;
2003}
2004
Romain Guy746b7402010-10-26 16:27:31 -07002005void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002006 bool bound = false;
2007 if (wrapS != texture->wrapS) {
2008 glBindTexture(GL_TEXTURE_2D, texture->id);
2009 bound = true;
2010 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2011 texture->wrapS = wrapS;
2012 }
2013 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002014 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002015 glBindTexture(GL_TEXTURE_2D, texture->id);
2016 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002017 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2018 texture->wrapT = wrapT;
2019 }
Romain Guya1db5742010-07-20 13:09:13 -07002020}
2021
Romain Guy9d5316e2010-06-24 19:30:36 -07002022}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002023}; // namespace android