blob: 1c06a0b3a107b156e611e47718660eb7474e3bca [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080034#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080035#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070036
37namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044#define RAD_TO_DEG (180.0f / 3.14159265f)
45#define MIN_ANGLE 0.001f
46
Romain Guydbc26d22010-10-11 17:58:29 -070047// TODO: This should be set in properties
48#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49
Romain Guy9d5316e2010-06-24 19:30:36 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy889f8d12010-07-29 14:37:42 -070054/**
55 * Structure mapping Skia xfermodes to OpenGL blending factors.
56 */
57struct Blender {
58 SkXfermode::Mode mode;
59 GLenum src;
60 GLenum dst;
61}; // struct Blender
62
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
Romain Guyf504a2f2011-05-26 16:40:55 -070066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
Romain Guyb146b122010-12-15 17:06:45 -080067 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070078};
Romain Guye4d01122010-06-16 18:44:05 -070079
Romain Guy87a76572010-09-13 18:11:21 -070080// This array contains the swapped version of each SkXfermode. For instance
81// this array's SrcOver blending mode is actually DstOver. You can refer to
82// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070083static const Blender gBlendsSwap[] = {
Romain Guyf504a2f2011-05-26 16:40:55 -070084 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
Romain Guyb146b122010-12-15 17:06:45 -080085 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070096};
97
Romain Guy889f8d12010-07-29 14:37:42 -070098static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080099 GL_TEXTURE0,
100 GL_TEXTURE1,
101 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700102};
103
Romain Guyf6a11b82010-06-23 17:47:49 -0700104///////////////////////////////////////////////////////////////////////////////
105// Constructors/destructor
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guyfb8b7632010-08-23 21:05:08 -0700108OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700109 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700110 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700111 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700112
Romain Guyac670c02010-07-27 17:39:27 -0700113 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
114
Romain Guyae5575b2010-07-29 18:48:04 -0700115 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700116}
117
Romain Guy85bf02f2010-06-22 13:11:24 -0700118OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700119 // The context has already been destroyed at this point, do not call
120 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700121}
122
Romain Guyf6a11b82010-06-23 17:47:49 -0700123///////////////////////////////////////////////////////////////////////////////
124// Setup
125///////////////////////////////////////////////////////////////////////////////
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700128 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700129 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700130
131 mWidth = width;
132 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700133
134 mFirstSnapshot->height = height;
135 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700136
137 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700138}
139
Romain Guy6b7bd242010-10-06 19:49:23 -0700140void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800141 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
142}
143
144void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800145 mCaches.clearGarbage();
146
Romain Guy8aef54f2010-09-01 15:13:49 -0700147 mSnapshot = new Snapshot(mFirstSnapshot,
148 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800149 mSnapshot->fbo = getTargetFbo();
150
Romain Guy8fb95422010-08-17 18:38:51 -0700151 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700152
Romain Guyfb8b7632010-08-23 21:05:08 -0700153 glViewport(0, 0, mWidth, mHeight);
Romain Guyd90f23e2010-09-09 11:47:54 -0700154 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guy7d7b5492011-01-24 16:33:45 -0800156 glEnable(GL_SCISSOR_TEST);
157 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
158 mSnapshot->setClip(left, top, right, bottom);
159
Romain Guy6b7bd242010-10-06 19:49:23 -0700160 if (!opaque) {
161 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
162 glClear(GL_COLOR_BUFFER_BIT);
163 }
Romain Guybb9524b2010-06-22 18:56:38 -0700164}
165
Romain Guyb025b9c2010-09-16 14:16:48 -0700166void OpenGLRenderer::finish() {
167#if DEBUG_OPENGL
168 GLenum status = GL_NO_ERROR;
169 while ((status = glGetError()) != GL_NO_ERROR) {
170 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800171 switch (status) {
172 case GL_OUT_OF_MEMORY:
173 LOGE(" OpenGLRenderer is out of memory!");
174 break;
175 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700176 }
177#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800178#if DEBUG_MEMORY_USAGE
179 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800180#else
181 if (mCaches.getDebugLevel() & kDebugMemory) {
182 mCaches.dumpMemoryUsage();
183 }
Romain Guyc15008e2010-11-10 11:59:15 -0800184#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700185}
186
Romain Guy6c319ca2011-01-11 14:29:25 -0800187void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700188 if (mCaches.currentProgram) {
189 if (mCaches.currentProgram->isInUse()) {
190 mCaches.currentProgram->remove();
191 mCaches.currentProgram = NULL;
192 }
193 }
Romain Guy50c0f092010-10-19 11:42:22 -0700194 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700195}
196
Romain Guy6c319ca2011-01-11 14:29:25 -0800197void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700198 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700199
200 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700201 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700202
Romain Guyf607bdc2010-09-10 19:20:06 -0700203 glDisable(GL_DITHER);
204
Romain Guy42f3a4b2011-01-19 13:42:26 -0800205 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700206 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700207
Romain Guy50c0f092010-10-19 11:42:22 -0700208 mCaches.blend = true;
209 glEnable(GL_BLEND);
210 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
211 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700212}
213
Romain Guycabfcc12011-03-07 18:06:46 -0800214bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800215 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800216 if (mDirtyClip) {
217 setScissorFromClip();
218 }
Romain Guyd643bb52011-03-01 14:55:21 -0800219
Romain Guy80911b82011-03-16 15:30:12 -0700220 Rect clip(*mSnapshot->clipRect);
221 clip.snapToPixelBoundaries();
222
Romain Guyd643bb52011-03-01 14:55:21 -0800223#if RENDER_LAYERS_AS_REGIONS
224 // Since we don't know what the functor will draw, let's dirty
225 // tne entire clip region
226 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800227 dirtyLayerUnchecked(clip, getRegion());
228 }
229#endif
230
Romain Guy08aa2cb2011-03-17 11:06:57 -0700231 DrawGlInfo info;
232 info.clipLeft = clip.left;
233 info.clipTop = clip.top;
234 info.clipRight = clip.right;
235 info.clipBottom = clip.bottom;
236 info.isLayer = hasLayer();
237 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700238
Romain Guy08aa2cb2011-03-17 11:06:57 -0700239 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800240
241 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700242 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800243 dirty.unionWith(localDirty);
244 }
245
Chet Haasedaf98e92011-01-10 14:10:36 -0800246 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800247 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800248}
249
Romain Guyf6a11b82010-06-23 17:47:49 -0700250///////////////////////////////////////////////////////////////////////////////
251// State management
252///////////////////////////////////////////////////////////////////////////////
253
Romain Guybb9524b2010-06-22 18:56:38 -0700254int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700255 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700256}
257
258int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700259 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700260}
261
262void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700263 if (mSaveCount > 1) {
264 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700265 }
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
268void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700269 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700270
Romain Guy8fb95422010-08-17 18:38:51 -0700271 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700272 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
Romain Guy8aef54f2010-09-01 15:13:49 -0700276int OpenGLRenderer::saveSnapshot(int flags) {
277 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700278 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700279}
280
281bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700282 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700283 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700284 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700285
Romain Guybd6b79b2010-06-26 00:13:53 -0700286 sp<Snapshot> current = mSnapshot;
287 sp<Snapshot> previous = mSnapshot->previous;
288
Romain Guyeb993562010-10-05 18:14:38 -0700289 if (restoreOrtho) {
290 Rect& r = previous->viewport;
291 glViewport(r.left, r.top, r.right, r.bottom);
292 mOrthoMatrix.load(current->orthoMatrix);
293 }
294
Romain Guy8b55f372010-08-18 17:10:07 -0700295 mSaveCount--;
296 mSnapshot = previous;
297
Romain Guy2542d192010-08-18 11:47:12 -0700298 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700299 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700300 }
Romain Guy2542d192010-08-18 11:47:12 -0700301
Romain Guy5ec99242010-11-03 16:19:08 -0700302 if (restoreLayer) {
303 composeLayer(current, previous);
304 }
305
Romain Guy2542d192010-08-18 11:47:12 -0700306 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700307}
308
Romain Guyf6a11b82010-06-23 17:47:49 -0700309///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700310// Layers
311///////////////////////////////////////////////////////////////////////////////
312
313int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700314 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700315 const GLuint previousFbo = mSnapshot->fbo;
316 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700317
Romain Guyaf636eb2010-12-09 17:47:21 -0800318 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700319 int alpha = 255;
320 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700321
Romain Guye45362c2010-11-03 19:58:32 -0700322 if (p) {
323 alpha = p->getAlpha();
324 if (!mCaches.extensions.hasFramebufferFetch()) {
325 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
326 if (!isMode) {
327 // Assume SRC_OVER
328 mode = SkXfermode::kSrcOver_Mode;
329 }
330 } else {
331 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700332 }
333 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700334 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700335 }
Romain Guyd55a8612010-06-28 17:42:46 -0700336
Romain Guydbc26d22010-10-11 17:58:29 -0700337 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
338 }
Romain Guyd55a8612010-06-28 17:42:46 -0700339
340 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700341}
342
343int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
344 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700345 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700346 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700347 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700348 SkPaint paint;
349 paint.setAlpha(alpha);
350 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700351 }
Romain Guyd55a8612010-06-28 17:42:46 -0700352}
Romain Guybd6b79b2010-06-26 00:13:53 -0700353
Romain Guy1c740bc2010-09-13 18:00:09 -0700354/**
355 * Layers are viewed by Skia are slightly different than layers in image editing
356 * programs (for instance.) When a layer is created, previously created layers
357 * and the frame buffer still receive every drawing command. For instance, if a
358 * layer is created and a shape intersecting the bounds of the layers and the
359 * framebuffer is draw, the shape will be drawn on both (unless the layer was
360 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
361 *
362 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
363 * texture. Unfortunately, this is inefficient as it requires every primitive to
364 * be drawn n + 1 times, where n is the number of active layers. In practice this
365 * means, for every primitive:
366 * - Switch active frame buffer
367 * - Change viewport, clip and projection matrix
368 * - Issue the drawing
369 *
370 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700371 * To avoid this, layers are implemented in a different way here, at least in the
372 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
373 * is set. When this flag is set we can redirect all drawing operations into a
374 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700375 *
376 * This implementation relies on the frame buffer being at least RGBA 8888. When
377 * a layer is created, only a texture is created, not an FBO. The content of the
378 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700379 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700380 * buffer and drawing continues as normal. This technique therefore treats the
381 * frame buffer as a scratch buffer for the layers.
382 *
383 * To compose the layers back onto the frame buffer, each layer texture
384 * (containing the original frame buffer data) is drawn as a simple quad over
385 * the frame buffer. The trick is that the quad is set as the composition
386 * destination in the blending equation, and the frame buffer becomes the source
387 * of the composition.
388 *
389 * Drawing layers with an alpha value requires an extra step before composition.
390 * An empty quad is drawn over the layer's region in the frame buffer. This quad
391 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
392 * quad is used to multiply the colors in the frame buffer. This is achieved by
393 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
394 * GL_ZERO, GL_SRC_ALPHA.
395 *
396 * Because glCopyTexImage2D() can be slow, an alternative implementation might
397 * be use to draw a single clipped layer. The implementation described above
398 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700399 *
400 * (1) The frame buffer is actually not cleared right away. To allow the GPU
401 * to potentially optimize series of calls to glCopyTexImage2D, the frame
402 * buffer is left untouched until the first drawing operation. Only when
403 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700404 */
Romain Guyd55a8612010-06-28 17:42:46 -0700405bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700406 float right, float bottom, int alpha, SkXfermode::Mode mode,
407 int flags, GLuint previousFbo) {
408 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700409 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700410
Romain Guyeb993562010-10-05 18:14:38 -0700411 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
412
Romain Guyf607bdc2010-09-10 19:20:06 -0700413 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700414 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700415 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700416 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700417
Romain Guyeb993562010-10-05 18:14:38 -0700418 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700419 if (bounds.intersect(*snapshot->clipRect)) {
420 // We cannot work with sub-pixels in this case
421 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700422
Romain Guyad37cd32011-03-15 11:12:25 -0700423 // When the layer is not an FBO, we may use glCopyTexImage so we
424 // need to make sure the layer does not extend outside the bounds
425 // of the framebuffer
426 if (!bounds.intersect(snapshot->previous->viewport)) {
427 bounds.setEmpty();
428 }
429 } else {
430 bounds.setEmpty();
431 }
Romain Guyeb993562010-10-05 18:14:38 -0700432 }
Romain Guybf434112010-09-16 14:40:17 -0700433
Romain Guy746b7402010-10-26 16:27:31 -0700434 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
435 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800436 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700437 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700438 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700439 }
440
441 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800442 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700443 return false;
444 }
Romain Guyf18fd992010-07-08 11:45:51 -0700445
Romain Guy5b3b3522010-10-27 18:57:51 -0700446 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700447 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700448 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700449 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700450 }
451
Romain Guydda570202010-07-06 11:39:32 -0700452 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700453 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700454 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700455 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
456 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800457 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700458
Romain Guy8fb95422010-08-17 18:38:51 -0700459 // Save the layer in the snapshot
460 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700461 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700462
Romain Guyeb993562010-10-05 18:14:38 -0700463 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700464 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700465 } else {
466 // Copy the framebuffer into the layer
467 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800468 if (!bounds.isEmpty()) {
469 if (layer->empty) {
470 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
471 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
472 layer->empty = false;
473 } else {
474 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
475 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
476 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700477
Romain Guy54be1cd2011-06-13 19:04:27 -0700478 // Enqueue the buffer coordinates to clear the corresponding region later
479 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700480 }
Romain Guyeb993562010-10-05 18:14:38 -0700481 }
Romain Guyf86ef572010-07-01 11:05:42 -0700482
Romain Guyd55a8612010-06-28 17:42:46 -0700483 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700484}
485
Romain Guy5b3b3522010-10-27 18:57:51 -0700486bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
487 GLuint previousFbo) {
488 layer->fbo = mCaches.fboCache.get();
489
490#if RENDER_LAYERS_AS_REGIONS
491 snapshot->region = &snapshot->layer->region;
492 snapshot->flags |= Snapshot::kFlagFboTarget;
493#endif
494
495 Rect clip(bounds);
496 snapshot->transform->mapRect(clip);
497 clip.intersect(*snapshot->clipRect);
498 clip.snapToPixelBoundaries();
499 clip.intersect(snapshot->previous->viewport);
500
501 mat4 inverse;
502 inverse.loadInverse(*mSnapshot->transform);
503
504 inverse.mapRect(clip);
505 clip.snapToPixelBoundaries();
506 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700507 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700508
509 snapshot->flags |= Snapshot::kFlagIsFboLayer;
510 snapshot->fbo = layer->fbo;
511 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700512 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
513 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
514 snapshot->height = bounds.getHeight();
515 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
516 snapshot->orthoMatrix.load(mOrthoMatrix);
517
518 // Bind texture to FBO
519 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
520 glBindTexture(GL_TEXTURE_2D, layer->texture);
521
522 // Initialize the texture if needed
523 if (layer->empty) {
524 layer->empty = false;
525 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
526 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
527 }
528
529 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
530 layer->texture, 0);
531
532#if DEBUG_LAYERS_AS_REGIONS
533 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
534 if (status != GL_FRAMEBUFFER_COMPLETE) {
535 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
536
537 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
538 glDeleteTextures(1, &layer->texture);
539 mCaches.fboCache.put(layer->fbo);
540
541 delete layer;
542
543 return false;
544 }
545#endif
546
547 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
548 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
549 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
550 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
551 glClear(GL_COLOR_BUFFER_BIT);
552
553 dirtyClip();
554
555 // Change the ortho projection
556 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
557 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
558
559 return true;
560}
561
Romain Guy1c740bc2010-09-13 18:00:09 -0700562/**
563 * Read the documentation of createLayer() before doing anything in this method.
564 */
Romain Guy1d83e192010-08-17 11:37:00 -0700565void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
566 if (!current->layer) {
567 LOGE("Attempting to compose a layer that does not exist");
568 return;
569 }
570
Romain Guy5b3b3522010-10-27 18:57:51 -0700571 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700572
573 if (fboLayer) {
574 // Unbind current FBO and restore previous one
575 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
576 }
577
Romain Guy1d83e192010-08-17 11:37:00 -0700578 Layer* layer = current->layer;
579 const Rect& rect = layer->layer;
580
Romain Guyeb993562010-10-05 18:14:38 -0700581 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700582 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700583 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700584 // Required below, composeLayerRect() will divide by 255
585 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700586 }
587
Romain Guy03750a02010-10-18 14:06:08 -0700588 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700589
Romain Guy746b7402010-10-26 16:27:31 -0700590 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700591
Romain Guy5b3b3522010-10-27 18:57:51 -0700592 // When the layer is stored in an FBO, we can save a bit of fillrate by
593 // drawing only the dirty region
594 if (fboLayer) {
595 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800596 if (layer->colorFilter) {
597 setupColorFilter(layer->colorFilter);
598 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700599 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800600 if (layer->colorFilter) {
601 resetColorFilter();
602 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700603 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800604 if (!rect.isEmpty()) {
605 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
606 composeLayerRect(layer, rect, true);
607 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 }
Romain Guy8b55f372010-08-18 17:10:07 -0700609
Romain Guyeb993562010-10-05 18:14:38 -0700610 if (fboLayer) {
611 // Detach the texture from the FBO
612 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
613 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
614 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
615
616 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
617 mCaches.fboCache.put(current->fbo);
618 }
619
Romain Guy746b7402010-10-26 16:27:31 -0700620 dirtyClip();
621
Romain Guyeb993562010-10-05 18:14:38 -0700622 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700623 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700624 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700625 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700626 delete layer;
627 }
628}
629
Romain Guyaa6c24c2011-04-28 18:40:04 -0700630void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
631 float alpha = layer->alpha / 255.0f;
632
633 setupDraw();
Romain Guy8f0095c2011-05-02 17:24:22 -0700634 if (layer->renderTarget == GL_TEXTURE_2D) {
635 setupDrawWithTexture();
636 } else {
637 setupDrawWithExternalTexture();
638 }
639 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640 setupDrawColor(alpha, alpha, alpha, alpha);
641 setupDrawColorFilter();
Romain Guya9489272011-06-22 20:58:11 -0700642 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700643 setupDrawProgram();
644 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
645 setupDrawPureColorUniforms();
646 setupDrawColorFilterUniforms();
Romain Guy8f0095c2011-05-02 17:24:22 -0700647 if (layer->renderTarget == GL_TEXTURE_2D) {
648 setupDrawTexture(layer->texture);
649 } else {
650 setupDrawExternalTexture(layer->texture);
651 }
652 setupDrawTextureTransformUniforms(layer->texTransform);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
654
655 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
656
657 finishDrawTexture();
658}
659
Romain Guy5b3b3522010-10-27 18:57:51 -0700660void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700661 if (!layer->isTextureLayer) {
662 const Rect& texCoords = layer->texCoords;
663 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
664 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700665
Romain Guyaa6c24c2011-04-28 18:40:04 -0700666 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
667 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
668 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
Romain Guy5b3b3522010-10-27 18:57:51 -0700669
Romain Guyaa6c24c2011-04-28 18:40:04 -0700670 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
671 } else {
672 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
673 drawTextureLayer(layer, rect);
674 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
675 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700676}
677
678void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
679#if RENDER_LAYERS_AS_REGIONS
680 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700681 layer->setRegionAsRect();
682
Romain Guy40667672011-03-18 14:34:03 -0700683 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700684
Romain Guy5b3b3522010-10-27 18:57:51 -0700685 layer->region.clear();
686 return;
687 }
688
689 if (!layer->region.isEmpty()) {
690 size_t count;
691 const android::Rect* rects = layer->region.getArray(&count);
692
Romain Guy5b3b3522010-10-27 18:57:51 -0700693 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700694 const float texX = 1.0f / float(layer->width);
695 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800696 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700697
698 TextureVertex* mesh = mCaches.getRegionMesh();
699 GLsizei numQuads = 0;
700
Romain Guy7230a742011-01-10 22:26:16 -0800701 setupDraw();
702 setupDrawWithTexture();
703 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800704 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800705 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
706 setupDrawProgram();
707 setupDrawDirtyRegionsDisabled();
708 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800709 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800710 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800711 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800712 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700713
714 for (size_t i = 0; i < count; i++) {
715 const android::Rect* r = &rects[i];
716
717 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800718 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700719 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800720 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700721
722 // TODO: Reject quads outside of the clip
723 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
724 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
725 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
726 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
727
728 numQuads++;
729
730 if (numQuads >= REGION_MESH_QUAD_COUNT) {
731 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
732 numQuads = 0;
733 mesh = mCaches.getRegionMesh();
734 }
735 }
736
737 if (numQuads > 0) {
738 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
739 }
740
741 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800742 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700743
744#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800745 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700746#endif
747
748 layer->region.clear();
749 }
750#else
751 composeLayerRect(layer, rect);
752#endif
753}
754
Romain Guy3a3133d2011-02-01 22:59:58 -0800755void OpenGLRenderer::drawRegionRects(const Region& region) {
756#if DEBUG_LAYERS_AS_REGIONS
757 size_t count;
758 const android::Rect* rects = region.getArray(&count);
759
760 uint32_t colors[] = {
761 0x7fff0000, 0x7f00ff00,
762 0x7f0000ff, 0x7fff00ff,
763 };
764
765 int offset = 0;
766 int32_t top = rects[0].top;
767
768 for (size_t i = 0; i < count; i++) {
769 if (top != rects[i].top) {
770 offset ^= 0x2;
771 top = rects[i].top;
772 }
773
774 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
775 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
776 SkXfermode::kSrcOver_Mode);
777 }
778#endif
779}
780
Romain Guy5b3b3522010-10-27 18:57:51 -0700781void OpenGLRenderer::dirtyLayer(const float left, const float top,
782 const float right, const float bottom, const mat4 transform) {
783#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800784 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700785 Rect bounds(left, top, right, bottom);
786 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800787 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700788 }
789#endif
790}
791
792void OpenGLRenderer::dirtyLayer(const float left, const float top,
793 const float right, const float bottom) {
794#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800795 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700796 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800797 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800798 }
799#endif
800}
801
802void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
803#if RENDER_LAYERS_AS_REGIONS
804 if (bounds.intersect(*mSnapshot->clipRect)) {
805 bounds.snapToPixelBoundaries();
806 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
807 if (!dirty.isEmpty()) {
808 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809 }
810 }
811#endif
812}
813
Romain Guy54be1cd2011-06-13 19:04:27 -0700814void OpenGLRenderer::clearLayerRegions() {
815 const size_t count = mLayers.size();
816 if (count == 0) return;
817
818 if (!mSnapshot->isIgnored()) {
819 // Doing several glScissor/glClear here can negatively impact
820 // GPUs with a tiler architecture, instead we draw quads with
821 // the Clear blending mode
822
823 // The list contains bounds that have already been clipped
824 // against their initial clip rect, and the current clip
825 // is likely different so we need to disable clipping here
826 glDisable(GL_SCISSOR_TEST);
827
828 Vertex mesh[count * 6];
829 Vertex* vertex = mesh;
830
831 for (uint32_t i = 0; i < count; i++) {
832 Rect* bounds = mLayers.itemAt(i);
833
834 Vertex::set(vertex++, bounds->left, bounds->bottom);
835 Vertex::set(vertex++, bounds->left, bounds->top);
836 Vertex::set(vertex++, bounds->right, bounds->top);
837 Vertex::set(vertex++, bounds->left, bounds->bottom);
838 Vertex::set(vertex++, bounds->right, bounds->top);
839 Vertex::set(vertex++, bounds->right, bounds->bottom);
840
841 delete bounds;
842 }
843
844 setupDraw(false);
845 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
846 setupDrawBlending(true, SkXfermode::kClear_Mode);
847 setupDrawProgram();
848 setupDrawPureColorUniforms();
849 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
850
851 mCaches.unbindMeshBuffer();
852 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
853 gVertexStride, &mesh[0].position[0]);
854 glDrawArrays(GL_TRIANGLES, 0, count * 6);
855
856 glEnable(GL_SCISSOR_TEST);
857 } else {
858 for (uint32_t i = 0; i < count; i++) {
859 delete mLayers.itemAt(i);
860 }
861 }
862
863 mLayers.clear();
864}
865
Romain Guybd6b79b2010-06-26 00:13:53 -0700866///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700867// Transforms
868///////////////////////////////////////////////////////////////////////////////
869
870void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700871 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700872}
873
874void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700875 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700876}
877
878void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700879 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700880}
881
Romain Guy807daf72011-01-18 11:19:19 -0800882void OpenGLRenderer::skew(float sx, float sy) {
883 mSnapshot->transform->skew(sx, sy);
884}
885
Romain Guyf6a11b82010-06-23 17:47:49 -0700886void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700887 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700888}
889
Romain Guy41030da2010-10-13 13:40:37 -0700890const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700891 if (mSnapshot->fbo != 0) {
892 return &mSnapshot->transform->data[0];
893 }
894 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700895}
896
Romain Guyf6a11b82010-06-23 17:47:49 -0700897void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700898 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700899}
900
901void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700902 SkMatrix transform;
903 mSnapshot->transform->copyTo(transform);
904 transform.preConcat(*matrix);
905 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700906}
907
908///////////////////////////////////////////////////////////////////////////////
909// Clipping
910///////////////////////////////////////////////////////////////////////////////
911
Romain Guybb9524b2010-06-22 18:56:38 -0700912void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700913 Rect clip(*mSnapshot->clipRect);
914 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700915 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700916 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700917}
918
919const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700920 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700921}
922
Romain Guyc7d53492010-06-25 13:41:57 -0700923bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800924 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700925 return true;
926 }
927
Romain Guy1d83e192010-08-17 11:37:00 -0700928 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700929 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700930 r.snapToPixelBoundaries();
931
932 Rect clipRect(*mSnapshot->clipRect);
933 clipRect.snapToPixelBoundaries();
934
935 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700936}
937
Romain Guy079ba2c2010-07-16 14:12:24 -0700938bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
939 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700940 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700941 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700942 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700943 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700944}
945
Romain Guyf6a11b82010-06-23 17:47:49 -0700946///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800947// Drawing commands
948///////////////////////////////////////////////////////////////////////////////
949
Romain Guy54be1cd2011-06-13 19:04:27 -0700950void OpenGLRenderer::setupDraw(bool clear) {
951 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -0800952 if (mDirtyClip) {
953 setScissorFromClip();
954 }
955 mDescription.reset();
956 mSetShaderColor = false;
957 mColorSet = false;
958 mColorA = mColorR = mColorG = mColorB = 0.0f;
959 mTextureUnit = 0;
960 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800961 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800962}
963
964void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
965 mDescription.hasTexture = true;
966 mDescription.hasAlpha8Texture = isAlpha8;
967}
968
Romain Guyaa6c24c2011-04-28 18:40:04 -0700969void OpenGLRenderer::setupDrawWithExternalTexture() {
970 mDescription.hasExternalTexture = true;
971}
972
Chet Haase5b0200b2011-04-13 17:58:08 -0700973void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -0700974 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -0700975}
976
Romain Guyed6fcb02011-03-21 13:11:28 -0700977void OpenGLRenderer::setupDrawPoint(float pointSize) {
978 mDescription.isPoint = true;
979 mDescription.pointSize = pointSize;
980}
981
Romain Guy70ca14e2010-12-13 18:24:33 -0800982void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800983 setupDrawColor(color, (color >> 24) & 0xFF);
984}
985
986void OpenGLRenderer::setupDrawColor(int color, int alpha) {
987 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -0700988 // Second divide of a by 255 is an optimization, allowing us to simply multiply
989 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -0800990 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800991 mColorR = a * ((color >> 16) & 0xFF);
992 mColorG = a * ((color >> 8) & 0xFF);
993 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800994 mColorSet = true;
995 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
996}
997
Romain Guy86568192010-12-14 15:55:39 -0800998void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
999 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001000 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1001 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001002 const float a = mColorA / 255.0f;
1003 mColorR = a * ((color >> 16) & 0xFF);
1004 mColorG = a * ((color >> 8) & 0xFF);
1005 mColorB = a * ((color ) & 0xFF);
1006 mColorSet = true;
1007 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1008}
1009
Romain Guy70ca14e2010-12-13 18:24:33 -08001010void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1011 mColorA = a;
1012 mColorR = r;
1013 mColorG = g;
1014 mColorB = b;
1015 mColorSet = true;
1016 mSetShaderColor = mDescription.setColor(r, g, b, a);
1017}
1018
Romain Guy86568192010-12-14 15:55:39 -08001019void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1020 mColorA = a;
1021 mColorR = r;
1022 mColorG = g;
1023 mColorB = b;
1024 mColorSet = true;
1025 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1026}
1027
Romain Guy70ca14e2010-12-13 18:24:33 -08001028void OpenGLRenderer::setupDrawShader() {
1029 if (mShader) {
1030 mShader->describe(mDescription, mCaches.extensions);
1031 }
1032}
1033
1034void OpenGLRenderer::setupDrawColorFilter() {
1035 if (mColorFilter) {
1036 mColorFilter->describe(mDescription, mCaches.extensions);
1037 }
1038}
1039
Romain Guyf09ef512011-05-27 11:43:46 -07001040void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1041 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1042 mColorA = 1.0f;
1043 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001044 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001045 }
1046}
1047
Romain Guy70ca14e2010-12-13 18:24:33 -08001048void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001049 // When the blending mode is kClear_Mode, we need to use a modulate color
1050 // argb=1,0,0,0
1051 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001052 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1053 mDescription, swapSrcDst);
1054}
1055
1056void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001057 // When the blending mode is kClear_Mode, we need to use a modulate color
1058 // argb=1,0,0,0
1059 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001060 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1061 mDescription, swapSrcDst);
1062}
1063
1064void OpenGLRenderer::setupDrawProgram() {
1065 useProgram(mCaches.programCache.get(mDescription));
1066}
1067
1068void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1069 mTrackDirtyRegions = false;
1070}
1071
1072void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1073 bool ignoreTransform) {
1074 mModelView.loadTranslate(left, top, 0.0f);
1075 if (!ignoreTransform) {
1076 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1077 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1078 } else {
1079 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1080 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1081 }
1082}
1083
Chet Haase8a5cc922011-04-26 07:28:09 -07001084void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1085 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001086}
1087
Romain Guy70ca14e2010-12-13 18:24:33 -08001088void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1089 bool ignoreTransform, bool ignoreModelView) {
1090 if (!ignoreModelView) {
1091 mModelView.loadTranslate(left, top, 0.0f);
1092 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001093 } else {
1094 mModelView.loadIdentity();
1095 }
Romain Guy86568192010-12-14 15:55:39 -08001096 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1097 if (!ignoreTransform) {
1098 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1099 if (mTrackDirtyRegions && dirty) {
1100 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1101 }
1102 } else {
1103 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1104 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1105 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001106}
1107
Romain Guyed6fcb02011-03-21 13:11:28 -07001108void OpenGLRenderer::setupDrawPointUniforms() {
1109 int slot = mCaches.currentProgram->getUniform("pointSize");
1110 glUniform1f(slot, mDescription.pointSize);
1111}
1112
Romain Guy70ca14e2010-12-13 18:24:33 -08001113void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001114 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001115 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1116 }
1117}
1118
Romain Guy86568192010-12-14 15:55:39 -08001119void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001120 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001121 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001122 }
1123}
1124
Romain Guy70ca14e2010-12-13 18:24:33 -08001125void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1126 if (mShader) {
1127 if (ignoreTransform) {
1128 mModelView.loadInverse(*mSnapshot->transform);
1129 }
1130 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1131 }
1132}
1133
Romain Guy8d0d4782010-12-14 20:13:35 -08001134void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1135 if (mShader) {
1136 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1137 }
1138}
1139
Romain Guy70ca14e2010-12-13 18:24:33 -08001140void OpenGLRenderer::setupDrawColorFilterUniforms() {
1141 if (mColorFilter) {
1142 mColorFilter->setupProgram(mCaches.currentProgram);
1143 }
1144}
1145
1146void OpenGLRenderer::setupDrawSimpleMesh() {
1147 mCaches.bindMeshBuffer();
1148 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1149 gMeshStride, 0);
1150}
1151
1152void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1153 bindTexture(texture);
1154 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1155
1156 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1157 glEnableVertexAttribArray(mTexCoordsSlot);
1158}
1159
Romain Guyaa6c24c2011-04-28 18:40:04 -07001160void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1161 bindExternalTexture(texture);
1162 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1163
1164 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1165 glEnableVertexAttribArray(mTexCoordsSlot);
1166}
1167
Romain Guy8f0095c2011-05-02 17:24:22 -07001168void OpenGLRenderer::setupDrawTextureTransform() {
1169 mDescription.hasTextureTransform = true;
1170}
1171
1172void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001173 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1174 GL_FALSE, &transform.data[0]);
1175}
1176
Romain Guy70ca14e2010-12-13 18:24:33 -08001177void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1178 if (!vertices) {
1179 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1180 } else {
1181 mCaches.unbindMeshBuffer();
1182 }
1183 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1184 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001185 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001186 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1187 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001188}
1189
Chet Haase5b0200b2011-04-13 17:58:08 -07001190void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1191 mCaches.unbindMeshBuffer();
1192 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1193 gVertexStride, vertices);
1194}
1195
1196/**
1197 * 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 -07001198 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1199 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1200 * attributes (one per vertex) are values from zero to one that tells the fragment
1201 * shader where the fragment is in relation to the line width/length overall; these values are
1202 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1203 * region of the line.
1204 * Note that we only pass down the width values in this setup function. The length coordinates
1205 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001206 */
Chet Haase99585ad2011-05-02 15:00:16 -07001207void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001208 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001209 mCaches.unbindMeshBuffer();
1210 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001211 gAAVertexStride, vertices);
1212 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1213 glEnableVertexAttribArray(widthSlot);
1214 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1215 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1216 glEnableVertexAttribArray(lengthSlot);
1217 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001218 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001219 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001220 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001221 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001222 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001223}
1224
Romain Guy70ca14e2010-12-13 18:24:33 -08001225void OpenGLRenderer::finishDrawTexture() {
1226 glDisableVertexAttribArray(mTexCoordsSlot);
1227}
1228
1229///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001230// Drawing
1231///////////////////////////////////////////////////////////////////////////////
1232
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001233bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1234 Rect& dirty, uint32_t level) {
1235 if (quickReject(0.0f, 0.0f, width, height)) {
1236 return false;
1237 }
1238
Romain Guy0fe478e2010-11-08 12:08:41 -08001239 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1240 // will be performed by the display list itself
1241 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001242 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001243 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001244
Chet Haasedaf98e92011-01-10 14:10:36 -08001245 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001246}
1247
Chet Haaseed30fd82011-04-22 16:18:45 -07001248void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1249 if (displayList) {
1250 displayList->output(*this, level);
1251 }
1252}
1253
Romain Guya168d732011-03-18 16:50:13 -07001254void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1255 int alpha;
1256 SkXfermode::Mode mode;
1257 getAlphaAndMode(paint, &alpha, &mode);
1258
1259 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1260
1261 float x = left;
1262 float y = top;
1263
1264 bool ignoreTransform = false;
1265 if (mSnapshot->transform->isPureTranslate()) {
1266 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1267 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1268 ignoreTransform = true;
1269 }
1270
1271 setupDraw();
1272 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001273 if (paint) {
1274 setupDrawAlpha8Color(paint->getColor(), alpha);
1275 }
Romain Guya168d732011-03-18 16:50:13 -07001276 setupDrawColorFilter();
1277 setupDrawShader();
1278 setupDrawBlending(true, mode);
1279 setupDrawProgram();
1280 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
1281 setupDrawTexture(texture->id);
1282 setupDrawPureColorUniforms();
1283 setupDrawColorFilterUniforms();
1284 setupDrawShaderUniforms();
1285 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1286
1287 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1288
1289 finishDrawTexture();
1290}
1291
Chet Haase5c13d892010-10-08 08:37:55 -07001292void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001293 const float right = left + bitmap->width();
1294 const float bottom = top + bitmap->height();
1295
1296 if (quickReject(left, top, right, bottom)) {
1297 return;
1298 }
1299
Romain Guy86568192010-12-14 15:55:39 -08001300 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001301 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001302 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001303 const AutoTexture autoCleanup(texture);
1304
Romain Guya168d732011-03-18 16:50:13 -07001305 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1306 drawAlphaBitmap(texture, left, top, paint);
1307 } else {
1308 drawTextureRect(left, top, right, bottom, texture, paint);
1309 }
Romain Guyce0537b2010-06-29 21:05:21 -07001310}
1311
Chet Haase5c13d892010-10-08 08:37:55 -07001312void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001313 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1314 const mat4 transform(*matrix);
1315 transform.mapRect(r);
1316
Romain Guy6926c722010-07-12 20:20:03 -07001317 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1318 return;
1319 }
1320
Romain Guy86568192010-12-14 15:55:39 -08001321 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001322 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001323 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001324 const AutoTexture autoCleanup(texture);
1325
Romain Guy5b3b3522010-10-27 18:57:51 -07001326 // This could be done in a cheaper way, all we need is pass the matrix
1327 // to the vertex shader. The save/restore is a bit overkill.
1328 save(SkCanvas::kMatrix_SaveFlag);
1329 concatMatrix(matrix);
1330 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1331 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001332}
1333
Romain Guy5a7b4662011-01-20 19:09:30 -08001334void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1335 float* vertices, int* colors, SkPaint* paint) {
1336 // TODO: Do a quickReject
1337 if (!vertices || mSnapshot->isIgnored()) {
1338 return;
1339 }
1340
1341 glActiveTexture(gTextureUnits[0]);
1342 Texture* texture = mCaches.textureCache.get(bitmap);
1343 if (!texture) return;
1344 const AutoTexture autoCleanup(texture);
1345 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1346
1347 int alpha;
1348 SkXfermode::Mode mode;
1349 getAlphaAndMode(paint, &alpha, &mode);
1350
Romain Guy5a7b4662011-01-20 19:09:30 -08001351 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001352
Romain Guyb18d2d02011-02-10 15:52:54 -08001353 float left = FLT_MAX;
1354 float top = FLT_MAX;
1355 float right = FLT_MIN;
1356 float bottom = FLT_MIN;
1357
1358#if RENDER_LAYERS_AS_REGIONS
1359 bool hasActiveLayer = hasLayer();
1360#else
1361 bool hasActiveLayer = false;
1362#endif
1363
Romain Guya566b7c2011-01-23 16:36:11 -08001364 // TODO: Support the colors array
1365 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001366 TextureVertex* vertex = mesh;
1367 for (int32_t y = 0; y < meshHeight; y++) {
1368 for (int32_t x = 0; x < meshWidth; x++) {
1369 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1370
1371 float u1 = float(x) / meshWidth;
1372 float u2 = float(x + 1) / meshWidth;
1373 float v1 = float(y) / meshHeight;
1374 float v2 = float(y + 1) / meshHeight;
1375
1376 int ax = i + (meshWidth + 1) * 2;
1377 int ay = ax + 1;
1378 int bx = i;
1379 int by = bx + 1;
1380 int cx = i + 2;
1381 int cy = cx + 1;
1382 int dx = i + (meshWidth + 1) * 2 + 2;
1383 int dy = dx + 1;
1384
1385 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1386 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1387 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1388
1389 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1390 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1391 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001392
1393#if RENDER_LAYERS_AS_REGIONS
1394 if (hasActiveLayer) {
1395 // TODO: This could be optimized to avoid unnecessary ops
1396 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1397 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1398 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1399 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1400 }
1401#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001402 }
1403 }
1404
Romain Guyb18d2d02011-02-10 15:52:54 -08001405#if RENDER_LAYERS_AS_REGIONS
1406 if (hasActiveLayer) {
1407 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1408 }
1409#endif
1410
Romain Guy5a7b4662011-01-20 19:09:30 -08001411 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1412 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001413 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001414}
1415
Romain Guy8ba548f2010-06-30 19:21:21 -07001416void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1417 float srcLeft, float srcTop, float srcRight, float srcBottom,
1418 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001419 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001420 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1421 return;
1422 }
1423
Romain Guy746b7402010-10-26 16:27:31 -07001424 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001425 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001426 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001427 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001428 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001429
Romain Guy8ba548f2010-06-30 19:21:21 -07001430 const float width = texture->width;
1431 const float height = texture->height;
1432
Romain Guy1e59f9d2011-05-26 18:39:34 -07001433 const float u1 = (srcLeft + 0.5f) / width;
1434 const float v1 = (srcTop + 0.5f) / height;
1435 const float u2 = (srcRight - 0.5f) / width;
1436 const float v2 = (srcBottom - 0.5f) / height;
Romain Guy8ba548f2010-06-30 19:21:21 -07001437
Romain Guy03750a02010-10-18 14:06:08 -07001438 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001439 resetDrawTextureTexCoords(u1, v1, u2, v2);
1440
Romain Guy03750a02010-10-18 14:06:08 -07001441 int alpha;
1442 SkXfermode::Mode mode;
1443 getAlphaAndMode(paint, &alpha, &mode);
1444
Romain Guy6620c6d2010-12-06 18:07:02 -08001445 if (mSnapshot->transform->isPureTranslate()) {
1446 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1447 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1448
1449 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1450 texture->id, alpha / 255.0f, mode, texture->blend,
1451 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1452 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1453 } else {
1454 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1455 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1456 GL_TRIANGLE_STRIP, gMeshCount);
1457 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001458
1459 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1460}
1461
Romain Guy4aa90572010-09-26 18:40:37 -07001462void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001463 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001464 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001465 if (quickReject(left, top, right, bottom)) {
1466 return;
1467 }
1468
Romain Guy746b7402010-10-26 16:27:31 -07001469 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001470 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001471 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001472 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001473 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001474
1475 int alpha;
1476 SkXfermode::Mode mode;
1477 getAlphaAndMode(paint, &alpha, &mode);
1478
Romain Guy2728f962010-10-08 18:36:15 -07001479 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001480 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001481
Romain Guya5ef39a2010-12-03 16:48:20 -08001482 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001483 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001484#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001485 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001486 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001487 const float offsetX = left + mSnapshot->transform->getTranslateX();
1488 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001489 const size_t count = mesh->quads.size();
1490 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001491 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001492 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001493 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1494 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1495 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001496 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001497 dirtyLayer(left + bounds.left, top + bounds.top,
1498 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001499 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001500 }
1501 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001502#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001503
Romain Guy6620c6d2010-12-06 18:07:02 -08001504 if (pureTranslate) {
1505 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1506 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1507
1508 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1509 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1510 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1511 true, !mesh->hasEmptyQuads);
1512 } else {
1513 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1514 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1515 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1516 true, !mesh->hasEmptyQuads);
1517 }
Romain Guy054dc182010-10-15 17:55:25 -07001518 }
Romain Guyf7f93552010-07-08 19:17:03 -07001519}
1520
Chet Haase99ecdc42011-05-06 12:06:34 -07001521/**
Chet Haase858aa932011-05-12 09:06:00 -07001522 * This function uses a similar approach to that of AA lines in the drawLines() function.
1523 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1524 * shader to compute the translucency of the color, determined by whether a given pixel is
1525 * within that boundary region and how far into the region it is.
1526 */
1527void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001528 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001529 float inverseScaleX = 1.0f;
1530 float inverseScaleY = 1.0f;
1531 // The quad that we use needs to account for scaling.
1532 if (!mSnapshot->transform->isPureTranslate()) {
1533 Matrix4 *mat = mSnapshot->transform;
1534 float m00 = mat->data[Matrix4::kScaleX];
1535 float m01 = mat->data[Matrix4::kSkewY];
1536 float m02 = mat->data[2];
1537 float m10 = mat->data[Matrix4::kSkewX];
1538 float m11 = mat->data[Matrix4::kScaleX];
1539 float m12 = mat->data[6];
1540 float scaleX = sqrt(m00 * m00 + m01 * m01);
1541 float scaleY = sqrt(m10 * m10 + m11 * m11);
1542 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1543 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1544 }
1545
1546 setupDraw();
1547 setupDrawAALine();
1548 setupDrawColor(color);
1549 setupDrawColorFilter();
1550 setupDrawShader();
1551 setupDrawBlending(true, mode);
1552 setupDrawProgram();
1553 setupDrawModelViewIdentity(true);
1554 setupDrawColorUniforms();
1555 setupDrawColorFilterUniforms();
1556 setupDrawShaderIdentityUniforms();
1557
1558 AAVertex rects[4];
1559 AAVertex* aaVertices = &rects[0];
1560 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1561 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1562
1563 float boundarySizeX = .5 * inverseScaleX;
1564 float boundarySizeY = .5 * inverseScaleY;
1565
1566 // Adjust the rect by the AA boundary padding
1567 left -= boundarySizeX;
1568 right += boundarySizeX;
1569 top -= boundarySizeY;
1570 bottom += boundarySizeY;
1571
1572 float width = right - left;
1573 float height = bottom - top;
1574
1575 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1576 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1577 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1578 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1579 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1580 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1581 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1582
1583 if (!quickReject(left, top, right, bottom)) {
1584 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1585 AAVertex::set(aaVertices++, left, top, 1, 0);
1586 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1587 AAVertex::set(aaVertices++, right, top, 0, 0);
1588 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1589 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1590 }
1591}
1592
1593/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001594 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1595 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1596 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1597 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1598 * of the line. Hairlines are more involved because we need to account for transform scaling
1599 * to end up with a one-pixel-wide line in screen space..
1600 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1601 * in combination with values that we calculate and pass down in this method. The basic approach
1602 * is that the quad we create contains both the core line area plus a bounding area in which
1603 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1604 * proportion of the width and the length of a given segment is represented by the boundary
1605 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1606 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1607 * on the inside). This ends up giving the result we want, with pixels that are completely
1608 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1609 * how far into the boundary region they are, which is determined by shader interpolation.
1610 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001611void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1612 if (mSnapshot->isIgnored()) return;
1613
1614 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001615 // We use half the stroke width here because we're going to position the quad
1616 // corner vertices half of the width away from the line endpoints
1617 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001618 // A stroke width of 0 has a special meaning in Skia:
1619 // it draws a line 1 px wide regardless of current transform
1620 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001621 float inverseScaleX = 1.0f;
1622 float inverseScaleY = 1.0f;
1623 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001624 int alpha;
1625 SkXfermode::Mode mode;
1626 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001627 int verticesCount = count;
1628 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001629 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001630 verticesCount += (count - 4);
1631 }
1632
1633 if (isHairLine || isAA) {
1634 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1635 // the line on the screen should always be one pixel wide regardless of scale. For
1636 // AA lines, we only want one pixel of translucent boundary around the quad.
1637 if (!mSnapshot->transform->isPureTranslate()) {
1638 Matrix4 *mat = mSnapshot->transform;
1639 float m00 = mat->data[Matrix4::kScaleX];
1640 float m01 = mat->data[Matrix4::kSkewY];
1641 float m02 = mat->data[2];
1642 float m10 = mat->data[Matrix4::kSkewX];
1643 float m11 = mat->data[Matrix4::kScaleX];
1644 float m12 = mat->data[6];
1645 float scaleX = sqrt(m00*m00 + m01*m01);
1646 float scaleY = sqrt(m10*m10 + m11*m11);
1647 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1648 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1649 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1650 scaled = true;
1651 }
1652 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001653 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001654
1655 getAlphaAndMode(paint, &alpha, &mode);
1656 setupDraw();
1657 if (isAA) {
1658 setupDrawAALine();
1659 }
1660 setupDrawColor(paint->getColor(), alpha);
1661 setupDrawColorFilter();
1662 setupDrawShader();
1663 if (isAA) {
1664 setupDrawBlending(true, mode);
1665 } else {
1666 setupDrawBlending(mode);
1667 }
1668 setupDrawProgram();
1669 setupDrawModelViewIdentity(true);
1670 setupDrawColorUniforms();
1671 setupDrawColorFilterUniforms();
1672 setupDrawShaderIdentityUniforms();
1673
1674 if (isHairLine) {
1675 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001676 halfStrokeWidth = isAA? 1 : .5;
1677 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001678 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001679 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001680 }
1681 Vertex lines[verticesCount];
1682 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001683 AAVertex wLines[verticesCount];
1684 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001685 if (!isAA) {
1686 setupDrawVertices(vertices);
1687 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001688 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1689 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001690 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001691 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1692 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001693 // We will need to calculate the actual width proportion on each segment for
1694 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1695 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1696 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001697 }
1698
Chet Haase99ecdc42011-05-06 12:06:34 -07001699 AAVertex* prevAAVertex = NULL;
1700 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001701
Chet Haase99585ad2011-05-02 15:00:16 -07001702 int boundaryLengthSlot = -1;
1703 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001704 int boundaryWidthSlot = -1;
1705 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001706 for (int i = 0; i < count; i += 4) {
1707 // a = start point, b = end point
1708 vec2 a(points[i], points[i + 1]);
1709 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001710 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001711 float boundaryLengthProportion = 0;
1712 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001713
Chet Haase5b0200b2011-04-13 17:58:08 -07001714 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001715 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001716 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001717 if (isAA) {
1718 float wideningFactor;
1719 if (fabs(n.x) >= fabs(n.y)) {
1720 wideningFactor = fabs(1.0f / n.x);
1721 } else {
1722 wideningFactor = fabs(1.0f / n.y);
1723 }
1724 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001725 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001726 if (scaled) {
1727 n.x *= inverseScaleX;
1728 n.y *= inverseScaleY;
1729 }
1730 } else if (scaled) {
1731 // Extend n by .5 pixel on each side, post-transform
1732 vec2 extendedN = n.copyNormalized();
1733 extendedN /= 2;
1734 extendedN.x *= inverseScaleX;
1735 extendedN.y *= inverseScaleY;
1736 float extendedNLength = extendedN.length();
1737 // We need to set this value on the shader prior to drawing
1738 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1739 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001740 }
1741 float x = n.x;
1742 n.x = -n.y;
1743 n.y = x;
1744
Chet Haase99585ad2011-05-02 15:00:16 -07001745 // aa lines expand the endpoint vertices to encompass the AA boundary
1746 if (isAA) {
1747 vec2 abVector = (b - a);
1748 length = abVector.length();
1749 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001750 if (scaled) {
1751 abVector.x *= inverseScaleX;
1752 abVector.y *= inverseScaleY;
1753 float abLength = abVector.length();
1754 boundaryLengthProportion = abLength / (length + abLength);
1755 } else {
1756 boundaryLengthProportion = .5 / (length + 1);
1757 }
1758 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001759 a -= abVector;
1760 b += abVector;
1761 }
1762
Chet Haase5b0200b2011-04-13 17:58:08 -07001763 // Four corners of the rectangle defining a thick line
1764 vec2 p1 = a - n;
1765 vec2 p2 = a + n;
1766 vec2 p3 = b + n;
1767 vec2 p4 = b - n;
1768
Chet Haase99585ad2011-05-02 15:00:16 -07001769
Chet Haase5b0200b2011-04-13 17:58:08 -07001770 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1771 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1772 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1773 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1774
1775 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001776 if (!isAA) {
1777 if (prevVertex != NULL) {
1778 // Issue two repeat vertices to create degenerate triangles to bridge
1779 // between the previous line and the new one. This is necessary because
1780 // we are creating a single triangle_strip which will contain
1781 // potentially discontinuous line segments.
1782 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1783 Vertex::set(vertices++, p1.x, p1.y);
1784 generatedVerticesCount += 2;
1785 }
1786 Vertex::set(vertices++, p1.x, p1.y);
1787 Vertex::set(vertices++, p2.x, p2.y);
1788 Vertex::set(vertices++, p4.x, p4.y);
1789 Vertex::set(vertices++, p3.x, p3.y);
1790 prevVertex = vertices - 1;
1791 generatedVerticesCount += 4;
1792 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001793 if (!isHairLine && scaled) {
1794 // Must set width proportions per-segment for scaled non-hairlines to use the
1795 // correct AA boundary dimensions
1796 if (boundaryWidthSlot < 0) {
1797 boundaryWidthSlot =
1798 mCaches.currentProgram->getUniform("boundaryWidth");
1799 inverseBoundaryWidthSlot =
1800 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1801 }
1802 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1803 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1804 }
Chet Haase99585ad2011-05-02 15:00:16 -07001805 if (boundaryLengthSlot < 0) {
1806 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1807 inverseBoundaryLengthSlot =
1808 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1809 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001810 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1811 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001812
Chet Haase5b0200b2011-04-13 17:58:08 -07001813 if (prevAAVertex != NULL) {
1814 // Issue two repeat vertices to create degenerate triangles to bridge
1815 // between the previous line and the new one. This is necessary because
1816 // we are creating a single triangle_strip which will contain
1817 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001818 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1819 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1820 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001821 generatedVerticesCount += 2;
1822 }
Chet Haase99585ad2011-05-02 15:00:16 -07001823 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1824 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1825 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1826 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001827 prevAAVertex = aaVertices - 1;
1828 generatedVerticesCount += 4;
1829 }
Chet Haase99585ad2011-05-02 15:00:16 -07001830 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1831 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1832 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001833 }
1834 }
1835 if (generatedVerticesCount > 0) {
1836 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1837 }
1838}
1839
Romain Guyed6fcb02011-03-21 13:11:28 -07001840void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1841 if (mSnapshot->isIgnored()) return;
1842
1843 // TODO: The paint's cap style defines whether the points are square or circular
1844 // TODO: Handle AA for round points
1845
Chet Haase5b0200b2011-04-13 17:58:08 -07001846 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001847 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001848 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001849 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001850 if (isHairLine) {
1851 // Now that we know it's hairline, we can set the effective width, to be used later
1852 strokeWidth = 1.0f;
1853 }
1854 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001855 int alpha;
1856 SkXfermode::Mode mode;
1857 getAlphaAndMode(paint, &alpha, &mode);
1858
1859 int verticesCount = count >> 1;
1860 int generatedVerticesCount = 0;
1861
1862 TextureVertex pointsData[verticesCount];
1863 TextureVertex* vertex = &pointsData[0];
1864
1865 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001866 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001867 setupDrawColor(paint->getColor(), alpha);
1868 setupDrawColorFilter();
1869 setupDrawShader();
1870 setupDrawBlending(mode);
1871 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001872 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001873 setupDrawColorUniforms();
1874 setupDrawColorFilterUniforms();
1875 setupDrawPointUniforms();
1876 setupDrawShaderIdentityUniforms();
1877 setupDrawMesh(vertex);
1878
1879 for (int i = 0; i < count; i += 2) {
1880 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1881 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001882 float left = points[i] - halfWidth;
1883 float right = points[i] + halfWidth;
1884 float top = points[i + 1] - halfWidth;
1885 float bottom = points [i + 1] + halfWidth;
1886 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001887 }
1888
1889 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1890}
1891
Romain Guy85bf02f2010-06-22 13:11:24 -07001892void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001893 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001894 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001895
Romain Guyae88e5e2010-10-22 17:49:18 -07001896 Rect& clip(*mSnapshot->clipRect);
1897 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001898
Romain Guy3d58c032010-07-14 16:34:53 -07001899 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001900}
Romain Guy9d5316e2010-06-24 19:30:36 -07001901
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001902void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001903 if (!texture) return;
1904 const AutoTexture autoCleanup(texture);
1905
1906 const float x = left + texture->left - texture->offset;
1907 const float y = top + texture->top - texture->offset;
1908
1909 drawPathTexture(texture, x, y, paint);
1910}
1911
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001912void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1913 float rx, float ry, SkPaint* paint) {
1914 if (mSnapshot->isIgnored()) return;
1915
1916 glActiveTexture(gTextureUnits[0]);
1917 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1918 right - left, bottom - top, rx, ry, paint);
1919 drawShape(left, top, texture, paint);
1920}
1921
Romain Guy01d58e42011-01-19 21:54:02 -08001922void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1923 if (mSnapshot->isIgnored()) return;
1924
1925 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001926 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001927 drawShape(x - radius, y - radius, texture, paint);
1928}
Romain Guy01d58e42011-01-19 21:54:02 -08001929
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001930void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1931 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001932
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001933 glActiveTexture(gTextureUnits[0]);
1934 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1935 drawShape(left, top, texture, paint);
1936}
1937
Romain Guy8b2f5262011-01-23 16:15:02 -08001938void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1939 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1940 if (mSnapshot->isIgnored()) return;
1941
1942 if (fabs(sweepAngle) >= 360.0f) {
1943 drawOval(left, top, right, bottom, paint);
1944 return;
1945 }
1946
1947 glActiveTexture(gTextureUnits[0]);
1948 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1949 startAngle, sweepAngle, useCenter, paint);
1950 drawShape(left, top, texture, paint);
1951}
1952
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001953void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1954 SkPaint* paint) {
1955 if (mSnapshot->isIgnored()) return;
1956
1957 glActiveTexture(gTextureUnits[0]);
1958 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1959 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001960}
1961
Chet Haase5c13d892010-10-08 08:37:55 -07001962void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001963 if (p->getStyle() != SkPaint::kFill_Style) {
1964 drawRectAsShape(left, top, right, bottom, p);
1965 return;
1966 }
1967
Romain Guy6926c722010-07-12 20:20:03 -07001968 if (quickReject(left, top, right, bottom)) {
1969 return;
1970 }
1971
Romain Guy026c5e162010-06-28 17:12:22 -07001972 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001973 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001974 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1975 if (!isMode) {
1976 // Assume SRC_OVER
1977 mode = SkXfermode::kSrcOver_Mode;
1978 }
1979 } else {
1980 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001981 }
1982
Romain Guy026c5e162010-06-28 17:12:22 -07001983 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07001984 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07001985 drawAARect(left, top, right, bottom, color, mode);
1986 } else {
1987 drawColorRect(left, top, right, bottom, color, mode);
1988 }
Romain Guyc7d53492010-06-25 13:41:57 -07001989}
Romain Guy9d5316e2010-06-24 19:30:36 -07001990
Romain Guye8e62a42010-07-23 18:55:21 -07001991void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1992 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001993 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001994 return;
1995 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001996 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001997
Romain Guy67ffc362011-06-03 18:50:11 -07001998 // TODO: We should probably make a copy of the paint instead of modifying
1999 // it; modifying the paint will change its generationID the first
2000 // time, which might impact caches. More investigation needed to
2001 // see if it matters.
2002 // If we make a copy, then drawTextDecorations() should *not* make
2003 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002004 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002005#if RENDER_TEXT_AS_GLYPHS
2006 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2007#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002008
Romain Guya674ab72010-08-10 17:26:42 -07002009 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002010 switch (paint->getTextAlign()) {
2011 case SkPaint::kCenter_Align:
2012 length = paint->measureText(text, bytesCount);
2013 x -= length / 2.0f;
2014 break;
2015 case SkPaint::kRight_Align:
2016 length = paint->measureText(text, bytesCount);
2017 x -= length;
2018 break;
2019 default:
2020 break;
2021 }
2022
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002023 const float oldX = x;
2024 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002025 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2026 if (pureTranslate) {
2027 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2028 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2029 }
2030
Romain Guyb45c0c92010-08-26 20:35:23 -07002031 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2032 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002033 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002034
Romain Guy86568192010-12-14 15:55:39 -08002035 int alpha;
2036 SkXfermode::Mode mode;
2037 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002038
Romain Guy1e45aae2010-08-13 19:39:53 -07002039 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002040 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002041 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2042 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002043 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002044
Romain Guy740bf2b2011-04-26 15:33:10 -07002045 const float sx = oldX - shadow->left + mShadowDx;
2046 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002047
Romain Guy86568192010-12-14 15:55:39 -08002048 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002049 int shadowColor = mShadowColor;
2050 if (mShader) {
2051 shadowColor = 0xffffffff;
2052 }
Romain Guy86568192010-12-14 15:55:39 -08002053
2054 glActiveTexture(gTextureUnits[0]);
2055 setupDraw();
2056 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002057 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2058 setupDrawColorFilter();
2059 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002060 setupDrawBlending(true, mode);
2061 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002062 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002063 setupDrawTexture(shadow->id);
2064 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002065 setupDrawColorFilterUniforms();
2066 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002067 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2068
Romain Guy0a417492010-08-16 20:26:20 -07002069 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002070
Romain Guy86568192010-12-14 15:55:39 -08002071 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002072 }
2073
Romain Guyb146b122010-12-15 17:06:45 -08002074 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2075 return;
2076 }
2077
Romain Guy6620c6d2010-12-06 18:07:02 -08002078 // Pick the appropriate texture filtering
2079 bool linearFilter = mSnapshot->transform->changesBounds();
2080 if (pureTranslate && !linearFilter) {
2081 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2082 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002083
Romain Guy86568192010-12-14 15:55:39 -08002084 glActiveTexture(gTextureUnits[0]);
2085 setupDraw();
2086 setupDrawDirtyRegionsDisabled();
2087 setupDrawWithTexture(true);
2088 setupDrawAlpha8Color(paint->getColor(), alpha);
2089 setupDrawColorFilter();
2090 setupDrawShader();
2091 setupDrawBlending(true, mode);
2092 setupDrawProgram();
2093 setupDrawModelView(x, y, x, y, pureTranslate, true);
2094 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2095 setupDrawPureColorUniforms();
2096 setupDrawColorFilterUniforms();
2097 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002098
Romain Guy6620c6d2010-12-06 18:07:02 -08002099 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002100 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2101
2102#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002103 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002104#else
Romain Guyf219da52011-01-16 12:54:25 -08002105 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002106#endif
Romain Guy03750a02010-10-18 14:06:08 -07002107 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002108
2109 // Tell font renderer the locations of position and texture coord
2110 // attributes so it can bind its data properly
2111 int positionSlot = mCaches.currentProgram->position;
2112 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002113 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002114 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002115#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002116 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002117 if (!pureTranslate) {
2118 mSnapshot->transform->mapRect(bounds);
2119 }
Romain Guyf219da52011-01-16 12:54:25 -08002120 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002121 }
2122#endif
2123 }
Romain Guy694b5192010-07-21 21:33:20 -07002124
2125 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002126 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002127
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002128 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002129}
2130
Romain Guy7fbcc042010-08-04 15:40:07 -07002131void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002132 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002133
Romain Guy01d58e42011-01-19 21:54:02 -08002134 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002135
Romain Guyfb8b7632010-08-23 21:05:08 -07002136 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002137 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002138 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002139
Romain Guy8b55f372010-08-18 17:10:07 -07002140 const float x = texture->left - texture->offset;
2141 const float y = texture->top - texture->offset;
2142
Romain Guy01d58e42011-01-19 21:54:02 -08002143 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002144}
2145
Romain Guyada830f2011-01-13 12:13:20 -08002146void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2147 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002148 return;
2149 }
2150
2151 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002152
2153 int alpha;
2154 SkXfermode::Mode mode;
2155 getAlphaAndMode(paint, &alpha, &mode);
2156
Romain Guyada830f2011-01-13 12:13:20 -08002157 layer->alpha = alpha;
2158 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08002159
Romain Guyf219da52011-01-16 12:54:25 -08002160#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002161 if (!layer->region.isEmpty()) {
2162 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002163 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002164 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002165 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002166 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002167
Romain Guyc88e3572011-01-22 00:32:12 -08002168 setupDraw();
2169 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002170 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002171 setupDrawColorFilter();
2172 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
2173 setupDrawProgram();
Romain Guy40667672011-03-18 14:34:03 -07002174 setupDrawModelViewTranslate(x, y,
2175 x + layer->layer.getWidth(), y + layer->layer.getHeight());
Romain Guyc88e3572011-01-22 00:32:12 -08002176 setupDrawPureColorUniforms();
2177 setupDrawColorFilterUniforms();
2178 setupDrawTexture(layer->texture);
Romain Guyc88e3572011-01-22 00:32:12 -08002179 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002180
Romain Guyc88e3572011-01-22 00:32:12 -08002181 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2182 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002183
Romain Guyc88e3572011-01-22 00:32:12 -08002184 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002185
2186#if DEBUG_LAYERS_AS_REGIONS
2187 drawRegionRects(layer->region);
2188#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002189 }
Romain Guyf219da52011-01-16 12:54:25 -08002190 }
2191#else
Romain Guyada830f2011-01-13 12:13:20 -08002192 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2193 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002194#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002195}
2196
Romain Guy6926c722010-07-12 20:20:03 -07002197///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002198// Shaders
2199///////////////////////////////////////////////////////////////////////////////
2200
2201void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002202 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002203}
2204
Romain Guy06f96e22010-07-30 19:18:16 -07002205void OpenGLRenderer::setupShader(SkiaShader* shader) {
2206 mShader = shader;
2207 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002208 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002209 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002210}
2211
Romain Guyd27977d2010-07-14 19:18:51 -07002212///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002213// Color filters
2214///////////////////////////////////////////////////////////////////////////////
2215
2216void OpenGLRenderer::resetColorFilter() {
2217 mColorFilter = NULL;
2218}
2219
2220void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2221 mColorFilter = filter;
2222}
2223
2224///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002225// Drop shadow
2226///////////////////////////////////////////////////////////////////////////////
2227
2228void OpenGLRenderer::resetShadow() {
2229 mHasShadow = false;
2230}
2231
2232void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2233 mHasShadow = true;
2234 mShadowRadius = radius;
2235 mShadowDx = dx;
2236 mShadowDy = dy;
2237 mShadowColor = color;
2238}
2239
2240///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002241// Drawing implementation
2242///////////////////////////////////////////////////////////////////////////////
2243
Romain Guy01d58e42011-01-19 21:54:02 -08002244void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2245 float x, float y, SkPaint* paint) {
2246 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2247 return;
2248 }
2249
2250 int alpha;
2251 SkXfermode::Mode mode;
2252 getAlphaAndMode(paint, &alpha, &mode);
2253
2254 setupDraw();
2255 setupDrawWithTexture(true);
2256 setupDrawAlpha8Color(paint->getColor(), alpha);
2257 setupDrawColorFilter();
2258 setupDrawShader();
2259 setupDrawBlending(true, mode);
2260 setupDrawProgram();
2261 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2262 setupDrawTexture(texture->id);
2263 setupDrawPureColorUniforms();
2264 setupDrawColorFilterUniforms();
2265 setupDrawShaderUniforms();
2266 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2267
2268 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2269
2270 finishDrawTexture();
2271}
2272
Romain Guyf607bdc2010-09-10 19:20:06 -07002273// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002274#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2275#define kStdUnderline_Offset (1.0f / 9.0f)
2276#define kStdUnderline_Thickness (1.0f / 18.0f)
2277
2278void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2279 float x, float y, SkPaint* paint) {
2280 // Handle underline and strike-through
2281 uint32_t flags = paint->getFlags();
2282 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002283 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002284 float underlineWidth = length;
2285 // If length is > 0.0f, we already measured the text for the text alignment
2286 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002287 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002288 }
2289
2290 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002291 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002292 case SkPaint::kCenter_Align:
2293 offsetX = underlineWidth * 0.5f;
2294 break;
2295 case SkPaint::kRight_Align:
2296 offsetX = underlineWidth;
2297 break;
2298 default:
2299 break;
2300 }
2301
2302 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002303 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002304 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002305
Romain Guye20ecbd2010-09-22 19:49:04 -07002306 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002307 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002308
Romain Guyf6834472011-01-23 13:32:12 -08002309 int linesCount = 0;
2310 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2311 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2312
2313 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002314 float points[pointsCount];
2315 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002316
2317 if (flags & SkPaint::kUnderlineText_Flag) {
2318 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002319 points[currentPoint++] = left;
2320 points[currentPoint++] = top;
2321 points[currentPoint++] = left + underlineWidth;
2322 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002323 }
2324
2325 if (flags & SkPaint::kStrikeThruText_Flag) {
2326 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002327 points[currentPoint++] = left;
2328 points[currentPoint++] = top;
2329 points[currentPoint++] = left + underlineWidth;
2330 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002331 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002332
Romain Guy726aeba2011-06-01 14:52:00 -07002333 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002334
Romain Guy726aeba2011-06-01 14:52:00 -07002335 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002336 }
2337 }
2338}
2339
Romain Guy026c5e162010-06-28 17:12:22 -07002340void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002341 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002342 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002343 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002344 color |= 0x00ffffff;
2345 }
2346
Romain Guy70ca14e2010-12-13 18:24:33 -08002347 setupDraw();
2348 setupDrawColor(color);
2349 setupDrawShader();
2350 setupDrawColorFilter();
2351 setupDrawBlending(mode);
2352 setupDrawProgram();
2353 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2354 setupDrawColorUniforms();
2355 setupDrawShaderUniforms(ignoreTransform);
2356 setupDrawColorFilterUniforms();
2357 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002358
Romain Guyc95c8d62010-09-17 15:31:32 -07002359 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2360}
2361
Romain Guy82ba8142010-07-09 13:25:56 -07002362void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002363 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002364 int alpha;
2365 SkXfermode::Mode mode;
2366 getAlphaAndMode(paint, &alpha, &mode);
2367
Romain Guy8164c2d2010-10-25 18:03:28 -07002368 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
2369
Romain Guy6620c6d2010-12-06 18:07:02 -08002370 if (mSnapshot->transform->isPureTranslate()) {
2371 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2372 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2373
2374 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2375 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2376 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2377 } else {
2378 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2379 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2380 GL_TRIANGLE_STRIP, gMeshCount);
2381 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002382}
2383
Romain Guybd6b79b2010-06-26 00:13:53 -07002384void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002385 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2386 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002387 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002388}
2389
2390void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002391 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002392 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002393 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002394
Romain Guy746b7402010-10-26 16:27:31 -07002395 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002396 setupDrawWithTexture();
2397 setupDrawColor(alpha, alpha, alpha, alpha);
2398 setupDrawColorFilter();
2399 setupDrawBlending(blend, mode, swapSrcDst);
2400 setupDrawProgram();
2401 if (!dirty) {
2402 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002403 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002404 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002405 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002406 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002407 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002408 }
Romain Guy86568192010-12-14 15:55:39 -08002409 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002410 setupDrawColorFilterUniforms();
2411 setupDrawTexture(texture);
2412 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002413
Romain Guy6820ac82010-09-15 18:11:50 -07002414 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002415
2416 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002417}
2418
Romain Guya5aed0d2010-09-09 14:42:43 -07002419void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002420 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002421 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2422 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002423 if (mode < SkXfermode::kPlus_Mode) {
2424 if (!mCaches.blend) {
2425 glEnable(GL_BLEND);
2426 }
Romain Guy82ba8142010-07-09 13:25:56 -07002427
Romain Guyf607bdc2010-09-10 19:20:06 -07002428 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2429 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002430
Romain Guya5aed0d2010-09-09 14:42:43 -07002431 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2432 glBlendFunc(sourceMode, destMode);
2433 mCaches.lastSrcMode = sourceMode;
2434 mCaches.lastDstMode = destMode;
2435 }
2436 } else {
2437 // These blend modes are not supported by OpenGL directly and have
2438 // to be implemented using shaders. Since the shader will perform
2439 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002440 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002441 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002442 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002443 }
2444
2445 if (mCaches.blend) {
2446 glDisable(GL_BLEND);
2447 }
2448 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002449 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002450 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002451 glDisable(GL_BLEND);
2452 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002453 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002454}
2455
Romain Guy889f8d12010-07-29 14:37:42 -07002456bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002457 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002458 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002459 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002460 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002461 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002462 }
Romain Guy6926c722010-07-12 20:20:03 -07002463 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002464}
2465
Romain Guy026c5e162010-06-28 17:12:22 -07002466void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002467 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002468 TextureVertex::setUV(v++, u1, v1);
2469 TextureVertex::setUV(v++, u2, v1);
2470 TextureVertex::setUV(v++, u1, v2);
2471 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002472}
2473
Chet Haase5c13d892010-10-08 08:37:55 -07002474void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002475 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002476 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002477 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2478 if (!isMode) {
2479 // Assume SRC_OVER
2480 *mode = SkXfermode::kSrcOver_Mode;
2481 }
2482 } else {
2483 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002484 }
2485
2486 // Skia draws using the color's alpha channel if < 255
2487 // Otherwise, it uses the paint's alpha
2488 int color = paint->getColor();
2489 *alpha = (color >> 24) & 0xFF;
2490 if (*alpha == 255) {
2491 *alpha = paint->getAlpha();
2492 }
2493 } else {
2494 *mode = SkXfermode::kSrcOver_Mode;
2495 *alpha = 255;
2496 }
Romain Guy026c5e162010-06-28 17:12:22 -07002497}
2498
Romain Guya5aed0d2010-09-09 14:42:43 -07002499SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002500 SkXfermode::Mode resultMode;
2501 if (!SkXfermode::AsMode(mode, &resultMode)) {
2502 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002503 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002504 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002505}
2506
Romain Guy746b7402010-10-26 16:27:31 -07002507void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002508 bool bound = false;
2509 if (wrapS != texture->wrapS) {
2510 glBindTexture(GL_TEXTURE_2D, texture->id);
2511 bound = true;
2512 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2513 texture->wrapS = wrapS;
2514 }
2515 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002516 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002517 glBindTexture(GL_TEXTURE_2D, texture->id);
2518 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002519 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2520 texture->wrapT = wrapT;
2521 }
Romain Guya1db5742010-07-20 13:09:13 -07002522}
2523
Romain Guy9d5316e2010-06-24 19:30:36 -07002524}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002525}; // namespace android