blob: 5343a059ec26bb23593695d932b09c9bf83d58ec [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 Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080034#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080035#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070036
37namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044#define RAD_TO_DEG (180.0f / 3.14159265f)
45#define MIN_ANGLE 0.001f
46
Romain Guydbc26d22010-10-11 17:58:29 -070047// TODO: This should be set in properties
48#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49
Romain Guy9d5316e2010-06-24 19:30:36 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy889f8d12010-07-29 14:37:42 -070054/**
55 * Structure mapping Skia xfermodes to OpenGL blending factors.
56 */
57struct Blender {
58 SkXfermode::Mode mode;
59 GLenum src;
60 GLenum dst;
61}; // struct Blender
62
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
Romain Guyf504a2f2011-05-26 16:40:55 -070066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
Romain Guyb146b122010-12-15 17:06:45 -080067 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070078};
Romain Guye4d01122010-06-16 18:44:05 -070079
Romain Guy87a76572010-09-13 18:11:21 -070080// This array contains the swapped version of each SkXfermode. For instance
81// this array's SrcOver blending mode is actually DstOver. You can refer to
82// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070083static const Blender gBlendsSwap[] = {
Romain Guyf504a2f2011-05-26 16:40:55 -070084 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
Romain Guyb146b122010-12-15 17:06:45 -080085 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070096};
97
Romain Guy889f8d12010-07-29 14:37:42 -070098static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080099 GL_TEXTURE0,
100 GL_TEXTURE1,
101 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700102};
103
Romain Guyf6a11b82010-06-23 17:47:49 -0700104///////////////////////////////////////////////////////////////////////////////
105// Constructors/destructor
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guyfb8b7632010-08-23 21:05:08 -0700108OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700109 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700110 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700111 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700112
Romain Guyac670c02010-07-27 17:39:27 -0700113 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
114
Romain Guyae5575b2010-07-29 18:48:04 -0700115 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700116}
117
Romain Guy85bf02f2010-06-22 13:11:24 -0700118OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700119 // The context has already been destroyed at this point, do not call
120 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700121}
122
Romain Guyf6a11b82010-06-23 17:47:49 -0700123///////////////////////////////////////////////////////////////////////////////
124// Setup
125///////////////////////////////////////////////////////////////////////////////
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700128 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700129 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700130
131 mWidth = width;
132 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700133
134 mFirstSnapshot->height = height;
135 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700136
137 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700138}
139
Romain Guy6b7bd242010-10-06 19:49:23 -0700140void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800141 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
142}
143
144void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800145 mCaches.clearGarbage();
146
Romain Guy8aef54f2010-09-01 15:13:49 -0700147 mSnapshot = new Snapshot(mFirstSnapshot,
148 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800149 mSnapshot->fbo = getTargetFbo();
150
Romain Guy8fb95422010-08-17 18:38:51 -0700151 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700152
Romain Guyfb8b7632010-08-23 21:05:08 -0700153 glViewport(0, 0, mWidth, mHeight);
154
Romain Guyd90f23e2010-09-09 11:47:54 -0700155 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700156
Romain Guy7d7b5492011-01-24 16:33:45 -0800157 glEnable(GL_SCISSOR_TEST);
158 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
159 mSnapshot->setClip(left, top, right, bottom);
160
Romain Guy6b7bd242010-10-06 19:49:23 -0700161 if (!opaque) {
162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
163 glClear(GL_COLOR_BUFFER_BIT);
164 }
Romain Guybb9524b2010-06-22 18:56:38 -0700165}
166
Romain Guyb025b9c2010-09-16 14:16:48 -0700167void OpenGLRenderer::finish() {
168#if DEBUG_OPENGL
169 GLenum status = GL_NO_ERROR;
170 while ((status = glGetError()) != GL_NO_ERROR) {
171 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800172 switch (status) {
173 case GL_OUT_OF_MEMORY:
174 LOGE(" OpenGLRenderer is out of memory!");
175 break;
176 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700177 }
178#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800179#if DEBUG_MEMORY_USAGE
180 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800181#else
182 if (mCaches.getDebugLevel() & kDebugMemory) {
183 mCaches.dumpMemoryUsage();
184 }
Romain Guyc15008e2010-11-10 11:59:15 -0800185#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700186}
187
Romain Guy6c319ca2011-01-11 14:29:25 -0800188void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700189 if (mCaches.currentProgram) {
190 if (mCaches.currentProgram->isInUse()) {
191 mCaches.currentProgram->remove();
192 mCaches.currentProgram = NULL;
193 }
194 }
Romain Guy50c0f092010-10-19 11:42:22 -0700195 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700196}
197
Romain Guy6c319ca2011-01-11 14:29:25 -0800198void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700199 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700200
201 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700202 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700203
Romain Guyf607bdc2010-09-10 19:20:06 -0700204 glDisable(GL_DITHER);
205
Romain Guy42f3a4b2011-01-19 13:42:26 -0800206 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700207 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700208
Romain Guy50c0f092010-10-19 11:42:22 -0700209 mCaches.blend = true;
210 glEnable(GL_BLEND);
211 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
212 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700213}
214
Romain Guycabfcc12011-03-07 18:06:46 -0800215bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800216 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800217 if (mDirtyClip) {
218 setScissorFromClip();
219 }
Romain Guyd643bb52011-03-01 14:55:21 -0800220
Romain Guy80911b82011-03-16 15:30:12 -0700221 Rect clip(*mSnapshot->clipRect);
222 clip.snapToPixelBoundaries();
223
Romain Guyd643bb52011-03-01 14:55:21 -0800224#if RENDER_LAYERS_AS_REGIONS
225 // Since we don't know what the functor will draw, let's dirty
226 // tne entire clip region
227 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800228 dirtyLayerUnchecked(clip, getRegion());
229 }
230#endif
231
Romain Guy08aa2cb2011-03-17 11:06:57 -0700232 DrawGlInfo info;
233 info.clipLeft = clip.left;
234 info.clipTop = clip.top;
235 info.clipRight = clip.right;
236 info.clipBottom = clip.bottom;
237 info.isLayer = hasLayer();
238 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700239
Romain Guy08aa2cb2011-03-17 11:06:57 -0700240 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800241
242 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700243 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800244 dirty.unionWith(localDirty);
245 }
246
Chet Haasedaf98e92011-01-10 14:10:36 -0800247 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800248 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800249}
250
Romain Guyf6a11b82010-06-23 17:47:49 -0700251///////////////////////////////////////////////////////////////////////////////
252// State management
253///////////////////////////////////////////////////////////////////////////////
254
Romain Guybb9524b2010-06-22 18:56:38 -0700255int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700256 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700257}
258
259int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700260 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700261}
262
263void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700264 if (mSaveCount > 1) {
265 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700266 }
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
269void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700270 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700271
Romain Guy8fb95422010-08-17 18:38:51 -0700272 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700273 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
Romain Guy8aef54f2010-09-01 15:13:49 -0700277int OpenGLRenderer::saveSnapshot(int flags) {
278 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700279 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
282bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700283 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700284 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700285 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700286
Romain Guybd6b79b2010-06-26 00:13:53 -0700287 sp<Snapshot> current = mSnapshot;
288 sp<Snapshot> previous = mSnapshot->previous;
289
Romain Guyeb993562010-10-05 18:14:38 -0700290 if (restoreOrtho) {
291 Rect& r = previous->viewport;
292 glViewport(r.left, r.top, r.right, r.bottom);
293 mOrthoMatrix.load(current->orthoMatrix);
294 }
295
Romain Guy8b55f372010-08-18 17:10:07 -0700296 mSaveCount--;
297 mSnapshot = previous;
298
Romain Guy2542d192010-08-18 11:47:12 -0700299 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700300 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700301 }
Romain Guy2542d192010-08-18 11:47:12 -0700302
Romain Guy5ec99242010-11-03 16:19:08 -0700303 if (restoreLayer) {
304 composeLayer(current, previous);
305 }
306
Romain Guy2542d192010-08-18 11:47:12 -0700307 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700308}
309
Romain Guyf6a11b82010-06-23 17:47:49 -0700310///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700311// Layers
312///////////////////////////////////////////////////////////////////////////////
313
314int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700315 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700316 const GLuint previousFbo = mSnapshot->fbo;
317 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700318
Romain Guyaf636eb2010-12-09 17:47:21 -0800319 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700320 int alpha = 255;
321 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700322
Romain Guye45362c2010-11-03 19:58:32 -0700323 if (p) {
324 alpha = p->getAlpha();
325 if (!mCaches.extensions.hasFramebufferFetch()) {
326 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
327 if (!isMode) {
328 // Assume SRC_OVER
329 mode = SkXfermode::kSrcOver_Mode;
330 }
331 } else {
332 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700333 }
334 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700335 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700336 }
Romain Guyd55a8612010-06-28 17:42:46 -0700337
Romain Guydbc26d22010-10-11 17:58:29 -0700338 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
339 }
Romain Guyd55a8612010-06-28 17:42:46 -0700340
341 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700342}
343
344int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
345 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700346 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700347 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700348 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700349 SkPaint paint;
350 paint.setAlpha(alpha);
351 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700352 }
Romain Guyd55a8612010-06-28 17:42:46 -0700353}
Romain Guybd6b79b2010-06-26 00:13:53 -0700354
Romain Guy1c740bc2010-09-13 18:00:09 -0700355/**
356 * Layers are viewed by Skia are slightly different than layers in image editing
357 * programs (for instance.) When a layer is created, previously created layers
358 * and the frame buffer still receive every drawing command. For instance, if a
359 * layer is created and a shape intersecting the bounds of the layers and the
360 * framebuffer is draw, the shape will be drawn on both (unless the layer was
361 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
362 *
363 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
364 * texture. Unfortunately, this is inefficient as it requires every primitive to
365 * be drawn n + 1 times, where n is the number of active layers. In practice this
366 * means, for every primitive:
367 * - Switch active frame buffer
368 * - Change viewport, clip and projection matrix
369 * - Issue the drawing
370 *
371 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700372 * To avoid this, layers are implemented in a different way here, at least in the
373 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
374 * is set. When this flag is set we can redirect all drawing operations into a
375 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700376 *
377 * This implementation relies on the frame buffer being at least RGBA 8888. When
378 * a layer is created, only a texture is created, not an FBO. The content of the
379 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700380 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700381 * buffer and drawing continues as normal. This technique therefore treats the
382 * frame buffer as a scratch buffer for the layers.
383 *
384 * To compose the layers back onto the frame buffer, each layer texture
385 * (containing the original frame buffer data) is drawn as a simple quad over
386 * the frame buffer. The trick is that the quad is set as the composition
387 * destination in the blending equation, and the frame buffer becomes the source
388 * of the composition.
389 *
390 * Drawing layers with an alpha value requires an extra step before composition.
391 * An empty quad is drawn over the layer's region in the frame buffer. This quad
392 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
393 * quad is used to multiply the colors in the frame buffer. This is achieved by
394 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
395 * GL_ZERO, GL_SRC_ALPHA.
396 *
397 * Because glCopyTexImage2D() can be slow, an alternative implementation might
398 * be use to draw a single clipped layer. The implementation described above
399 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700400 *
401 * (1) The frame buffer is actually not cleared right away. To allow the GPU
402 * to potentially optimize series of calls to glCopyTexImage2D, the frame
403 * buffer is left untouched until the first drawing operation. Only when
404 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700405 */
Romain Guyd55a8612010-06-28 17:42:46 -0700406bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700407 float right, float bottom, int alpha, SkXfermode::Mode mode,
408 int flags, GLuint previousFbo) {
409 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700410 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700411
Romain Guyeb993562010-10-05 18:14:38 -0700412 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
413
Romain Guyf607bdc2010-09-10 19:20:06 -0700414 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700415 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700416 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700417 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700420 if (bounds.intersect(*snapshot->clipRect)) {
421 // We cannot work with sub-pixels in this case
422 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700423
Romain Guyad37cd32011-03-15 11:12:25 -0700424 // When the layer is not an FBO, we may use glCopyTexImage so we
425 // need to make sure the layer does not extend outside the bounds
426 // of the framebuffer
427 if (!bounds.intersect(snapshot->previous->viewport)) {
428 bounds.setEmpty();
429 }
430 } else {
431 bounds.setEmpty();
432 }
Romain Guyeb993562010-10-05 18:14:38 -0700433 }
Romain Guybf434112010-09-16 14:40:17 -0700434
Romain Guy746b7402010-10-26 16:27:31 -0700435 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
436 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800437 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700438 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700439 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700440 }
441
442 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800443 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700444 return false;
445 }
Romain Guyf18fd992010-07-08 11:45:51 -0700446
Romain Guy5b3b3522010-10-27 18:57:51 -0700447 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700448 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700449 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700450 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700451 }
452
Romain Guydda57022010-07-06 11:39:32 -0700453 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700454 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700455 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700456 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
457 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800458 layer->colorFilter = mColorFilter;
Romain Guydda57022010-07-06 11:39:32 -0700459
Romain Guy8fb95422010-08-17 18:38:51 -0700460 // Save the layer in the snapshot
461 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700462 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700463
Romain Guyeb993562010-10-05 18:14:38 -0700464 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700465 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700466 } else {
467 // Copy the framebuffer into the layer
468 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800469 if (!bounds.isEmpty()) {
470 if (layer->empty) {
471 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
472 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
473 layer->empty = false;
474 } else {
475 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
476 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
477 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700478
Romain Guy54be1cd2011-06-13 19:04:27 -0700479 // Enqueue the buffer coordinates to clear the corresponding region later
480 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700481 }
Romain Guyeb993562010-10-05 18:14:38 -0700482 }
Romain Guyf86ef572010-07-01 11:05:42 -0700483
Romain Guyd55a8612010-06-28 17:42:46 -0700484 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700485}
486
Romain Guy5b3b3522010-10-27 18:57:51 -0700487bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
488 GLuint previousFbo) {
489 layer->fbo = mCaches.fboCache.get();
490
491#if RENDER_LAYERS_AS_REGIONS
492 snapshot->region = &snapshot->layer->region;
493 snapshot->flags |= Snapshot::kFlagFboTarget;
494#endif
495
496 Rect clip(bounds);
497 snapshot->transform->mapRect(clip);
498 clip.intersect(*snapshot->clipRect);
499 clip.snapToPixelBoundaries();
500 clip.intersect(snapshot->previous->viewport);
501
502 mat4 inverse;
503 inverse.loadInverse(*mSnapshot->transform);
504
505 inverse.mapRect(clip);
506 clip.snapToPixelBoundaries();
507 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700508 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700509
510 snapshot->flags |= Snapshot::kFlagIsFboLayer;
511 snapshot->fbo = layer->fbo;
512 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700513 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
514 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
515 snapshot->height = bounds.getHeight();
516 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
517 snapshot->orthoMatrix.load(mOrthoMatrix);
518
519 // Bind texture to FBO
520 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
521 glBindTexture(GL_TEXTURE_2D, layer->texture);
522
523 // Initialize the texture if needed
524 if (layer->empty) {
525 layer->empty = false;
526 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
527 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
528 }
529
530 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
531 layer->texture, 0);
532
533#if DEBUG_LAYERS_AS_REGIONS
534 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
535 if (status != GL_FRAMEBUFFER_COMPLETE) {
536 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
537
538 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
539 glDeleteTextures(1, &layer->texture);
540 mCaches.fboCache.put(layer->fbo);
541
542 delete layer;
543
544 return false;
545 }
546#endif
547
548 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
549 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
550 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
551 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
552 glClear(GL_COLOR_BUFFER_BIT);
553
554 dirtyClip();
555
556 // Change the ortho projection
557 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
558 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
559
560 return true;
561}
562
Romain Guy1c740bc2010-09-13 18:00:09 -0700563/**
564 * Read the documentation of createLayer() before doing anything in this method.
565 */
Romain Guy1d83e192010-08-17 11:37:00 -0700566void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
567 if (!current->layer) {
568 LOGE("Attempting to compose a layer that does not exist");
569 return;
570 }
571
Romain Guy5b3b3522010-10-27 18:57:51 -0700572 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700573
574 if (fboLayer) {
575 // Unbind current FBO and restore previous one
576 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
577 }
578
Romain Guy1d83e192010-08-17 11:37:00 -0700579 Layer* layer = current->layer;
580 const Rect& rect = layer->layer;
581
Romain Guyeb993562010-10-05 18:14:38 -0700582 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700583 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700584 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700585 // Required below, composeLayerRect() will divide by 255
586 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700587 }
588
Romain Guy03750a02010-10-18 14:06:08 -0700589 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700590
Romain Guy746b7402010-10-26 16:27:31 -0700591 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700592
Romain Guy5b3b3522010-10-27 18:57:51 -0700593 // When the layer is stored in an FBO, we can save a bit of fillrate by
594 // drawing only the dirty region
595 if (fboLayer) {
596 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800597 if (layer->colorFilter) {
598 setupColorFilter(layer->colorFilter);
599 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700600 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800601 if (layer->colorFilter) {
602 resetColorFilter();
603 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800605 if (!rect.isEmpty()) {
606 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
607 composeLayerRect(layer, rect, true);
608 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 }
Romain Guy8b55f372010-08-18 17:10:07 -0700610
Romain Guyeb993562010-10-05 18:14:38 -0700611 if (fboLayer) {
612 // Detach the texture from the FBO
613 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
614 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
615 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
616
617 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
618 mCaches.fboCache.put(current->fbo);
619 }
620
Romain Guy746b7402010-10-26 16:27:31 -0700621 dirtyClip();
622
Romain Guyeb993562010-10-05 18:14:38 -0700623 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700624 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700625 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700626 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700627 delete layer;
628 }
629}
630
Romain Guyaa6c24c2011-04-28 18:40:04 -0700631void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
632 float alpha = layer->alpha / 255.0f;
633
634 setupDraw();
Romain Guy8f0095c2011-05-02 17:24:22 -0700635 if (layer->renderTarget == GL_TEXTURE_2D) {
636 setupDrawWithTexture();
637 } else {
638 setupDrawWithExternalTexture();
639 }
640 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700641 setupDrawColor(alpha, alpha, alpha, alpha);
642 setupDrawColorFilter();
643 setupDrawBlending(layer->blend, layer->mode);
644 setupDrawProgram();
645 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
646 setupDrawPureColorUniforms();
647 setupDrawColorFilterUniforms();
Romain Guy8f0095c2011-05-02 17:24:22 -0700648 if (layer->renderTarget == GL_TEXTURE_2D) {
649 setupDrawTexture(layer->texture);
650 } else {
651 setupDrawExternalTexture(layer->texture);
652 }
653 setupDrawTextureTransformUniforms(layer->texTransform);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700654 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
655
656 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
657
658 finishDrawTexture();
659}
660
Romain Guy5b3b3522010-10-27 18:57:51 -0700661void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700662 if (!layer->isTextureLayer) {
663 const Rect& texCoords = layer->texCoords;
664 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
665 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700666
Romain Guyaa6c24c2011-04-28 18:40:04 -0700667 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
668 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
669 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
Romain Guy5b3b3522010-10-27 18:57:51 -0700670
Romain Guyaa6c24c2011-04-28 18:40:04 -0700671 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
672 } else {
673 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
674 drawTextureLayer(layer, rect);
675 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
676 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700677}
678
679void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
680#if RENDER_LAYERS_AS_REGIONS
681 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700682 layer->setRegionAsRect();
683
Romain Guy40667672011-03-18 14:34:03 -0700684 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700685
Romain Guy5b3b3522010-10-27 18:57:51 -0700686 layer->region.clear();
687 return;
688 }
689
690 if (!layer->region.isEmpty()) {
691 size_t count;
692 const android::Rect* rects = layer->region.getArray(&count);
693
Romain Guy5b3b3522010-10-27 18:57:51 -0700694 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700695 const float texX = 1.0f / float(layer->width);
696 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800697 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700698
699 TextureVertex* mesh = mCaches.getRegionMesh();
700 GLsizei numQuads = 0;
701
Romain Guy7230a742011-01-10 22:26:16 -0800702 setupDraw();
703 setupDrawWithTexture();
704 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800705 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800706 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
707 setupDrawProgram();
708 setupDrawDirtyRegionsDisabled();
709 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800710 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800711 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800712 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800713 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700714
715 for (size_t i = 0; i < count; i++) {
716 const android::Rect* r = &rects[i];
717
718 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800719 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700720 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800721 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700722
723 // TODO: Reject quads outside of the clip
724 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
725 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
726 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
727 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
728
729 numQuads++;
730
731 if (numQuads >= REGION_MESH_QUAD_COUNT) {
732 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
733 numQuads = 0;
734 mesh = mCaches.getRegionMesh();
735 }
736 }
737
738 if (numQuads > 0) {
739 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
740 }
741
742 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800743 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700744
745#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800746 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700747#endif
748
749 layer->region.clear();
750 }
751#else
752 composeLayerRect(layer, rect);
753#endif
754}
755
Romain Guy3a3133d2011-02-01 22:59:58 -0800756void OpenGLRenderer::drawRegionRects(const Region& region) {
757#if DEBUG_LAYERS_AS_REGIONS
758 size_t count;
759 const android::Rect* rects = region.getArray(&count);
760
761 uint32_t colors[] = {
762 0x7fff0000, 0x7f00ff00,
763 0x7f0000ff, 0x7fff00ff,
764 };
765
766 int offset = 0;
767 int32_t top = rects[0].top;
768
769 for (size_t i = 0; i < count; i++) {
770 if (top != rects[i].top) {
771 offset ^= 0x2;
772 top = rects[i].top;
773 }
774
775 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
776 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
777 SkXfermode::kSrcOver_Mode);
778 }
779#endif
780}
781
Romain Guy5b3b3522010-10-27 18:57:51 -0700782void OpenGLRenderer::dirtyLayer(const float left, const float top,
783 const float right, const float bottom, const mat4 transform) {
784#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800785 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700786 Rect bounds(left, top, right, bottom);
787 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800788 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700789 }
790#endif
791}
792
793void OpenGLRenderer::dirtyLayer(const float left, const float top,
794 const float right, const float bottom) {
795#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800796 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700797 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800798 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800799 }
800#endif
801}
802
803void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
804#if RENDER_LAYERS_AS_REGIONS
805 if (bounds.intersect(*mSnapshot->clipRect)) {
806 bounds.snapToPixelBoundaries();
807 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
808 if (!dirty.isEmpty()) {
809 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700810 }
811 }
812#endif
813}
814
Romain Guy54be1cd2011-06-13 19:04:27 -0700815void OpenGLRenderer::clearLayerRegions() {
816 const size_t count = mLayers.size();
817 if (count == 0) return;
818
819 if (!mSnapshot->isIgnored()) {
820 // Doing several glScissor/glClear here can negatively impact
821 // GPUs with a tiler architecture, instead we draw quads with
822 // the Clear blending mode
823
824 // The list contains bounds that have already been clipped
825 // against their initial clip rect, and the current clip
826 // is likely different so we need to disable clipping here
827 glDisable(GL_SCISSOR_TEST);
828
829 Vertex mesh[count * 6];
830 Vertex* vertex = mesh;
831
832 for (uint32_t i = 0; i < count; i++) {
833 Rect* bounds = mLayers.itemAt(i);
834
835 Vertex::set(vertex++, bounds->left, bounds->bottom);
836 Vertex::set(vertex++, bounds->left, bounds->top);
837 Vertex::set(vertex++, bounds->right, bounds->top);
838 Vertex::set(vertex++, bounds->left, bounds->bottom);
839 Vertex::set(vertex++, bounds->right, bounds->top);
840 Vertex::set(vertex++, bounds->right, bounds->bottom);
841
842 delete bounds;
843 }
844
845 setupDraw(false);
846 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
847 setupDrawBlending(true, SkXfermode::kClear_Mode);
848 setupDrawProgram();
849 setupDrawPureColorUniforms();
850 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
851
852 mCaches.unbindMeshBuffer();
853 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
854 gVertexStride, &mesh[0].position[0]);
855 glDrawArrays(GL_TRIANGLES, 0, count * 6);
856
857 glEnable(GL_SCISSOR_TEST);
858 } else {
859 for (uint32_t i = 0; i < count; i++) {
860 delete mLayers.itemAt(i);
861 }
862 }
863
864 mLayers.clear();
865}
866
Romain Guybd6b79b2010-06-26 00:13:53 -0700867///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700868// Transforms
869///////////////////////////////////////////////////////////////////////////////
870
871void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700872 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700873}
874
875void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700876 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700877}
878
879void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700880 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700881}
882
Romain Guy807daf72011-01-18 11:19:19 -0800883void OpenGLRenderer::skew(float sx, float sy) {
884 mSnapshot->transform->skew(sx, sy);
885}
886
Romain Guyf6a11b82010-06-23 17:47:49 -0700887void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700888 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700889}
890
Romain Guy41030da2010-10-13 13:40:37 -0700891const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700892 if (mSnapshot->fbo != 0) {
893 return &mSnapshot->transform->data[0];
894 }
895 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700896}
897
Romain Guyf6a11b82010-06-23 17:47:49 -0700898void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700899 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700900}
901
902void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700903 SkMatrix transform;
904 mSnapshot->transform->copyTo(transform);
905 transform.preConcat(*matrix);
906 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700907}
908
909///////////////////////////////////////////////////////////////////////////////
910// Clipping
911///////////////////////////////////////////////////////////////////////////////
912
Romain Guybb9524b2010-06-22 18:56:38 -0700913void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700914 Rect clip(*mSnapshot->clipRect);
915 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700916 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700917 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700918}
919
920const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700921 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700922}
923
Romain Guyc7d53492010-06-25 13:41:57 -0700924bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800925 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700926 return true;
927 }
928
Romain Guy1d83e192010-08-17 11:37:00 -0700929 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700930 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700931 r.snapToPixelBoundaries();
932
933 Rect clipRect(*mSnapshot->clipRect);
934 clipRect.snapToPixelBoundaries();
935
936 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700937}
938
Romain Guy079ba2c2010-07-16 14:12:24 -0700939bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
940 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700941 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700942 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700943 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700944 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700945}
946
Romain Guyf6a11b82010-06-23 17:47:49 -0700947///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800948// Drawing commands
949///////////////////////////////////////////////////////////////////////////////
950
Romain Guy54be1cd2011-06-13 19:04:27 -0700951void OpenGLRenderer::setupDraw(bool clear) {
952 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -0800953 if (mDirtyClip) {
954 setScissorFromClip();
955 }
956 mDescription.reset();
957 mSetShaderColor = false;
958 mColorSet = false;
959 mColorA = mColorR = mColorG = mColorB = 0.0f;
960 mTextureUnit = 0;
961 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800962 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800963}
964
965void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
966 mDescription.hasTexture = true;
967 mDescription.hasAlpha8Texture = isAlpha8;
968}
969
Romain Guyaa6c24c2011-04-28 18:40:04 -0700970void OpenGLRenderer::setupDrawWithExternalTexture() {
971 mDescription.hasExternalTexture = true;
972}
973
Chet Haase5b0200b2011-04-13 17:58:08 -0700974void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -0700975 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -0700976}
977
Romain Guyed6fcb02011-03-21 13:11:28 -0700978void OpenGLRenderer::setupDrawPoint(float pointSize) {
979 mDescription.isPoint = true;
980 mDescription.pointSize = pointSize;
981}
982
Romain Guy70ca14e2010-12-13 18:24:33 -0800983void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800984 setupDrawColor(color, (color >> 24) & 0xFF);
985}
986
987void OpenGLRenderer::setupDrawColor(int color, int alpha) {
988 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700989 // Second divide of a by 255 is an optimization, allowing us to simply multiply
990 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -0800991 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800992 mColorR = a * ((color >> 16) & 0xFF);
993 mColorG = a * ((color >> 8) & 0xFF);
994 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800995 mColorSet = true;
996 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
997}
998
Romain Guy86568192010-12-14 15:55:39 -0800999void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1000 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001001 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1002 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001003 const float a = mColorA / 255.0f;
1004 mColorR = a * ((color >> 16) & 0xFF);
1005 mColorG = a * ((color >> 8) & 0xFF);
1006 mColorB = a * ((color ) & 0xFF);
1007 mColorSet = true;
1008 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1009}
1010
Romain Guy70ca14e2010-12-13 18:24:33 -08001011void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1012 mColorA = a;
1013 mColorR = r;
1014 mColorG = g;
1015 mColorB = b;
1016 mColorSet = true;
1017 mSetShaderColor = mDescription.setColor(r, g, b, a);
1018}
1019
Romain Guy86568192010-12-14 15:55:39 -08001020void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1021 mColorA = a;
1022 mColorR = r;
1023 mColorG = g;
1024 mColorB = b;
1025 mColorSet = true;
1026 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1027}
1028
Romain Guy70ca14e2010-12-13 18:24:33 -08001029void OpenGLRenderer::setupDrawShader() {
1030 if (mShader) {
1031 mShader->describe(mDescription, mCaches.extensions);
1032 }
1033}
1034
1035void OpenGLRenderer::setupDrawColorFilter() {
1036 if (mColorFilter) {
1037 mColorFilter->describe(mDescription, mCaches.extensions);
1038 }
1039}
1040
Romain Guyf09ef512011-05-27 11:43:46 -07001041void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1042 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1043 mColorA = 1.0f;
1044 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001045 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001046 }
1047}
1048
Romain Guy70ca14e2010-12-13 18:24:33 -08001049void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001050 // When the blending mode is kClear_Mode, we need to use a modulate color
1051 // argb=1,0,0,0
1052 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001053 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1054 mDescription, swapSrcDst);
1055}
1056
1057void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001058 // When the blending mode is kClear_Mode, we need to use a modulate color
1059 // argb=1,0,0,0
1060 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001061 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1062 mDescription, swapSrcDst);
1063}
1064
1065void OpenGLRenderer::setupDrawProgram() {
1066 useProgram(mCaches.programCache.get(mDescription));
1067}
1068
1069void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1070 mTrackDirtyRegions = false;
1071}
1072
1073void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1074 bool ignoreTransform) {
1075 mModelView.loadTranslate(left, top, 0.0f);
1076 if (!ignoreTransform) {
1077 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1078 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1079 } else {
1080 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1081 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1082 }
1083}
1084
Chet Haase8a5cc922011-04-26 07:28:09 -07001085void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1086 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001087}
1088
Romain Guy70ca14e2010-12-13 18:24:33 -08001089void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1090 bool ignoreTransform, bool ignoreModelView) {
1091 if (!ignoreModelView) {
1092 mModelView.loadTranslate(left, top, 0.0f);
1093 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001094 } else {
1095 mModelView.loadIdentity();
1096 }
Romain Guy86568192010-12-14 15:55:39 -08001097 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1098 if (!ignoreTransform) {
1099 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1100 if (mTrackDirtyRegions && dirty) {
1101 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1102 }
1103 } else {
1104 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1105 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1106 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001107}
1108
Romain Guyed6fcb02011-03-21 13:11:28 -07001109void OpenGLRenderer::setupDrawPointUniforms() {
1110 int slot = mCaches.currentProgram->getUniform("pointSize");
1111 glUniform1f(slot, mDescription.pointSize);
1112}
1113
Romain Guy70ca14e2010-12-13 18:24:33 -08001114void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001115 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001116 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1117 }
1118}
1119
Romain Guy86568192010-12-14 15:55:39 -08001120void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001121 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001122 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001123 }
1124}
1125
Romain Guy70ca14e2010-12-13 18:24:33 -08001126void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1127 if (mShader) {
1128 if (ignoreTransform) {
1129 mModelView.loadInverse(*mSnapshot->transform);
1130 }
1131 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1132 }
1133}
1134
Romain Guy8d0d4782010-12-14 20:13:35 -08001135void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1136 if (mShader) {
1137 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1138 }
1139}
1140
Romain Guy70ca14e2010-12-13 18:24:33 -08001141void OpenGLRenderer::setupDrawColorFilterUniforms() {
1142 if (mColorFilter) {
1143 mColorFilter->setupProgram(mCaches.currentProgram);
1144 }
1145}
1146
1147void OpenGLRenderer::setupDrawSimpleMesh() {
1148 mCaches.bindMeshBuffer();
1149 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1150 gMeshStride, 0);
1151}
1152
1153void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1154 bindTexture(texture);
1155 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1156
1157 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1158 glEnableVertexAttribArray(mTexCoordsSlot);
1159}
1160
Romain Guyaa6c24c2011-04-28 18:40:04 -07001161void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1162 bindExternalTexture(texture);
1163 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1164
1165 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1166 glEnableVertexAttribArray(mTexCoordsSlot);
1167}
1168
Romain Guy8f0095c2011-05-02 17:24:22 -07001169void OpenGLRenderer::setupDrawTextureTransform() {
1170 mDescription.hasTextureTransform = true;
1171}
1172
1173void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001174 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1175 GL_FALSE, &transform.data[0]);
1176}
1177
Romain Guy70ca14e2010-12-13 18:24:33 -08001178void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1179 if (!vertices) {
1180 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1181 } else {
1182 mCaches.unbindMeshBuffer();
1183 }
1184 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1185 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001186 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001187 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1188 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001189}
1190
Chet Haase5b0200b2011-04-13 17:58:08 -07001191void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1192 mCaches.unbindMeshBuffer();
1193 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1194 gVertexStride, vertices);
1195}
1196
1197/**
1198 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001199 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1200 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1201 * attributes (one per vertex) are values from zero to one that tells the fragment
1202 * shader where the fragment is in relation to the line width/length overall; these values are
1203 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1204 * region of the line.
1205 * Note that we only pass down the width values in this setup function. The length coordinates
1206 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001207 */
Chet Haase99585ad2011-05-02 15:00:16 -07001208void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001209 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001210 mCaches.unbindMeshBuffer();
1211 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001212 gAAVertexStride, vertices);
1213 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1214 glEnableVertexAttribArray(widthSlot);
1215 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1216 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1217 glEnableVertexAttribArray(lengthSlot);
1218 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001219 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001220 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001221 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001222 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001223 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001224}
1225
Romain Guy70ca14e2010-12-13 18:24:33 -08001226void OpenGLRenderer::finishDrawTexture() {
1227 glDisableVertexAttribArray(mTexCoordsSlot);
1228}
1229
1230///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001231// Drawing
1232///////////////////////////////////////////////////////////////////////////////
1233
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001234bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1235 Rect& dirty, uint32_t level) {
1236 if (quickReject(0.0f, 0.0f, width, height)) {
1237 return false;
1238 }
1239
Romain Guy0fe478e2010-11-08 12:08:41 -08001240 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1241 // will be performed by the display list itself
1242 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001243 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001244 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001245
Chet Haasedaf98e92011-01-10 14:10:36 -08001246 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001247}
1248
Chet Haaseed30fd82011-04-22 16:18:45 -07001249void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1250 if (displayList) {
1251 displayList->output(*this, level);
1252 }
1253}
1254
Romain Guya168d732011-03-18 16:50:13 -07001255void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1256 int alpha;
1257 SkXfermode::Mode mode;
1258 getAlphaAndMode(paint, &alpha, &mode);
1259
1260 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1261
1262 float x = left;
1263 float y = top;
1264
1265 bool ignoreTransform = false;
1266 if (mSnapshot->transform->isPureTranslate()) {
1267 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1268 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1269 ignoreTransform = true;
1270 }
1271
1272 setupDraw();
1273 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001274 if (paint) {
1275 setupDrawAlpha8Color(paint->getColor(), alpha);
1276 }
Romain Guya168d732011-03-18 16:50:13 -07001277 setupDrawColorFilter();
1278 setupDrawShader();
1279 setupDrawBlending(true, mode);
1280 setupDrawProgram();
1281 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
1282 setupDrawTexture(texture->id);
1283 setupDrawPureColorUniforms();
1284 setupDrawColorFilterUniforms();
1285 setupDrawShaderUniforms();
1286 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1287
1288 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1289
1290 finishDrawTexture();
1291}
1292
Chet Haase5c13d892010-10-08 08:37:55 -07001293void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001294 const float right = left + bitmap->width();
1295 const float bottom = top + bitmap->height();
1296
1297 if (quickReject(left, top, right, bottom)) {
1298 return;
1299 }
1300
Romain Guy86568192010-12-14 15:55:39 -08001301 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001302 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001303 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001304 const AutoTexture autoCleanup(texture);
1305
Romain Guya168d732011-03-18 16:50:13 -07001306 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1307 drawAlphaBitmap(texture, left, top, paint);
1308 } else {
1309 drawTextureRect(left, top, right, bottom, texture, paint);
1310 }
Romain Guyce0537b2010-06-29 21:05:21 -07001311}
1312
Chet Haase5c13d892010-10-08 08:37:55 -07001313void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001314 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1315 const mat4 transform(*matrix);
1316 transform.mapRect(r);
1317
Romain Guy6926c722010-07-12 20:20:03 -07001318 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1319 return;
1320 }
1321
Romain Guy86568192010-12-14 15:55:39 -08001322 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001323 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001324 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001325 const AutoTexture autoCleanup(texture);
1326
Romain Guy5b3b3522010-10-27 18:57:51 -07001327 // This could be done in a cheaper way, all we need is pass the matrix
1328 // to the vertex shader. The save/restore is a bit overkill.
1329 save(SkCanvas::kMatrix_SaveFlag);
1330 concatMatrix(matrix);
1331 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1332 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001333}
1334
Romain Guy5a7b4662011-01-20 19:09:30 -08001335void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1336 float* vertices, int* colors, SkPaint* paint) {
1337 // TODO: Do a quickReject
1338 if (!vertices || mSnapshot->isIgnored()) {
1339 return;
1340 }
1341
1342 glActiveTexture(gTextureUnits[0]);
1343 Texture* texture = mCaches.textureCache.get(bitmap);
1344 if (!texture) return;
1345 const AutoTexture autoCleanup(texture);
1346 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1347
1348 int alpha;
1349 SkXfermode::Mode mode;
1350 getAlphaAndMode(paint, &alpha, &mode);
1351
Romain Guy5a7b4662011-01-20 19:09:30 -08001352 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001353
Romain Guyb18d2d02011-02-10 15:52:54 -08001354 float left = FLT_MAX;
1355 float top = FLT_MAX;
1356 float right = FLT_MIN;
1357 float bottom = FLT_MIN;
1358
1359#if RENDER_LAYERS_AS_REGIONS
1360 bool hasActiveLayer = hasLayer();
1361#else
1362 bool hasActiveLayer = false;
1363#endif
1364
Romain Guya566b7c2011-01-23 16:36:11 -08001365 // TODO: Support the colors array
1366 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001367 TextureVertex* vertex = mesh;
1368 for (int32_t y = 0; y < meshHeight; y++) {
1369 for (int32_t x = 0; x < meshWidth; x++) {
1370 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1371
1372 float u1 = float(x) / meshWidth;
1373 float u2 = float(x + 1) / meshWidth;
1374 float v1 = float(y) / meshHeight;
1375 float v2 = float(y + 1) / meshHeight;
1376
1377 int ax = i + (meshWidth + 1) * 2;
1378 int ay = ax + 1;
1379 int bx = i;
1380 int by = bx + 1;
1381 int cx = i + 2;
1382 int cy = cx + 1;
1383 int dx = i + (meshWidth + 1) * 2 + 2;
1384 int dy = dx + 1;
1385
1386 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1387 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1388 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1389
1390 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1391 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1392 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001393
1394#if RENDER_LAYERS_AS_REGIONS
1395 if (hasActiveLayer) {
1396 // TODO: This could be optimized to avoid unnecessary ops
1397 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1398 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1399 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1400 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1401 }
1402#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001403 }
1404 }
1405
Romain Guyb18d2d02011-02-10 15:52:54 -08001406#if RENDER_LAYERS_AS_REGIONS
1407 if (hasActiveLayer) {
1408 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1409 }
1410#endif
1411
Romain Guy5a7b4662011-01-20 19:09:30 -08001412 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1413 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001414 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001415}
1416
Romain Guy8ba548f2010-06-30 19:21:21 -07001417void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1418 float srcLeft, float srcTop, float srcRight, float srcBottom,
1419 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001420 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001421 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1422 return;
1423 }
1424
Romain Guy746b7402010-10-26 16:27:31 -07001425 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001426 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001427 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001428 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001429 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001430
Romain Guy8ba548f2010-06-30 19:21:21 -07001431 const float width = texture->width;
1432 const float height = texture->height;
1433
Romain Guy1e59f9d2011-05-26 18:39:34 -07001434 const float u1 = (srcLeft + 0.5f) / width;
1435 const float v1 = (srcTop + 0.5f) / height;
1436 const float u2 = (srcRight - 0.5f) / width;
1437 const float v2 = (srcBottom - 0.5f) / height;
Romain Guy8ba548f2010-06-30 19:21:21 -07001438
Romain Guy03750a02010-10-18 14:06:08 -07001439 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001440 resetDrawTextureTexCoords(u1, v1, u2, v2);
1441
Romain Guy03750a02010-10-18 14:06:08 -07001442 int alpha;
1443 SkXfermode::Mode mode;
1444 getAlphaAndMode(paint, &alpha, &mode);
1445
Romain Guy6620c6d2010-12-06 18:07:02 -08001446 if (mSnapshot->transform->isPureTranslate()) {
1447 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1448 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1449
1450 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1451 texture->id, alpha / 255.0f, mode, texture->blend,
1452 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1453 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1454 } else {
1455 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1456 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1457 GL_TRIANGLE_STRIP, gMeshCount);
1458 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001459
1460 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1461}
1462
Romain Guy4aa90572010-09-26 18:40:37 -07001463void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001464 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001465 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001466 if (quickReject(left, top, right, bottom)) {
1467 return;
1468 }
1469
Romain Guy746b7402010-10-26 16:27:31 -07001470 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001471 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001472 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001473 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001474 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001475
1476 int alpha;
1477 SkXfermode::Mode mode;
1478 getAlphaAndMode(paint, &alpha, &mode);
1479
Romain Guy2728f962010-10-08 18:36:15 -07001480 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001481 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001482
Romain Guya5ef39a2010-12-03 16:48:20 -08001483 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001484 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001485#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001486 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001487 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001488 const float offsetX = left + mSnapshot->transform->getTranslateX();
1489 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001490 const size_t count = mesh->quads.size();
1491 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001492 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001493 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001494 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1495 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1496 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001497 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001498 dirtyLayer(left + bounds.left, top + bounds.top,
1499 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001500 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001501 }
1502 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001503#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001504
Romain Guy6620c6d2010-12-06 18:07:02 -08001505 if (pureTranslate) {
1506 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1507 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1508
1509 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1510 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1511 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1512 true, !mesh->hasEmptyQuads);
1513 } else {
1514 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1515 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1516 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1517 true, !mesh->hasEmptyQuads);
1518 }
Romain Guy054dc182010-10-15 17:55:25 -07001519 }
Romain Guyf7f93552010-07-08 19:17:03 -07001520}
1521
Chet Haase99ecdc42011-05-06 12:06:34 -07001522/**
Chet Haase858aa932011-05-12 09:06:00 -07001523 * This function uses a similar approach to that of AA lines in the drawLines() function.
1524 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1525 * shader to compute the translucency of the color, determined by whether a given pixel is
1526 * within that boundary region and how far into the region it is.
1527 */
1528void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001529 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001530 float inverseScaleX = 1.0f;
1531 float inverseScaleY = 1.0f;
1532 // The quad that we use needs to account for scaling.
1533 if (!mSnapshot->transform->isPureTranslate()) {
1534 Matrix4 *mat = mSnapshot->transform;
1535 float m00 = mat->data[Matrix4::kScaleX];
1536 float m01 = mat->data[Matrix4::kSkewY];
1537 float m02 = mat->data[2];
1538 float m10 = mat->data[Matrix4::kSkewX];
1539 float m11 = mat->data[Matrix4::kScaleX];
1540 float m12 = mat->data[6];
1541 float scaleX = sqrt(m00 * m00 + m01 * m01);
1542 float scaleY = sqrt(m10 * m10 + m11 * m11);
1543 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1544 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1545 }
1546
1547 setupDraw();
1548 setupDrawAALine();
1549 setupDrawColor(color);
1550 setupDrawColorFilter();
1551 setupDrawShader();
1552 setupDrawBlending(true, mode);
1553 setupDrawProgram();
1554 setupDrawModelViewIdentity(true);
1555 setupDrawColorUniforms();
1556 setupDrawColorFilterUniforms();
1557 setupDrawShaderIdentityUniforms();
1558
1559 AAVertex rects[4];
1560 AAVertex* aaVertices = &rects[0];
1561 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1562 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1563
1564 float boundarySizeX = .5 * inverseScaleX;
1565 float boundarySizeY = .5 * inverseScaleY;
1566
1567 // Adjust the rect by the AA boundary padding
1568 left -= boundarySizeX;
1569 right += boundarySizeX;
1570 top -= boundarySizeY;
1571 bottom += boundarySizeY;
1572
1573 float width = right - left;
1574 float height = bottom - top;
1575
1576 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1577 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1578 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1579 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1580 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1581 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1582 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1583
1584 if (!quickReject(left, top, right, bottom)) {
1585 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1586 AAVertex::set(aaVertices++, left, top, 1, 0);
1587 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1588 AAVertex::set(aaVertices++, right, top, 0, 0);
1589 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1590 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1591 }
1592}
1593
1594/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001595 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1596 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1597 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1598 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1599 * of the line. Hairlines are more involved because we need to account for transform scaling
1600 * to end up with a one-pixel-wide line in screen space..
1601 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1602 * in combination with values that we calculate and pass down in this method. The basic approach
1603 * is that the quad we create contains both the core line area plus a bounding area in which
1604 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1605 * proportion of the width and the length of a given segment is represented by the boundary
1606 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1607 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1608 * on the inside). This ends up giving the result we want, with pixels that are completely
1609 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1610 * how far into the boundary region they are, which is determined by shader interpolation.
1611 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001612void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1613 if (mSnapshot->isIgnored()) return;
1614
1615 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001616 // We use half the stroke width here because we're going to position the quad
1617 // corner vertices half of the width away from the line endpoints
1618 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001619 // A stroke width of 0 has a special meaning in Skia:
1620 // it draws a line 1 px wide regardless of current transform
1621 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001622 float inverseScaleX = 1.0f;
1623 float inverseScaleY = 1.0f;
1624 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001625 int alpha;
1626 SkXfermode::Mode mode;
1627 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001628 int verticesCount = count;
1629 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001630 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001631 verticesCount += (count - 4);
1632 }
1633
1634 if (isHairLine || isAA) {
1635 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1636 // the line on the screen should always be one pixel wide regardless of scale. For
1637 // AA lines, we only want one pixel of translucent boundary around the quad.
1638 if (!mSnapshot->transform->isPureTranslate()) {
1639 Matrix4 *mat = mSnapshot->transform;
1640 float m00 = mat->data[Matrix4::kScaleX];
1641 float m01 = mat->data[Matrix4::kSkewY];
1642 float m02 = mat->data[2];
1643 float m10 = mat->data[Matrix4::kSkewX];
1644 float m11 = mat->data[Matrix4::kScaleX];
1645 float m12 = mat->data[6];
1646 float scaleX = sqrt(m00*m00 + m01*m01);
1647 float scaleY = sqrt(m10*m10 + m11*m11);
1648 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1649 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1650 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1651 scaled = true;
1652 }
1653 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001654 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001655
1656 getAlphaAndMode(paint, &alpha, &mode);
1657 setupDraw();
1658 if (isAA) {
1659 setupDrawAALine();
1660 }
1661 setupDrawColor(paint->getColor(), alpha);
1662 setupDrawColorFilter();
1663 setupDrawShader();
1664 if (isAA) {
1665 setupDrawBlending(true, mode);
1666 } else {
1667 setupDrawBlending(mode);
1668 }
1669 setupDrawProgram();
1670 setupDrawModelViewIdentity(true);
1671 setupDrawColorUniforms();
1672 setupDrawColorFilterUniforms();
1673 setupDrawShaderIdentityUniforms();
1674
1675 if (isHairLine) {
1676 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001677 halfStrokeWidth = isAA? 1 : .5;
1678 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001679 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001680 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001681 }
1682 Vertex lines[verticesCount];
1683 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001684 AAVertex wLines[verticesCount];
1685 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001686 if (!isAA) {
1687 setupDrawVertices(vertices);
1688 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001689 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1690 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001691 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001692 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1693 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001694 // We will need to calculate the actual width proportion on each segment for
1695 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1696 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1697 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001698 }
1699
Chet Haase99ecdc42011-05-06 12:06:34 -07001700 AAVertex* prevAAVertex = NULL;
1701 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001702
Chet Haase99585ad2011-05-02 15:00:16 -07001703 int boundaryLengthSlot = -1;
1704 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001705 int boundaryWidthSlot = -1;
1706 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001707 for (int i = 0; i < count; i += 4) {
1708 // a = start point, b = end point
1709 vec2 a(points[i], points[i + 1]);
1710 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001711 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001712 float boundaryLengthProportion = 0;
1713 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001714
Chet Haase5b0200b2011-04-13 17:58:08 -07001715 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001716 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001717 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001718 if (isAA) {
1719 float wideningFactor;
1720 if (fabs(n.x) >= fabs(n.y)) {
1721 wideningFactor = fabs(1.0f / n.x);
1722 } else {
1723 wideningFactor = fabs(1.0f / n.y);
1724 }
1725 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001726 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001727 if (scaled) {
1728 n.x *= inverseScaleX;
1729 n.y *= inverseScaleY;
1730 }
1731 } else if (scaled) {
1732 // Extend n by .5 pixel on each side, post-transform
1733 vec2 extendedN = n.copyNormalized();
1734 extendedN /= 2;
1735 extendedN.x *= inverseScaleX;
1736 extendedN.y *= inverseScaleY;
1737 float extendedNLength = extendedN.length();
1738 // We need to set this value on the shader prior to drawing
1739 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1740 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001741 }
1742 float x = n.x;
1743 n.x = -n.y;
1744 n.y = x;
1745
Chet Haase99585ad2011-05-02 15:00:16 -07001746 // aa lines expand the endpoint vertices to encompass the AA boundary
1747 if (isAA) {
1748 vec2 abVector = (b - a);
1749 length = abVector.length();
1750 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001751 if (scaled) {
1752 abVector.x *= inverseScaleX;
1753 abVector.y *= inverseScaleY;
1754 float abLength = abVector.length();
1755 boundaryLengthProportion = abLength / (length + abLength);
1756 } else {
1757 boundaryLengthProportion = .5 / (length + 1);
1758 }
1759 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001760 a -= abVector;
1761 b += abVector;
1762 }
1763
Chet Haase5b0200b2011-04-13 17:58:08 -07001764 // Four corners of the rectangle defining a thick line
1765 vec2 p1 = a - n;
1766 vec2 p2 = a + n;
1767 vec2 p3 = b + n;
1768 vec2 p4 = b - n;
1769
Chet Haase99585ad2011-05-02 15:00:16 -07001770
Chet Haase5b0200b2011-04-13 17:58:08 -07001771 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1772 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1773 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1774 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1775
1776 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001777 if (!isAA) {
1778 if (prevVertex != NULL) {
1779 // Issue two repeat vertices to create degenerate triangles to bridge
1780 // between the previous line and the new one. This is necessary because
1781 // we are creating a single triangle_strip which will contain
1782 // potentially discontinuous line segments.
1783 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1784 Vertex::set(vertices++, p1.x, p1.y);
1785 generatedVerticesCount += 2;
1786 }
1787 Vertex::set(vertices++, p1.x, p1.y);
1788 Vertex::set(vertices++, p2.x, p2.y);
1789 Vertex::set(vertices++, p4.x, p4.y);
1790 Vertex::set(vertices++, p3.x, p3.y);
1791 prevVertex = vertices - 1;
1792 generatedVerticesCount += 4;
1793 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001794 if (!isHairLine && scaled) {
1795 // Must set width proportions per-segment for scaled non-hairlines to use the
1796 // correct AA boundary dimensions
1797 if (boundaryWidthSlot < 0) {
1798 boundaryWidthSlot =
1799 mCaches.currentProgram->getUniform("boundaryWidth");
1800 inverseBoundaryWidthSlot =
1801 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1802 }
1803 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1804 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1805 }
Chet Haase99585ad2011-05-02 15:00:16 -07001806 if (boundaryLengthSlot < 0) {
1807 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1808 inverseBoundaryLengthSlot =
1809 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1810 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001811 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1812 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001813
Chet Haase5b0200b2011-04-13 17:58:08 -07001814 if (prevAAVertex != NULL) {
1815 // Issue two repeat vertices to create degenerate triangles to bridge
1816 // between the previous line and the new one. This is necessary because
1817 // we are creating a single triangle_strip which will contain
1818 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001819 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1820 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1821 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001822 generatedVerticesCount += 2;
1823 }
Chet Haase99585ad2011-05-02 15:00:16 -07001824 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1825 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1826 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1827 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001828 prevAAVertex = aaVertices - 1;
1829 generatedVerticesCount += 4;
1830 }
Chet Haase99585ad2011-05-02 15:00:16 -07001831 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1832 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1833 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001834 }
1835 }
1836 if (generatedVerticesCount > 0) {
1837 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1838 }
1839}
1840
Romain Guyed6fcb02011-03-21 13:11:28 -07001841void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1842 if (mSnapshot->isIgnored()) return;
1843
1844 // TODO: The paint's cap style defines whether the points are square or circular
1845 // TODO: Handle AA for round points
1846
Chet Haase5b0200b2011-04-13 17:58:08 -07001847 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001848 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001849 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001850 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001851 if (isHairLine) {
1852 // Now that we know it's hairline, we can set the effective width, to be used later
1853 strokeWidth = 1.0f;
1854 }
1855 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001856 int alpha;
1857 SkXfermode::Mode mode;
1858 getAlphaAndMode(paint, &alpha, &mode);
1859
1860 int verticesCount = count >> 1;
1861 int generatedVerticesCount = 0;
1862
1863 TextureVertex pointsData[verticesCount];
1864 TextureVertex* vertex = &pointsData[0];
1865
1866 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001867 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001868 setupDrawColor(paint->getColor(), alpha);
1869 setupDrawColorFilter();
1870 setupDrawShader();
1871 setupDrawBlending(mode);
1872 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001873 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001874 setupDrawColorUniforms();
1875 setupDrawColorFilterUniforms();
1876 setupDrawPointUniforms();
1877 setupDrawShaderIdentityUniforms();
1878 setupDrawMesh(vertex);
1879
1880 for (int i = 0; i < count; i += 2) {
1881 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1882 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001883 float left = points[i] - halfWidth;
1884 float right = points[i] + halfWidth;
1885 float top = points[i + 1] - halfWidth;
1886 float bottom = points [i + 1] + halfWidth;
1887 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001888 }
1889
1890 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1891}
1892
Romain Guy85bf02f2010-06-22 13:11:24 -07001893void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001894 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001895 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001896
Romain Guyae88e5e2010-10-22 17:49:18 -07001897 Rect& clip(*mSnapshot->clipRect);
1898 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001899
Romain Guy3d58c032010-07-14 16:34:53 -07001900 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001901}
Romain Guy9d5316e2010-06-24 19:30:36 -07001902
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001903void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001904 if (!texture) return;
1905 const AutoTexture autoCleanup(texture);
1906
1907 const float x = left + texture->left - texture->offset;
1908 const float y = top + texture->top - texture->offset;
1909
1910 drawPathTexture(texture, x, y, paint);
1911}
1912
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001913void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1914 float rx, float ry, SkPaint* paint) {
1915 if (mSnapshot->isIgnored()) return;
1916
1917 glActiveTexture(gTextureUnits[0]);
1918 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1919 right - left, bottom - top, rx, ry, paint);
1920 drawShape(left, top, texture, paint);
1921}
1922
Romain Guy01d58e42011-01-19 21:54:02 -08001923void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1924 if (mSnapshot->isIgnored()) return;
1925
1926 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001927 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001928 drawShape(x - radius, y - radius, texture, paint);
1929}
Romain Guy01d58e42011-01-19 21:54:02 -08001930
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001931void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1932 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001933
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001934 glActiveTexture(gTextureUnits[0]);
1935 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1936 drawShape(left, top, texture, paint);
1937}
1938
Romain Guy8b2f5262011-01-23 16:15:02 -08001939void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1940 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1941 if (mSnapshot->isIgnored()) return;
1942
1943 if (fabs(sweepAngle) >= 360.0f) {
1944 drawOval(left, top, right, bottom, paint);
1945 return;
1946 }
1947
1948 glActiveTexture(gTextureUnits[0]);
1949 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1950 startAngle, sweepAngle, useCenter, paint);
1951 drawShape(left, top, texture, paint);
1952}
1953
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001954void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1955 SkPaint* paint) {
1956 if (mSnapshot->isIgnored()) return;
1957
1958 glActiveTexture(gTextureUnits[0]);
1959 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1960 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001961}
1962
Chet Haase5c13d892010-10-08 08:37:55 -07001963void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001964 if (p->getStyle() != SkPaint::kFill_Style) {
1965 drawRectAsShape(left, top, right, bottom, p);
1966 return;
1967 }
1968
Romain Guy6926c722010-07-12 20:20:03 -07001969 if (quickReject(left, top, right, bottom)) {
1970 return;
1971 }
1972
Romain Guy026c5e162010-06-28 17:12:22 -07001973 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001974 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001975 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1976 if (!isMode) {
1977 // Assume SRC_OVER
1978 mode = SkXfermode::kSrcOver_Mode;
1979 }
1980 } else {
1981 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001982 }
1983
Romain Guy026c5e162010-06-28 17:12:22 -07001984 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07001985 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07001986 drawAARect(left, top, right, bottom, color, mode);
1987 } else {
1988 drawColorRect(left, top, right, bottom, color, mode);
1989 }
Romain Guyc7d53492010-06-25 13:41:57 -07001990}
Romain Guy9d5316e2010-06-24 19:30:36 -07001991
Romain Guye8e62a42010-07-23 18:55:21 -07001992void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1993 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001994 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001995 return;
1996 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001997 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001998
Romain Guy67ffc362011-06-03 18:50:11 -07001999 // TODO: We should probably make a copy of the paint instead of modifying
2000 // it; modifying the paint will change its generationID the first
2001 // time, which might impact caches. More investigation needed to
2002 // see if it matters.
2003 // If we make a copy, then drawTextDecorations() should *not* make
2004 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002005 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002006#if RENDER_TEXT_AS_GLYPHS
2007 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2008#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002009
Romain Guya674ab72010-08-10 17:26:42 -07002010 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002011 switch (paint->getTextAlign()) {
2012 case SkPaint::kCenter_Align:
2013 length = paint->measureText(text, bytesCount);
2014 x -= length / 2.0f;
2015 break;
2016 case SkPaint::kRight_Align:
2017 length = paint->measureText(text, bytesCount);
2018 x -= length;
2019 break;
2020 default:
2021 break;
2022 }
2023
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002024 const float oldX = x;
2025 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002026 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2027 if (pureTranslate) {
2028 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2029 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2030 }
2031
Romain Guyb45c0c92010-08-26 20:35:23 -07002032 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2033 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002034 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002035
Romain Guy86568192010-12-14 15:55:39 -08002036 int alpha;
2037 SkXfermode::Mode mode;
2038 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002039
Romain Guy1e45aae2010-08-13 19:39:53 -07002040 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002041 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002042 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2043 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002044 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002045
Romain Guy740bf2b2011-04-26 15:33:10 -07002046 const float sx = oldX - shadow->left + mShadowDx;
2047 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002048
Romain Guy86568192010-12-14 15:55:39 -08002049 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002050 int shadowColor = mShadowColor;
2051 if (mShader) {
2052 shadowColor = 0xffffffff;
2053 }
Romain Guy86568192010-12-14 15:55:39 -08002054
2055 glActiveTexture(gTextureUnits[0]);
2056 setupDraw();
2057 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002058 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2059 setupDrawColorFilter();
2060 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002061 setupDrawBlending(true, mode);
2062 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002063 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002064 setupDrawTexture(shadow->id);
2065 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002066 setupDrawColorFilterUniforms();
2067 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002068 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2069
Romain Guy0a417492010-08-16 20:26:20 -07002070 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002071
Romain Guy86568192010-12-14 15:55:39 -08002072 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002073 }
2074
Romain Guyb146b122010-12-15 17:06:45 -08002075 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2076 return;
2077 }
2078
Romain Guy6620c6d2010-12-06 18:07:02 -08002079 // Pick the appropriate texture filtering
2080 bool linearFilter = mSnapshot->transform->changesBounds();
2081 if (pureTranslate && !linearFilter) {
2082 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2083 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002084
Romain Guy86568192010-12-14 15:55:39 -08002085 glActiveTexture(gTextureUnits[0]);
2086 setupDraw();
2087 setupDrawDirtyRegionsDisabled();
2088 setupDrawWithTexture(true);
2089 setupDrawAlpha8Color(paint->getColor(), alpha);
2090 setupDrawColorFilter();
2091 setupDrawShader();
2092 setupDrawBlending(true, mode);
2093 setupDrawProgram();
2094 setupDrawModelView(x, y, x, y, pureTranslate, true);
2095 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2096 setupDrawPureColorUniforms();
2097 setupDrawColorFilterUniforms();
2098 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002099
Romain Guy6620c6d2010-12-06 18:07:02 -08002100 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002101 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2102
2103#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002104 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002105#else
Romain Guyf219da52011-01-16 12:54:25 -08002106 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002107#endif
Romain Guy03750a02010-10-18 14:06:08 -07002108 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002109
2110 // Tell font renderer the locations of position and texture coord
2111 // attributes so it can bind its data properly
2112 int positionSlot = mCaches.currentProgram->position;
2113 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002114 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002115 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002116#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002117 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002118 if (!pureTranslate) {
2119 mSnapshot->transform->mapRect(bounds);
2120 }
Romain Guyf219da52011-01-16 12:54:25 -08002121 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002122 }
2123#endif
2124 }
Romain Guy694b5192010-07-21 21:33:20 -07002125
2126 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002127 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002128
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002129 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002130}
2131
Romain Guy7fbcc042010-08-04 15:40:07 -07002132void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002133 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002134
Romain Guy01d58e42011-01-19 21:54:02 -08002135 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002136
Romain Guyfb8b7632010-08-23 21:05:08 -07002137 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002138 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002139 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002140
Romain Guy8b55f372010-08-18 17:10:07 -07002141 const float x = texture->left - texture->offset;
2142 const float y = texture->top - texture->offset;
2143
Romain Guy01d58e42011-01-19 21:54:02 -08002144 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002145}
2146
Romain Guyada830f2011-01-13 12:13:20 -08002147void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2148 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002149 return;
2150 }
2151
2152 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002153
2154 int alpha;
2155 SkXfermode::Mode mode;
2156 getAlphaAndMode(paint, &alpha, &mode);
2157
Romain Guyada830f2011-01-13 12:13:20 -08002158 layer->alpha = alpha;
2159 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08002160
Romain Guyf219da52011-01-16 12:54:25 -08002161#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002162 if (!layer->region.isEmpty()) {
2163 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002164 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002165 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002166 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002167 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002168
Romain Guyc88e3572011-01-22 00:32:12 -08002169 setupDraw();
2170 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002171 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002172 setupDrawColorFilter();
2173 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
2174 setupDrawProgram();
Romain Guy40667672011-03-18 14:34:03 -07002175 setupDrawModelViewTranslate(x, y,
2176 x + layer->layer.getWidth(), y + layer->layer.getHeight());
Romain Guyc88e3572011-01-22 00:32:12 -08002177 setupDrawPureColorUniforms();
2178 setupDrawColorFilterUniforms();
2179 setupDrawTexture(layer->texture);
Romain Guyc88e3572011-01-22 00:32:12 -08002180 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002181
Romain Guyc88e3572011-01-22 00:32:12 -08002182 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2183 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002184
Romain Guyc88e3572011-01-22 00:32:12 -08002185 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002186
2187#if DEBUG_LAYERS_AS_REGIONS
2188 drawRegionRects(layer->region);
2189#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002190 }
Romain Guyf219da52011-01-16 12:54:25 -08002191 }
2192#else
Romain Guyada830f2011-01-13 12:13:20 -08002193 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2194 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002195#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002196}
2197
Romain Guy6926c722010-07-12 20:20:03 -07002198///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002199// Shaders
2200///////////////////////////////////////////////////////////////////////////////
2201
2202void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002203 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002204}
2205
Romain Guy06f96e22010-07-30 19:18:16 -07002206void OpenGLRenderer::setupShader(SkiaShader* shader) {
2207 mShader = shader;
2208 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002209 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002210 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002211}
2212
Romain Guyd27977d2010-07-14 19:18:51 -07002213///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002214// Color filters
2215///////////////////////////////////////////////////////////////////////////////
2216
2217void OpenGLRenderer::resetColorFilter() {
2218 mColorFilter = NULL;
2219}
2220
2221void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2222 mColorFilter = filter;
2223}
2224
2225///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002226// Drop shadow
2227///////////////////////////////////////////////////////////////////////////////
2228
2229void OpenGLRenderer::resetShadow() {
2230 mHasShadow = false;
2231}
2232
2233void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2234 mHasShadow = true;
2235 mShadowRadius = radius;
2236 mShadowDx = dx;
2237 mShadowDy = dy;
2238 mShadowColor = color;
2239}
2240
2241///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002242// Drawing implementation
2243///////////////////////////////////////////////////////////////////////////////
2244
Romain Guy01d58e42011-01-19 21:54:02 -08002245void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2246 float x, float y, SkPaint* paint) {
2247 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2248 return;
2249 }
2250
2251 int alpha;
2252 SkXfermode::Mode mode;
2253 getAlphaAndMode(paint, &alpha, &mode);
2254
2255 setupDraw();
2256 setupDrawWithTexture(true);
2257 setupDrawAlpha8Color(paint->getColor(), alpha);
2258 setupDrawColorFilter();
2259 setupDrawShader();
2260 setupDrawBlending(true, mode);
2261 setupDrawProgram();
2262 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2263 setupDrawTexture(texture->id);
2264 setupDrawPureColorUniforms();
2265 setupDrawColorFilterUniforms();
2266 setupDrawShaderUniforms();
2267 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2268
2269 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2270
2271 finishDrawTexture();
2272}
2273
Romain Guyf607bdc2010-09-10 19:20:06 -07002274// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002275#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2276#define kStdUnderline_Offset (1.0f / 9.0f)
2277#define kStdUnderline_Thickness (1.0f / 18.0f)
2278
2279void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2280 float x, float y, SkPaint* paint) {
2281 // Handle underline and strike-through
2282 uint32_t flags = paint->getFlags();
2283 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002284 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002285 float underlineWidth = length;
2286 // If length is > 0.0f, we already measured the text for the text alignment
2287 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002288 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002289 }
2290
2291 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002292 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002293 case SkPaint::kCenter_Align:
2294 offsetX = underlineWidth * 0.5f;
2295 break;
2296 case SkPaint::kRight_Align:
2297 offsetX = underlineWidth;
2298 break;
2299 default:
2300 break;
2301 }
2302
2303 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002304 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002305 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002306
Romain Guye20ecbd2010-09-22 19:49:04 -07002307 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002308 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002309
Romain Guyf6834472011-01-23 13:32:12 -08002310 int linesCount = 0;
2311 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2312 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2313
2314 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002315 float points[pointsCount];
2316 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002317
2318 if (flags & SkPaint::kUnderlineText_Flag) {
2319 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002320 points[currentPoint++] = left;
2321 points[currentPoint++] = top;
2322 points[currentPoint++] = left + underlineWidth;
2323 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002324 }
2325
2326 if (flags & SkPaint::kStrikeThruText_Flag) {
2327 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002328 points[currentPoint++] = left;
2329 points[currentPoint++] = top;
2330 points[currentPoint++] = left + underlineWidth;
2331 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002332 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002333
Romain Guy726aeba2011-06-01 14:52:00 -07002334 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002335
Romain Guy726aeba2011-06-01 14:52:00 -07002336 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002337 }
2338 }
2339}
2340
Romain Guy026c5e162010-06-28 17:12:22 -07002341void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002342 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002343 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002344 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002345 color |= 0x00ffffff;
2346 }
2347
Romain Guy70ca14e2010-12-13 18:24:33 -08002348 setupDraw();
2349 setupDrawColor(color);
2350 setupDrawShader();
2351 setupDrawColorFilter();
2352 setupDrawBlending(mode);
2353 setupDrawProgram();
2354 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2355 setupDrawColorUniforms();
2356 setupDrawShaderUniforms(ignoreTransform);
2357 setupDrawColorFilterUniforms();
2358 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002359
Romain Guyc95c8d62010-09-17 15:31:32 -07002360 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2361}
2362
Romain Guy82ba8142010-07-09 13:25:56 -07002363void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002364 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002365 int alpha;
2366 SkXfermode::Mode mode;
2367 getAlphaAndMode(paint, &alpha, &mode);
2368
Romain Guy8164c2d2010-10-25 18:03:28 -07002369 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
2370
Romain Guy6620c6d2010-12-06 18:07:02 -08002371 if (mSnapshot->transform->isPureTranslate()) {
2372 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2373 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2374
2375 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2376 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2377 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2378 } else {
2379 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2380 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2381 GL_TRIANGLE_STRIP, gMeshCount);
2382 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002383}
2384
Romain Guybd6b79b2010-06-26 00:13:53 -07002385void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002386 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2387 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002388 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002389}
2390
2391void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002392 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002393 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002394 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002395
Romain Guy746b7402010-10-26 16:27:31 -07002396 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002397 setupDrawWithTexture();
2398 setupDrawColor(alpha, alpha, alpha, alpha);
2399 setupDrawColorFilter();
2400 setupDrawBlending(blend, mode, swapSrcDst);
2401 setupDrawProgram();
2402 if (!dirty) {
2403 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002404 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002405 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002406 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002407 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002408 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002409 }
Romain Guy86568192010-12-14 15:55:39 -08002410 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002411 setupDrawColorFilterUniforms();
2412 setupDrawTexture(texture);
2413 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002414
Romain Guy6820ac82010-09-15 18:11:50 -07002415 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002416
2417 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002418}
2419
Romain Guya5aed0d2010-09-09 14:42:43 -07002420void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002421 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002422 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2423 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002424 if (mode < SkXfermode::kPlus_Mode) {
2425 if (!mCaches.blend) {
2426 glEnable(GL_BLEND);
2427 }
Romain Guy82ba8142010-07-09 13:25:56 -07002428
Romain Guyf607bdc2010-09-10 19:20:06 -07002429 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2430 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002431
Romain Guya5aed0d2010-09-09 14:42:43 -07002432 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2433 glBlendFunc(sourceMode, destMode);
2434 mCaches.lastSrcMode = sourceMode;
2435 mCaches.lastDstMode = destMode;
2436 }
2437 } else {
2438 // These blend modes are not supported by OpenGL directly and have
2439 // to be implemented using shaders. Since the shader will perform
2440 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002441 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002442 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002443 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002444 }
2445
2446 if (mCaches.blend) {
2447 glDisable(GL_BLEND);
2448 }
2449 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002450 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002451 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002452 glDisable(GL_BLEND);
2453 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002454 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002455}
2456
Romain Guy889f8d12010-07-29 14:37:42 -07002457bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002458 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002459 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002460 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002461 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002462 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002463 }
Romain Guy6926c722010-07-12 20:20:03 -07002464 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002465}
2466
Romain Guy026c5e162010-06-28 17:12:22 -07002467void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002468 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002469 TextureVertex::setUV(v++, u1, v1);
2470 TextureVertex::setUV(v++, u2, v1);
2471 TextureVertex::setUV(v++, u1, v2);
2472 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002473}
2474
Chet Haase5c13d892010-10-08 08:37:55 -07002475void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002476 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002477 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002478 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2479 if (!isMode) {
2480 // Assume SRC_OVER
2481 *mode = SkXfermode::kSrcOver_Mode;
2482 }
2483 } else {
2484 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002485 }
2486
2487 // Skia draws using the color's alpha channel if < 255
2488 // Otherwise, it uses the paint's alpha
2489 int color = paint->getColor();
2490 *alpha = (color >> 24) & 0xFF;
2491 if (*alpha == 255) {
2492 *alpha = paint->getAlpha();
2493 }
2494 } else {
2495 *mode = SkXfermode::kSrcOver_Mode;
2496 *alpha = 255;
2497 }
Romain Guy026c5e162010-06-28 17:12:22 -07002498}
2499
Romain Guya5aed0d2010-09-09 14:42:43 -07002500SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002501 SkXfermode::Mode resultMode;
2502 if (!SkXfermode::AsMode(mode, &resultMode)) {
2503 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002504 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002505 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002506}
2507
Romain Guy746b7402010-10-26 16:27:31 -07002508void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002509 bool bound = false;
2510 if (wrapS != texture->wrapS) {
2511 glBindTexture(GL_TEXTURE_2D, texture->id);
2512 bound = true;
2513 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2514 texture->wrapS = wrapS;
2515 }
2516 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002517 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002518 glBindTexture(GL_TEXTURE_2D, texture->id);
2519 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002520 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2521 texture->wrapT = wrapT;
2522 }
Romain Guya1db5742010-07-20 13:09:13 -07002523}
2524
Romain Guy9d5316e2010-06-24 19:30:36 -07002525}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002526}; // namespace android