blob: 049e9b70bca92d5a1217c07cc53125513b35de1a [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();
640 setupDrawWithExternalTexture();
641 setupDrawColor(alpha, alpha, alpha, alpha);
642 setupDrawColorFilter();
643 setupDrawBlending(layer->blend, layer->mode);
644 setupDrawProgram();
645 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
646 setupDrawPureColorUniforms();
647 setupDrawColorFilterUniforms();
648 setupDrawExternalTexture(layer->texture);
649 setupDrawTextureTransform(layer->texTransform);
650 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
651
652 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
653
654 finishDrawTexture();
655}
656
Romain Guy5b3b3522010-10-27 18:57:51 -0700657void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 if (!layer->isTextureLayer) {
659 const Rect& texCoords = layer->texCoords;
660 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
661 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700662
Romain Guyaa6c24c2011-04-28 18:40:04 -0700663 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
664 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
665 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
Romain Guy5b3b3522010-10-27 18:57:51 -0700666
Romain Guyaa6c24c2011-04-28 18:40:04 -0700667 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
668 } else {
669 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
670 drawTextureLayer(layer, rect);
671 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
672 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700673}
674
675void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
676#if RENDER_LAYERS_AS_REGIONS
677 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700678 layer->setRegionAsRect();
679
Romain Guy40667672011-03-18 14:34:03 -0700680 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700681
Romain Guy5b3b3522010-10-27 18:57:51 -0700682 layer->region.clear();
683 return;
684 }
685
686 if (!layer->region.isEmpty()) {
687 size_t count;
688 const android::Rect* rects = layer->region.getArray(&count);
689
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700691 const float texX = 1.0f / float(layer->width);
692 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800693 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700694
695 TextureVertex* mesh = mCaches.getRegionMesh();
696 GLsizei numQuads = 0;
697
Romain Guy7230a742011-01-10 22:26:16 -0800698 setupDraw();
699 setupDrawWithTexture();
700 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800701 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800702 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
703 setupDrawProgram();
704 setupDrawDirtyRegionsDisabled();
705 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800706 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800707 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800708 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800709 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700710
711 for (size_t i = 0; i < count; i++) {
712 const android::Rect* r = &rects[i];
713
714 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800715 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700716 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800717 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700718
719 // TODO: Reject quads outside of the clip
720 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
721 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
722 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
723 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
724
725 numQuads++;
726
727 if (numQuads >= REGION_MESH_QUAD_COUNT) {
728 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
729 numQuads = 0;
730 mesh = mCaches.getRegionMesh();
731 }
732 }
733
734 if (numQuads > 0) {
735 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
736 }
737
738 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800739 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700740
741#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800742 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700743#endif
744
745 layer->region.clear();
746 }
747#else
748 composeLayerRect(layer, rect);
749#endif
750}
751
Romain Guy3a3133d2011-02-01 22:59:58 -0800752void OpenGLRenderer::drawRegionRects(const Region& region) {
753#if DEBUG_LAYERS_AS_REGIONS
754 size_t count;
755 const android::Rect* rects = region.getArray(&count);
756
757 uint32_t colors[] = {
758 0x7fff0000, 0x7f00ff00,
759 0x7f0000ff, 0x7fff00ff,
760 };
761
762 int offset = 0;
763 int32_t top = rects[0].top;
764
765 for (size_t i = 0; i < count; i++) {
766 if (top != rects[i].top) {
767 offset ^= 0x2;
768 top = rects[i].top;
769 }
770
771 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
772 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
773 SkXfermode::kSrcOver_Mode);
774 }
775#endif
776}
777
Romain Guy5b3b3522010-10-27 18:57:51 -0700778void OpenGLRenderer::dirtyLayer(const float left, const float top,
779 const float right, const float bottom, const mat4 transform) {
780#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800781 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700782 Rect bounds(left, top, right, bottom);
783 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800784 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 }
786#endif
787}
788
789void OpenGLRenderer::dirtyLayer(const float left, const float top,
790 const float right, const float bottom) {
791#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800792 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700793 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800794 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800795 }
796#endif
797}
798
799void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
800#if RENDER_LAYERS_AS_REGIONS
801 if (bounds.intersect(*mSnapshot->clipRect)) {
802 bounds.snapToPixelBoundaries();
803 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
804 if (!dirty.isEmpty()) {
805 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806 }
807 }
808#endif
809}
810
Romain Guybd6b79b2010-06-26 00:13:53 -0700811///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700812// Transforms
813///////////////////////////////////////////////////////////////////////////////
814
815void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700816 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700817}
818
819void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700820 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700821}
822
823void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700824 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700825}
826
Romain Guy807daf72011-01-18 11:19:19 -0800827void OpenGLRenderer::skew(float sx, float sy) {
828 mSnapshot->transform->skew(sx, sy);
829}
830
Romain Guyf6a11b82010-06-23 17:47:49 -0700831void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700832 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700833}
834
Romain Guy41030da2010-10-13 13:40:37 -0700835const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700836 if (mSnapshot->fbo != 0) {
837 return &mSnapshot->transform->data[0];
838 }
839 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700840}
841
Romain Guyf6a11b82010-06-23 17:47:49 -0700842void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700843 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700844}
845
846void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700847 SkMatrix transform;
848 mSnapshot->transform->copyTo(transform);
849 transform.preConcat(*matrix);
850 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700851}
852
853///////////////////////////////////////////////////////////////////////////////
854// Clipping
855///////////////////////////////////////////////////////////////////////////////
856
Romain Guybb9524b2010-06-22 18:56:38 -0700857void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700858 Rect clip(*mSnapshot->clipRect);
859 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700860 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700861 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700862}
863
864const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700865 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700866}
867
Romain Guyc7d53492010-06-25 13:41:57 -0700868bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800869 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700870 return true;
871 }
872
Romain Guy1d83e192010-08-17 11:37:00 -0700873 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700874 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700875 r.snapToPixelBoundaries();
876
877 Rect clipRect(*mSnapshot->clipRect);
878 clipRect.snapToPixelBoundaries();
879
880 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700881}
882
Romain Guy079ba2c2010-07-16 14:12:24 -0700883bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
884 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700885 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700886 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700887 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700888 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700889}
890
Romain Guyf6a11b82010-06-23 17:47:49 -0700891///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800892// Drawing commands
893///////////////////////////////////////////////////////////////////////////////
894
895void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800896 if (mDirtyClip) {
897 setScissorFromClip();
898 }
899 mDescription.reset();
900 mSetShaderColor = false;
901 mColorSet = false;
902 mColorA = mColorR = mColorG = mColorB = 0.0f;
903 mTextureUnit = 0;
904 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800905 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800906}
907
908void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
909 mDescription.hasTexture = true;
910 mDescription.hasAlpha8Texture = isAlpha8;
911}
912
Romain Guyaa6c24c2011-04-28 18:40:04 -0700913void OpenGLRenderer::setupDrawWithExternalTexture() {
914 mDescription.hasExternalTexture = true;
915}
916
Chet Haase5b0200b2011-04-13 17:58:08 -0700917void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -0700918 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -0700919}
920
Romain Guyed6fcb02011-03-21 13:11:28 -0700921void OpenGLRenderer::setupDrawPoint(float pointSize) {
922 mDescription.isPoint = true;
923 mDescription.pointSize = pointSize;
924}
925
Romain Guy70ca14e2010-12-13 18:24:33 -0800926void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800927 setupDrawColor(color, (color >> 24) & 0xFF);
928}
929
930void OpenGLRenderer::setupDrawColor(int color, int alpha) {
931 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700932 // Second divide of a by 255 is an optimization, allowing us to simply multiply
933 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -0800934 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800935 mColorR = a * ((color >> 16) & 0xFF);
936 mColorG = a * ((color >> 8) & 0xFF);
937 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800938 mColorSet = true;
939 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
940}
941
Romain Guy86568192010-12-14 15:55:39 -0800942void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
943 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700944 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
945 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -0800946 const float a = mColorA / 255.0f;
947 mColorR = a * ((color >> 16) & 0xFF);
948 mColorG = a * ((color >> 8) & 0xFF);
949 mColorB = a * ((color ) & 0xFF);
950 mColorSet = true;
951 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
952}
953
Romain Guy70ca14e2010-12-13 18:24:33 -0800954void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
955 mColorA = a;
956 mColorR = r;
957 mColorG = g;
958 mColorB = b;
959 mColorSet = true;
960 mSetShaderColor = mDescription.setColor(r, g, b, a);
961}
962
Romain Guy86568192010-12-14 15:55:39 -0800963void OpenGLRenderer::setupDrawAlpha8Color(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.setAlpha8Color(r, g, b, a);
970}
971
Romain Guy70ca14e2010-12-13 18:24:33 -0800972void OpenGLRenderer::setupDrawShader() {
973 if (mShader) {
974 mShader->describe(mDescription, mCaches.extensions);
975 }
976}
977
978void OpenGLRenderer::setupDrawColorFilter() {
979 if (mColorFilter) {
980 mColorFilter->describe(mDescription, mCaches.extensions);
981 }
982}
983
984void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
985 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
986 mDescription, swapSrcDst);
987}
988
989void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
990 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
991 mDescription, swapSrcDst);
992}
993
994void OpenGLRenderer::setupDrawProgram() {
995 useProgram(mCaches.programCache.get(mDescription));
996}
997
998void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
999 mTrackDirtyRegions = false;
1000}
1001
1002void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1003 bool ignoreTransform) {
1004 mModelView.loadTranslate(left, top, 0.0f);
1005 if (!ignoreTransform) {
1006 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1007 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1008 } else {
1009 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1010 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1011 }
1012}
1013
Chet Haase8a5cc922011-04-26 07:28:09 -07001014void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1015 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001016}
1017
Romain Guy70ca14e2010-12-13 18:24:33 -08001018void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1019 bool ignoreTransform, bool ignoreModelView) {
1020 if (!ignoreModelView) {
1021 mModelView.loadTranslate(left, top, 0.0f);
1022 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001023 } else {
1024 mModelView.loadIdentity();
1025 }
Romain Guy86568192010-12-14 15:55:39 -08001026 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1027 if (!ignoreTransform) {
1028 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1029 if (mTrackDirtyRegions && dirty) {
1030 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1031 }
1032 } else {
1033 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1034 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1035 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001036}
1037
Romain Guyed6fcb02011-03-21 13:11:28 -07001038void OpenGLRenderer::setupDrawPointUniforms() {
1039 int slot = mCaches.currentProgram->getUniform("pointSize");
1040 glUniform1f(slot, mDescription.pointSize);
1041}
1042
Romain Guy70ca14e2010-12-13 18:24:33 -08001043void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001044 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001045 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1046 }
1047}
1048
Romain Guy86568192010-12-14 15:55:39 -08001049void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001050 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001051 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001052 }
1053}
1054
Romain Guy70ca14e2010-12-13 18:24:33 -08001055void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1056 if (mShader) {
1057 if (ignoreTransform) {
1058 mModelView.loadInverse(*mSnapshot->transform);
1059 }
1060 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1061 }
1062}
1063
Romain Guy8d0d4782010-12-14 20:13:35 -08001064void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1065 if (mShader) {
1066 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1067 }
1068}
1069
Romain Guy70ca14e2010-12-13 18:24:33 -08001070void OpenGLRenderer::setupDrawColorFilterUniforms() {
1071 if (mColorFilter) {
1072 mColorFilter->setupProgram(mCaches.currentProgram);
1073 }
1074}
1075
1076void OpenGLRenderer::setupDrawSimpleMesh() {
1077 mCaches.bindMeshBuffer();
1078 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1079 gMeshStride, 0);
1080}
1081
1082void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1083 bindTexture(texture);
1084 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1085
1086 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1087 glEnableVertexAttribArray(mTexCoordsSlot);
1088}
1089
Romain Guyaa6c24c2011-04-28 18:40:04 -07001090void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1091 bindExternalTexture(texture);
1092 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1093
1094 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1095 glEnableVertexAttribArray(mTexCoordsSlot);
1096}
1097
1098void OpenGLRenderer::setupDrawTextureTransform(mat4& transform) {
1099 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1100 GL_FALSE, &transform.data[0]);
1101}
1102
Romain Guy70ca14e2010-12-13 18:24:33 -08001103void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1104 if (!vertices) {
1105 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1106 } else {
1107 mCaches.unbindMeshBuffer();
1108 }
1109 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1110 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001111 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001112 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1113 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001114}
1115
Chet Haase5b0200b2011-04-13 17:58:08 -07001116void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1117 mCaches.unbindMeshBuffer();
1118 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1119 gVertexStride, vertices);
1120}
1121
1122/**
1123 * 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 -07001124 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1125 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1126 * attributes (one per vertex) are values from zero to one that tells the fragment
1127 * shader where the fragment is in relation to the line width/length overall; these values are
1128 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1129 * region of the line.
1130 * Note that we only pass down the width values in this setup function. The length coordinates
1131 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001132 */
Chet Haase99585ad2011-05-02 15:00:16 -07001133void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
1134 GLvoid* lengthCoords, float strokeWidth) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001135 mCaches.unbindMeshBuffer();
1136 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001137 gAAVertexStride, vertices);
1138 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1139 glEnableVertexAttribArray(widthSlot);
1140 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1141 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1142 glEnableVertexAttribArray(lengthSlot);
1143 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001144 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001145 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001146 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1147 float boundaryWidth = (1 - strokeWidth) / 2;
Chet Haase5b0200b2011-04-13 17:58:08 -07001148 glUniform1f(boundaryWidthSlot, boundaryWidth);
1149 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidth));
1150}
1151
Romain Guy70ca14e2010-12-13 18:24:33 -08001152void OpenGLRenderer::finishDrawTexture() {
1153 glDisableVertexAttribArray(mTexCoordsSlot);
1154}
1155
1156///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001157// Drawing
1158///////////////////////////////////////////////////////////////////////////////
1159
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001160bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1161 Rect& dirty, uint32_t level) {
1162 if (quickReject(0.0f, 0.0f, width, height)) {
1163 return false;
1164 }
1165
Romain Guy0fe478e2010-11-08 12:08:41 -08001166 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1167 // will be performed by the display list itself
1168 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001169 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001170 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001171
Chet Haasedaf98e92011-01-10 14:10:36 -08001172 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001173}
1174
Romain Guya168d732011-03-18 16:50:13 -07001175void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1176 int alpha;
1177 SkXfermode::Mode mode;
1178 getAlphaAndMode(paint, &alpha, &mode);
1179
1180 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1181
1182 float x = left;
1183 float y = top;
1184
1185 bool ignoreTransform = false;
1186 if (mSnapshot->transform->isPureTranslate()) {
1187 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1188 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1189 ignoreTransform = true;
1190 }
1191
1192 setupDraw();
1193 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001194 if (paint) {
1195 setupDrawAlpha8Color(paint->getColor(), alpha);
1196 }
Romain Guya168d732011-03-18 16:50:13 -07001197 setupDrawColorFilter();
1198 setupDrawShader();
1199 setupDrawBlending(true, mode);
1200 setupDrawProgram();
1201 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
1202 setupDrawTexture(texture->id);
1203 setupDrawPureColorUniforms();
1204 setupDrawColorFilterUniforms();
1205 setupDrawShaderUniforms();
1206 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1207
1208 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1209
1210 finishDrawTexture();
1211}
1212
Chet Haase5c13d892010-10-08 08:37:55 -07001213void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001214 const float right = left + bitmap->width();
1215 const float bottom = top + bitmap->height();
1216
1217 if (quickReject(left, top, right, bottom)) {
1218 return;
1219 }
1220
Romain Guy86568192010-12-14 15:55:39 -08001221 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001222 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001223 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001224 const AutoTexture autoCleanup(texture);
1225
Romain Guya168d732011-03-18 16:50:13 -07001226 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1227 drawAlphaBitmap(texture, left, top, paint);
1228 } else {
1229 drawTextureRect(left, top, right, bottom, texture, paint);
1230 }
Romain Guyce0537b2010-06-29 21:05:21 -07001231}
1232
Chet Haase5c13d892010-10-08 08:37:55 -07001233void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001234 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1235 const mat4 transform(*matrix);
1236 transform.mapRect(r);
1237
Romain Guy6926c722010-07-12 20:20:03 -07001238 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1239 return;
1240 }
1241
Romain Guy86568192010-12-14 15:55:39 -08001242 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001243 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001244 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001245 const AutoTexture autoCleanup(texture);
1246
Romain Guy5b3b3522010-10-27 18:57:51 -07001247 // This could be done in a cheaper way, all we need is pass the matrix
1248 // to the vertex shader. The save/restore is a bit overkill.
1249 save(SkCanvas::kMatrix_SaveFlag);
1250 concatMatrix(matrix);
1251 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1252 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001253}
1254
Romain Guy5a7b4662011-01-20 19:09:30 -08001255void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1256 float* vertices, int* colors, SkPaint* paint) {
1257 // TODO: Do a quickReject
1258 if (!vertices || mSnapshot->isIgnored()) {
1259 return;
1260 }
1261
1262 glActiveTexture(gTextureUnits[0]);
1263 Texture* texture = mCaches.textureCache.get(bitmap);
1264 if (!texture) return;
1265 const AutoTexture autoCleanup(texture);
1266 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1267
1268 int alpha;
1269 SkXfermode::Mode mode;
1270 getAlphaAndMode(paint, &alpha, &mode);
1271
Romain Guy5a7b4662011-01-20 19:09:30 -08001272 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001273
Romain Guyb18d2d02011-02-10 15:52:54 -08001274 float left = FLT_MAX;
1275 float top = FLT_MAX;
1276 float right = FLT_MIN;
1277 float bottom = FLT_MIN;
1278
1279#if RENDER_LAYERS_AS_REGIONS
1280 bool hasActiveLayer = hasLayer();
1281#else
1282 bool hasActiveLayer = false;
1283#endif
1284
Romain Guya566b7c2011-01-23 16:36:11 -08001285 // TODO: Support the colors array
1286 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001287 TextureVertex* vertex = mesh;
1288 for (int32_t y = 0; y < meshHeight; y++) {
1289 for (int32_t x = 0; x < meshWidth; x++) {
1290 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1291
1292 float u1 = float(x) / meshWidth;
1293 float u2 = float(x + 1) / meshWidth;
1294 float v1 = float(y) / meshHeight;
1295 float v2 = float(y + 1) / meshHeight;
1296
1297 int ax = i + (meshWidth + 1) * 2;
1298 int ay = ax + 1;
1299 int bx = i;
1300 int by = bx + 1;
1301 int cx = i + 2;
1302 int cy = cx + 1;
1303 int dx = i + (meshWidth + 1) * 2 + 2;
1304 int dy = dx + 1;
1305
1306 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1307 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1308 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1309
1310 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1311 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1312 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001313
1314#if RENDER_LAYERS_AS_REGIONS
1315 if (hasActiveLayer) {
1316 // TODO: This could be optimized to avoid unnecessary ops
1317 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1318 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1319 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1320 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1321 }
1322#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001323 }
1324 }
1325
Romain Guyb18d2d02011-02-10 15:52:54 -08001326#if RENDER_LAYERS_AS_REGIONS
1327 if (hasActiveLayer) {
1328 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1329 }
1330#endif
1331
Romain Guy5a7b4662011-01-20 19:09:30 -08001332 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1333 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001334 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001335}
1336
Romain Guy8ba548f2010-06-30 19:21:21 -07001337void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1338 float srcLeft, float srcTop, float srcRight, float srcBottom,
1339 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001340 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001341 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1342 return;
1343 }
1344
Romain Guy746b7402010-10-26 16:27:31 -07001345 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001346 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001347 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001348 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001349 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001350
Romain Guy8ba548f2010-06-30 19:21:21 -07001351 const float width = texture->width;
1352 const float height = texture->height;
1353
1354 const float u1 = srcLeft / width;
1355 const float v1 = srcTop / height;
1356 const float u2 = srcRight / width;
1357 const float v2 = srcBottom / height;
1358
Romain Guy03750a02010-10-18 14:06:08 -07001359 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001360 resetDrawTextureTexCoords(u1, v1, u2, v2);
1361
Romain Guy03750a02010-10-18 14:06:08 -07001362 int alpha;
1363 SkXfermode::Mode mode;
1364 getAlphaAndMode(paint, &alpha, &mode);
1365
Romain Guy6620c6d2010-12-06 18:07:02 -08001366 if (mSnapshot->transform->isPureTranslate()) {
1367 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1368 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1369
1370 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1371 texture->id, alpha / 255.0f, mode, texture->blend,
1372 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1373 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1374 } else {
1375 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1376 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1377 GL_TRIANGLE_STRIP, gMeshCount);
1378 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001379
1380 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1381}
1382
Romain Guy4aa90572010-09-26 18:40:37 -07001383void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001384 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001385 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001386 if (quickReject(left, top, right, bottom)) {
1387 return;
1388 }
1389
Romain Guy746b7402010-10-26 16:27:31 -07001390 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001391 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001392 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001393 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001394 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001395
1396 int alpha;
1397 SkXfermode::Mode mode;
1398 getAlphaAndMode(paint, &alpha, &mode);
1399
Romain Guy2728f962010-10-08 18:36:15 -07001400 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001401 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001402
Romain Guya5ef39a2010-12-03 16:48:20 -08001403 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001404 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001405#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001406 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001407 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001408 const float offsetX = left + mSnapshot->transform->getTranslateX();
1409 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001410 const size_t count = mesh->quads.size();
1411 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001412 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001413 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001414 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1415 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1416 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001417 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001418 dirtyLayer(left + bounds.left, top + bounds.top,
1419 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001420 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001421 }
1422 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001423#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001424
Romain Guy6620c6d2010-12-06 18:07:02 -08001425 if (pureTranslate) {
1426 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1427 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1428
1429 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1430 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1431 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1432 true, !mesh->hasEmptyQuads);
1433 } else {
1434 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1435 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1436 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1437 true, !mesh->hasEmptyQuads);
1438 }
Romain Guy054dc182010-10-15 17:55:25 -07001439 }
Romain Guyf7f93552010-07-08 19:17:03 -07001440}
1441
Chet Haase8a5cc922011-04-26 07:28:09 -07001442void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1443 if (mSnapshot->isIgnored()) return;
1444
1445 const bool isAA = paint->isAntiAlias();
1446 float strokeWidth = paint->getStrokeWidth() * 0.5f;
1447 // A stroke width of 0 has a special meaning in Skia:
1448 // it draws a line 1 px wide regardless of current transform
1449 bool isHairLine = paint->getStrokeWidth() == 0.0f;
1450 int alpha;
1451 SkXfermode::Mode mode;
1452 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001453 int verticesCount = count;
1454 if (count > 4) {
1455 // Polyline: account for extra vertices needed for continous tri-strip
1456 verticesCount += (count -4);
1457 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001458
1459 getAlphaAndMode(paint, &alpha, &mode);
1460 setupDraw();
1461 if (isAA) {
1462 setupDrawAALine();
1463 }
1464 setupDrawColor(paint->getColor(), alpha);
1465 setupDrawColorFilter();
1466 setupDrawShader();
1467 if (isAA) {
1468 setupDrawBlending(true, mode);
1469 } else {
1470 setupDrawBlending(mode);
1471 }
1472 setupDrawProgram();
1473 setupDrawModelViewIdentity(true);
1474 setupDrawColorUniforms();
1475 setupDrawColorFilterUniforms();
1476 setupDrawShaderIdentityUniforms();
1477
1478 if (isHairLine) {
1479 // Set a real stroke width to be used in quad construction
1480 strokeWidth = .5;
1481 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001482 if (isAA) {
1483 // Expand boundary to enable AA calculations on the quad border
1484 strokeWidth += .5f;
1485 }
1486 Vertex lines[verticesCount];
1487 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001488 AAVertex wLines[verticesCount];
1489 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001490 if (!isAA) {
1491 setupDrawVertices(vertices);
1492 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001493 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1494 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase5b0200b2011-04-13 17:58:08 -07001495 // innerProportion is the ratio of the inner (non-AA) port of the line to the total
1496 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1497 // This value is used in the fragment shader to determine how to fill fragments.
1498 float innerProportion = fmax(strokeWidth - 1.0f, 0) / (strokeWidth + .5f);
Chet Haase99585ad2011-05-02 15:00:16 -07001499 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, innerProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001500 }
1501
Chet Haase99585ad2011-05-02 15:00:16 -07001502 AAVertex *prevAAVertex = NULL;
Chet Haase5b0200b2011-04-13 17:58:08 -07001503 Vertex *prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001504 float inverseScaleX = 1.0f;
1505 float inverseScaleY = 1.0f;
1506
Chet Haase8a5cc922011-04-26 07:28:09 -07001507 if (isHairLine) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001508 // The quad that we use for AA hairlines needs to account for scaling because the line
1509 // should always be one pixel wide regardless of scale.
Chet Haase5b0200b2011-04-13 17:58:08 -07001510 if (!mSnapshot->transform->isPureTranslate()) {
1511 Matrix4 *mat = mSnapshot->transform;
1512 float m00 = mat->data[Matrix4::kScaleX];
1513 float m01 = mat->data[Matrix4::kSkewY];
1514 float m02 = mat->data[2];
1515 float m10 = mat->data[Matrix4::kSkewX];
1516 float m11 = mat->data[Matrix4::kScaleX];
1517 float m12 = mat->data[6];
1518 float scaleX = sqrt(m00*m00 + m01*m01);
1519 float scaleY = sqrt(m10*m10 + m11*m11);
1520 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1521 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1522 }
1523 }
Romain Guy740bf2b2011-04-26 15:33:10 -07001524
Chet Haase99585ad2011-05-02 15:00:16 -07001525 int boundaryLengthSlot = -1;
1526 int inverseBoundaryLengthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001527 for (int i = 0; i < count; i += 4) {
1528 // a = start point, b = end point
1529 vec2 a(points[i], points[i + 1]);
1530 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001531 float length = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001532
Chet Haase5b0200b2011-04-13 17:58:08 -07001533 // Find the normal to the line
1534 vec2 n = (b - a).copyNormalized() * strokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001535 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001536 if (isAA) {
1537 float wideningFactor;
1538 if (fabs(n.x) >= fabs(n.y)) {
1539 wideningFactor = fabs(1.0f / n.x);
1540 } else {
1541 wideningFactor = fabs(1.0f / n.y);
1542 }
1543 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001544 }
Chet Haase99585ad2011-05-02 15:00:16 -07001545 n.x *= inverseScaleX;
1546 n.y *= inverseScaleY;
Chet Haase5b0200b2011-04-13 17:58:08 -07001547 }
1548 float x = n.x;
1549 n.x = -n.y;
1550 n.y = x;
1551
Chet Haase99585ad2011-05-02 15:00:16 -07001552 // aa lines expand the endpoint vertices to encompass the AA boundary
1553 if (isAA) {
1554 vec2 abVector = (b - a);
1555 length = abVector.length();
1556 abVector.normalize();
1557 a -= abVector;
1558 b += abVector;
1559 }
1560
Chet Haase5b0200b2011-04-13 17:58:08 -07001561 // Four corners of the rectangle defining a thick line
1562 vec2 p1 = a - n;
1563 vec2 p2 = a + n;
1564 vec2 p3 = b + n;
1565 vec2 p4 = b - n;
1566
Chet Haase99585ad2011-05-02 15:00:16 -07001567
Chet Haase5b0200b2011-04-13 17:58:08 -07001568 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1569 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1570 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1571 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1572
1573 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001574 if (!isAA) {
1575 if (prevVertex != NULL) {
1576 // Issue two repeat vertices to create degenerate triangles to bridge
1577 // between the previous line and the new one. This is necessary because
1578 // we are creating a single triangle_strip which will contain
1579 // potentially discontinuous line segments.
1580 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1581 Vertex::set(vertices++, p1.x, p1.y);
1582 generatedVerticesCount += 2;
1583 }
1584 Vertex::set(vertices++, p1.x, p1.y);
1585 Vertex::set(vertices++, p2.x, p2.y);
1586 Vertex::set(vertices++, p4.x, p4.y);
1587 Vertex::set(vertices++, p3.x, p3.y);
1588 prevVertex = vertices - 1;
1589 generatedVerticesCount += 4;
1590 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001591 if (boundaryLengthSlot < 0) {
1592 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1593 inverseBoundaryLengthSlot =
1594 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1595 }
1596 float innerProportion = (length) / (length + 2);
1597 float boundaryLength = (1 - innerProportion) / 2;
1598 glUniform1f(boundaryLengthSlot, boundaryLength);
1599 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLength));
1600
Chet Haase5b0200b2011-04-13 17:58:08 -07001601 if (prevAAVertex != NULL) {
1602 // Issue two repeat vertices to create degenerate triangles to bridge
1603 // between the previous line and the new one. This is necessary because
1604 // we are creating a single triangle_strip which will contain
1605 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001606 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1607 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1608 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001609 generatedVerticesCount += 2;
1610 }
Chet Haase99585ad2011-05-02 15:00:16 -07001611 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1612 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1613 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1614 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001615 prevAAVertex = aaVertices - 1;
1616 generatedVerticesCount += 4;
1617 }
Chet Haase99585ad2011-05-02 15:00:16 -07001618 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1619 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1620 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001621 }
1622 }
1623 if (generatedVerticesCount > 0) {
1624 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1625 }
1626}
1627
Romain Guyed6fcb02011-03-21 13:11:28 -07001628void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1629 if (mSnapshot->isIgnored()) return;
1630
1631 // TODO: The paint's cap style defines whether the points are square or circular
1632 // TODO: Handle AA for round points
1633
Chet Haase5b0200b2011-04-13 17:58:08 -07001634 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001635 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001636 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001637 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001638 if (isHairLine) {
1639 // Now that we know it's hairline, we can set the effective width, to be used later
1640 strokeWidth = 1.0f;
1641 }
1642 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001643 int alpha;
1644 SkXfermode::Mode mode;
1645 getAlphaAndMode(paint, &alpha, &mode);
1646
1647 int verticesCount = count >> 1;
1648 int generatedVerticesCount = 0;
1649
1650 TextureVertex pointsData[verticesCount];
1651 TextureVertex* vertex = &pointsData[0];
1652
1653 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001654 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001655 setupDrawColor(paint->getColor(), alpha);
1656 setupDrawColorFilter();
1657 setupDrawShader();
1658 setupDrawBlending(mode);
1659 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001660 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001661 setupDrawColorUniforms();
1662 setupDrawColorFilterUniforms();
1663 setupDrawPointUniforms();
1664 setupDrawShaderIdentityUniforms();
1665 setupDrawMesh(vertex);
1666
1667 for (int i = 0; i < count; i += 2) {
1668 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1669 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001670 float left = points[i] - halfWidth;
1671 float right = points[i] + halfWidth;
1672 float top = points[i + 1] - halfWidth;
1673 float bottom = points [i + 1] + halfWidth;
1674 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001675 }
1676
1677 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1678}
1679
Romain Guy85bf02f2010-06-22 13:11:24 -07001680void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001681 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001682 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001683
Romain Guyae88e5e2010-10-22 17:49:18 -07001684 Rect& clip(*mSnapshot->clipRect);
1685 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001686
Romain Guy3d58c032010-07-14 16:34:53 -07001687 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001688}
Romain Guy9d5316e2010-06-24 19:30:36 -07001689
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001690void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001691 if (!texture) return;
1692 const AutoTexture autoCleanup(texture);
1693
1694 const float x = left + texture->left - texture->offset;
1695 const float y = top + texture->top - texture->offset;
1696
1697 drawPathTexture(texture, x, y, paint);
1698}
1699
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001700void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1701 float rx, float ry, SkPaint* paint) {
1702 if (mSnapshot->isIgnored()) return;
1703
1704 glActiveTexture(gTextureUnits[0]);
1705 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1706 right - left, bottom - top, rx, ry, paint);
1707 drawShape(left, top, texture, paint);
1708}
1709
Romain Guy01d58e42011-01-19 21:54:02 -08001710void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1711 if (mSnapshot->isIgnored()) return;
1712
1713 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001714 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001715 drawShape(x - radius, y - radius, texture, paint);
1716}
Romain Guy01d58e42011-01-19 21:54:02 -08001717
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001718void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1719 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001720
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001721 glActiveTexture(gTextureUnits[0]);
1722 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1723 drawShape(left, top, texture, paint);
1724}
1725
Romain Guy8b2f5262011-01-23 16:15:02 -08001726void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1727 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1728 if (mSnapshot->isIgnored()) return;
1729
1730 if (fabs(sweepAngle) >= 360.0f) {
1731 drawOval(left, top, right, bottom, paint);
1732 return;
1733 }
1734
1735 glActiveTexture(gTextureUnits[0]);
1736 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1737 startAngle, sweepAngle, useCenter, paint);
1738 drawShape(left, top, texture, paint);
1739}
1740
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001741void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1742 SkPaint* paint) {
1743 if (mSnapshot->isIgnored()) return;
1744
1745 glActiveTexture(gTextureUnits[0]);
1746 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1747 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001748}
1749
Chet Haase5c13d892010-10-08 08:37:55 -07001750void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001751 if (p->getStyle() != SkPaint::kFill_Style) {
1752 drawRectAsShape(left, top, right, bottom, p);
1753 return;
1754 }
1755
Romain Guy6926c722010-07-12 20:20:03 -07001756 if (quickReject(left, top, right, bottom)) {
1757 return;
1758 }
1759
Romain Guy026c5e162010-06-28 17:12:22 -07001760 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001761 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001762 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1763 if (!isMode) {
1764 // Assume SRC_OVER
1765 mode = SkXfermode::kSrcOver_Mode;
1766 }
1767 } else {
1768 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001769 }
1770
Romain Guy026c5e162010-06-28 17:12:22 -07001771 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001772 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001773}
Romain Guy9d5316e2010-06-24 19:30:36 -07001774
Romain Guye8e62a42010-07-23 18:55:21 -07001775void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1776 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001777 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001778 return;
1779 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001780 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001781
Romain Guyf607bdc2010-09-10 19:20:06 -07001782 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001783
Romain Guya674ab72010-08-10 17:26:42 -07001784 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001785 switch (paint->getTextAlign()) {
1786 case SkPaint::kCenter_Align:
1787 length = paint->measureText(text, bytesCount);
1788 x -= length / 2.0f;
1789 break;
1790 case SkPaint::kRight_Align:
1791 length = paint->measureText(text, bytesCount);
1792 x -= length;
1793 break;
1794 default:
1795 break;
1796 }
1797
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001798 const float oldX = x;
1799 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001800 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1801 if (pureTranslate) {
1802 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1803 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1804 }
1805
Romain Guyb45c0c92010-08-26 20:35:23 -07001806 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1807 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001808 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001809
Romain Guy86568192010-12-14 15:55:39 -08001810 int alpha;
1811 SkXfermode::Mode mode;
1812 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001813
Romain Guy1e45aae2010-08-13 19:39:53 -07001814 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001815 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001816 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001817 count, mShadowRadius);
1818 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001819
Romain Guy740bf2b2011-04-26 15:33:10 -07001820 const float sx = oldX - shadow->left + mShadowDx;
1821 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001822
Romain Guy86568192010-12-14 15:55:39 -08001823 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07001824 int shadowColor = mShadowColor;
1825 if (mShader) {
1826 shadowColor = 0xffffffff;
1827 }
Romain Guy86568192010-12-14 15:55:39 -08001828
1829 glActiveTexture(gTextureUnits[0]);
1830 setupDraw();
1831 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07001832 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1833 setupDrawColorFilter();
1834 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08001835 setupDrawBlending(true, mode);
1836 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07001837 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08001838 setupDrawTexture(shadow->id);
1839 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07001840 setupDrawColorFilterUniforms();
1841 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08001842 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1843
Romain Guy0a417492010-08-16 20:26:20 -07001844 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07001845
Romain Guy86568192010-12-14 15:55:39 -08001846 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001847 }
1848
Romain Guyb146b122010-12-15 17:06:45 -08001849 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1850 return;
1851 }
1852
Romain Guy6620c6d2010-12-06 18:07:02 -08001853 // Pick the appropriate texture filtering
1854 bool linearFilter = mSnapshot->transform->changesBounds();
1855 if (pureTranslate && !linearFilter) {
1856 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1857 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001858
Romain Guy86568192010-12-14 15:55:39 -08001859 glActiveTexture(gTextureUnits[0]);
1860 setupDraw();
1861 setupDrawDirtyRegionsDisabled();
1862 setupDrawWithTexture(true);
1863 setupDrawAlpha8Color(paint->getColor(), alpha);
1864 setupDrawColorFilter();
1865 setupDrawShader();
1866 setupDrawBlending(true, mode);
1867 setupDrawProgram();
1868 setupDrawModelView(x, y, x, y, pureTranslate, true);
1869 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1870 setupDrawPureColorUniforms();
1871 setupDrawColorFilterUniforms();
1872 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001873
Romain Guy6620c6d2010-12-06 18:07:02 -08001874 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001875 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1876
1877#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001878 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001879#else
Romain Guyf219da52011-01-16 12:54:25 -08001880 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001881#endif
Romain Guy03750a02010-10-18 14:06:08 -07001882 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001883
1884 // Tell font renderer the locations of position and texture coord
1885 // attributes so it can bind its data properly
1886 int positionSlot = mCaches.currentProgram->position;
1887 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001888 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001889 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001890#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001891 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001892 if (!pureTranslate) {
1893 mSnapshot->transform->mapRect(bounds);
1894 }
Romain Guyf219da52011-01-16 12:54:25 -08001895 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001896 }
1897#endif
1898 }
Romain Guy694b5192010-07-21 21:33:20 -07001899
1900 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001901 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001902
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001903 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001904}
1905
Romain Guy7fbcc042010-08-04 15:40:07 -07001906void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001907 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001908
Romain Guy01d58e42011-01-19 21:54:02 -08001909 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001910
Romain Guyfb8b7632010-08-23 21:05:08 -07001911 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001912 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001913 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001914
Romain Guy8b55f372010-08-18 17:10:07 -07001915 const float x = texture->left - texture->offset;
1916 const float y = texture->top - texture->offset;
1917
Romain Guy01d58e42011-01-19 21:54:02 -08001918 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001919}
1920
Romain Guyada830f2011-01-13 12:13:20 -08001921void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1922 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001923 return;
1924 }
1925
1926 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001927
1928 int alpha;
1929 SkXfermode::Mode mode;
1930 getAlphaAndMode(paint, &alpha, &mode);
1931
Romain Guyada830f2011-01-13 12:13:20 -08001932 layer->alpha = alpha;
1933 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001934
Romain Guyf219da52011-01-16 12:54:25 -08001935#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001936 if (!layer->region.isEmpty()) {
1937 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07001938 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08001939 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001940 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001941 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001942
Romain Guyc88e3572011-01-22 00:32:12 -08001943 setupDraw();
1944 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001945 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001946 setupDrawColorFilter();
1947 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1948 setupDrawProgram();
Romain Guy40667672011-03-18 14:34:03 -07001949 setupDrawModelViewTranslate(x, y,
1950 x + layer->layer.getWidth(), y + layer->layer.getHeight());
Romain Guyc88e3572011-01-22 00:32:12 -08001951 setupDrawPureColorUniforms();
1952 setupDrawColorFilterUniforms();
1953 setupDrawTexture(layer->texture);
Romain Guyc88e3572011-01-22 00:32:12 -08001954 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001955
Romain Guyc88e3572011-01-22 00:32:12 -08001956 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1957 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001958
Romain Guyc88e3572011-01-22 00:32:12 -08001959 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001960
1961#if DEBUG_LAYERS_AS_REGIONS
1962 drawRegionRects(layer->region);
1963#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001964 }
Romain Guyf219da52011-01-16 12:54:25 -08001965 }
1966#else
Romain Guyada830f2011-01-13 12:13:20 -08001967 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1968 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001969#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001970}
1971
Romain Guy6926c722010-07-12 20:20:03 -07001972///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001973// Shaders
1974///////////////////////////////////////////////////////////////////////////////
1975
1976void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001977 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001978}
1979
Romain Guy06f96e22010-07-30 19:18:16 -07001980void OpenGLRenderer::setupShader(SkiaShader* shader) {
1981 mShader = shader;
1982 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001983 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001984 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001985}
1986
Romain Guyd27977d2010-07-14 19:18:51 -07001987///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001988// Color filters
1989///////////////////////////////////////////////////////////////////////////////
1990
1991void OpenGLRenderer::resetColorFilter() {
1992 mColorFilter = NULL;
1993}
1994
1995void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1996 mColorFilter = filter;
1997}
1998
1999///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002000// Drop shadow
2001///////////////////////////////////////////////////////////////////////////////
2002
2003void OpenGLRenderer::resetShadow() {
2004 mHasShadow = false;
2005}
2006
2007void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2008 mHasShadow = true;
2009 mShadowRadius = radius;
2010 mShadowDx = dx;
2011 mShadowDy = dy;
2012 mShadowColor = color;
2013}
2014
2015///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002016// Drawing implementation
2017///////////////////////////////////////////////////////////////////////////////
2018
Romain Guy01d58e42011-01-19 21:54:02 -08002019void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2020 float x, float y, SkPaint* paint) {
2021 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2022 return;
2023 }
2024
2025 int alpha;
2026 SkXfermode::Mode mode;
2027 getAlphaAndMode(paint, &alpha, &mode);
2028
2029 setupDraw();
2030 setupDrawWithTexture(true);
2031 setupDrawAlpha8Color(paint->getColor(), alpha);
2032 setupDrawColorFilter();
2033 setupDrawShader();
2034 setupDrawBlending(true, mode);
2035 setupDrawProgram();
2036 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2037 setupDrawTexture(texture->id);
2038 setupDrawPureColorUniforms();
2039 setupDrawColorFilterUniforms();
2040 setupDrawShaderUniforms();
2041 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2042
2043 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2044
2045 finishDrawTexture();
2046}
2047
Romain Guyf607bdc2010-09-10 19:20:06 -07002048// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002049#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2050#define kStdUnderline_Offset (1.0f / 9.0f)
2051#define kStdUnderline_Thickness (1.0f / 18.0f)
2052
2053void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2054 float x, float y, SkPaint* paint) {
2055 // Handle underline and strike-through
2056 uint32_t flags = paint->getFlags();
2057 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
2058 float underlineWidth = length;
2059 // If length is > 0.0f, we already measured the text for the text alignment
2060 if (length <= 0.0f) {
2061 underlineWidth = paint->measureText(text, bytesCount);
2062 }
2063
2064 float offsetX = 0;
2065 switch (paint->getTextAlign()) {
2066 case SkPaint::kCenter_Align:
2067 offsetX = underlineWidth * 0.5f;
2068 break;
2069 case SkPaint::kRight_Align:
2070 offsetX = underlineWidth;
2071 break;
2072 default:
2073 break;
2074 }
2075
2076 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07002077 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002078 // TODO: Support stroke width < 1.0f when we have AA lines
2079 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002080
Romain Guye20ecbd2010-09-22 19:49:04 -07002081 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002082 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002083
Romain Guyf6834472011-01-23 13:32:12 -08002084 int linesCount = 0;
2085 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2086 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2087
2088 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002089 float points[pointsCount];
2090 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002091
2092 if (flags & SkPaint::kUnderlineText_Flag) {
2093 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002094 points[currentPoint++] = left;
2095 points[currentPoint++] = top;
2096 points[currentPoint++] = left + underlineWidth;
2097 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002098 }
2099
2100 if (flags & SkPaint::kStrikeThruText_Flag) {
2101 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002102 points[currentPoint++] = left;
2103 points[currentPoint++] = top;
2104 points[currentPoint++] = left + underlineWidth;
2105 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002106 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002107
2108 SkPaint linesPaint(*paint);
2109 linesPaint.setStrokeWidth(strokeWidth);
2110
2111 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07002112 }
2113 }
2114}
2115
Romain Guy026c5e162010-06-28 17:12:22 -07002116void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002117 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002118 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002119 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002120 color |= 0x00ffffff;
2121 }
2122
Romain Guy70ca14e2010-12-13 18:24:33 -08002123 setupDraw();
2124 setupDrawColor(color);
2125 setupDrawShader();
2126 setupDrawColorFilter();
2127 setupDrawBlending(mode);
2128 setupDrawProgram();
2129 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2130 setupDrawColorUniforms();
2131 setupDrawShaderUniforms(ignoreTransform);
2132 setupDrawColorFilterUniforms();
2133 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002134
Romain Guyc95c8d62010-09-17 15:31:32 -07002135 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2136}
2137
Romain Guy82ba8142010-07-09 13:25:56 -07002138void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002139 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002140 int alpha;
2141 SkXfermode::Mode mode;
2142 getAlphaAndMode(paint, &alpha, &mode);
2143
Romain Guy8164c2d2010-10-25 18:03:28 -07002144 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
2145
Romain Guy6620c6d2010-12-06 18:07:02 -08002146 if (mSnapshot->transform->isPureTranslate()) {
2147 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2148 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2149
2150 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2151 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2152 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2153 } else {
2154 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2155 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2156 GL_TRIANGLE_STRIP, gMeshCount);
2157 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002158}
2159
Romain Guybd6b79b2010-06-26 00:13:53 -07002160void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002161 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2162 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002163 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002164}
2165
2166void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002167 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002168 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002169 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002170
Romain Guy746b7402010-10-26 16:27:31 -07002171 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002172 setupDrawWithTexture();
2173 setupDrawColor(alpha, alpha, alpha, alpha);
2174 setupDrawColorFilter();
2175 setupDrawBlending(blend, mode, swapSrcDst);
2176 setupDrawProgram();
2177 if (!dirty) {
2178 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002179 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002180 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002181 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002182 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002183 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002184 }
Romain Guy86568192010-12-14 15:55:39 -08002185 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002186 setupDrawColorFilterUniforms();
2187 setupDrawTexture(texture);
2188 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002189
Romain Guy6820ac82010-09-15 18:11:50 -07002190 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002191
2192 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002193}
2194
Romain Guya5aed0d2010-09-09 14:42:43 -07002195void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002196 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002197 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2198 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002199 if (mode < SkXfermode::kPlus_Mode) {
2200 if (!mCaches.blend) {
2201 glEnable(GL_BLEND);
2202 }
Romain Guy82ba8142010-07-09 13:25:56 -07002203
Romain Guyf607bdc2010-09-10 19:20:06 -07002204 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2205 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002206
Romain Guya5aed0d2010-09-09 14:42:43 -07002207 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2208 glBlendFunc(sourceMode, destMode);
2209 mCaches.lastSrcMode = sourceMode;
2210 mCaches.lastDstMode = destMode;
2211 }
2212 } else {
2213 // These blend modes are not supported by OpenGL directly and have
2214 // to be implemented using shaders. Since the shader will perform
2215 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002216 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002217 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002218 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002219 }
2220
2221 if (mCaches.blend) {
2222 glDisable(GL_BLEND);
2223 }
2224 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002225 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002226 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002227 glDisable(GL_BLEND);
2228 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002229 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002230}
2231
Romain Guy889f8d12010-07-29 14:37:42 -07002232bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002233 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002234 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002235 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002236 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002237 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002238 }
Romain Guy6926c722010-07-12 20:20:03 -07002239 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002240}
2241
Romain Guy026c5e162010-06-28 17:12:22 -07002242void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002243 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002244 TextureVertex::setUV(v++, u1, v1);
2245 TextureVertex::setUV(v++, u2, v1);
2246 TextureVertex::setUV(v++, u1, v2);
2247 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002248}
2249
Chet Haase5c13d892010-10-08 08:37:55 -07002250void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002251 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002252 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002253 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2254 if (!isMode) {
2255 // Assume SRC_OVER
2256 *mode = SkXfermode::kSrcOver_Mode;
2257 }
2258 } else {
2259 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002260 }
2261
2262 // Skia draws using the color's alpha channel if < 255
2263 // Otherwise, it uses the paint's alpha
2264 int color = paint->getColor();
2265 *alpha = (color >> 24) & 0xFF;
2266 if (*alpha == 255) {
2267 *alpha = paint->getAlpha();
2268 }
2269 } else {
2270 *mode = SkXfermode::kSrcOver_Mode;
2271 *alpha = 255;
2272 }
Romain Guy026c5e162010-06-28 17:12:22 -07002273}
2274
Romain Guya5aed0d2010-09-09 14:42:43 -07002275SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08002276 // In the future we should look at unifying the Porter-Duff modes and
2277 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002278 if (mode == NULL) {
2279 return SkXfermode::kSrcOver_Mode;
2280 }
2281 return mode->fMode;
2282}
2283
Romain Guy746b7402010-10-26 16:27:31 -07002284void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002285 bool bound = false;
2286 if (wrapS != texture->wrapS) {
2287 glBindTexture(GL_TEXTURE_2D, texture->id);
2288 bound = true;
2289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2290 texture->wrapS = wrapS;
2291 }
2292 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002293 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002294 glBindTexture(GL_TEXTURE_2D, texture->id);
2295 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2297 texture->wrapT = wrapT;
2298 }
Romain Guya1db5742010-07-20 13:09:13 -07002299}
2300
Romain Guy9d5316e2010-06-24 19:30:36 -07002301}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002302}; // namespace android