blob: 1f652010e627b6ab456dec84141133cd170e4c0b [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 Guyae88e5e2010-10-22 17:49:18 -0700406 if (fboLayer) {
407 // Clear the previous layer regions before we change the viewport
408 clearLayerRegions();
409 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700410 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700411
Romain Guyeb993562010-10-05 18:14:38 -0700412 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700413 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700414
Romain Guyae88e5e2010-10-22 17:49:18 -0700415 // We cannot work with sub-pixels in this case
416 bounds.snapToPixelBoundaries();
417
Romain Guyae517592010-10-22 10:40:27 -0700418 // When the layer is not an FBO, we may use glCopyTexImage so we
419 // need to make sure the layer does not extend outside the bounds
420 // of the framebuffer
421 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700422 }
Romain Guybf434112010-09-16 14:40:17 -0700423
Romain Guy746b7402010-10-26 16:27:31 -0700424 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
425 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800426 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700427 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700428 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700429 }
430
431 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800432 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700433 return false;
434 }
Romain Guyf18fd992010-07-08 11:45:51 -0700435
Romain Guy5b3b3522010-10-27 18:57:51 -0700436 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700437 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700438 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700439 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700440 }
441
Romain Guydda57022010-07-06 11:39:32 -0700442 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700443 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700444 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700445 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
446 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800447 layer->colorFilter = mColorFilter;
Romain Guydda57022010-07-06 11:39:32 -0700448
Romain Guy8fb95422010-08-17 18:38:51 -0700449 // Save the layer in the snapshot
450 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700451 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700452
Romain Guyeb993562010-10-05 18:14:38 -0700453 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700454 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700455 } else {
456 // Copy the framebuffer into the layer
457 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800458 if (!bounds.isEmpty()) {
459 if (layer->empty) {
460 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
461 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
462 layer->empty = false;
463 } else {
464 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
465 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
466 }
467 // Enqueue the buffer coordinates to clear the corresponding region later
468 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700469 }
Romain Guyeb993562010-10-05 18:14:38 -0700470 }
Romain Guyf86ef572010-07-01 11:05:42 -0700471
Romain Guyd55a8612010-06-28 17:42:46 -0700472 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700473}
474
Romain Guy5b3b3522010-10-27 18:57:51 -0700475bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
476 GLuint previousFbo) {
477 layer->fbo = mCaches.fboCache.get();
478
479#if RENDER_LAYERS_AS_REGIONS
480 snapshot->region = &snapshot->layer->region;
481 snapshot->flags |= Snapshot::kFlagFboTarget;
482#endif
483
484 Rect clip(bounds);
485 snapshot->transform->mapRect(clip);
486 clip.intersect(*snapshot->clipRect);
487 clip.snapToPixelBoundaries();
488 clip.intersect(snapshot->previous->viewport);
489
490 mat4 inverse;
491 inverse.loadInverse(*mSnapshot->transform);
492
493 inverse.mapRect(clip);
494 clip.snapToPixelBoundaries();
495 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700496 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700497
498 snapshot->flags |= Snapshot::kFlagIsFboLayer;
499 snapshot->fbo = layer->fbo;
500 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700501 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
502 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
503 snapshot->height = bounds.getHeight();
504 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
505 snapshot->orthoMatrix.load(mOrthoMatrix);
506
507 // Bind texture to FBO
508 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
509 glBindTexture(GL_TEXTURE_2D, layer->texture);
510
511 // Initialize the texture if needed
512 if (layer->empty) {
513 layer->empty = false;
514 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
515 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
516 }
517
518 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
519 layer->texture, 0);
520
521#if DEBUG_LAYERS_AS_REGIONS
522 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
523 if (status != GL_FRAMEBUFFER_COMPLETE) {
524 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
525
526 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
527 glDeleteTextures(1, &layer->texture);
528 mCaches.fboCache.put(layer->fbo);
529
530 delete layer;
531
532 return false;
533 }
534#endif
535
536 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
537 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
538 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
539 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
540 glClear(GL_COLOR_BUFFER_BIT);
541
542 dirtyClip();
543
544 // Change the ortho projection
545 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
546 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
547
548 return true;
549}
550
Romain Guy1c740bc2010-09-13 18:00:09 -0700551/**
552 * Read the documentation of createLayer() before doing anything in this method.
553 */
Romain Guy1d83e192010-08-17 11:37:00 -0700554void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
555 if (!current->layer) {
556 LOGE("Attempting to compose a layer that does not exist");
557 return;
558 }
559
Romain Guy5b3b3522010-10-27 18:57:51 -0700560 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700561
562 if (fboLayer) {
563 // Unbind current FBO and restore previous one
564 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
565 }
566
Romain Guy1d83e192010-08-17 11:37:00 -0700567 Layer* layer = current->layer;
568 const Rect& rect = layer->layer;
569
Romain Guyeb993562010-10-05 18:14:38 -0700570 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700571 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700572 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700573 // Required below, composeLayerRect() will divide by 255
574 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700575 }
576
Romain Guy03750a02010-10-18 14:06:08 -0700577 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700578
Romain Guy746b7402010-10-26 16:27:31 -0700579 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700580
Romain Guy5b3b3522010-10-27 18:57:51 -0700581 // When the layer is stored in an FBO, we can save a bit of fillrate by
582 // drawing only the dirty region
583 if (fboLayer) {
584 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800585 if (layer->colorFilter) {
586 setupColorFilter(layer->colorFilter);
587 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800589 if (layer->colorFilter) {
590 resetColorFilter();
591 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700592 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800593 if (!rect.isEmpty()) {
594 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
595 composeLayerRect(layer, rect, true);
596 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700597 }
Romain Guy8b55f372010-08-18 17:10:07 -0700598
Romain Guyeb993562010-10-05 18:14:38 -0700599 if (fboLayer) {
600 // Detach the texture from the FBO
601 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
602 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
603 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
604
605 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
606 mCaches.fboCache.put(current->fbo);
607 }
608
Romain Guy746b7402010-10-26 16:27:31 -0700609 dirtyClip();
610
Romain Guyeb993562010-10-05 18:14:38 -0700611 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700612 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700613 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700614 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700615 delete layer;
616 }
617}
618
Romain Guy5b3b3522010-10-27 18:57:51 -0700619void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
620 const Rect& texCoords = layer->texCoords;
621 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
622
623 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
624 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
625 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
626
627 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
628}
629
630void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
631#if RENDER_LAYERS_AS_REGIONS
632 if (layer->region.isRect()) {
633 composeLayerRect(layer, rect);
634 layer->region.clear();
635 return;
636 }
637
638 if (!layer->region.isEmpty()) {
639 size_t count;
640 const android::Rect* rects = layer->region.getArray(&count);
641
Romain Guy5b3b3522010-10-27 18:57:51 -0700642 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700643 const float texX = 1.0f / float(layer->width);
644 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800645 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700646
647 TextureVertex* mesh = mCaches.getRegionMesh();
648 GLsizei numQuads = 0;
649
Romain Guy7230a742011-01-10 22:26:16 -0800650 setupDraw();
651 setupDrawWithTexture();
652 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800653 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800654 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
655 setupDrawProgram();
656 setupDrawDirtyRegionsDisabled();
657 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800658 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800659 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800660 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800661 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700662
663 for (size_t i = 0; i < count; i++) {
664 const android::Rect* r = &rects[i];
665
666 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800667 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700668 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800669 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700670
671 // TODO: Reject quads outside of the clip
672 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
673 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
674 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
675 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
676
677 numQuads++;
678
679 if (numQuads >= REGION_MESH_QUAD_COUNT) {
680 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
681 numQuads = 0;
682 mesh = mCaches.getRegionMesh();
683 }
684 }
685
686 if (numQuads > 0) {
687 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
688 }
689
690 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800691 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700692
693#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800694 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700695#endif
696
697 layer->region.clear();
698 }
699#else
700 composeLayerRect(layer, rect);
701#endif
702}
703
Romain Guy3a3133d2011-02-01 22:59:58 -0800704void OpenGLRenderer::drawRegionRects(const Region& region) {
705#if DEBUG_LAYERS_AS_REGIONS
706 size_t count;
707 const android::Rect* rects = region.getArray(&count);
708
709 uint32_t colors[] = {
710 0x7fff0000, 0x7f00ff00,
711 0x7f0000ff, 0x7fff00ff,
712 };
713
714 int offset = 0;
715 int32_t top = rects[0].top;
716
717 for (size_t i = 0; i < count; i++) {
718 if (top != rects[i].top) {
719 offset ^= 0x2;
720 top = rects[i].top;
721 }
722
723 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
724 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
725 SkXfermode::kSrcOver_Mode);
726 }
727#endif
728}
729
Romain Guy5b3b3522010-10-27 18:57:51 -0700730void OpenGLRenderer::dirtyLayer(const float left, const float top,
731 const float right, const float bottom, const mat4 transform) {
732#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800733 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700734 Rect bounds(left, top, right, bottom);
735 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800736 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700737 }
738#endif
739}
740
741void OpenGLRenderer::dirtyLayer(const float left, const float top,
742 const float right, const float bottom) {
743#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800744 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800746 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800747 }
748#endif
749}
750
751void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
752#if RENDER_LAYERS_AS_REGIONS
753 if (bounds.intersect(*mSnapshot->clipRect)) {
754 bounds.snapToPixelBoundaries();
755 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
756 if (!dirty.isEmpty()) {
757 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700758 }
759 }
760#endif
761}
762
Romain Guy86942302010-09-12 13:02:16 -0700763void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800764 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700765
Romain Guy5b3b3522010-10-27 18:57:51 -0700766 Rect clipRect(*mSnapshot->clipRect);
767 clipRect.snapToPixelBoundaries();
768
Romain Guy86942302010-09-12 13:02:16 -0700769 for (uint32_t i = 0; i < mLayers.size(); i++) {
770 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700771 if (clipRect.intersects(*bounds)) {
772 // Clear the framebuffer where the layer will draw
773 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
774 bounds->getWidth(), bounds->getHeight());
775 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
776 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700777
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 // Restore the clip
779 dirtyClip();
780 }
Romain Guy86942302010-09-12 13:02:16 -0700781
782 delete bounds;
783 }
Romain Guy86942302010-09-12 13:02:16 -0700784
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700786}
787
Romain Guybd6b79b2010-06-26 00:13:53 -0700788///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700789// Transforms
790///////////////////////////////////////////////////////////////////////////////
791
792void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700793 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700794}
795
796void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700797 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700798}
799
800void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700801 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700802}
803
Romain Guy807daf72011-01-18 11:19:19 -0800804void OpenGLRenderer::skew(float sx, float sy) {
805 mSnapshot->transform->skew(sx, sy);
806}
807
Romain Guyf6a11b82010-06-23 17:47:49 -0700808void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700809 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700810}
811
Romain Guy41030da2010-10-13 13:40:37 -0700812const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700813 if (mSnapshot->fbo != 0) {
814 return &mSnapshot->transform->data[0];
815 }
816 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700817}
818
Romain Guyf6a11b82010-06-23 17:47:49 -0700819void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700820 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700821}
822
823void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700824 SkMatrix transform;
825 mSnapshot->transform->copyTo(transform);
826 transform.preConcat(*matrix);
827 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700828}
829
830///////////////////////////////////////////////////////////////////////////////
831// Clipping
832///////////////////////////////////////////////////////////////////////////////
833
Romain Guybb9524b2010-06-22 18:56:38 -0700834void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700835 Rect clip(*mSnapshot->clipRect);
836 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700837 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700838 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700839}
840
841const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700842 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700843}
844
Romain Guyc7d53492010-06-25 13:41:57 -0700845bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800846 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700847 return true;
848 }
849
Romain Guy1d83e192010-08-17 11:37:00 -0700850 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700851 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700852 r.snapToPixelBoundaries();
853
854 Rect clipRect(*mSnapshot->clipRect);
855 clipRect.snapToPixelBoundaries();
856
857 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700858}
859
Romain Guy079ba2c2010-07-16 14:12:24 -0700860bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
861 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700862 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700863 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700864 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700865 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700866}
867
Romain Guyf6a11b82010-06-23 17:47:49 -0700868///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800869// Drawing commands
870///////////////////////////////////////////////////////////////////////////////
871
872void OpenGLRenderer::setupDraw() {
873 clearLayerRegions();
874 if (mDirtyClip) {
875 setScissorFromClip();
876 }
877 mDescription.reset();
878 mSetShaderColor = false;
879 mColorSet = false;
880 mColorA = mColorR = mColorG = mColorB = 0.0f;
881 mTextureUnit = 0;
882 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800883 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800884}
885
886void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
887 mDescription.hasTexture = true;
888 mDescription.hasAlpha8Texture = isAlpha8;
889}
890
891void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800892 setupDrawColor(color, (color >> 24) & 0xFF);
893}
894
895void OpenGLRenderer::setupDrawColor(int color, int alpha) {
896 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800897 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800898 mColorR = a * ((color >> 16) & 0xFF);
899 mColorG = a * ((color >> 8) & 0xFF);
900 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800901 mColorSet = true;
902 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
903}
904
Romain Guy86568192010-12-14 15:55:39 -0800905void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
906 mColorA = alpha / 255.0f;
907 const float a = mColorA / 255.0f;
908 mColorR = a * ((color >> 16) & 0xFF);
909 mColorG = a * ((color >> 8) & 0xFF);
910 mColorB = a * ((color ) & 0xFF);
911 mColorSet = true;
912 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
913}
914
Romain Guy70ca14e2010-12-13 18:24:33 -0800915void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
916 mColorA = a;
917 mColorR = r;
918 mColorG = g;
919 mColorB = b;
920 mColorSet = true;
921 mSetShaderColor = mDescription.setColor(r, g, b, a);
922}
923
Romain Guy86568192010-12-14 15:55:39 -0800924void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
925 mColorA = a;
926 mColorR = r;
927 mColorG = g;
928 mColorB = b;
929 mColorSet = true;
930 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
931}
932
Romain Guy70ca14e2010-12-13 18:24:33 -0800933void OpenGLRenderer::setupDrawShader() {
934 if (mShader) {
935 mShader->describe(mDescription, mCaches.extensions);
936 }
937}
938
939void OpenGLRenderer::setupDrawColorFilter() {
940 if (mColorFilter) {
941 mColorFilter->describe(mDescription, mCaches.extensions);
942 }
943}
944
945void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
946 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
947 mDescription, swapSrcDst);
948}
949
950void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
951 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
952 mDescription, swapSrcDst);
953}
954
955void OpenGLRenderer::setupDrawProgram() {
956 useProgram(mCaches.programCache.get(mDescription));
957}
958
959void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
960 mTrackDirtyRegions = false;
961}
962
963void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
964 bool ignoreTransform) {
965 mModelView.loadTranslate(left, top, 0.0f);
966 if (!ignoreTransform) {
967 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
968 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
969 } else {
970 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
971 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
972 }
973}
974
Romain Guy8d0d4782010-12-14 20:13:35 -0800975void OpenGLRenderer::setupDrawModelViewIdentity() {
976 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
977}
978
Romain Guy70ca14e2010-12-13 18:24:33 -0800979void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
980 bool ignoreTransform, bool ignoreModelView) {
981 if (!ignoreModelView) {
982 mModelView.loadTranslate(left, top, 0.0f);
983 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800984 } else {
985 mModelView.loadIdentity();
986 }
Romain Guy86568192010-12-14 15:55:39 -0800987 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
988 if (!ignoreTransform) {
989 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
990 if (mTrackDirtyRegions && dirty) {
991 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
992 }
993 } else {
994 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
995 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
996 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800997}
998
999void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001000 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001001 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1002 }
1003}
1004
Romain Guy86568192010-12-14 15:55:39 -08001005void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001006 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001007 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001008 }
1009}
1010
Romain Guy70ca14e2010-12-13 18:24:33 -08001011void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1012 if (mShader) {
1013 if (ignoreTransform) {
1014 mModelView.loadInverse(*mSnapshot->transform);
1015 }
1016 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1017 }
1018}
1019
Romain Guy8d0d4782010-12-14 20:13:35 -08001020void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1021 if (mShader) {
1022 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1023 }
1024}
1025
Romain Guy70ca14e2010-12-13 18:24:33 -08001026void OpenGLRenderer::setupDrawColorFilterUniforms() {
1027 if (mColorFilter) {
1028 mColorFilter->setupProgram(mCaches.currentProgram);
1029 }
1030}
1031
1032void OpenGLRenderer::setupDrawSimpleMesh() {
1033 mCaches.bindMeshBuffer();
1034 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1035 gMeshStride, 0);
1036}
1037
1038void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1039 bindTexture(texture);
1040 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1041
1042 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1043 glEnableVertexAttribArray(mTexCoordsSlot);
1044}
1045
1046void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1047 if (!vertices) {
1048 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1049 } else {
1050 mCaches.unbindMeshBuffer();
1051 }
1052 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1053 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001054 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001055 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1056 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001057}
1058
1059void OpenGLRenderer::finishDrawTexture() {
1060 glDisableVertexAttribArray(mTexCoordsSlot);
1061}
1062
1063///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001064// Drawing
1065///////////////////////////////////////////////////////////////////////////////
1066
Romain Guycabfcc12011-03-07 18:06:46 -08001067bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001068 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1069 // will be performed by the display list itself
1070 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001071 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001072 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001073 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001074}
1075
Chet Haase5c13d892010-10-08 08:37:55 -07001076void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001077 const float right = left + bitmap->width();
1078 const float bottom = top + bitmap->height();
1079
1080 if (quickReject(left, top, right, bottom)) {
1081 return;
1082 }
1083
Romain Guy86568192010-12-14 15:55:39 -08001084 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001085 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001086 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001087 const AutoTexture autoCleanup(texture);
1088
Romain Guy6926c722010-07-12 20:20:03 -07001089 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001090}
1091
Chet Haase5c13d892010-10-08 08:37:55 -07001092void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001093 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1094 const mat4 transform(*matrix);
1095 transform.mapRect(r);
1096
Romain Guy6926c722010-07-12 20:20:03 -07001097 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1098 return;
1099 }
1100
Romain Guy86568192010-12-14 15:55:39 -08001101 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001102 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001103 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001104 const AutoTexture autoCleanup(texture);
1105
Romain Guy5b3b3522010-10-27 18:57:51 -07001106 // This could be done in a cheaper way, all we need is pass the matrix
1107 // to the vertex shader. The save/restore is a bit overkill.
1108 save(SkCanvas::kMatrix_SaveFlag);
1109 concatMatrix(matrix);
1110 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1111 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001112}
1113
Romain Guy5a7b4662011-01-20 19:09:30 -08001114void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1115 float* vertices, int* colors, SkPaint* paint) {
1116 // TODO: Do a quickReject
1117 if (!vertices || mSnapshot->isIgnored()) {
1118 return;
1119 }
1120
1121 glActiveTexture(gTextureUnits[0]);
1122 Texture* texture = mCaches.textureCache.get(bitmap);
1123 if (!texture) return;
1124 const AutoTexture autoCleanup(texture);
1125 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1126
1127 int alpha;
1128 SkXfermode::Mode mode;
1129 getAlphaAndMode(paint, &alpha, &mode);
1130
Romain Guy5a7b4662011-01-20 19:09:30 -08001131 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001132
Romain Guyb18d2d02011-02-10 15:52:54 -08001133 float left = FLT_MAX;
1134 float top = FLT_MAX;
1135 float right = FLT_MIN;
1136 float bottom = FLT_MIN;
1137
1138#if RENDER_LAYERS_AS_REGIONS
1139 bool hasActiveLayer = hasLayer();
1140#else
1141 bool hasActiveLayer = false;
1142#endif
1143
Romain Guya566b7c2011-01-23 16:36:11 -08001144 // TODO: Support the colors array
1145 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001146 TextureVertex* vertex = mesh;
1147 for (int32_t y = 0; y < meshHeight; y++) {
1148 for (int32_t x = 0; x < meshWidth; x++) {
1149 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1150
1151 float u1 = float(x) / meshWidth;
1152 float u2 = float(x + 1) / meshWidth;
1153 float v1 = float(y) / meshHeight;
1154 float v2 = float(y + 1) / meshHeight;
1155
1156 int ax = i + (meshWidth + 1) * 2;
1157 int ay = ax + 1;
1158 int bx = i;
1159 int by = bx + 1;
1160 int cx = i + 2;
1161 int cy = cx + 1;
1162 int dx = i + (meshWidth + 1) * 2 + 2;
1163 int dy = dx + 1;
1164
1165 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1166 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1167 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1168
1169 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1170 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1171 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001172
1173#if RENDER_LAYERS_AS_REGIONS
1174 if (hasActiveLayer) {
1175 // TODO: This could be optimized to avoid unnecessary ops
1176 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1177 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1178 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1179 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1180 }
1181#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001182 }
1183 }
1184
Romain Guyb18d2d02011-02-10 15:52:54 -08001185#if RENDER_LAYERS_AS_REGIONS
1186 if (hasActiveLayer) {
1187 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1188 }
1189#endif
1190
Romain Guy5a7b4662011-01-20 19:09:30 -08001191 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1192 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001193 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001194}
1195
Romain Guy8ba548f2010-06-30 19:21:21 -07001196void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1197 float srcLeft, float srcTop, float srcRight, float srcBottom,
1198 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001199 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001200 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1201 return;
1202 }
1203
Romain Guy746b7402010-10-26 16:27:31 -07001204 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001205 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001206 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001207 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001208 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001209
Romain Guy8ba548f2010-06-30 19:21:21 -07001210 const float width = texture->width;
1211 const float height = texture->height;
1212
1213 const float u1 = srcLeft / width;
1214 const float v1 = srcTop / height;
1215 const float u2 = srcRight / width;
1216 const float v2 = srcBottom / height;
1217
Romain Guy03750a02010-10-18 14:06:08 -07001218 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001219 resetDrawTextureTexCoords(u1, v1, u2, v2);
1220
Romain Guy03750a02010-10-18 14:06:08 -07001221 int alpha;
1222 SkXfermode::Mode mode;
1223 getAlphaAndMode(paint, &alpha, &mode);
1224
Romain Guy6620c6d2010-12-06 18:07:02 -08001225 if (mSnapshot->transform->isPureTranslate()) {
1226 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1227 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1228
1229 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1230 texture->id, alpha / 255.0f, mode, texture->blend,
1231 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1232 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1233 } else {
1234 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1235 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1236 GL_TRIANGLE_STRIP, gMeshCount);
1237 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001238
1239 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1240}
1241
Romain Guy4aa90572010-09-26 18:40:37 -07001242void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001243 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001244 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001245 if (quickReject(left, top, right, bottom)) {
1246 return;
1247 }
1248
Romain Guy746b7402010-10-26 16:27:31 -07001249 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001250 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001251 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001252 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001253 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001254
1255 int alpha;
1256 SkXfermode::Mode mode;
1257 getAlphaAndMode(paint, &alpha, &mode);
1258
Romain Guy2728f962010-10-08 18:36:15 -07001259 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001260 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001261
Romain Guya5ef39a2010-12-03 16:48:20 -08001262 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001263 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001264#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001265 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001266 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001267 const float offsetX = left + mSnapshot->transform->getTranslateX();
1268 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001269 const size_t count = mesh->quads.size();
1270 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001271 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001272 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001273 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1274 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1275 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001276 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001277 dirtyLayer(left + bounds.left, top + bounds.top,
1278 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001279 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001280 }
1281 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001282#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001283
Romain Guy6620c6d2010-12-06 18:07:02 -08001284 if (pureTranslate) {
1285 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1286 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1287
1288 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1289 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1290 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1291 true, !mesh->hasEmptyQuads);
1292 } else {
1293 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1294 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1295 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1296 true, !mesh->hasEmptyQuads);
1297 }
Romain Guy054dc182010-10-15 17:55:25 -07001298 }
Romain Guyf7f93552010-07-08 19:17:03 -07001299}
1300
Chet Haase5c13d892010-10-08 08:37:55 -07001301void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001302 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001303
Romain Guya957eea2010-12-08 18:34:42 -08001304 const bool isAA = paint->isAntiAlias();
1305 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1306 // A stroke width of 0 has a special meaningin Skia:
1307 // it draws an unscaled 1px wide line
1308 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1309
Romain Guy759ea802010-09-16 20:49:46 -07001310 int alpha;
1311 SkXfermode::Mode mode;
1312 getAlphaAndMode(paint, &alpha, &mode);
1313
Romain Guya957eea2010-12-08 18:34:42 -08001314 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001315 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001316 if (!isHairLine) {
1317 // TODO: AA needs more vertices
1318 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001319 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001320 // TODO: AA will be different
1321 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001322 }
1323
Romain Guya957eea2010-12-08 18:34:42 -08001324 TextureVertex lines[verticesCount];
1325 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001326
Romain Guy8d0d4782010-12-14 20:13:35 -08001327 setupDraw();
1328 setupDrawColor(paint->getColor(), alpha);
1329 setupDrawColorFilter();
1330 setupDrawShader();
1331 setupDrawBlending(mode);
1332 setupDrawProgram();
1333 setupDrawModelViewIdentity();
1334 setupDrawColorUniforms();
1335 setupDrawColorFilterUniforms();
1336 setupDrawShaderIdentityUniforms();
1337 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001338
1339 if (!isHairLine) {
1340 // TODO: Handle the AA case
1341 for (int i = 0; i < count; i += 4) {
1342 // a = start point, b = end point
1343 vec2 a(points[i], points[i + 1]);
1344 vec2 b(points[i + 2], points[i + 3]);
1345
1346 // Bias to snap to the same pixels as Skia
1347 a += 0.375;
1348 b += 0.375;
1349
1350 // Find the normal to the line
1351 vec2 n = (b - a).copyNormalized() * strokeWidth;
1352 float x = n.x;
1353 n.x = -n.y;
1354 n.y = x;
1355
1356 // Four corners of the rectangle defining a thick line
1357 vec2 p1 = a - n;
1358 vec2 p2 = a + n;
1359 vec2 p3 = b + n;
1360 vec2 p4 = b - n;
1361
Romain Guy7230a742011-01-10 22:26:16 -08001362 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1363 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1364 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1365 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001366
Romain Guy7230a742011-01-10 22:26:16 -08001367 if (!quickReject(left, top, right, bottom)) {
1368 // Draw the line as 2 triangles, could be optimized
1369 // by using only 4 vertices and the correct indices
1370 // Also we should probably used non textured vertices
1371 // when line AA is disabled to save on bandwidth
1372 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1373 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1374 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1375 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1376 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1377 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1378
1379 generatedVerticesCount += 6;
1380
1381 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1382 }
Romain Guya957eea2010-12-08 18:34:42 -08001383 }
1384
Romain Guy7230a742011-01-10 22:26:16 -08001385 if (generatedVerticesCount > 0) {
1386 // GL_LINE does not give the result we want to match Skia
1387 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1388 }
Romain Guya957eea2010-12-08 18:34:42 -08001389 } else {
1390 // TODO: Handle the AA case
1391 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001392 const float left = fmin(points[i], points[i + 1]);
1393 const float right = fmax(points[i], points[i + 1]);
1394 const float top = fmin(points[i + 2], points[i + 3]);
1395 const float bottom = fmax(points[i + 2], points[i + 3]);
1396
1397 if (!quickReject(left, top, right, bottom)) {
1398 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1399 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1400
1401 generatedVerticesCount += 2;
1402
1403 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1404 }
Romain Guya957eea2010-12-08 18:34:42 -08001405 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001406
Romain Guy7230a742011-01-10 22:26:16 -08001407 if (generatedVerticesCount > 0) {
1408 glLineWidth(1.0f);
1409 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1410 }
Romain Guy29d89972010-09-22 16:10:57 -07001411 }
Romain Guy759ea802010-09-16 20:49:46 -07001412}
1413
Romain Guy85bf02f2010-06-22 13:11:24 -07001414void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001415 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001416 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001417
Romain Guyae88e5e2010-10-22 17:49:18 -07001418 Rect& clip(*mSnapshot->clipRect);
1419 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001420
Romain Guy3d58c032010-07-14 16:34:53 -07001421 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001422}
Romain Guy9d5316e2010-06-24 19:30:36 -07001423
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001424void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001425 if (!texture) return;
1426 const AutoTexture autoCleanup(texture);
1427
1428 const float x = left + texture->left - texture->offset;
1429 const float y = top + texture->top - texture->offset;
1430
1431 drawPathTexture(texture, x, y, paint);
1432}
1433
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001434void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1435 float rx, float ry, SkPaint* paint) {
1436 if (mSnapshot->isIgnored()) return;
1437
1438 glActiveTexture(gTextureUnits[0]);
1439 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1440 right - left, bottom - top, rx, ry, paint);
1441 drawShape(left, top, texture, paint);
1442}
1443
Romain Guy01d58e42011-01-19 21:54:02 -08001444void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1445 if (mSnapshot->isIgnored()) return;
1446
1447 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001448 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001449 drawShape(x - radius, y - radius, texture, paint);
1450}
Romain Guy01d58e42011-01-19 21:54:02 -08001451
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001452void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1453 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001454
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001455 glActiveTexture(gTextureUnits[0]);
1456 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1457 drawShape(left, top, texture, paint);
1458}
1459
Romain Guy8b2f5262011-01-23 16:15:02 -08001460void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1461 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1462 if (mSnapshot->isIgnored()) return;
1463
1464 if (fabs(sweepAngle) >= 360.0f) {
1465 drawOval(left, top, right, bottom, paint);
1466 return;
1467 }
1468
1469 glActiveTexture(gTextureUnits[0]);
1470 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1471 startAngle, sweepAngle, useCenter, paint);
1472 drawShape(left, top, texture, paint);
1473}
1474
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001475void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1476 SkPaint* paint) {
1477 if (mSnapshot->isIgnored()) return;
1478
1479 glActiveTexture(gTextureUnits[0]);
1480 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1481 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001482}
1483
Chet Haase5c13d892010-10-08 08:37:55 -07001484void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001485 if (p->getStyle() != SkPaint::kFill_Style) {
1486 drawRectAsShape(left, top, right, bottom, p);
1487 return;
1488 }
1489
Romain Guy6926c722010-07-12 20:20:03 -07001490 if (quickReject(left, top, right, bottom)) {
1491 return;
1492 }
1493
Romain Guy026c5e162010-06-28 17:12:22 -07001494 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001495 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001496 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1497 if (!isMode) {
1498 // Assume SRC_OVER
1499 mode = SkXfermode::kSrcOver_Mode;
1500 }
1501 } else {
1502 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001503 }
1504
Romain Guy026c5e162010-06-28 17:12:22 -07001505 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001506 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001507}
Romain Guy9d5316e2010-06-24 19:30:36 -07001508
Romain Guye8e62a42010-07-23 18:55:21 -07001509void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1510 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001511 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001512 return;
1513 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001514 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001515
Romain Guyf607bdc2010-09-10 19:20:06 -07001516 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001517
Romain Guya674ab72010-08-10 17:26:42 -07001518 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001519 switch (paint->getTextAlign()) {
1520 case SkPaint::kCenter_Align:
1521 length = paint->measureText(text, bytesCount);
1522 x -= length / 2.0f;
1523 break;
1524 case SkPaint::kRight_Align:
1525 length = paint->measureText(text, bytesCount);
1526 x -= length;
1527 break;
1528 default:
1529 break;
1530 }
1531
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001532 const float oldX = x;
1533 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001534 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1535 if (pureTranslate) {
1536 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1537 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1538 }
1539
Romain Guyb45c0c92010-08-26 20:35:23 -07001540 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1541 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001542 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001543
Romain Guy86568192010-12-14 15:55:39 -08001544 int alpha;
1545 SkXfermode::Mode mode;
1546 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001547
Romain Guy1e45aae2010-08-13 19:39:53 -07001548 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001549 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001550 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001551 count, mShadowRadius);
1552 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001553
Romain Guy86568192010-12-14 15:55:39 -08001554 const float sx = x - shadow->left + mShadowDx;
1555 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001556
Romain Guy86568192010-12-14 15:55:39 -08001557 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1558
1559 glActiveTexture(gTextureUnits[0]);
1560 setupDraw();
1561 setupDrawWithTexture(true);
1562 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1563 setupDrawBlending(true, mode);
1564 setupDrawProgram();
1565 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1566 setupDrawTexture(shadow->id);
1567 setupDrawPureColorUniforms();
1568 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1569
Romain Guy0a417492010-08-16 20:26:20 -07001570 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001571 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001572 }
1573
Romain Guyb146b122010-12-15 17:06:45 -08001574 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1575 return;
1576 }
1577
Romain Guy6620c6d2010-12-06 18:07:02 -08001578 // Pick the appropriate texture filtering
1579 bool linearFilter = mSnapshot->transform->changesBounds();
1580 if (pureTranslate && !linearFilter) {
1581 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1582 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001583
Romain Guy86568192010-12-14 15:55:39 -08001584 glActiveTexture(gTextureUnits[0]);
1585 setupDraw();
1586 setupDrawDirtyRegionsDisabled();
1587 setupDrawWithTexture(true);
1588 setupDrawAlpha8Color(paint->getColor(), alpha);
1589 setupDrawColorFilter();
1590 setupDrawShader();
1591 setupDrawBlending(true, mode);
1592 setupDrawProgram();
1593 setupDrawModelView(x, y, x, y, pureTranslate, true);
1594 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1595 setupDrawPureColorUniforms();
1596 setupDrawColorFilterUniforms();
1597 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001598
Romain Guy6620c6d2010-12-06 18:07:02 -08001599 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001600 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1601
1602#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001603 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001604#else
Romain Guyf219da52011-01-16 12:54:25 -08001605 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001606#endif
Romain Guy03750a02010-10-18 14:06:08 -07001607 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001608
1609 // Tell font renderer the locations of position and texture coord
1610 // attributes so it can bind its data properly
1611 int positionSlot = mCaches.currentProgram->position;
1612 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001613 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001614 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001615#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001616 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001617 if (!pureTranslate) {
1618 mSnapshot->transform->mapRect(bounds);
1619 }
Romain Guyf219da52011-01-16 12:54:25 -08001620 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001621 }
1622#endif
1623 }
Romain Guy694b5192010-07-21 21:33:20 -07001624
1625 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001626 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001627
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001628 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001629}
1630
Romain Guy7fbcc042010-08-04 15:40:07 -07001631void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001632 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001633
Romain Guy01d58e42011-01-19 21:54:02 -08001634 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001635
Romain Guyfb8b7632010-08-23 21:05:08 -07001636 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001637 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001638 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001639
Romain Guy8b55f372010-08-18 17:10:07 -07001640 const float x = texture->left - texture->offset;
1641 const float y = texture->top - texture->offset;
1642
Romain Guy01d58e42011-01-19 21:54:02 -08001643 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001644}
1645
Romain Guyada830f2011-01-13 12:13:20 -08001646void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1647 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001648 return;
1649 }
1650
1651 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001652
1653 int alpha;
1654 SkXfermode::Mode mode;
1655 getAlphaAndMode(paint, &alpha, &mode);
1656
Romain Guyada830f2011-01-13 12:13:20 -08001657 layer->alpha = alpha;
1658 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001659
Romain Guyf219da52011-01-16 12:54:25 -08001660#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001661 if (!layer->region.isEmpty()) {
1662 if (layer->region.isRect()) {
1663 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1664 composeLayerRect(layer, r);
1665 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001666 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001667 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001668
Romain Guyc88e3572011-01-22 00:32:12 -08001669 setupDraw();
1670 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001671 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001672 setupDrawColorFilter();
1673 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1674 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001675 setupDrawPureColorUniforms();
1676 setupDrawColorFilterUniforms();
1677 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001678 // TODO: The current layer, if any, will be dirtied with the bounding box
1679 // of the layer we are drawing. Since the layer we are drawing has
1680 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001681 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1682 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001683
Romain Guyc88e3572011-01-22 00:32:12 -08001684 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1685 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001686
Romain Guyc88e3572011-01-22 00:32:12 -08001687 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001688
1689#if DEBUG_LAYERS_AS_REGIONS
1690 drawRegionRects(layer->region);
1691#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001692 }
Romain Guyf219da52011-01-16 12:54:25 -08001693 }
1694#else
Romain Guyada830f2011-01-13 12:13:20 -08001695 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1696 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001697#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001698}
1699
Romain Guy6926c722010-07-12 20:20:03 -07001700///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001701// Shaders
1702///////////////////////////////////////////////////////////////////////////////
1703
1704void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001705 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001706}
1707
Romain Guy06f96e22010-07-30 19:18:16 -07001708void OpenGLRenderer::setupShader(SkiaShader* shader) {
1709 mShader = shader;
1710 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001711 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001712 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001713}
1714
Romain Guyd27977d2010-07-14 19:18:51 -07001715///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001716// Color filters
1717///////////////////////////////////////////////////////////////////////////////
1718
1719void OpenGLRenderer::resetColorFilter() {
1720 mColorFilter = NULL;
1721}
1722
1723void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1724 mColorFilter = filter;
1725}
1726
1727///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001728// Drop shadow
1729///////////////////////////////////////////////////////////////////////////////
1730
1731void OpenGLRenderer::resetShadow() {
1732 mHasShadow = false;
1733}
1734
1735void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1736 mHasShadow = true;
1737 mShadowRadius = radius;
1738 mShadowDx = dx;
1739 mShadowDy = dy;
1740 mShadowColor = color;
1741}
1742
1743///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001744// Drawing implementation
1745///////////////////////////////////////////////////////////////////////////////
1746
Romain Guy01d58e42011-01-19 21:54:02 -08001747void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1748 float x, float y, SkPaint* paint) {
1749 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1750 return;
1751 }
1752
1753 int alpha;
1754 SkXfermode::Mode mode;
1755 getAlphaAndMode(paint, &alpha, &mode);
1756
1757 setupDraw();
1758 setupDrawWithTexture(true);
1759 setupDrawAlpha8Color(paint->getColor(), alpha);
1760 setupDrawColorFilter();
1761 setupDrawShader();
1762 setupDrawBlending(true, mode);
1763 setupDrawProgram();
1764 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1765 setupDrawTexture(texture->id);
1766 setupDrawPureColorUniforms();
1767 setupDrawColorFilterUniforms();
1768 setupDrawShaderUniforms();
1769 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1770
1771 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1772
1773 finishDrawTexture();
1774}
1775
Romain Guyf607bdc2010-09-10 19:20:06 -07001776// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001777#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1778#define kStdUnderline_Offset (1.0f / 9.0f)
1779#define kStdUnderline_Thickness (1.0f / 18.0f)
1780
1781void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1782 float x, float y, SkPaint* paint) {
1783 // Handle underline and strike-through
1784 uint32_t flags = paint->getFlags();
1785 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1786 float underlineWidth = length;
1787 // If length is > 0.0f, we already measured the text for the text alignment
1788 if (length <= 0.0f) {
1789 underlineWidth = paint->measureText(text, bytesCount);
1790 }
1791
1792 float offsetX = 0;
1793 switch (paint->getTextAlign()) {
1794 case SkPaint::kCenter_Align:
1795 offsetX = underlineWidth * 0.5f;
1796 break;
1797 case SkPaint::kRight_Align:
1798 offsetX = underlineWidth;
1799 break;
1800 default:
1801 break;
1802 }
1803
1804 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001805 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001806 // TODO: Support stroke width < 1.0f when we have AA lines
1807 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001808
Romain Guye20ecbd2010-09-22 19:49:04 -07001809 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001810 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001811
Romain Guyf6834472011-01-23 13:32:12 -08001812 int linesCount = 0;
1813 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1814 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1815
1816 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001817 float points[pointsCount];
1818 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001819
1820 if (flags & SkPaint::kUnderlineText_Flag) {
1821 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001822 points[currentPoint++] = left;
1823 points[currentPoint++] = top;
1824 points[currentPoint++] = left + underlineWidth;
1825 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001826 }
1827
1828 if (flags & SkPaint::kStrikeThruText_Flag) {
1829 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001830 points[currentPoint++] = left;
1831 points[currentPoint++] = top;
1832 points[currentPoint++] = left + underlineWidth;
1833 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001834 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001835
1836 SkPaint linesPaint(*paint);
1837 linesPaint.setStrokeWidth(strokeWidth);
1838
1839 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001840 }
1841 }
1842}
1843
Romain Guy026c5e162010-06-28 17:12:22 -07001844void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001845 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001846 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001847 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001848 color |= 0x00ffffff;
1849 }
1850
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 setupDraw();
1852 setupDrawColor(color);
1853 setupDrawShader();
1854 setupDrawColorFilter();
1855 setupDrawBlending(mode);
1856 setupDrawProgram();
1857 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1858 setupDrawColorUniforms();
1859 setupDrawShaderUniforms(ignoreTransform);
1860 setupDrawColorFilterUniforms();
1861 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001862
Romain Guyc95c8d62010-09-17 15:31:32 -07001863 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1864}
1865
Romain Guy82ba8142010-07-09 13:25:56 -07001866void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001867 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001868 int alpha;
1869 SkXfermode::Mode mode;
1870 getAlphaAndMode(paint, &alpha, &mode);
1871
Romain Guy8164c2d2010-10-25 18:03:28 -07001872 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1873
Romain Guy6620c6d2010-12-06 18:07:02 -08001874 if (mSnapshot->transform->isPureTranslate()) {
1875 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1876 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1877
1878 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1879 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1880 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1881 } else {
1882 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1883 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1884 GL_TRIANGLE_STRIP, gMeshCount);
1885 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001886}
1887
Romain Guybd6b79b2010-06-26 00:13:53 -07001888void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001889 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1890 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001891 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001892}
1893
1894void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001895 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001896 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001897 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001898
Romain Guy746b7402010-10-26 16:27:31 -07001899 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001900 setupDrawWithTexture();
1901 setupDrawColor(alpha, alpha, alpha, alpha);
1902 setupDrawColorFilter();
1903 setupDrawBlending(blend, mode, swapSrcDst);
1904 setupDrawProgram();
1905 if (!dirty) {
1906 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001907 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001908 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001909 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001910 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001911 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001912 }
Romain Guy86568192010-12-14 15:55:39 -08001913 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001914 setupDrawColorFilterUniforms();
1915 setupDrawTexture(texture);
1916 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001917
Romain Guy6820ac82010-09-15 18:11:50 -07001918 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001919
1920 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001921}
1922
Romain Guya5aed0d2010-09-09 14:42:43 -07001923void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001924 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001925 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1926 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001927 if (mode < SkXfermode::kPlus_Mode) {
1928 if (!mCaches.blend) {
1929 glEnable(GL_BLEND);
1930 }
Romain Guy82ba8142010-07-09 13:25:56 -07001931
Romain Guyf607bdc2010-09-10 19:20:06 -07001932 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1933 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001934
Romain Guya5aed0d2010-09-09 14:42:43 -07001935 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1936 glBlendFunc(sourceMode, destMode);
1937 mCaches.lastSrcMode = sourceMode;
1938 mCaches.lastDstMode = destMode;
1939 }
1940 } else {
1941 // These blend modes are not supported by OpenGL directly and have
1942 // to be implemented using shaders. Since the shader will perform
1943 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001944 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001945 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001946 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001947 }
1948
1949 if (mCaches.blend) {
1950 glDisable(GL_BLEND);
1951 }
1952 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001953 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001954 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001955 glDisable(GL_BLEND);
1956 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001957 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001958}
1959
Romain Guy889f8d12010-07-29 14:37:42 -07001960bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001961 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001962 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001963 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001964 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001965 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001966 }
Romain Guy6926c722010-07-12 20:20:03 -07001967 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001968}
1969
Romain Guy026c5e162010-06-28 17:12:22 -07001970void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001971 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001972 TextureVertex::setUV(v++, u1, v1);
1973 TextureVertex::setUV(v++, u2, v1);
1974 TextureVertex::setUV(v++, u1, v2);
1975 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001976}
1977
Chet Haase5c13d892010-10-08 08:37:55 -07001978void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001979 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001980 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001981 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1982 if (!isMode) {
1983 // Assume SRC_OVER
1984 *mode = SkXfermode::kSrcOver_Mode;
1985 }
1986 } else {
1987 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001988 }
1989
1990 // Skia draws using the color's alpha channel if < 255
1991 // Otherwise, it uses the paint's alpha
1992 int color = paint->getColor();
1993 *alpha = (color >> 24) & 0xFF;
1994 if (*alpha == 255) {
1995 *alpha = paint->getAlpha();
1996 }
1997 } else {
1998 *mode = SkXfermode::kSrcOver_Mode;
1999 *alpha = 255;
2000 }
Romain Guy026c5e162010-06-28 17:12:22 -07002001}
2002
Romain Guya5aed0d2010-09-09 14:42:43 -07002003SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08002004 // In the future we should look at unifying the Porter-Duff modes and
2005 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002006 if (mode == NULL) {
2007 return SkXfermode::kSrcOver_Mode;
2008 }
2009 return mode->fMode;
2010}
2011
Romain Guy746b7402010-10-26 16:27:31 -07002012void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002013 bool bound = false;
2014 if (wrapS != texture->wrapS) {
2015 glBindTexture(GL_TEXTURE_2D, texture->id);
2016 bound = true;
2017 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2018 texture->wrapS = wrapS;
2019 }
2020 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002021 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002022 glBindTexture(GL_TEXTURE_2D, texture->id);
2023 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002024 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2025 texture->wrapT = wrapT;
2026 }
Romain Guya1db5742010-07-20 13:09:13 -07002027}
2028
Romain Guy9d5316e2010-06-24 19:30:36 -07002029}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002030}; // namespace android