blob: 5a86fe3880bb8f6163274d0159ceb4ad77892af0 [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 Guyb146b122010-12-15 17:06:45 -080066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
67 { 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 Guyb146b122010-12-15 17:06:45 -080084 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
85 { 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
479 // Clear the framebuffer where the layer will draw
480 glScissor(bounds.left, mSnapshot->height - bounds.bottom,
481 bounds.getWidth(), bounds.getHeight());
482 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
483 glClear(GL_COLOR_BUFFER_BIT);
484
485 dirtyClip();
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
494 layer->fbo = mCaches.fboCache.get();
495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
516 snapshot->fbo = layer->fbo;
517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
525 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
526 glBindTexture(GL_TEXTURE_2D, layer->texture);
527
528 // Initialize the texture if needed
529 if (layer->empty) {
530 layer->empty = false;
531 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
532 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
533 }
534
535 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
536 layer->texture, 0);
537
538#if DEBUG_LAYERS_AS_REGIONS
539 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
540 if (status != GL_FRAMEBUFFER_COMPLETE) {
541 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
542
543 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
544 glDeleteTextures(1, &layer->texture);
545 mCaches.fboCache.put(layer->fbo);
546
547 delete layer;
548
549 return false;
550 }
551#endif
552
553 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
554 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
555 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
556 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
557 glClear(GL_COLOR_BUFFER_BIT);
558
559 dirtyClip();
560
561 // Change the ortho projection
562 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
563 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
564
565 return true;
566}
567
Romain Guy1c740bc2010-09-13 18:00:09 -0700568/**
569 * Read the documentation of createLayer() before doing anything in this method.
570 */
Romain Guy1d83e192010-08-17 11:37:00 -0700571void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
572 if (!current->layer) {
573 LOGE("Attempting to compose a layer that does not exist");
574 return;
575 }
576
Romain Guy5b3b3522010-10-27 18:57:51 -0700577 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700578
579 if (fboLayer) {
580 // Unbind current FBO and restore previous one
581 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
582 }
583
Romain Guy1d83e192010-08-17 11:37:00 -0700584 Layer* layer = current->layer;
585 const Rect& rect = layer->layer;
586
Romain Guyeb993562010-10-05 18:14:38 -0700587 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700588 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700589 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700590 // Required below, composeLayerRect() will divide by 255
591 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700592 }
593
Romain Guy03750a02010-10-18 14:06:08 -0700594 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700595
Romain Guy746b7402010-10-26 16:27:31 -0700596 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700597
Romain Guy5b3b3522010-10-27 18:57:51 -0700598 // When the layer is stored in an FBO, we can save a bit of fillrate by
599 // drawing only the dirty region
600 if (fboLayer) {
601 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800602 if (layer->colorFilter) {
603 setupColorFilter(layer->colorFilter);
604 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700605 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800606 if (layer->colorFilter) {
607 resetColorFilter();
608 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800610 if (!rect.isEmpty()) {
611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
612 composeLayerRect(layer, rect, true);
613 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700614 }
Romain Guy8b55f372010-08-18 17:10:07 -0700615
Romain Guyeb993562010-10-05 18:14:38 -0700616 if (fboLayer) {
617 // Detach the texture from the FBO
618 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
619 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
620 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
621
622 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
623 mCaches.fboCache.put(current->fbo);
624 }
625
Romain Guy746b7402010-10-26 16:27:31 -0700626 dirtyClip();
627
Romain Guyeb993562010-10-05 18:14:38 -0700628 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700629 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700630 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700631 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700632 delete layer;
633 }
634}
635
Romain Guyaa6c24c2011-04-28 18:40:04 -0700636void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
637 float alpha = layer->alpha / 255.0f;
638
639 setupDraw();
Romain Guy8f0095c2011-05-02 17:24:22 -0700640 if (layer->renderTarget == GL_TEXTURE_2D) {
641 setupDrawWithTexture();
642 } else {
643 setupDrawWithExternalTexture();
644 }
645 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700646 setupDrawColor(alpha, alpha, alpha, alpha);
647 setupDrawColorFilter();
648 setupDrawBlending(layer->blend, layer->mode);
649 setupDrawProgram();
650 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
651 setupDrawPureColorUniforms();
652 setupDrawColorFilterUniforms();
Romain Guy8f0095c2011-05-02 17:24:22 -0700653 if (layer->renderTarget == GL_TEXTURE_2D) {
654 setupDrawTexture(layer->texture);
655 } else {
656 setupDrawExternalTexture(layer->texture);
657 }
658 setupDrawTextureTransformUniforms(layer->texTransform);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700659 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
660
661 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
662
663 finishDrawTexture();
664}
665
Romain Guy5b3b3522010-10-27 18:57:51 -0700666void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700667 if (!layer->isTextureLayer) {
668 const Rect& texCoords = layer->texCoords;
669 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
670 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700671
Romain Guyaa6c24c2011-04-28 18:40:04 -0700672 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
673 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
674 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
Romain Guy5b3b3522010-10-27 18:57:51 -0700675
Romain Guyaa6c24c2011-04-28 18:40:04 -0700676 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
677 } else {
678 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
679 drawTextureLayer(layer, rect);
680 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
681 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700682}
683
684void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
685#if RENDER_LAYERS_AS_REGIONS
686 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700687 layer->setRegionAsRect();
688
Romain Guy40667672011-03-18 14:34:03 -0700689 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700690
Romain Guy5b3b3522010-10-27 18:57:51 -0700691 layer->region.clear();
692 return;
693 }
694
695 if (!layer->region.isEmpty()) {
696 size_t count;
697 const android::Rect* rects = layer->region.getArray(&count);
698
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700700 const float texX = 1.0f / float(layer->width);
701 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800702 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700703
704 TextureVertex* mesh = mCaches.getRegionMesh();
705 GLsizei numQuads = 0;
706
Romain Guy7230a742011-01-10 22:26:16 -0800707 setupDraw();
708 setupDrawWithTexture();
709 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800710 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800711 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
712 setupDrawProgram();
713 setupDrawDirtyRegionsDisabled();
714 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800715 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800716 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800717 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800718 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700719
720 for (size_t i = 0; i < count; i++) {
721 const android::Rect* r = &rects[i];
722
723 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800724 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700725 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800726 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700727
728 // TODO: Reject quads outside of the clip
729 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
730 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
731 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
732 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
733
734 numQuads++;
735
736 if (numQuads >= REGION_MESH_QUAD_COUNT) {
737 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
738 numQuads = 0;
739 mesh = mCaches.getRegionMesh();
740 }
741 }
742
743 if (numQuads > 0) {
744 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
745 }
746
747 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800748 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700749
750#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800751 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700752#endif
753
754 layer->region.clear();
755 }
756#else
757 composeLayerRect(layer, rect);
758#endif
759}
760
Romain Guy3a3133d2011-02-01 22:59:58 -0800761void OpenGLRenderer::drawRegionRects(const Region& region) {
762#if DEBUG_LAYERS_AS_REGIONS
763 size_t count;
764 const android::Rect* rects = region.getArray(&count);
765
766 uint32_t colors[] = {
767 0x7fff0000, 0x7f00ff00,
768 0x7f0000ff, 0x7fff00ff,
769 };
770
771 int offset = 0;
772 int32_t top = rects[0].top;
773
774 for (size_t i = 0; i < count; i++) {
775 if (top != rects[i].top) {
776 offset ^= 0x2;
777 top = rects[i].top;
778 }
779
780 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
781 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
782 SkXfermode::kSrcOver_Mode);
783 }
784#endif
785}
786
Romain Guy5b3b3522010-10-27 18:57:51 -0700787void OpenGLRenderer::dirtyLayer(const float left, const float top,
788 const float right, const float bottom, const mat4 transform) {
789#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800790 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700791 Rect bounds(left, top, right, bottom);
792 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800793 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700794 }
795#endif
796}
797
798void OpenGLRenderer::dirtyLayer(const float left, const float top,
799 const float right, const float bottom) {
800#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800801 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700802 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800803 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800804 }
805#endif
806}
807
808void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
809#if RENDER_LAYERS_AS_REGIONS
810 if (bounds.intersect(*mSnapshot->clipRect)) {
811 bounds.snapToPixelBoundaries();
812 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
813 if (!dirty.isEmpty()) {
814 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815 }
816 }
817#endif
818}
819
Romain Guybd6b79b2010-06-26 00:13:53 -0700820///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700821// Transforms
822///////////////////////////////////////////////////////////////////////////////
823
824void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700825 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700826}
827
828void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700829 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700830}
831
832void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700833 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700834}
835
Romain Guy807daf72011-01-18 11:19:19 -0800836void OpenGLRenderer::skew(float sx, float sy) {
837 mSnapshot->transform->skew(sx, sy);
838}
839
Romain Guyf6a11b82010-06-23 17:47:49 -0700840void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700841 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700842}
843
Romain Guy41030da2010-10-13 13:40:37 -0700844const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700845 if (mSnapshot->fbo != 0) {
846 return &mSnapshot->transform->data[0];
847 }
848 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700849}
850
Romain Guyf6a11b82010-06-23 17:47:49 -0700851void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700852 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700853}
854
855void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700856 SkMatrix transform;
857 mSnapshot->transform->copyTo(transform);
858 transform.preConcat(*matrix);
859 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700860}
861
862///////////////////////////////////////////////////////////////////////////////
863// Clipping
864///////////////////////////////////////////////////////////////////////////////
865
Romain Guybb9524b2010-06-22 18:56:38 -0700866void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700867 Rect clip(*mSnapshot->clipRect);
868 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700869 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700870 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700871}
872
873const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700874 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700875}
876
Romain Guyc7d53492010-06-25 13:41:57 -0700877bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800878 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700879 return true;
880 }
881
Romain Guy1d83e192010-08-17 11:37:00 -0700882 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700883 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700884 r.snapToPixelBoundaries();
885
886 Rect clipRect(*mSnapshot->clipRect);
887 clipRect.snapToPixelBoundaries();
888
889 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700890}
891
Romain Guy079ba2c2010-07-16 14:12:24 -0700892bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
893 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700894 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700895 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700896 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700897 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700898}
899
Romain Guyf6a11b82010-06-23 17:47:49 -0700900///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800901// Drawing commands
902///////////////////////////////////////////////////////////////////////////////
903
904void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800905 if (mDirtyClip) {
906 setScissorFromClip();
907 }
908 mDescription.reset();
909 mSetShaderColor = false;
910 mColorSet = false;
911 mColorA = mColorR = mColorG = mColorB = 0.0f;
912 mTextureUnit = 0;
913 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800914 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800915}
916
917void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
918 mDescription.hasTexture = true;
919 mDescription.hasAlpha8Texture = isAlpha8;
920}
921
Romain Guyaa6c24c2011-04-28 18:40:04 -0700922void OpenGLRenderer::setupDrawWithExternalTexture() {
923 mDescription.hasExternalTexture = true;
924}
925
Chet Haase5b0200b2011-04-13 17:58:08 -0700926void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -0700927 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -0700928}
929
Romain Guyed6fcb02011-03-21 13:11:28 -0700930void OpenGLRenderer::setupDrawPoint(float pointSize) {
931 mDescription.isPoint = true;
932 mDescription.pointSize = pointSize;
933}
934
Romain Guy70ca14e2010-12-13 18:24:33 -0800935void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800936 setupDrawColor(color, (color >> 24) & 0xFF);
937}
938
939void OpenGLRenderer::setupDrawColor(int color, int alpha) {
940 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700941 // Second divide of a by 255 is an optimization, allowing us to simply multiply
942 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -0800943 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800944 mColorR = a * ((color >> 16) & 0xFF);
945 mColorG = a * ((color >> 8) & 0xFF);
946 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800947 mColorSet = true;
948 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
949}
950
Romain Guy86568192010-12-14 15:55:39 -0800951void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
952 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700953 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
954 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -0800955 const float a = mColorA / 255.0f;
956 mColorR = a * ((color >> 16) & 0xFF);
957 mColorG = a * ((color >> 8) & 0xFF);
958 mColorB = a * ((color ) & 0xFF);
959 mColorSet = true;
960 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
961}
962
Romain Guy70ca14e2010-12-13 18:24:33 -0800963void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
964 mColorA = a;
965 mColorR = r;
966 mColorG = g;
967 mColorB = b;
968 mColorSet = true;
969 mSetShaderColor = mDescription.setColor(r, g, b, a);
970}
971
Romain Guy86568192010-12-14 15:55:39 -0800972void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
973 mColorA = a;
974 mColorR = r;
975 mColorG = g;
976 mColorB = b;
977 mColorSet = true;
978 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
979}
980
Romain Guy70ca14e2010-12-13 18:24:33 -0800981void OpenGLRenderer::setupDrawShader() {
982 if (mShader) {
983 mShader->describe(mDescription, mCaches.extensions);
984 }
985}
986
987void OpenGLRenderer::setupDrawColorFilter() {
988 if (mColorFilter) {
989 mColorFilter->describe(mDescription, mCaches.extensions);
990 }
991}
992
993void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
994 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
995 mDescription, swapSrcDst);
996}
997
998void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
999 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1000 mDescription, swapSrcDst);
1001}
1002
1003void OpenGLRenderer::setupDrawProgram() {
1004 useProgram(mCaches.programCache.get(mDescription));
1005}
1006
1007void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1008 mTrackDirtyRegions = false;
1009}
1010
1011void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1012 bool ignoreTransform) {
1013 mModelView.loadTranslate(left, top, 0.0f);
1014 if (!ignoreTransform) {
1015 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1016 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1017 } else {
1018 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1019 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1020 }
1021}
1022
Chet Haase8a5cc922011-04-26 07:28:09 -07001023void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1024 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001025}
1026
Romain Guy70ca14e2010-12-13 18:24:33 -08001027void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1028 bool ignoreTransform, bool ignoreModelView) {
1029 if (!ignoreModelView) {
1030 mModelView.loadTranslate(left, top, 0.0f);
1031 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001032 } else {
1033 mModelView.loadIdentity();
1034 }
Romain Guy86568192010-12-14 15:55:39 -08001035 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1036 if (!ignoreTransform) {
1037 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1038 if (mTrackDirtyRegions && dirty) {
1039 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1040 }
1041 } else {
1042 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1043 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1044 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001045}
1046
Romain Guyed6fcb02011-03-21 13:11:28 -07001047void OpenGLRenderer::setupDrawPointUniforms() {
1048 int slot = mCaches.currentProgram->getUniform("pointSize");
1049 glUniform1f(slot, mDescription.pointSize);
1050}
1051
Romain Guy70ca14e2010-12-13 18:24:33 -08001052void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001053 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001054 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1055 }
1056}
1057
Romain Guy86568192010-12-14 15:55:39 -08001058void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001059 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001060 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001061 }
1062}
1063
Romain Guy70ca14e2010-12-13 18:24:33 -08001064void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1065 if (mShader) {
1066 if (ignoreTransform) {
1067 mModelView.loadInverse(*mSnapshot->transform);
1068 }
1069 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1070 }
1071}
1072
Romain Guy8d0d4782010-12-14 20:13:35 -08001073void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1074 if (mShader) {
1075 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1076 }
1077}
1078
Romain Guy70ca14e2010-12-13 18:24:33 -08001079void OpenGLRenderer::setupDrawColorFilterUniforms() {
1080 if (mColorFilter) {
1081 mColorFilter->setupProgram(mCaches.currentProgram);
1082 }
1083}
1084
1085void OpenGLRenderer::setupDrawSimpleMesh() {
1086 mCaches.bindMeshBuffer();
1087 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1088 gMeshStride, 0);
1089}
1090
1091void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1092 bindTexture(texture);
1093 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1094
1095 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1096 glEnableVertexAttribArray(mTexCoordsSlot);
1097}
1098
Romain Guyaa6c24c2011-04-28 18:40:04 -07001099void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1100 bindExternalTexture(texture);
1101 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1102
1103 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1104 glEnableVertexAttribArray(mTexCoordsSlot);
1105}
1106
Romain Guy8f0095c2011-05-02 17:24:22 -07001107void OpenGLRenderer::setupDrawTextureTransform() {
1108 mDescription.hasTextureTransform = true;
1109}
1110
1111void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001112 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1113 GL_FALSE, &transform.data[0]);
1114}
1115
Romain Guy70ca14e2010-12-13 18:24:33 -08001116void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1117 if (!vertices) {
1118 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1119 } else {
1120 mCaches.unbindMeshBuffer();
1121 }
1122 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1123 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001124 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001125 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1126 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001127}
1128
Chet Haase5b0200b2011-04-13 17:58:08 -07001129void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1130 mCaches.unbindMeshBuffer();
1131 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1132 gVertexStride, vertices);
1133}
1134
1135/**
1136 * 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 -07001137 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1138 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1139 * attributes (one per vertex) are values from zero to one that tells the fragment
1140 * shader where the fragment is in relation to the line width/length overall; these values are
1141 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1142 * region of the line.
1143 * Note that we only pass down the width values in this setup function. The length coordinates
1144 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001145 */
Chet Haase99585ad2011-05-02 15:00:16 -07001146void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001147 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001148 mCaches.unbindMeshBuffer();
1149 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001150 gAAVertexStride, vertices);
1151 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1152 glEnableVertexAttribArray(widthSlot);
1153 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1154 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1155 glEnableVertexAttribArray(lengthSlot);
1156 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001157 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001158 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001159 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001160 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001161 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001162}
1163
Romain Guy70ca14e2010-12-13 18:24:33 -08001164void OpenGLRenderer::finishDrawTexture() {
1165 glDisableVertexAttribArray(mTexCoordsSlot);
1166}
1167
1168///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001169// Drawing
1170///////////////////////////////////////////////////////////////////////////////
1171
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001172bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1173 Rect& dirty, uint32_t level) {
1174 if (quickReject(0.0f, 0.0f, width, height)) {
1175 return false;
1176 }
1177
Romain Guy0fe478e2010-11-08 12:08:41 -08001178 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1179 // will be performed by the display list itself
1180 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001181 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001182 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001183
Chet Haasedaf98e92011-01-10 14:10:36 -08001184 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001185}
1186
Chet Haaseed30fd82011-04-22 16:18:45 -07001187void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1188 if (displayList) {
1189 displayList->output(*this, level);
1190 }
1191}
1192
Romain Guya168d732011-03-18 16:50:13 -07001193void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1194 int alpha;
1195 SkXfermode::Mode mode;
1196 getAlphaAndMode(paint, &alpha, &mode);
1197
1198 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1199
1200 float x = left;
1201 float y = top;
1202
1203 bool ignoreTransform = false;
1204 if (mSnapshot->transform->isPureTranslate()) {
1205 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1206 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1207 ignoreTransform = true;
1208 }
1209
1210 setupDraw();
1211 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001212 if (paint) {
1213 setupDrawAlpha8Color(paint->getColor(), alpha);
1214 }
Romain Guya168d732011-03-18 16:50:13 -07001215 setupDrawColorFilter();
1216 setupDrawShader();
1217 setupDrawBlending(true, mode);
1218 setupDrawProgram();
1219 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
1220 setupDrawTexture(texture->id);
1221 setupDrawPureColorUniforms();
1222 setupDrawColorFilterUniforms();
1223 setupDrawShaderUniforms();
1224 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1225
1226 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1227
1228 finishDrawTexture();
1229}
1230
Chet Haase5c13d892010-10-08 08:37:55 -07001231void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001232 const float right = left + bitmap->width();
1233 const float bottom = top + bitmap->height();
1234
1235 if (quickReject(left, top, right, bottom)) {
1236 return;
1237 }
1238
Romain Guy86568192010-12-14 15:55:39 -08001239 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001240 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001241 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001242 const AutoTexture autoCleanup(texture);
1243
Romain Guya168d732011-03-18 16:50:13 -07001244 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1245 drawAlphaBitmap(texture, left, top, paint);
1246 } else {
1247 drawTextureRect(left, top, right, bottom, texture, paint);
1248 }
Romain Guyce0537b2010-06-29 21:05:21 -07001249}
1250
Chet Haase5c13d892010-10-08 08:37:55 -07001251void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001252 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1253 const mat4 transform(*matrix);
1254 transform.mapRect(r);
1255
Romain Guy6926c722010-07-12 20:20:03 -07001256 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1257 return;
1258 }
1259
Romain Guy86568192010-12-14 15:55:39 -08001260 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001261 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001262 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001263 const AutoTexture autoCleanup(texture);
1264
Romain Guy5b3b3522010-10-27 18:57:51 -07001265 // This could be done in a cheaper way, all we need is pass the matrix
1266 // to the vertex shader. The save/restore is a bit overkill.
1267 save(SkCanvas::kMatrix_SaveFlag);
1268 concatMatrix(matrix);
1269 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1270 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001271}
1272
Romain Guy5a7b4662011-01-20 19:09:30 -08001273void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1274 float* vertices, int* colors, SkPaint* paint) {
1275 // TODO: Do a quickReject
1276 if (!vertices || mSnapshot->isIgnored()) {
1277 return;
1278 }
1279
1280 glActiveTexture(gTextureUnits[0]);
1281 Texture* texture = mCaches.textureCache.get(bitmap);
1282 if (!texture) return;
1283 const AutoTexture autoCleanup(texture);
1284 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1285
1286 int alpha;
1287 SkXfermode::Mode mode;
1288 getAlphaAndMode(paint, &alpha, &mode);
1289
Romain Guy5a7b4662011-01-20 19:09:30 -08001290 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001291
Romain Guyb18d2d02011-02-10 15:52:54 -08001292 float left = FLT_MAX;
1293 float top = FLT_MAX;
1294 float right = FLT_MIN;
1295 float bottom = FLT_MIN;
1296
1297#if RENDER_LAYERS_AS_REGIONS
1298 bool hasActiveLayer = hasLayer();
1299#else
1300 bool hasActiveLayer = false;
1301#endif
1302
Romain Guya566b7c2011-01-23 16:36:11 -08001303 // TODO: Support the colors array
1304 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001305 TextureVertex* vertex = mesh;
1306 for (int32_t y = 0; y < meshHeight; y++) {
1307 for (int32_t x = 0; x < meshWidth; x++) {
1308 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1309
1310 float u1 = float(x) / meshWidth;
1311 float u2 = float(x + 1) / meshWidth;
1312 float v1 = float(y) / meshHeight;
1313 float v2 = float(y + 1) / meshHeight;
1314
1315 int ax = i + (meshWidth + 1) * 2;
1316 int ay = ax + 1;
1317 int bx = i;
1318 int by = bx + 1;
1319 int cx = i + 2;
1320 int cy = cx + 1;
1321 int dx = i + (meshWidth + 1) * 2 + 2;
1322 int dy = dx + 1;
1323
1324 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1325 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1326 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1327
1328 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1329 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1330 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001331
1332#if RENDER_LAYERS_AS_REGIONS
1333 if (hasActiveLayer) {
1334 // TODO: This could be optimized to avoid unnecessary ops
1335 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1336 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1337 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1338 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1339 }
1340#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001341 }
1342 }
1343
Romain Guyb18d2d02011-02-10 15:52:54 -08001344#if RENDER_LAYERS_AS_REGIONS
1345 if (hasActiveLayer) {
1346 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1347 }
1348#endif
1349
Romain Guy5a7b4662011-01-20 19:09:30 -08001350 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1351 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001352 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001353}
1354
Romain Guy8ba548f2010-06-30 19:21:21 -07001355void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1356 float srcLeft, float srcTop, float srcRight, float srcBottom,
1357 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001358 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001359 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1360 return;
1361 }
1362
Romain Guy746b7402010-10-26 16:27:31 -07001363 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001364 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001365 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001366 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001367 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001368
Romain Guy8ba548f2010-06-30 19:21:21 -07001369 const float width = texture->width;
1370 const float height = texture->height;
1371
1372 const float u1 = srcLeft / width;
1373 const float v1 = srcTop / height;
1374 const float u2 = srcRight / width;
1375 const float v2 = srcBottom / height;
1376
Romain Guy03750a02010-10-18 14:06:08 -07001377 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001378 resetDrawTextureTexCoords(u1, v1, u2, v2);
1379
Romain Guy03750a02010-10-18 14:06:08 -07001380 int alpha;
1381 SkXfermode::Mode mode;
1382 getAlphaAndMode(paint, &alpha, &mode);
1383
Romain Guy6620c6d2010-12-06 18:07:02 -08001384 if (mSnapshot->transform->isPureTranslate()) {
1385 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1386 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1387
1388 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1389 texture->id, alpha / 255.0f, mode, texture->blend,
1390 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1391 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1392 } else {
1393 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1394 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1395 GL_TRIANGLE_STRIP, gMeshCount);
1396 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001397
1398 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1399}
1400
Romain Guy4aa90572010-09-26 18:40:37 -07001401void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001402 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001403 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001404 if (quickReject(left, top, right, bottom)) {
1405 return;
1406 }
1407
Romain Guy746b7402010-10-26 16:27:31 -07001408 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001409 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001410 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001411 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001412 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001413
1414 int alpha;
1415 SkXfermode::Mode mode;
1416 getAlphaAndMode(paint, &alpha, &mode);
1417
Romain Guy2728f962010-10-08 18:36:15 -07001418 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001419 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001420
Romain Guya5ef39a2010-12-03 16:48:20 -08001421 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001422 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001423#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001424 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001425 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001426 const float offsetX = left + mSnapshot->transform->getTranslateX();
1427 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001428 const size_t count = mesh->quads.size();
1429 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001430 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001431 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001432 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1433 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1434 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001435 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001436 dirtyLayer(left + bounds.left, top + bounds.top,
1437 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001438 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001439 }
1440 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001441#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001442
Romain Guy6620c6d2010-12-06 18:07:02 -08001443 if (pureTranslate) {
1444 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1445 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1446
1447 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1448 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1449 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1450 true, !mesh->hasEmptyQuads);
1451 } else {
1452 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1453 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1454 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1455 true, !mesh->hasEmptyQuads);
1456 }
Romain Guy054dc182010-10-15 17:55:25 -07001457 }
Romain Guyf7f93552010-07-08 19:17:03 -07001458}
1459
Chet Haase99ecdc42011-05-06 12:06:34 -07001460/**
Chet Haase858aa932011-05-12 09:06:00 -07001461 * This function uses a similar approach to that of AA lines in the drawLines() function.
1462 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1463 * shader to compute the translucency of the color, determined by whether a given pixel is
1464 * within that boundary region and how far into the region it is.
1465 */
1466void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
1467 int color, SkXfermode::Mode mode)
1468{
1469 float inverseScaleX = 1.0f;
1470 float inverseScaleY = 1.0f;
1471 // The quad that we use needs to account for scaling.
1472 if (!mSnapshot->transform->isPureTranslate()) {
1473 Matrix4 *mat = mSnapshot->transform;
1474 float m00 = mat->data[Matrix4::kScaleX];
1475 float m01 = mat->data[Matrix4::kSkewY];
1476 float m02 = mat->data[2];
1477 float m10 = mat->data[Matrix4::kSkewX];
1478 float m11 = mat->data[Matrix4::kScaleX];
1479 float m12 = mat->data[6];
1480 float scaleX = sqrt(m00 * m00 + m01 * m01);
1481 float scaleY = sqrt(m10 * m10 + m11 * m11);
1482 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1483 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1484 }
1485
1486 setupDraw();
1487 setupDrawAALine();
1488 setupDrawColor(color);
1489 setupDrawColorFilter();
1490 setupDrawShader();
1491 setupDrawBlending(true, mode);
1492 setupDrawProgram();
1493 setupDrawModelViewIdentity(true);
1494 setupDrawColorUniforms();
1495 setupDrawColorFilterUniforms();
1496 setupDrawShaderIdentityUniforms();
1497
1498 AAVertex rects[4];
1499 AAVertex* aaVertices = &rects[0];
1500 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1501 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1502
1503 float boundarySizeX = .5 * inverseScaleX;
1504 float boundarySizeY = .5 * inverseScaleY;
1505
1506 // Adjust the rect by the AA boundary padding
1507 left -= boundarySizeX;
1508 right += boundarySizeX;
1509 top -= boundarySizeY;
1510 bottom += boundarySizeY;
1511
1512 float width = right - left;
1513 float height = bottom - top;
1514
1515 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1516 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1517 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1518 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1519 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1520 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1521 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1522
1523 if (!quickReject(left, top, right, bottom)) {
1524 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1525 AAVertex::set(aaVertices++, left, top, 1, 0);
1526 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1527 AAVertex::set(aaVertices++, right, top, 0, 0);
1528 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1529 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1530 }
1531}
1532
1533/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001534 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1535 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1536 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1537 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1538 * of the line. Hairlines are more involved because we need to account for transform scaling
1539 * to end up with a one-pixel-wide line in screen space..
1540 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1541 * in combination with values that we calculate and pass down in this method. The basic approach
1542 * is that the quad we create contains both the core line area plus a bounding area in which
1543 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1544 * proportion of the width and the length of a given segment is represented by the boundary
1545 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1546 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1547 * on the inside). This ends up giving the result we want, with pixels that are completely
1548 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1549 * how far into the boundary region they are, which is determined by shader interpolation.
1550 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001551void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1552 if (mSnapshot->isIgnored()) return;
1553
1554 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001555 // We use half the stroke width here because we're going to position the quad
1556 // corner vertices half of the width away from the line endpoints
1557 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001558 // A stroke width of 0 has a special meaning in Skia:
1559 // it draws a line 1 px wide regardless of current transform
1560 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001561 float inverseScaleX = 1.0f;
1562 float inverseScaleY = 1.0f;
1563 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001564 int alpha;
1565 SkXfermode::Mode mode;
1566 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001567 int verticesCount = count;
1568 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001569 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001570 verticesCount += (count - 4);
1571 }
1572
1573 if (isHairLine || isAA) {
1574 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1575 // the line on the screen should always be one pixel wide regardless of scale. For
1576 // AA lines, we only want one pixel of translucent boundary around the quad.
1577 if (!mSnapshot->transform->isPureTranslate()) {
1578 Matrix4 *mat = mSnapshot->transform;
1579 float m00 = mat->data[Matrix4::kScaleX];
1580 float m01 = mat->data[Matrix4::kSkewY];
1581 float m02 = mat->data[2];
1582 float m10 = mat->data[Matrix4::kSkewX];
1583 float m11 = mat->data[Matrix4::kScaleX];
1584 float m12 = mat->data[6];
1585 float scaleX = sqrt(m00*m00 + m01*m01);
1586 float scaleY = sqrt(m10*m10 + m11*m11);
1587 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1588 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1589 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1590 scaled = true;
1591 }
1592 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001593 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001594
1595 getAlphaAndMode(paint, &alpha, &mode);
1596 setupDraw();
1597 if (isAA) {
1598 setupDrawAALine();
1599 }
1600 setupDrawColor(paint->getColor(), alpha);
1601 setupDrawColorFilter();
1602 setupDrawShader();
1603 if (isAA) {
1604 setupDrawBlending(true, mode);
1605 } else {
1606 setupDrawBlending(mode);
1607 }
1608 setupDrawProgram();
1609 setupDrawModelViewIdentity(true);
1610 setupDrawColorUniforms();
1611 setupDrawColorFilterUniforms();
1612 setupDrawShaderIdentityUniforms();
1613
1614 if (isHairLine) {
1615 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001616 halfStrokeWidth = isAA? 1 : .5;
1617 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001618 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001619 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001620 }
1621 Vertex lines[verticesCount];
1622 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001623 AAVertex wLines[verticesCount];
1624 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001625 if (!isAA) {
1626 setupDrawVertices(vertices);
1627 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001628 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1629 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001630 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001631 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1632 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001633 // We will need to calculate the actual width proportion on each segment for
1634 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1635 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1636 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001637 }
1638
Chet Haase99ecdc42011-05-06 12:06:34 -07001639 AAVertex* prevAAVertex = NULL;
1640 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001641
Chet Haase99585ad2011-05-02 15:00:16 -07001642 int boundaryLengthSlot = -1;
1643 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001644 int boundaryWidthSlot = -1;
1645 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001646 for (int i = 0; i < count; i += 4) {
1647 // a = start point, b = end point
1648 vec2 a(points[i], points[i + 1]);
1649 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001650 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001651 float boundaryLengthProportion = 0;
1652 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001653
Chet Haase5b0200b2011-04-13 17:58:08 -07001654 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001655 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001656 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001657 if (isAA) {
1658 float wideningFactor;
1659 if (fabs(n.x) >= fabs(n.y)) {
1660 wideningFactor = fabs(1.0f / n.x);
1661 } else {
1662 wideningFactor = fabs(1.0f / n.y);
1663 }
1664 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001665 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001666 if (scaled) {
1667 n.x *= inverseScaleX;
1668 n.y *= inverseScaleY;
1669 }
1670 } else if (scaled) {
1671 // Extend n by .5 pixel on each side, post-transform
1672 vec2 extendedN = n.copyNormalized();
1673 extendedN /= 2;
1674 extendedN.x *= inverseScaleX;
1675 extendedN.y *= inverseScaleY;
1676 float extendedNLength = extendedN.length();
1677 // We need to set this value on the shader prior to drawing
1678 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1679 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001680 }
1681 float x = n.x;
1682 n.x = -n.y;
1683 n.y = x;
1684
Chet Haase99585ad2011-05-02 15:00:16 -07001685 // aa lines expand the endpoint vertices to encompass the AA boundary
1686 if (isAA) {
1687 vec2 abVector = (b - a);
1688 length = abVector.length();
1689 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001690 if (scaled) {
1691 abVector.x *= inverseScaleX;
1692 abVector.y *= inverseScaleY;
1693 float abLength = abVector.length();
1694 boundaryLengthProportion = abLength / (length + abLength);
1695 } else {
1696 boundaryLengthProportion = .5 / (length + 1);
1697 }
1698 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001699 a -= abVector;
1700 b += abVector;
1701 }
1702
Chet Haase5b0200b2011-04-13 17:58:08 -07001703 // Four corners of the rectangle defining a thick line
1704 vec2 p1 = a - n;
1705 vec2 p2 = a + n;
1706 vec2 p3 = b + n;
1707 vec2 p4 = b - n;
1708
Chet Haase99585ad2011-05-02 15:00:16 -07001709
Chet Haase5b0200b2011-04-13 17:58:08 -07001710 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1711 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1712 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1713 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1714
1715 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001716 if (!isAA) {
1717 if (prevVertex != NULL) {
1718 // Issue two repeat vertices to create degenerate triangles to bridge
1719 // between the previous line and the new one. This is necessary because
1720 // we are creating a single triangle_strip which will contain
1721 // potentially discontinuous line segments.
1722 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1723 Vertex::set(vertices++, p1.x, p1.y);
1724 generatedVerticesCount += 2;
1725 }
1726 Vertex::set(vertices++, p1.x, p1.y);
1727 Vertex::set(vertices++, p2.x, p2.y);
1728 Vertex::set(vertices++, p4.x, p4.y);
1729 Vertex::set(vertices++, p3.x, p3.y);
1730 prevVertex = vertices - 1;
1731 generatedVerticesCount += 4;
1732 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001733 if (!isHairLine && scaled) {
1734 // Must set width proportions per-segment for scaled non-hairlines to use the
1735 // correct AA boundary dimensions
1736 if (boundaryWidthSlot < 0) {
1737 boundaryWidthSlot =
1738 mCaches.currentProgram->getUniform("boundaryWidth");
1739 inverseBoundaryWidthSlot =
1740 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1741 }
1742 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1743 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1744 }
Chet Haase99585ad2011-05-02 15:00:16 -07001745 if (boundaryLengthSlot < 0) {
1746 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1747 inverseBoundaryLengthSlot =
1748 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1749 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001750 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1751 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001752
Chet Haase5b0200b2011-04-13 17:58:08 -07001753 if (prevAAVertex != NULL) {
1754 // Issue two repeat vertices to create degenerate triangles to bridge
1755 // between the previous line and the new one. This is necessary because
1756 // we are creating a single triangle_strip which will contain
1757 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001758 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1759 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1760 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001761 generatedVerticesCount += 2;
1762 }
Chet Haase99585ad2011-05-02 15:00:16 -07001763 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1764 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1765 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1766 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001767 prevAAVertex = aaVertices - 1;
1768 generatedVerticesCount += 4;
1769 }
Chet Haase99585ad2011-05-02 15:00:16 -07001770 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1771 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1772 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001773 }
1774 }
1775 if (generatedVerticesCount > 0) {
1776 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1777 }
1778}
1779
Romain Guyed6fcb02011-03-21 13:11:28 -07001780void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1781 if (mSnapshot->isIgnored()) return;
1782
1783 // TODO: The paint's cap style defines whether the points are square or circular
1784 // TODO: Handle AA for round points
1785
Chet Haase5b0200b2011-04-13 17:58:08 -07001786 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001787 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001788 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001789 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001790 if (isHairLine) {
1791 // Now that we know it's hairline, we can set the effective width, to be used later
1792 strokeWidth = 1.0f;
1793 }
1794 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001795 int alpha;
1796 SkXfermode::Mode mode;
1797 getAlphaAndMode(paint, &alpha, &mode);
1798
1799 int verticesCount = count >> 1;
1800 int generatedVerticesCount = 0;
1801
1802 TextureVertex pointsData[verticesCount];
1803 TextureVertex* vertex = &pointsData[0];
1804
1805 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001806 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001807 setupDrawColor(paint->getColor(), alpha);
1808 setupDrawColorFilter();
1809 setupDrawShader();
1810 setupDrawBlending(mode);
1811 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001812 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001813 setupDrawColorUniforms();
1814 setupDrawColorFilterUniforms();
1815 setupDrawPointUniforms();
1816 setupDrawShaderIdentityUniforms();
1817 setupDrawMesh(vertex);
1818
1819 for (int i = 0; i < count; i += 2) {
1820 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1821 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001822 float left = points[i] - halfWidth;
1823 float right = points[i] + halfWidth;
1824 float top = points[i + 1] - halfWidth;
1825 float bottom = points [i + 1] + halfWidth;
1826 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001827 }
1828
1829 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1830}
1831
Romain Guy85bf02f2010-06-22 13:11:24 -07001832void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001833 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001834 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001835
Romain Guyae88e5e2010-10-22 17:49:18 -07001836 Rect& clip(*mSnapshot->clipRect);
1837 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001838
Romain Guy3d58c032010-07-14 16:34:53 -07001839 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001840}
Romain Guy9d5316e2010-06-24 19:30:36 -07001841
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001842void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001843 if (!texture) return;
1844 const AutoTexture autoCleanup(texture);
1845
1846 const float x = left + texture->left - texture->offset;
1847 const float y = top + texture->top - texture->offset;
1848
1849 drawPathTexture(texture, x, y, paint);
1850}
1851
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001852void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1853 float rx, float ry, SkPaint* paint) {
1854 if (mSnapshot->isIgnored()) return;
1855
1856 glActiveTexture(gTextureUnits[0]);
1857 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1858 right - left, bottom - top, rx, ry, paint);
1859 drawShape(left, top, texture, paint);
1860}
1861
Romain Guy01d58e42011-01-19 21:54:02 -08001862void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1863 if (mSnapshot->isIgnored()) return;
1864
1865 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001866 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001867 drawShape(x - radius, y - radius, texture, paint);
1868}
Romain Guy01d58e42011-01-19 21:54:02 -08001869
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001870void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1871 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001872
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001873 glActiveTexture(gTextureUnits[0]);
1874 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1875 drawShape(left, top, texture, paint);
1876}
1877
Romain Guy8b2f5262011-01-23 16:15:02 -08001878void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1879 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1880 if (mSnapshot->isIgnored()) return;
1881
1882 if (fabs(sweepAngle) >= 360.0f) {
1883 drawOval(left, top, right, bottom, paint);
1884 return;
1885 }
1886
1887 glActiveTexture(gTextureUnits[0]);
1888 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1889 startAngle, sweepAngle, useCenter, paint);
1890 drawShape(left, top, texture, paint);
1891}
1892
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001893void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1894 SkPaint* paint) {
1895 if (mSnapshot->isIgnored()) return;
1896
1897 glActiveTexture(gTextureUnits[0]);
1898 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1899 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001900}
1901
Chet Haase5c13d892010-10-08 08:37:55 -07001902void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001903 if (p->getStyle() != SkPaint::kFill_Style) {
1904 drawRectAsShape(left, top, right, bottom, p);
1905 return;
1906 }
1907
Romain Guy6926c722010-07-12 20:20:03 -07001908 if (quickReject(left, top, right, bottom)) {
1909 return;
1910 }
1911
Romain Guy026c5e162010-06-28 17:12:22 -07001912 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001913 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001914 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1915 if (!isMode) {
1916 // Assume SRC_OVER
1917 mode = SkXfermode::kSrcOver_Mode;
1918 }
1919 } else {
1920 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001921 }
1922
Romain Guy026c5e162010-06-28 17:12:22 -07001923 int color = p->getColor();
Chet Haase858aa932011-05-12 09:06:00 -07001924 if (p->isAntiAlias()) {
1925 drawAARect(left, top, right, bottom, color, mode);
1926 } else {
1927 drawColorRect(left, top, right, bottom, color, mode);
1928 }
Romain Guyc7d53492010-06-25 13:41:57 -07001929}
Romain Guy9d5316e2010-06-24 19:30:36 -07001930
Romain Guye8e62a42010-07-23 18:55:21 -07001931void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1932 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001933 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001934 return;
1935 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001936 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001937
Romain Guyf607bdc2010-09-10 19:20:06 -07001938 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001939
Romain Guya674ab72010-08-10 17:26:42 -07001940 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001941 switch (paint->getTextAlign()) {
1942 case SkPaint::kCenter_Align:
1943 length = paint->measureText(text, bytesCount);
1944 x -= length / 2.0f;
1945 break;
1946 case SkPaint::kRight_Align:
1947 length = paint->measureText(text, bytesCount);
1948 x -= length;
1949 break;
1950 default:
1951 break;
1952 }
1953
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001954 const float oldX = x;
1955 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001956 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1957 if (pureTranslate) {
1958 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1959 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1960 }
1961
Romain Guyb45c0c92010-08-26 20:35:23 -07001962 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1963 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001964 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001965
Romain Guy86568192010-12-14 15:55:39 -08001966 int alpha;
1967 SkXfermode::Mode mode;
1968 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001969
Romain Guy1e45aae2010-08-13 19:39:53 -07001970 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001971 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001972 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001973 count, mShadowRadius);
1974 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001975
Romain Guy740bf2b2011-04-26 15:33:10 -07001976 const float sx = oldX - shadow->left + mShadowDx;
1977 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001978
Romain Guy86568192010-12-14 15:55:39 -08001979 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07001980 int shadowColor = mShadowColor;
1981 if (mShader) {
1982 shadowColor = 0xffffffff;
1983 }
Romain Guy86568192010-12-14 15:55:39 -08001984
1985 glActiveTexture(gTextureUnits[0]);
1986 setupDraw();
1987 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07001988 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1989 setupDrawColorFilter();
1990 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08001991 setupDrawBlending(true, mode);
1992 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07001993 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08001994 setupDrawTexture(shadow->id);
1995 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07001996 setupDrawColorFilterUniforms();
1997 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08001998 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1999
Romain Guy0a417492010-08-16 20:26:20 -07002000 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002001
Romain Guy86568192010-12-14 15:55:39 -08002002 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002003 }
2004
Romain Guyb146b122010-12-15 17:06:45 -08002005 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2006 return;
2007 }
2008
Romain Guy6620c6d2010-12-06 18:07:02 -08002009 // Pick the appropriate texture filtering
2010 bool linearFilter = mSnapshot->transform->changesBounds();
2011 if (pureTranslate && !linearFilter) {
2012 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2013 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002014
Romain Guy86568192010-12-14 15:55:39 -08002015 glActiveTexture(gTextureUnits[0]);
2016 setupDraw();
2017 setupDrawDirtyRegionsDisabled();
2018 setupDrawWithTexture(true);
2019 setupDrawAlpha8Color(paint->getColor(), alpha);
2020 setupDrawColorFilter();
2021 setupDrawShader();
2022 setupDrawBlending(true, mode);
2023 setupDrawProgram();
2024 setupDrawModelView(x, y, x, y, pureTranslate, true);
2025 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2026 setupDrawPureColorUniforms();
2027 setupDrawColorFilterUniforms();
2028 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002029
Romain Guy6620c6d2010-12-06 18:07:02 -08002030 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002031 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2032
2033#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002034 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002035#else
Romain Guyf219da52011-01-16 12:54:25 -08002036 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002037#endif
Romain Guy03750a02010-10-18 14:06:08 -07002038 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002039
2040 // Tell font renderer the locations of position and texture coord
2041 // attributes so it can bind its data properly
2042 int positionSlot = mCaches.currentProgram->position;
2043 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002044 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002045 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002046#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002047 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002048 if (!pureTranslate) {
2049 mSnapshot->transform->mapRect(bounds);
2050 }
Romain Guyf219da52011-01-16 12:54:25 -08002051 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002052 }
2053#endif
2054 }
Romain Guy694b5192010-07-21 21:33:20 -07002055
2056 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002057 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002058
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002059 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002060}
2061
Romain Guy7fbcc042010-08-04 15:40:07 -07002062void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002063 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002064
Romain Guy01d58e42011-01-19 21:54:02 -08002065 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002066
Romain Guyfb8b7632010-08-23 21:05:08 -07002067 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002068 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002069 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002070
Romain Guy8b55f372010-08-18 17:10:07 -07002071 const float x = texture->left - texture->offset;
2072 const float y = texture->top - texture->offset;
2073
Romain Guy01d58e42011-01-19 21:54:02 -08002074 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002075}
2076
Romain Guyada830f2011-01-13 12:13:20 -08002077void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2078 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002079 return;
2080 }
2081
2082 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002083
2084 int alpha;
2085 SkXfermode::Mode mode;
2086 getAlphaAndMode(paint, &alpha, &mode);
2087
Romain Guyada830f2011-01-13 12:13:20 -08002088 layer->alpha = alpha;
2089 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08002090
Romain Guyf219da52011-01-16 12:54:25 -08002091#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002092 if (!layer->region.isEmpty()) {
2093 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002094 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002095 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002096 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002097 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002098
Romain Guyc88e3572011-01-22 00:32:12 -08002099 setupDraw();
2100 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002101 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002102 setupDrawColorFilter();
2103 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
2104 setupDrawProgram();
Romain Guy40667672011-03-18 14:34:03 -07002105 setupDrawModelViewTranslate(x, y,
2106 x + layer->layer.getWidth(), y + layer->layer.getHeight());
Romain Guyc88e3572011-01-22 00:32:12 -08002107 setupDrawPureColorUniforms();
2108 setupDrawColorFilterUniforms();
2109 setupDrawTexture(layer->texture);
Romain Guyc88e3572011-01-22 00:32:12 -08002110 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002111
Romain Guyc88e3572011-01-22 00:32:12 -08002112 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2113 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002114
Romain Guyc88e3572011-01-22 00:32:12 -08002115 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002116
2117#if DEBUG_LAYERS_AS_REGIONS
2118 drawRegionRects(layer->region);
2119#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002120 }
Romain Guyf219da52011-01-16 12:54:25 -08002121 }
2122#else
Romain Guyada830f2011-01-13 12:13:20 -08002123 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2124 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002125#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002126}
2127
Romain Guy6926c722010-07-12 20:20:03 -07002128///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002129// Shaders
2130///////////////////////////////////////////////////////////////////////////////
2131
2132void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002133 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002134}
2135
Romain Guy06f96e22010-07-30 19:18:16 -07002136void OpenGLRenderer::setupShader(SkiaShader* shader) {
2137 mShader = shader;
2138 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002139 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002140 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002141}
2142
Romain Guyd27977d2010-07-14 19:18:51 -07002143///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002144// Color filters
2145///////////////////////////////////////////////////////////////////////////////
2146
2147void OpenGLRenderer::resetColorFilter() {
2148 mColorFilter = NULL;
2149}
2150
2151void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2152 mColorFilter = filter;
2153}
2154
2155///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002156// Drop shadow
2157///////////////////////////////////////////////////////////////////////////////
2158
2159void OpenGLRenderer::resetShadow() {
2160 mHasShadow = false;
2161}
2162
2163void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2164 mHasShadow = true;
2165 mShadowRadius = radius;
2166 mShadowDx = dx;
2167 mShadowDy = dy;
2168 mShadowColor = color;
2169}
2170
2171///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002172// Drawing implementation
2173///////////////////////////////////////////////////////////////////////////////
2174
Romain Guy01d58e42011-01-19 21:54:02 -08002175void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2176 float x, float y, SkPaint* paint) {
2177 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2178 return;
2179 }
2180
2181 int alpha;
2182 SkXfermode::Mode mode;
2183 getAlphaAndMode(paint, &alpha, &mode);
2184
2185 setupDraw();
2186 setupDrawWithTexture(true);
2187 setupDrawAlpha8Color(paint->getColor(), alpha);
2188 setupDrawColorFilter();
2189 setupDrawShader();
2190 setupDrawBlending(true, mode);
2191 setupDrawProgram();
2192 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2193 setupDrawTexture(texture->id);
2194 setupDrawPureColorUniforms();
2195 setupDrawColorFilterUniforms();
2196 setupDrawShaderUniforms();
2197 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2198
2199 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2200
2201 finishDrawTexture();
2202}
2203
Romain Guyf607bdc2010-09-10 19:20:06 -07002204// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002205#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2206#define kStdUnderline_Offset (1.0f / 9.0f)
2207#define kStdUnderline_Thickness (1.0f / 18.0f)
2208
2209void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2210 float x, float y, SkPaint* paint) {
2211 // Handle underline and strike-through
2212 uint32_t flags = paint->getFlags();
2213 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
2214 float underlineWidth = length;
2215 // If length is > 0.0f, we already measured the text for the text alignment
2216 if (length <= 0.0f) {
2217 underlineWidth = paint->measureText(text, bytesCount);
2218 }
2219
2220 float offsetX = 0;
2221 switch (paint->getTextAlign()) {
2222 case SkPaint::kCenter_Align:
2223 offsetX = underlineWidth * 0.5f;
2224 break;
2225 case SkPaint::kRight_Align:
2226 offsetX = underlineWidth;
2227 break;
2228 default:
2229 break;
2230 }
2231
2232 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07002233 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002234 // TODO: Support stroke width < 1.0f when we have AA lines
2235 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002236
Romain Guye20ecbd2010-09-22 19:49:04 -07002237 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002238 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002239
Romain Guyf6834472011-01-23 13:32:12 -08002240 int linesCount = 0;
2241 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2242 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2243
2244 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002245 float points[pointsCount];
2246 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002247
2248 if (flags & SkPaint::kUnderlineText_Flag) {
2249 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002250 points[currentPoint++] = left;
2251 points[currentPoint++] = top;
2252 points[currentPoint++] = left + underlineWidth;
2253 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002254 }
2255
2256 if (flags & SkPaint::kStrikeThruText_Flag) {
2257 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002258 points[currentPoint++] = left;
2259 points[currentPoint++] = top;
2260 points[currentPoint++] = left + underlineWidth;
2261 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002262 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002263
2264 SkPaint linesPaint(*paint);
2265 linesPaint.setStrokeWidth(strokeWidth);
2266
2267 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07002268 }
2269 }
2270}
2271
Romain Guy026c5e162010-06-28 17:12:22 -07002272void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002273 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002274 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002275 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002276 color |= 0x00ffffff;
2277 }
2278
Romain Guy70ca14e2010-12-13 18:24:33 -08002279 setupDraw();
2280 setupDrawColor(color);
2281 setupDrawShader();
2282 setupDrawColorFilter();
2283 setupDrawBlending(mode);
2284 setupDrawProgram();
2285 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2286 setupDrawColorUniforms();
2287 setupDrawShaderUniforms(ignoreTransform);
2288 setupDrawColorFilterUniforms();
2289 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002290
Romain Guyc95c8d62010-09-17 15:31:32 -07002291 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2292}
2293
Romain Guy82ba8142010-07-09 13:25:56 -07002294void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002295 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002296 int alpha;
2297 SkXfermode::Mode mode;
2298 getAlphaAndMode(paint, &alpha, &mode);
2299
Romain Guy8164c2d2010-10-25 18:03:28 -07002300 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
2301
Romain Guy6620c6d2010-12-06 18:07:02 -08002302 if (mSnapshot->transform->isPureTranslate()) {
2303 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2304 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2305
2306 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2307 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2308 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2309 } else {
2310 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2311 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2312 GL_TRIANGLE_STRIP, gMeshCount);
2313 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002314}
2315
Romain Guybd6b79b2010-06-26 00:13:53 -07002316void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002317 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2318 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002319 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002320}
2321
2322void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002323 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002324 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002325 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002326
Romain Guy746b7402010-10-26 16:27:31 -07002327 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002328 setupDrawWithTexture();
2329 setupDrawColor(alpha, alpha, alpha, alpha);
2330 setupDrawColorFilter();
2331 setupDrawBlending(blend, mode, swapSrcDst);
2332 setupDrawProgram();
2333 if (!dirty) {
2334 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002335 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002336 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002337 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002338 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002339 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002340 }
Romain Guy86568192010-12-14 15:55:39 -08002341 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002342 setupDrawColorFilterUniforms();
2343 setupDrawTexture(texture);
2344 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002345
Romain Guy6820ac82010-09-15 18:11:50 -07002346 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002347
2348 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002349}
2350
Romain Guya5aed0d2010-09-09 14:42:43 -07002351void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002352 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002353 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2354 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002355 if (mode < SkXfermode::kPlus_Mode) {
2356 if (!mCaches.blend) {
2357 glEnable(GL_BLEND);
2358 }
Romain Guy82ba8142010-07-09 13:25:56 -07002359
Romain Guyf607bdc2010-09-10 19:20:06 -07002360 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2361 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002362
Romain Guya5aed0d2010-09-09 14:42:43 -07002363 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2364 glBlendFunc(sourceMode, destMode);
2365 mCaches.lastSrcMode = sourceMode;
2366 mCaches.lastDstMode = destMode;
2367 }
2368 } else {
2369 // These blend modes are not supported by OpenGL directly and have
2370 // to be implemented using shaders. Since the shader will perform
2371 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002372 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002373 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002374 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002375 }
2376
2377 if (mCaches.blend) {
2378 glDisable(GL_BLEND);
2379 }
2380 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002381 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002382 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002383 glDisable(GL_BLEND);
2384 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002385 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002386}
2387
Romain Guy889f8d12010-07-29 14:37:42 -07002388bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002389 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002390 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002391 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002392 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002393 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002394 }
Romain Guy6926c722010-07-12 20:20:03 -07002395 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002396}
2397
Romain Guy026c5e162010-06-28 17:12:22 -07002398void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002399 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002400 TextureVertex::setUV(v++, u1, v1);
2401 TextureVertex::setUV(v++, u2, v1);
2402 TextureVertex::setUV(v++, u1, v2);
2403 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002404}
2405
Chet Haase5c13d892010-10-08 08:37:55 -07002406void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002407 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002408 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002409 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2410 if (!isMode) {
2411 // Assume SRC_OVER
2412 *mode = SkXfermode::kSrcOver_Mode;
2413 }
2414 } else {
2415 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002416 }
2417
2418 // Skia draws using the color's alpha channel if < 255
2419 // Otherwise, it uses the paint's alpha
2420 int color = paint->getColor();
2421 *alpha = (color >> 24) & 0xFF;
2422 if (*alpha == 255) {
2423 *alpha = paint->getAlpha();
2424 }
2425 } else {
2426 *mode = SkXfermode::kSrcOver_Mode;
2427 *alpha = 255;
2428 }
Romain Guy026c5e162010-06-28 17:12:22 -07002429}
2430
Romain Guya5aed0d2010-09-09 14:42:43 -07002431SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08002432 // In the future we should look at unifying the Porter-Duff modes and
2433 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002434 if (mode == NULL) {
2435 return SkXfermode::kSrcOver_Mode;
2436 }
2437 return mode->fMode;
2438}
2439
Romain Guy746b7402010-10-26 16:27:31 -07002440void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002441 bool bound = false;
2442 if (wrapS != texture->wrapS) {
2443 glBindTexture(GL_TEXTURE_2D, texture->id);
2444 bound = true;
2445 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2446 texture->wrapS = wrapS;
2447 }
2448 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002449 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002450 glBindTexture(GL_TEXTURE_2D, texture->id);
2451 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002452 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2453 texture->wrapT = wrapT;
2454 }
Romain Guya1db5742010-07-20 13:09:13 -07002455}
2456
Romain Guy9d5316e2010-06-24 19:30:36 -07002457}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002458}; // namespace android