blob: d26580408be8cab49dd58b99db300c46f0fe1c63 [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
Romain Guy80911b82011-03-16 15:30:12 -0700219 Rect clip(*mSnapshot->clipRect);
220 clip.snapToPixelBoundaries();
221
Romain Guyd643bb52011-03-01 14:55:21 -0800222#if RENDER_LAYERS_AS_REGIONS
223 // Since we don't know what the functor will draw, let's dirty
224 // tne entire clip region
225 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800226 dirtyLayerUnchecked(clip, getRegion());
227 }
228#endif
229
Romain Guy80911b82011-03-16 15:30:12 -0700230 struct {
231 // Input: current clip rect
232 int clipLeft;
233 int clipTop;
234 int clipRight;
235 int clipBottom;
236
237 // Output: dirty region to redraw
238 float dirtyLeft;
239 float dirtyTop;
240 float dirtyRight;
241 float dirtyBottom;
242 } constraints;
243
244 constraints.clipLeft = clip.left;
245 constraints.clipTop = clip.top;
246 constraints.clipRight = clip.right;
247 constraints.clipBottom = clip.bottom;
248
249 status_t result = (*functor)(0, &constraints);
Romain Guycabfcc12011-03-07 18:06:46 -0800250
251 if (result != 0) {
Romain Guy80911b82011-03-16 15:30:12 -0700252 Rect localDirty(constraints.dirtyLeft, constraints.dirtyTop,
253 constraints.dirtyRight, constraints.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800254 dirty.unionWith(localDirty);
255 }
256
Chet Haasedaf98e92011-01-10 14:10:36 -0800257 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800258 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
262// State management
263///////////////////////////////////////////////////////////////////////////////
264
Romain Guybb9524b2010-06-22 18:56:38 -0700265int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700266 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
269int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700270 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700271}
272
273void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700274 if (mSaveCount > 1) {
275 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700276 }
Romain Guybb9524b2010-06-22 18:56:38 -0700277}
278
279void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700280 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700281
Romain Guy8fb95422010-08-17 18:38:51 -0700282 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700283 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700284 }
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
Romain Guy8aef54f2010-09-01 15:13:49 -0700287int OpenGLRenderer::saveSnapshot(int flags) {
288 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700289 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700290}
291
292bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700293 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700294 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700295 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700296
Romain Guybd6b79b2010-06-26 00:13:53 -0700297 sp<Snapshot> current = mSnapshot;
298 sp<Snapshot> previous = mSnapshot->previous;
299
Romain Guyeb993562010-10-05 18:14:38 -0700300 if (restoreOrtho) {
301 Rect& r = previous->viewport;
302 glViewport(r.left, r.top, r.right, r.bottom);
303 mOrthoMatrix.load(current->orthoMatrix);
304 }
305
Romain Guy8b55f372010-08-18 17:10:07 -0700306 mSaveCount--;
307 mSnapshot = previous;
308
Romain Guy2542d192010-08-18 11:47:12 -0700309 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700310 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700311 }
Romain Guy2542d192010-08-18 11:47:12 -0700312
Romain Guy5ec99242010-11-03 16:19:08 -0700313 if (restoreLayer) {
314 composeLayer(current, previous);
315 }
316
Romain Guy2542d192010-08-18 11:47:12 -0700317 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700318}
319
Romain Guyf6a11b82010-06-23 17:47:49 -0700320///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700321// Layers
322///////////////////////////////////////////////////////////////////////////////
323
324int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700325 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700326 const GLuint previousFbo = mSnapshot->fbo;
327 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700328
Romain Guyaf636eb2010-12-09 17:47:21 -0800329 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700330 int alpha = 255;
331 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700332
Romain Guye45362c2010-11-03 19:58:32 -0700333 if (p) {
334 alpha = p->getAlpha();
335 if (!mCaches.extensions.hasFramebufferFetch()) {
336 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
337 if (!isMode) {
338 // Assume SRC_OVER
339 mode = SkXfermode::kSrcOver_Mode;
340 }
341 } else {
342 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700343 }
344 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700345 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700346 }
Romain Guyd55a8612010-06-28 17:42:46 -0700347
Romain Guydbc26d22010-10-11 17:58:29 -0700348 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
349 }
Romain Guyd55a8612010-06-28 17:42:46 -0700350
351 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700352}
353
354int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
355 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700356 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700358 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700359 SkPaint paint;
360 paint.setAlpha(alpha);
361 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700362 }
Romain Guyd55a8612010-06-28 17:42:46 -0700363}
Romain Guybd6b79b2010-06-26 00:13:53 -0700364
Romain Guy1c740bc2010-09-13 18:00:09 -0700365/**
366 * Layers are viewed by Skia are slightly different than layers in image editing
367 * programs (for instance.) When a layer is created, previously created layers
368 * and the frame buffer still receive every drawing command. For instance, if a
369 * layer is created and a shape intersecting the bounds of the layers and the
370 * framebuffer is draw, the shape will be drawn on both (unless the layer was
371 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
372 *
373 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
374 * texture. Unfortunately, this is inefficient as it requires every primitive to
375 * be drawn n + 1 times, where n is the number of active layers. In practice this
376 * means, for every primitive:
377 * - Switch active frame buffer
378 * - Change viewport, clip and projection matrix
379 * - Issue the drawing
380 *
381 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700382 * To avoid this, layers are implemented in a different way here, at least in the
383 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
384 * is set. When this flag is set we can redirect all drawing operations into a
385 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 *
387 * This implementation relies on the frame buffer being at least RGBA 8888. When
388 * a layer is created, only a texture is created, not an FBO. The content of the
389 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700390 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700391 * buffer and drawing continues as normal. This technique therefore treats the
392 * frame buffer as a scratch buffer for the layers.
393 *
394 * To compose the layers back onto the frame buffer, each layer texture
395 * (containing the original frame buffer data) is drawn as a simple quad over
396 * the frame buffer. The trick is that the quad is set as the composition
397 * destination in the blending equation, and the frame buffer becomes the source
398 * of the composition.
399 *
400 * Drawing layers with an alpha value requires an extra step before composition.
401 * An empty quad is drawn over the layer's region in the frame buffer. This quad
402 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
403 * quad is used to multiply the colors in the frame buffer. This is achieved by
404 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
405 * GL_ZERO, GL_SRC_ALPHA.
406 *
407 * Because glCopyTexImage2D() can be slow, an alternative implementation might
408 * be use to draw a single clipped layer. The implementation described above
409 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700410 *
411 * (1) The frame buffer is actually not cleared right away. To allow the GPU
412 * to potentially optimize series of calls to glCopyTexImage2D, the frame
413 * buffer is left untouched until the first drawing operation. Only when
414 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700415 */
Romain Guyd55a8612010-06-28 17:42:46 -0700416bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700417 float right, float bottom, int alpha, SkXfermode::Mode mode,
418 int flags, GLuint previousFbo) {
419 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700420 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700421
Romain Guyeb993562010-10-05 18:14:38 -0700422 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
423
Romain Guyf607bdc2010-09-10 19:20:06 -0700424 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700425 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700426 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700427 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700428
Romain Guyeb993562010-10-05 18:14:38 -0700429 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700430 if (bounds.intersect(*snapshot->clipRect)) {
431 // We cannot work with sub-pixels in this case
432 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700433
Romain Guyad37cd32011-03-15 11:12:25 -0700434 // When the layer is not an FBO, we may use glCopyTexImage so we
435 // need to make sure the layer does not extend outside the bounds
436 // of the framebuffer
437 if (!bounds.intersect(snapshot->previous->viewport)) {
438 bounds.setEmpty();
439 }
440 } else {
441 bounds.setEmpty();
442 }
Romain Guyeb993562010-10-05 18:14:38 -0700443 }
Romain Guybf434112010-09-16 14:40:17 -0700444
Romain Guy746b7402010-10-26 16:27:31 -0700445 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
446 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800447 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700448 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700449 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700450 }
451
452 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800453 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700454 return false;
455 }
Romain Guyf18fd992010-07-08 11:45:51 -0700456
Romain Guy5b3b3522010-10-27 18:57:51 -0700457 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700458 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700459 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700460 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700461 }
462
Romain Guydda57022010-07-06 11:39:32 -0700463 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700464 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700465 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700466 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
467 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800468 layer->colorFilter = mColorFilter;
Romain Guydda57022010-07-06 11:39:32 -0700469
Romain Guy8fb95422010-08-17 18:38:51 -0700470 // Save the layer in the snapshot
471 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700472 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700473
Romain Guyeb993562010-10-05 18:14:38 -0700474 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700475 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700476 } else {
477 // Copy the framebuffer into the layer
478 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800479 if (!bounds.isEmpty()) {
480 if (layer->empty) {
481 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
482 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
483 layer->empty = false;
484 } else {
485 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
486 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
487 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700488
489 // Clear the framebuffer where the layer will draw
490 glScissor(bounds.left, mSnapshot->height - bounds.bottom,
491 bounds.getWidth(), bounds.getHeight());
492 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
493 glClear(GL_COLOR_BUFFER_BIT);
494
495 dirtyClip();
Romain Guyae88e5e2010-10-22 17:49:18 -0700496 }
Romain Guyeb993562010-10-05 18:14:38 -0700497 }
Romain Guyf86ef572010-07-01 11:05:42 -0700498
Romain Guyd55a8612010-06-28 17:42:46 -0700499 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700500}
501
Romain Guy5b3b3522010-10-27 18:57:51 -0700502bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
503 GLuint previousFbo) {
504 layer->fbo = mCaches.fboCache.get();
505
506#if RENDER_LAYERS_AS_REGIONS
507 snapshot->region = &snapshot->layer->region;
508 snapshot->flags |= Snapshot::kFlagFboTarget;
509#endif
510
511 Rect clip(bounds);
512 snapshot->transform->mapRect(clip);
513 clip.intersect(*snapshot->clipRect);
514 clip.snapToPixelBoundaries();
515 clip.intersect(snapshot->previous->viewport);
516
517 mat4 inverse;
518 inverse.loadInverse(*mSnapshot->transform);
519
520 inverse.mapRect(clip);
521 clip.snapToPixelBoundaries();
522 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700523 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700524
525 snapshot->flags |= Snapshot::kFlagIsFboLayer;
526 snapshot->fbo = layer->fbo;
527 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700528 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
529 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
530 snapshot->height = bounds.getHeight();
531 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
532 snapshot->orthoMatrix.load(mOrthoMatrix);
533
534 // Bind texture to FBO
535 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
536 glBindTexture(GL_TEXTURE_2D, layer->texture);
537
538 // Initialize the texture if needed
539 if (layer->empty) {
540 layer->empty = false;
541 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
542 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
543 }
544
545 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
546 layer->texture, 0);
547
548#if DEBUG_LAYERS_AS_REGIONS
549 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
550 if (status != GL_FRAMEBUFFER_COMPLETE) {
551 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
552
553 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
554 glDeleteTextures(1, &layer->texture);
555 mCaches.fboCache.put(layer->fbo);
556
557 delete layer;
558
559 return false;
560 }
561#endif
562
563 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
564 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
565 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
566 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
567 glClear(GL_COLOR_BUFFER_BIT);
568
569 dirtyClip();
570
571 // Change the ortho projection
572 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
573 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
574
575 return true;
576}
577
Romain Guy1c740bc2010-09-13 18:00:09 -0700578/**
579 * Read the documentation of createLayer() before doing anything in this method.
580 */
Romain Guy1d83e192010-08-17 11:37:00 -0700581void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
582 if (!current->layer) {
583 LOGE("Attempting to compose a layer that does not exist");
584 return;
585 }
586
Romain Guy5b3b3522010-10-27 18:57:51 -0700587 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700588
589 if (fboLayer) {
590 // Unbind current FBO and restore previous one
591 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
592 }
593
Romain Guy1d83e192010-08-17 11:37:00 -0700594 Layer* layer = current->layer;
595 const Rect& rect = layer->layer;
596
Romain Guyeb993562010-10-05 18:14:38 -0700597 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700598 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700599 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700600 // Required below, composeLayerRect() will divide by 255
601 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700602 }
603
Romain Guy03750a02010-10-18 14:06:08 -0700604 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700605
Romain Guy746b7402010-10-26 16:27:31 -0700606 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700607
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 // When the layer is stored in an FBO, we can save a bit of fillrate by
609 // drawing only the dirty region
610 if (fboLayer) {
611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800612 if (layer->colorFilter) {
613 setupColorFilter(layer->colorFilter);
614 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800616 if (layer->colorFilter) {
617 resetColorFilter();
618 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700619 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800620 if (!rect.isEmpty()) {
621 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
622 composeLayerRect(layer, rect, true);
623 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700624 }
Romain Guy8b55f372010-08-18 17:10:07 -0700625
Romain Guyeb993562010-10-05 18:14:38 -0700626 if (fboLayer) {
627 // Detach the texture from the FBO
628 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
629 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
630 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
631
632 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
633 mCaches.fboCache.put(current->fbo);
634 }
635
Romain Guy746b7402010-10-26 16:27:31 -0700636 dirtyClip();
637
Romain Guyeb993562010-10-05 18:14:38 -0700638 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700639 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700640 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700641 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700642 delete layer;
643 }
644}
645
Romain Guy5b3b3522010-10-27 18:57:51 -0700646void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
647 const Rect& texCoords = layer->texCoords;
648 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
649
650 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
651 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
652 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
653
654 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
655}
656
657void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
658#if RENDER_LAYERS_AS_REGIONS
Romain Guy6217a712011-03-15 16:32:28 -0700659#if RENDER_LAYERS_RECT_AS_RECT
Romain Guy5b3b3522010-10-27 18:57:51 -0700660 if (layer->region.isRect()) {
661 composeLayerRect(layer, rect);
662 layer->region.clear();
663 return;
664 }
Romain Guy6217a712011-03-15 16:32:28 -0700665#endif
Romain Guy5b3b3522010-10-27 18:57:51 -0700666
667 if (!layer->region.isEmpty()) {
668 size_t count;
669 const android::Rect* rects = layer->region.getArray(&count);
670
Romain Guy5b3b3522010-10-27 18:57:51 -0700671 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700672 const float texX = 1.0f / float(layer->width);
673 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800674 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700675
676 TextureVertex* mesh = mCaches.getRegionMesh();
677 GLsizei numQuads = 0;
678
Romain Guy7230a742011-01-10 22:26:16 -0800679 setupDraw();
680 setupDrawWithTexture();
681 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800682 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800683 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
684 setupDrawProgram();
685 setupDrawDirtyRegionsDisabled();
686 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800687 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800688 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800689 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800690 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700691
692 for (size_t i = 0; i < count; i++) {
693 const android::Rect* r = &rects[i];
694
695 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800696 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700697 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800698 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700699
700 // TODO: Reject quads outside of the clip
701 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
702 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
703 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
704 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
705
706 numQuads++;
707
708 if (numQuads >= REGION_MESH_QUAD_COUNT) {
709 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
710 numQuads = 0;
711 mesh = mCaches.getRegionMesh();
712 }
713 }
714
715 if (numQuads > 0) {
716 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
717 }
718
719 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800720 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700721
722#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800723 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700724#endif
725
726 layer->region.clear();
727 }
728#else
729 composeLayerRect(layer, rect);
730#endif
731}
732
Romain Guy3a3133d2011-02-01 22:59:58 -0800733void OpenGLRenderer::drawRegionRects(const Region& region) {
734#if DEBUG_LAYERS_AS_REGIONS
735 size_t count;
736 const android::Rect* rects = region.getArray(&count);
737
738 uint32_t colors[] = {
739 0x7fff0000, 0x7f00ff00,
740 0x7f0000ff, 0x7fff00ff,
741 };
742
743 int offset = 0;
744 int32_t top = rects[0].top;
745
746 for (size_t i = 0; i < count; i++) {
747 if (top != rects[i].top) {
748 offset ^= 0x2;
749 top = rects[i].top;
750 }
751
752 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
753 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
754 SkXfermode::kSrcOver_Mode);
755 }
756#endif
757}
758
Romain Guy5b3b3522010-10-27 18:57:51 -0700759void OpenGLRenderer::dirtyLayer(const float left, const float top,
760 const float right, const float bottom, const mat4 transform) {
761#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800762 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700763 Rect bounds(left, top, right, bottom);
764 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800765 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700766 }
767#endif
768}
769
770void OpenGLRenderer::dirtyLayer(const float left, const float top,
771 const float right, const float bottom) {
772#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800773 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700774 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800775 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800776 }
777#endif
778}
779
780void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
781#if RENDER_LAYERS_AS_REGIONS
782 if (bounds.intersect(*mSnapshot->clipRect)) {
783 bounds.snapToPixelBoundaries();
784 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
785 if (!dirty.isEmpty()) {
786 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700787 }
788 }
789#endif
790}
791
Romain Guybd6b79b2010-06-26 00:13:53 -0700792///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700793// Transforms
794///////////////////////////////////////////////////////////////////////////////
795
796void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700797 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700798}
799
800void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700801 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700802}
803
804void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700805 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700806}
807
Romain Guy807daf72011-01-18 11:19:19 -0800808void OpenGLRenderer::skew(float sx, float sy) {
809 mSnapshot->transform->skew(sx, sy);
810}
811
Romain Guyf6a11b82010-06-23 17:47:49 -0700812void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700813 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700814}
815
Romain Guy41030da2010-10-13 13:40:37 -0700816const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700817 if (mSnapshot->fbo != 0) {
818 return &mSnapshot->transform->data[0];
819 }
820 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700821}
822
Romain Guyf6a11b82010-06-23 17:47:49 -0700823void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700824 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700825}
826
827void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700828 SkMatrix transform;
829 mSnapshot->transform->copyTo(transform);
830 transform.preConcat(*matrix);
831 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700832}
833
834///////////////////////////////////////////////////////////////////////////////
835// Clipping
836///////////////////////////////////////////////////////////////////////////////
837
Romain Guybb9524b2010-06-22 18:56:38 -0700838void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700839 Rect clip(*mSnapshot->clipRect);
840 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700841 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700842 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700843}
844
845const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700846 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700847}
848
Romain Guyc7d53492010-06-25 13:41:57 -0700849bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800850 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700851 return true;
852 }
853
Romain Guy1d83e192010-08-17 11:37:00 -0700854 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700855 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700856 r.snapToPixelBoundaries();
857
858 Rect clipRect(*mSnapshot->clipRect);
859 clipRect.snapToPixelBoundaries();
860
861 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700862}
863
Romain Guy079ba2c2010-07-16 14:12:24 -0700864bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
865 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700866 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700867 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700868 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700869 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700870}
871
Romain Guyf6a11b82010-06-23 17:47:49 -0700872///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800873// Drawing commands
874///////////////////////////////////////////////////////////////////////////////
875
876void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800877 if (mDirtyClip) {
878 setScissorFromClip();
879 }
880 mDescription.reset();
881 mSetShaderColor = false;
882 mColorSet = false;
883 mColorA = mColorR = mColorG = mColorB = 0.0f;
884 mTextureUnit = 0;
885 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800886 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800887}
888
889void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
890 mDescription.hasTexture = true;
891 mDescription.hasAlpha8Texture = isAlpha8;
892}
893
894void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800895 setupDrawColor(color, (color >> 24) & 0xFF);
896}
897
898void OpenGLRenderer::setupDrawColor(int color, int alpha) {
899 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800900 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800901 mColorR = a * ((color >> 16) & 0xFF);
902 mColorG = a * ((color >> 8) & 0xFF);
903 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800904 mColorSet = true;
905 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
906}
907
Romain Guy86568192010-12-14 15:55:39 -0800908void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
909 mColorA = alpha / 255.0f;
910 const float a = mColorA / 255.0f;
911 mColorR = a * ((color >> 16) & 0xFF);
912 mColorG = a * ((color >> 8) & 0xFF);
913 mColorB = a * ((color ) & 0xFF);
914 mColorSet = true;
915 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
916}
917
Romain Guy70ca14e2010-12-13 18:24:33 -0800918void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
919 mColorA = a;
920 mColorR = r;
921 mColorG = g;
922 mColorB = b;
923 mColorSet = true;
924 mSetShaderColor = mDescription.setColor(r, g, b, a);
925}
926
Romain Guy86568192010-12-14 15:55:39 -0800927void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
928 mColorA = a;
929 mColorR = r;
930 mColorG = g;
931 mColorB = b;
932 mColorSet = true;
933 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
934}
935
Romain Guy70ca14e2010-12-13 18:24:33 -0800936void OpenGLRenderer::setupDrawShader() {
937 if (mShader) {
938 mShader->describe(mDescription, mCaches.extensions);
939 }
940}
941
942void OpenGLRenderer::setupDrawColorFilter() {
943 if (mColorFilter) {
944 mColorFilter->describe(mDescription, mCaches.extensions);
945 }
946}
947
948void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
949 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
950 mDescription, swapSrcDst);
951}
952
953void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
954 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
955 mDescription, swapSrcDst);
956}
957
958void OpenGLRenderer::setupDrawProgram() {
959 useProgram(mCaches.programCache.get(mDescription));
960}
961
962void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
963 mTrackDirtyRegions = false;
964}
965
966void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
967 bool ignoreTransform) {
968 mModelView.loadTranslate(left, top, 0.0f);
969 if (!ignoreTransform) {
970 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
971 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
972 } else {
973 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
974 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
975 }
976}
977
Romain Guy8d0d4782010-12-14 20:13:35 -0800978void OpenGLRenderer::setupDrawModelViewIdentity() {
979 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
980}
981
Romain Guy70ca14e2010-12-13 18:24:33 -0800982void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
983 bool ignoreTransform, bool ignoreModelView) {
984 if (!ignoreModelView) {
985 mModelView.loadTranslate(left, top, 0.0f);
986 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800987 } else {
988 mModelView.loadIdentity();
989 }
Romain Guy86568192010-12-14 15:55:39 -0800990 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
991 if (!ignoreTransform) {
992 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
993 if (mTrackDirtyRegions && dirty) {
994 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
995 }
996 } else {
997 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
998 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
999 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001000}
1001
1002void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001003 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001004 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1005 }
1006}
1007
Romain Guy86568192010-12-14 15:55:39 -08001008void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001009 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001010 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001011 }
1012}
1013
Romain Guy70ca14e2010-12-13 18:24:33 -08001014void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1015 if (mShader) {
1016 if (ignoreTransform) {
1017 mModelView.loadInverse(*mSnapshot->transform);
1018 }
1019 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1020 }
1021}
1022
Romain Guy8d0d4782010-12-14 20:13:35 -08001023void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1024 if (mShader) {
1025 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1026 }
1027}
1028
Romain Guy70ca14e2010-12-13 18:24:33 -08001029void OpenGLRenderer::setupDrawColorFilterUniforms() {
1030 if (mColorFilter) {
1031 mColorFilter->setupProgram(mCaches.currentProgram);
1032 }
1033}
1034
1035void OpenGLRenderer::setupDrawSimpleMesh() {
1036 mCaches.bindMeshBuffer();
1037 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1038 gMeshStride, 0);
1039}
1040
1041void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1042 bindTexture(texture);
1043 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1044
1045 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1046 glEnableVertexAttribArray(mTexCoordsSlot);
1047}
1048
1049void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1050 if (!vertices) {
1051 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1052 } else {
1053 mCaches.unbindMeshBuffer();
1054 }
1055 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1056 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001057 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001058 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1059 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001060}
1061
1062void OpenGLRenderer::finishDrawTexture() {
1063 glDisableVertexAttribArray(mTexCoordsSlot);
1064}
1065
1066///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001067// Drawing
1068///////////////////////////////////////////////////////////////////////////////
1069
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001070bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1071 Rect& dirty, uint32_t level) {
1072 if (quickReject(0.0f, 0.0f, width, height)) {
1073 return false;
1074 }
1075
Romain Guy0fe478e2010-11-08 12:08:41 -08001076 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1077 // will be performed by the display list itself
1078 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001079 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001080 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001081
Chet Haasedaf98e92011-01-10 14:10:36 -08001082 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001083}
1084
Chet Haase5c13d892010-10-08 08:37:55 -07001085void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001086 const float right = left + bitmap->width();
1087 const float bottom = top + bitmap->height();
1088
1089 if (quickReject(left, top, right, bottom)) {
1090 return;
1091 }
1092
Romain Guy86568192010-12-14 15:55:39 -08001093 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001094 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001095 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001096 const AutoTexture autoCleanup(texture);
1097
Romain Guy6926c722010-07-12 20:20:03 -07001098 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001099}
1100
Chet Haase5c13d892010-10-08 08:37:55 -07001101void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001102 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1103 const mat4 transform(*matrix);
1104 transform.mapRect(r);
1105
Romain Guy6926c722010-07-12 20:20:03 -07001106 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1107 return;
1108 }
1109
Romain Guy86568192010-12-14 15:55:39 -08001110 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001111 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001112 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001113 const AutoTexture autoCleanup(texture);
1114
Romain Guy5b3b3522010-10-27 18:57:51 -07001115 // This could be done in a cheaper way, all we need is pass the matrix
1116 // to the vertex shader. The save/restore is a bit overkill.
1117 save(SkCanvas::kMatrix_SaveFlag);
1118 concatMatrix(matrix);
1119 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1120 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001121}
1122
Romain Guy5a7b4662011-01-20 19:09:30 -08001123void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1124 float* vertices, int* colors, SkPaint* paint) {
1125 // TODO: Do a quickReject
1126 if (!vertices || mSnapshot->isIgnored()) {
1127 return;
1128 }
1129
1130 glActiveTexture(gTextureUnits[0]);
1131 Texture* texture = mCaches.textureCache.get(bitmap);
1132 if (!texture) return;
1133 const AutoTexture autoCleanup(texture);
1134 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1135
1136 int alpha;
1137 SkXfermode::Mode mode;
1138 getAlphaAndMode(paint, &alpha, &mode);
1139
Romain Guy5a7b4662011-01-20 19:09:30 -08001140 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001141
Romain Guyb18d2d02011-02-10 15:52:54 -08001142 float left = FLT_MAX;
1143 float top = FLT_MAX;
1144 float right = FLT_MIN;
1145 float bottom = FLT_MIN;
1146
1147#if RENDER_LAYERS_AS_REGIONS
1148 bool hasActiveLayer = hasLayer();
1149#else
1150 bool hasActiveLayer = false;
1151#endif
1152
Romain Guya566b7c2011-01-23 16:36:11 -08001153 // TODO: Support the colors array
1154 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001155 TextureVertex* vertex = mesh;
1156 for (int32_t y = 0; y < meshHeight; y++) {
1157 for (int32_t x = 0; x < meshWidth; x++) {
1158 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1159
1160 float u1 = float(x) / meshWidth;
1161 float u2 = float(x + 1) / meshWidth;
1162 float v1 = float(y) / meshHeight;
1163 float v2 = float(y + 1) / meshHeight;
1164
1165 int ax = i + (meshWidth + 1) * 2;
1166 int ay = ax + 1;
1167 int bx = i;
1168 int by = bx + 1;
1169 int cx = i + 2;
1170 int cy = cx + 1;
1171 int dx = i + (meshWidth + 1) * 2 + 2;
1172 int dy = dx + 1;
1173
1174 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1175 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1176 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1177
1178 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1179 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1180 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001181
1182#if RENDER_LAYERS_AS_REGIONS
1183 if (hasActiveLayer) {
1184 // TODO: This could be optimized to avoid unnecessary ops
1185 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1186 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1187 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1188 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1189 }
1190#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001191 }
1192 }
1193
Romain Guyb18d2d02011-02-10 15:52:54 -08001194#if RENDER_LAYERS_AS_REGIONS
1195 if (hasActiveLayer) {
1196 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1197 }
1198#endif
1199
Romain Guy5a7b4662011-01-20 19:09:30 -08001200 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1201 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001202 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001203}
1204
Romain Guy8ba548f2010-06-30 19:21:21 -07001205void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1206 float srcLeft, float srcTop, float srcRight, float srcBottom,
1207 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001208 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001209 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1210 return;
1211 }
1212
Romain Guy746b7402010-10-26 16:27:31 -07001213 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001214 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001215 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001216 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001217 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001218
Romain Guy8ba548f2010-06-30 19:21:21 -07001219 const float width = texture->width;
1220 const float height = texture->height;
1221
1222 const float u1 = srcLeft / width;
1223 const float v1 = srcTop / height;
1224 const float u2 = srcRight / width;
1225 const float v2 = srcBottom / height;
1226
Romain Guy03750a02010-10-18 14:06:08 -07001227 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001228 resetDrawTextureTexCoords(u1, v1, u2, v2);
1229
Romain Guy03750a02010-10-18 14:06:08 -07001230 int alpha;
1231 SkXfermode::Mode mode;
1232 getAlphaAndMode(paint, &alpha, &mode);
1233
Romain Guy6620c6d2010-12-06 18:07:02 -08001234 if (mSnapshot->transform->isPureTranslate()) {
1235 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1236 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1237
1238 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1239 texture->id, alpha / 255.0f, mode, texture->blend,
1240 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1241 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1242 } else {
1243 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1244 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1245 GL_TRIANGLE_STRIP, gMeshCount);
1246 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001247
1248 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1249}
1250
Romain Guy4aa90572010-09-26 18:40:37 -07001251void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001252 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001253 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001254 if (quickReject(left, top, right, bottom)) {
1255 return;
1256 }
1257
Romain Guy746b7402010-10-26 16:27:31 -07001258 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001259 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001260 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001261 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001262 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001263
1264 int alpha;
1265 SkXfermode::Mode mode;
1266 getAlphaAndMode(paint, &alpha, &mode);
1267
Romain Guy2728f962010-10-08 18:36:15 -07001268 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001269 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001270
Romain Guya5ef39a2010-12-03 16:48:20 -08001271 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001272 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001273#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001274 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001275 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001276 const float offsetX = left + mSnapshot->transform->getTranslateX();
1277 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001278 const size_t count = mesh->quads.size();
1279 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001280 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001281 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001282 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1283 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1284 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001285 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001286 dirtyLayer(left + bounds.left, top + bounds.top,
1287 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001288 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001289 }
1290 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001291#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001292
Romain Guy6620c6d2010-12-06 18:07:02 -08001293 if (pureTranslate) {
1294 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1295 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1296
1297 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1298 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1299 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1300 true, !mesh->hasEmptyQuads);
1301 } else {
1302 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1303 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1304 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1305 true, !mesh->hasEmptyQuads);
1306 }
Romain Guy054dc182010-10-15 17:55:25 -07001307 }
Romain Guyf7f93552010-07-08 19:17:03 -07001308}
1309
Chet Haase5c13d892010-10-08 08:37:55 -07001310void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001311 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001312
Romain Guya957eea2010-12-08 18:34:42 -08001313 const bool isAA = paint->isAntiAlias();
1314 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1315 // A stroke width of 0 has a special meaningin Skia:
1316 // it draws an unscaled 1px wide line
1317 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1318
Romain Guy759ea802010-09-16 20:49:46 -07001319 int alpha;
1320 SkXfermode::Mode mode;
1321 getAlphaAndMode(paint, &alpha, &mode);
1322
Romain Guya957eea2010-12-08 18:34:42 -08001323 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001324 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001325 if (!isHairLine) {
1326 // TODO: AA needs more vertices
1327 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001328 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001329 // TODO: AA will be different
1330 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001331 }
1332
Romain Guya957eea2010-12-08 18:34:42 -08001333 TextureVertex lines[verticesCount];
1334 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001335
Romain Guy8d0d4782010-12-14 20:13:35 -08001336 setupDraw();
1337 setupDrawColor(paint->getColor(), alpha);
1338 setupDrawColorFilter();
1339 setupDrawShader();
1340 setupDrawBlending(mode);
1341 setupDrawProgram();
1342 setupDrawModelViewIdentity();
1343 setupDrawColorUniforms();
1344 setupDrawColorFilterUniforms();
1345 setupDrawShaderIdentityUniforms();
1346 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001347
1348 if (!isHairLine) {
1349 // TODO: Handle the AA case
1350 for (int i = 0; i < count; i += 4) {
1351 // a = start point, b = end point
1352 vec2 a(points[i], points[i + 1]);
1353 vec2 b(points[i + 2], points[i + 3]);
1354
1355 // Bias to snap to the same pixels as Skia
1356 a += 0.375;
1357 b += 0.375;
1358
1359 // Find the normal to the line
1360 vec2 n = (b - a).copyNormalized() * strokeWidth;
1361 float x = n.x;
1362 n.x = -n.y;
1363 n.y = x;
1364
1365 // Four corners of the rectangle defining a thick line
1366 vec2 p1 = a - n;
1367 vec2 p2 = a + n;
1368 vec2 p3 = b + n;
1369 vec2 p4 = b - n;
1370
Romain Guy7230a742011-01-10 22:26:16 -08001371 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1372 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1373 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1374 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001375
Romain Guy7230a742011-01-10 22:26:16 -08001376 if (!quickReject(left, top, right, bottom)) {
1377 // Draw the line as 2 triangles, could be optimized
1378 // by using only 4 vertices and the correct indices
1379 // Also we should probably used non textured vertices
1380 // when line AA is disabled to save on bandwidth
1381 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1382 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1383 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1384 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1385 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1386 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1387
1388 generatedVerticesCount += 6;
1389
1390 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1391 }
Romain Guya957eea2010-12-08 18:34:42 -08001392 }
1393
Romain Guy7230a742011-01-10 22:26:16 -08001394 if (generatedVerticesCount > 0) {
1395 // GL_LINE does not give the result we want to match Skia
1396 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1397 }
Romain Guya957eea2010-12-08 18:34:42 -08001398 } else {
1399 // TODO: Handle the AA case
1400 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001401 const float left = fmin(points[i], points[i + 1]);
1402 const float right = fmax(points[i], points[i + 1]);
1403 const float top = fmin(points[i + 2], points[i + 3]);
1404 const float bottom = fmax(points[i + 2], points[i + 3]);
1405
1406 if (!quickReject(left, top, right, bottom)) {
1407 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1408 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1409
1410 generatedVerticesCount += 2;
1411
1412 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1413 }
Romain Guya957eea2010-12-08 18:34:42 -08001414 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001415
Romain Guy7230a742011-01-10 22:26:16 -08001416 if (generatedVerticesCount > 0) {
1417 glLineWidth(1.0f);
1418 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1419 }
Romain Guy29d89972010-09-22 16:10:57 -07001420 }
Romain Guy759ea802010-09-16 20:49:46 -07001421}
1422
Romain Guy85bf02f2010-06-22 13:11:24 -07001423void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001424 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001425 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001426
Romain Guyae88e5e2010-10-22 17:49:18 -07001427 Rect& clip(*mSnapshot->clipRect);
1428 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001429
Romain Guy3d58c032010-07-14 16:34:53 -07001430 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001431}
Romain Guy9d5316e2010-06-24 19:30:36 -07001432
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001433void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001434 if (!texture) return;
1435 const AutoTexture autoCleanup(texture);
1436
1437 const float x = left + texture->left - texture->offset;
1438 const float y = top + texture->top - texture->offset;
1439
1440 drawPathTexture(texture, x, y, paint);
1441}
1442
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001443void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1444 float rx, float ry, SkPaint* paint) {
1445 if (mSnapshot->isIgnored()) return;
1446
1447 glActiveTexture(gTextureUnits[0]);
1448 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1449 right - left, bottom - top, rx, ry, paint);
1450 drawShape(left, top, texture, paint);
1451}
1452
Romain Guy01d58e42011-01-19 21:54:02 -08001453void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1454 if (mSnapshot->isIgnored()) return;
1455
1456 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001457 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001458 drawShape(x - radius, y - radius, texture, paint);
1459}
Romain Guy01d58e42011-01-19 21:54:02 -08001460
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001461void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1462 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001463
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001464 glActiveTexture(gTextureUnits[0]);
1465 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1466 drawShape(left, top, texture, paint);
1467}
1468
Romain Guy8b2f5262011-01-23 16:15:02 -08001469void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1470 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1471 if (mSnapshot->isIgnored()) return;
1472
1473 if (fabs(sweepAngle) >= 360.0f) {
1474 drawOval(left, top, right, bottom, paint);
1475 return;
1476 }
1477
1478 glActiveTexture(gTextureUnits[0]);
1479 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1480 startAngle, sweepAngle, useCenter, paint);
1481 drawShape(left, top, texture, paint);
1482}
1483
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001484void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1485 SkPaint* paint) {
1486 if (mSnapshot->isIgnored()) return;
1487
1488 glActiveTexture(gTextureUnits[0]);
1489 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1490 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001491}
1492
Chet Haase5c13d892010-10-08 08:37:55 -07001493void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001494 if (p->getStyle() != SkPaint::kFill_Style) {
1495 drawRectAsShape(left, top, right, bottom, p);
1496 return;
1497 }
1498
Romain Guy6926c722010-07-12 20:20:03 -07001499 if (quickReject(left, top, right, bottom)) {
1500 return;
1501 }
1502
Romain Guy026c5e162010-06-28 17:12:22 -07001503 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001504 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001505 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1506 if (!isMode) {
1507 // Assume SRC_OVER
1508 mode = SkXfermode::kSrcOver_Mode;
1509 }
1510 } else {
1511 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001512 }
1513
Romain Guy026c5e162010-06-28 17:12:22 -07001514 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001515 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001516}
Romain Guy9d5316e2010-06-24 19:30:36 -07001517
Romain Guye8e62a42010-07-23 18:55:21 -07001518void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1519 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001520 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001521 return;
1522 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001523 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001524
Romain Guyf607bdc2010-09-10 19:20:06 -07001525 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001526
Romain Guya674ab72010-08-10 17:26:42 -07001527 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001528 switch (paint->getTextAlign()) {
1529 case SkPaint::kCenter_Align:
1530 length = paint->measureText(text, bytesCount);
1531 x -= length / 2.0f;
1532 break;
1533 case SkPaint::kRight_Align:
1534 length = paint->measureText(text, bytesCount);
1535 x -= length;
1536 break;
1537 default:
1538 break;
1539 }
1540
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001541 const float oldX = x;
1542 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001543 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1544 if (pureTranslate) {
1545 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1546 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1547 }
1548
Romain Guyb45c0c92010-08-26 20:35:23 -07001549 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1550 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001551 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001552
Romain Guy86568192010-12-14 15:55:39 -08001553 int alpha;
1554 SkXfermode::Mode mode;
1555 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001556
Romain Guy1e45aae2010-08-13 19:39:53 -07001557 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001558 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001559 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001560 count, mShadowRadius);
1561 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001562
Romain Guy86568192010-12-14 15:55:39 -08001563 const float sx = x - shadow->left + mShadowDx;
1564 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001565
Romain Guy86568192010-12-14 15:55:39 -08001566 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1567
1568 glActiveTexture(gTextureUnits[0]);
1569 setupDraw();
1570 setupDrawWithTexture(true);
1571 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1572 setupDrawBlending(true, mode);
1573 setupDrawProgram();
1574 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1575 setupDrawTexture(shadow->id);
1576 setupDrawPureColorUniforms();
1577 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1578
Romain Guy0a417492010-08-16 20:26:20 -07001579 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001580 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001581 }
1582
Romain Guyb146b122010-12-15 17:06:45 -08001583 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1584 return;
1585 }
1586
Romain Guy6620c6d2010-12-06 18:07:02 -08001587 // Pick the appropriate texture filtering
1588 bool linearFilter = mSnapshot->transform->changesBounds();
1589 if (pureTranslate && !linearFilter) {
1590 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1591 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001592
Romain Guy86568192010-12-14 15:55:39 -08001593 glActiveTexture(gTextureUnits[0]);
1594 setupDraw();
1595 setupDrawDirtyRegionsDisabled();
1596 setupDrawWithTexture(true);
1597 setupDrawAlpha8Color(paint->getColor(), alpha);
1598 setupDrawColorFilter();
1599 setupDrawShader();
1600 setupDrawBlending(true, mode);
1601 setupDrawProgram();
1602 setupDrawModelView(x, y, x, y, pureTranslate, true);
1603 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1604 setupDrawPureColorUniforms();
1605 setupDrawColorFilterUniforms();
1606 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001607
Romain Guy6620c6d2010-12-06 18:07:02 -08001608 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001609 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1610
1611#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001612 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001613#else
Romain Guyf219da52011-01-16 12:54:25 -08001614 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001615#endif
Romain Guy03750a02010-10-18 14:06:08 -07001616 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001617
1618 // Tell font renderer the locations of position and texture coord
1619 // attributes so it can bind its data properly
1620 int positionSlot = mCaches.currentProgram->position;
1621 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001622 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001623 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001624#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001625 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001626 if (!pureTranslate) {
1627 mSnapshot->transform->mapRect(bounds);
1628 }
Romain Guyf219da52011-01-16 12:54:25 -08001629 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001630 }
1631#endif
1632 }
Romain Guy694b5192010-07-21 21:33:20 -07001633
1634 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001635 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001636
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001637 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001638}
1639
Romain Guy7fbcc042010-08-04 15:40:07 -07001640void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001641 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001642
Romain Guy01d58e42011-01-19 21:54:02 -08001643 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001644
Romain Guyfb8b7632010-08-23 21:05:08 -07001645 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001646 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001647 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001648
Romain Guy8b55f372010-08-18 17:10:07 -07001649 const float x = texture->left - texture->offset;
1650 const float y = texture->top - texture->offset;
1651
Romain Guy01d58e42011-01-19 21:54:02 -08001652 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001653}
1654
Romain Guyada830f2011-01-13 12:13:20 -08001655void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1656 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001657 return;
1658 }
1659
1660 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001661
1662 int alpha;
1663 SkXfermode::Mode mode;
1664 getAlphaAndMode(paint, &alpha, &mode);
1665
Romain Guyada830f2011-01-13 12:13:20 -08001666 layer->alpha = alpha;
1667 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001668
Romain Guyf219da52011-01-16 12:54:25 -08001669#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001670 if (!layer->region.isEmpty()) {
Romain Guy6217a712011-03-15 16:32:28 -07001671#if RENDER_LAYERS_RECT_AS_RECT
Romain Guyc88e3572011-01-22 00:32:12 -08001672 if (layer->region.isRect()) {
1673 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1674 composeLayerRect(layer, r);
1675 } else if (layer->mesh) {
Romain Guy6217a712011-03-15 16:32:28 -07001676#else
1677 if (layer->mesh) {
1678#endif
Romain Guy81683962011-01-24 20:40:18 -08001679 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001680 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001681
Romain Guyc88e3572011-01-22 00:32:12 -08001682 setupDraw();
1683 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001684 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001685 setupDrawColorFilter();
1686 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1687 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001688 setupDrawPureColorUniforms();
1689 setupDrawColorFilterUniforms();
1690 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001691 // TODO: The current layer, if any, will be dirtied with the bounding box
1692 // of the layer we are drawing. Since the layer we are drawing has
1693 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001694 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1695 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001696
Romain Guyc88e3572011-01-22 00:32:12 -08001697 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1698 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001699
Romain Guyc88e3572011-01-22 00:32:12 -08001700 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001701
1702#if DEBUG_LAYERS_AS_REGIONS
1703 drawRegionRects(layer->region);
1704#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001705 }
Romain Guyf219da52011-01-16 12:54:25 -08001706 }
1707#else
Romain Guyada830f2011-01-13 12:13:20 -08001708 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1709 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001710#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001711}
1712
Romain Guy6926c722010-07-12 20:20:03 -07001713///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001714// Shaders
1715///////////////////////////////////////////////////////////////////////////////
1716
1717void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001718 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001719}
1720
Romain Guy06f96e22010-07-30 19:18:16 -07001721void OpenGLRenderer::setupShader(SkiaShader* shader) {
1722 mShader = shader;
1723 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001724 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001725 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001726}
1727
Romain Guyd27977d2010-07-14 19:18:51 -07001728///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001729// Color filters
1730///////////////////////////////////////////////////////////////////////////////
1731
1732void OpenGLRenderer::resetColorFilter() {
1733 mColorFilter = NULL;
1734}
1735
1736void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1737 mColorFilter = filter;
1738}
1739
1740///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001741// Drop shadow
1742///////////////////////////////////////////////////////////////////////////////
1743
1744void OpenGLRenderer::resetShadow() {
1745 mHasShadow = false;
1746}
1747
1748void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1749 mHasShadow = true;
1750 mShadowRadius = radius;
1751 mShadowDx = dx;
1752 mShadowDy = dy;
1753 mShadowColor = color;
1754}
1755
1756///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001757// Drawing implementation
1758///////////////////////////////////////////////////////////////////////////////
1759
Romain Guy01d58e42011-01-19 21:54:02 -08001760void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1761 float x, float y, SkPaint* paint) {
1762 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1763 return;
1764 }
1765
1766 int alpha;
1767 SkXfermode::Mode mode;
1768 getAlphaAndMode(paint, &alpha, &mode);
1769
1770 setupDraw();
1771 setupDrawWithTexture(true);
1772 setupDrawAlpha8Color(paint->getColor(), alpha);
1773 setupDrawColorFilter();
1774 setupDrawShader();
1775 setupDrawBlending(true, mode);
1776 setupDrawProgram();
1777 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1778 setupDrawTexture(texture->id);
1779 setupDrawPureColorUniforms();
1780 setupDrawColorFilterUniforms();
1781 setupDrawShaderUniforms();
1782 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1783
1784 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1785
1786 finishDrawTexture();
1787}
1788
Romain Guyf607bdc2010-09-10 19:20:06 -07001789// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001790#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1791#define kStdUnderline_Offset (1.0f / 9.0f)
1792#define kStdUnderline_Thickness (1.0f / 18.0f)
1793
1794void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1795 float x, float y, SkPaint* paint) {
1796 // Handle underline and strike-through
1797 uint32_t flags = paint->getFlags();
1798 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1799 float underlineWidth = length;
1800 // If length is > 0.0f, we already measured the text for the text alignment
1801 if (length <= 0.0f) {
1802 underlineWidth = paint->measureText(text, bytesCount);
1803 }
1804
1805 float offsetX = 0;
1806 switch (paint->getTextAlign()) {
1807 case SkPaint::kCenter_Align:
1808 offsetX = underlineWidth * 0.5f;
1809 break;
1810 case SkPaint::kRight_Align:
1811 offsetX = underlineWidth;
1812 break;
1813 default:
1814 break;
1815 }
1816
1817 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001818 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001819 // TODO: Support stroke width < 1.0f when we have AA lines
1820 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001821
Romain Guye20ecbd2010-09-22 19:49:04 -07001822 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001823 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001824
Romain Guyf6834472011-01-23 13:32:12 -08001825 int linesCount = 0;
1826 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1827 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1828
1829 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001830 float points[pointsCount];
1831 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001832
1833 if (flags & SkPaint::kUnderlineText_Flag) {
1834 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001835 points[currentPoint++] = left;
1836 points[currentPoint++] = top;
1837 points[currentPoint++] = left + underlineWidth;
1838 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001839 }
1840
1841 if (flags & SkPaint::kStrikeThruText_Flag) {
1842 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001843 points[currentPoint++] = left;
1844 points[currentPoint++] = top;
1845 points[currentPoint++] = left + underlineWidth;
1846 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001847 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001848
1849 SkPaint linesPaint(*paint);
1850 linesPaint.setStrokeWidth(strokeWidth);
1851
1852 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001853 }
1854 }
1855}
1856
Romain Guy026c5e162010-06-28 17:12:22 -07001857void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001858 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001859 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001860 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001861 color |= 0x00ffffff;
1862 }
1863
Romain Guy70ca14e2010-12-13 18:24:33 -08001864 setupDraw();
1865 setupDrawColor(color);
1866 setupDrawShader();
1867 setupDrawColorFilter();
1868 setupDrawBlending(mode);
1869 setupDrawProgram();
1870 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1871 setupDrawColorUniforms();
1872 setupDrawShaderUniforms(ignoreTransform);
1873 setupDrawColorFilterUniforms();
1874 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001875
Romain Guyc95c8d62010-09-17 15:31:32 -07001876 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1877}
1878
Romain Guy82ba8142010-07-09 13:25:56 -07001879void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001880 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001881 int alpha;
1882 SkXfermode::Mode mode;
1883 getAlphaAndMode(paint, &alpha, &mode);
1884
Romain Guy8164c2d2010-10-25 18:03:28 -07001885 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1886
Romain Guy6620c6d2010-12-06 18:07:02 -08001887 if (mSnapshot->transform->isPureTranslate()) {
1888 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1889 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1890
1891 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1892 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1893 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1894 } else {
1895 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1896 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1897 GL_TRIANGLE_STRIP, gMeshCount);
1898 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001899}
1900
Romain Guybd6b79b2010-06-26 00:13:53 -07001901void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001902 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1903 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001904 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001905}
1906
1907void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001908 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001909 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001910 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001911
Romain Guy746b7402010-10-26 16:27:31 -07001912 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001913 setupDrawWithTexture();
1914 setupDrawColor(alpha, alpha, alpha, alpha);
1915 setupDrawColorFilter();
1916 setupDrawBlending(blend, mode, swapSrcDst);
1917 setupDrawProgram();
1918 if (!dirty) {
1919 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001920 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001921 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001922 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001923 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001924 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001925 }
Romain Guy86568192010-12-14 15:55:39 -08001926 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001927 setupDrawColorFilterUniforms();
1928 setupDrawTexture(texture);
1929 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001930
Romain Guy6820ac82010-09-15 18:11:50 -07001931 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001932
1933 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001934}
1935
Romain Guya5aed0d2010-09-09 14:42:43 -07001936void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001937 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001938 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1939 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001940 if (mode < SkXfermode::kPlus_Mode) {
1941 if (!mCaches.blend) {
1942 glEnable(GL_BLEND);
1943 }
Romain Guy82ba8142010-07-09 13:25:56 -07001944
Romain Guyf607bdc2010-09-10 19:20:06 -07001945 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1946 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001947
Romain Guya5aed0d2010-09-09 14:42:43 -07001948 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1949 glBlendFunc(sourceMode, destMode);
1950 mCaches.lastSrcMode = sourceMode;
1951 mCaches.lastDstMode = destMode;
1952 }
1953 } else {
1954 // These blend modes are not supported by OpenGL directly and have
1955 // to be implemented using shaders. Since the shader will perform
1956 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001957 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001958 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001959 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001960 }
1961
1962 if (mCaches.blend) {
1963 glDisable(GL_BLEND);
1964 }
1965 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001966 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001967 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001968 glDisable(GL_BLEND);
1969 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001970 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001971}
1972
Romain Guy889f8d12010-07-29 14:37:42 -07001973bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001974 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001975 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001976 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001977 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001978 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001979 }
Romain Guy6926c722010-07-12 20:20:03 -07001980 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001981}
1982
Romain Guy026c5e162010-06-28 17:12:22 -07001983void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001984 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001985 TextureVertex::setUV(v++, u1, v1);
1986 TextureVertex::setUV(v++, u2, v1);
1987 TextureVertex::setUV(v++, u1, v2);
1988 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001989}
1990
Chet Haase5c13d892010-10-08 08:37:55 -07001991void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001992 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001993 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001994 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1995 if (!isMode) {
1996 // Assume SRC_OVER
1997 *mode = SkXfermode::kSrcOver_Mode;
1998 }
1999 } else {
2000 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002001 }
2002
2003 // Skia draws using the color's alpha channel if < 255
2004 // Otherwise, it uses the paint's alpha
2005 int color = paint->getColor();
2006 *alpha = (color >> 24) & 0xFF;
2007 if (*alpha == 255) {
2008 *alpha = paint->getAlpha();
2009 }
2010 } else {
2011 *mode = SkXfermode::kSrcOver_Mode;
2012 *alpha = 255;
2013 }
Romain Guy026c5e162010-06-28 17:12:22 -07002014}
2015
Romain Guya5aed0d2010-09-09 14:42:43 -07002016SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08002017 // In the future we should look at unifying the Porter-Duff modes and
2018 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002019 if (mode == NULL) {
2020 return SkXfermode::kSrcOver_Mode;
2021 }
2022 return mode->fMode;
2023}
2024
Romain Guy746b7402010-10-26 16:27:31 -07002025void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002026 bool bound = false;
2027 if (wrapS != texture->wrapS) {
2028 glBindTexture(GL_TEXTURE_2D, texture->id);
2029 bound = true;
2030 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2031 texture->wrapS = wrapS;
2032 }
2033 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002034 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002035 glBindTexture(GL_TEXTURE_2D, texture->id);
2036 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002037 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2038 texture->wrapT = wrapT;
2039 }
Romain Guya1db5742010-07-20 13:09:13 -07002040}
2041
Romain Guy9d5316e2010-06-24 19:30:36 -07002042}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002043}; // namespace android