blob: 4864cff6931d1439605a578610477742a88d0093 [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 Guyf2fc4602011-07-19 15:20:03 -0700128 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700129 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700130 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700131
132 mWidth = width;
133 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700134
135 mFirstSnapshot->height = height;
136 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700137
138 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700139}
140
Romain Guy6b7bd242010-10-06 19:49:23 -0700141void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800142 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
143}
144
145void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800146 mCaches.clearGarbage();
147
Romain Guy8aef54f2010-09-01 15:13:49 -0700148 mSnapshot = new Snapshot(mFirstSnapshot,
149 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800150 mSnapshot->fbo = getTargetFbo();
151
Romain Guy8fb95422010-08-17 18:38:51 -0700152 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700153
Romain Guyfb8b7632010-08-23 21:05:08 -0700154 glViewport(0, 0, mWidth, mHeight);
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 Guy9ace8f52011-07-07 20:50:11 -0700452 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700453 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700454 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
455 bounds.getWidth() / float(layer->getWidth()), 0.0f);
456 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700457
Romain Guy8fb95422010-08-17 18:38:51 -0700458 // Save the layer in the snapshot
459 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700460 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700461
Romain Guyeb993562010-10-05 18:14:38 -0700462 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700463 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700464 } else {
465 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700466 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800467 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700468 if (layer->isEmpty()) {
469 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
470 bounds.left, snapshot->height - bounds.bottom,
471 layer->getWidth(), layer->getHeight(), 0);
472 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800473 } 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) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700488 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700489
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;
Romain Guy9ace8f52011-07-07 20:50:11 -0700510 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700511 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
Romain Guy9ace8f52011-07-07 20:50:11 -0700519 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
520 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700521
522 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700523 if (layer->isEmpty()) {
524 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
525 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700526 }
527
528 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700530
531#if DEBUG_LAYERS_AS_REGIONS
532 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
533 if (status != GL_FRAMEBUFFER_COMPLETE) {
534 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
535
536 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700537 layer->deleteTexture();
538 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700539
540 delete layer;
541
542 return false;
543 }
544#endif
545
546 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
547 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
548 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
549 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
550 glClear(GL_COLOR_BUFFER_BIT);
551
552 dirtyClip();
553
554 // Change the ortho projection
555 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
556 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
557
558 return true;
559}
560
Romain Guy1c740bc2010-09-13 18:00:09 -0700561/**
562 * Read the documentation of createLayer() before doing anything in this method.
563 */
Romain Guy1d83e192010-08-17 11:37:00 -0700564void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
565 if (!current->layer) {
566 LOGE("Attempting to compose a layer that does not exist");
567 return;
568 }
569
Romain Guy5b3b3522010-10-27 18:57:51 -0700570 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700571
572 if (fboLayer) {
573 // Unbind current FBO and restore previous one
574 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
575 }
576
Romain Guy1d83e192010-08-17 11:37:00 -0700577 Layer* layer = current->layer;
578 const Rect& rect = layer->layer;
579
Romain Guy9ace8f52011-07-07 20:50:11 -0700580 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700581 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700582 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700583 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700584 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700585 }
586
Romain Guy03750a02010-10-18 14:06:08 -0700587 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700588
Romain Guy746b7402010-10-26 16:27:31 -0700589 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700590
Romain Guy5b3b3522010-10-27 18:57:51 -0700591 // When the layer is stored in an FBO, we can save a bit of fillrate by
592 // drawing only the dirty region
593 if (fboLayer) {
594 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700595 if (layer->getColorFilter()) {
596 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800597 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700598 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700599 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800600 resetColorFilter();
601 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700602 } else if (!rect.isEmpty()) {
603 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
604 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700605 }
Romain Guy8b55f372010-08-18 17:10:07 -0700606
Romain Guyeb993562010-10-05 18:14:38 -0700607 if (fboLayer) {
608 // Detach the texture from the FBO
609 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
610 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
611 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
612
613 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
614 mCaches.fboCache.put(current->fbo);
615 }
616
Romain Guy746b7402010-10-26 16:27:31 -0700617 dirtyClip();
618
Romain Guyeb993562010-10-05 18:14:38 -0700619 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700620 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700621 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700622 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700623 delete layer;
624 }
625}
626
Romain Guyaa6c24c2011-04-28 18:40:04 -0700627void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700629
630 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700631 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700632 setupDrawWithTexture();
633 } else {
634 setupDrawWithExternalTexture();
635 }
636 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700637 setupDrawColor(alpha, alpha, alpha, alpha);
638 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700639 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700641 setupDrawPureColorUniforms();
642 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700643 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
644 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700645 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700646 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700647 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700648 if (mSnapshot->transform->isPureTranslate() &&
649 layer->getWidth() == (uint32_t) rect.getWidth() &&
650 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
652 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
653
654 layer->setFilter(GL_NEAREST, GL_NEAREST);
655 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
656 } else {
657 layer->setFilter(GL_LINEAR, GL_LINEAR);
658 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
659 }
660 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700661 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
662
663 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
664
665 finishDrawTexture();
666}
667
Romain Guy5b3b3522010-10-27 18:57:51 -0700668void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700669 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700670 const Rect& texCoords = layer->texCoords;
671 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
672 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700673
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 float x = rect.left;
675 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700676 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700677 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700678 layer->getHeight() == (uint32_t) rect.getHeight();
679
680 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 // When we're swapping, the layer is already in screen coordinates
682 if (!swap) {
683 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
684 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
685 }
686
687 layer->setFilter(GL_NEAREST, GL_NEAREST, true);
688 } else {
689 layer->setFilter(GL_LINEAR, GL_LINEAR, true);
690 }
691
692 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
693 layer->getTexture(), layer->getAlpha() / 255.0f,
694 layer->getMode(), layer->isBlend(),
695 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
696 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700697
Romain Guyaa6c24c2011-04-28 18:40:04 -0700698 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
699 } else {
700 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
701 drawTextureLayer(layer, rect);
702 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
703 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700704}
705
706void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
707#if RENDER_LAYERS_AS_REGIONS
708 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700709 layer->setRegionAsRect();
710
Romain Guy40667672011-03-18 14:34:03 -0700711 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700712
Romain Guy5b3b3522010-10-27 18:57:51 -0700713 layer->region.clear();
714 return;
715 }
716
717 if (!layer->region.isEmpty()) {
718 size_t count;
719 const android::Rect* rects = layer->region.getArray(&count);
720
Romain Guy9ace8f52011-07-07 20:50:11 -0700721 const float alpha = layer->getAlpha() / 255.0f;
722 const float texX = 1.0f / float(layer->getWidth());
723 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800724 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700725
726 TextureVertex* mesh = mCaches.getRegionMesh();
727 GLsizei numQuads = 0;
728
Romain Guy7230a742011-01-10 22:26:16 -0800729 setupDraw();
730 setupDrawWithTexture();
731 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800732 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800734 setupDrawProgram();
735 setupDrawDirtyRegionsDisabled();
736 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800737 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700738 setupDrawTexture(layer->getTexture());
739 if (mSnapshot->transform->isPureTranslate()) {
740 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
741 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
742
743 layer->setFilter(GL_NEAREST, GL_NEAREST);
744 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
745 } else {
746 layer->setFilter(GL_LINEAR, GL_LINEAR);
747 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
748 }
Romain Guy7230a742011-01-10 22:26:16 -0800749 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700750
751 for (size_t i = 0; i < count; i++) {
752 const android::Rect* r = &rects[i];
753
754 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800755 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700756 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800757 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700758
759 // TODO: Reject quads outside of the clip
760 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
761 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
762 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
763 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
764
765 numQuads++;
766
767 if (numQuads >= REGION_MESH_QUAD_COUNT) {
768 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
769 numQuads = 0;
770 mesh = mCaches.getRegionMesh();
771 }
772 }
773
774 if (numQuads > 0) {
775 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
776 }
777
778 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800779 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
781#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800782 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700783#endif
784
785 layer->region.clear();
786 }
787#else
788 composeLayerRect(layer, rect);
789#endif
790}
791
Romain Guy3a3133d2011-02-01 22:59:58 -0800792void OpenGLRenderer::drawRegionRects(const Region& region) {
793#if DEBUG_LAYERS_AS_REGIONS
794 size_t count;
795 const android::Rect* rects = region.getArray(&count);
796
797 uint32_t colors[] = {
798 0x7fff0000, 0x7f00ff00,
799 0x7f0000ff, 0x7fff00ff,
800 };
801
802 int offset = 0;
803 int32_t top = rects[0].top;
804
805 for (size_t i = 0; i < count; i++) {
806 if (top != rects[i].top) {
807 offset ^= 0x2;
808 top = rects[i].top;
809 }
810
811 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
812 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
813 SkXfermode::kSrcOver_Mode);
814 }
815#endif
816}
817
Romain Guy5b3b3522010-10-27 18:57:51 -0700818void OpenGLRenderer::dirtyLayer(const float left, const float top,
819 const float right, const float bottom, const mat4 transform) {
820#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800821 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700822 Rect bounds(left, top, right, bottom);
823 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800824 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700825 }
826#endif
827}
828
829void OpenGLRenderer::dirtyLayer(const float left, const float top,
830 const float right, const float bottom) {
831#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800832 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700833 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800834 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800835 }
836#endif
837}
838
839void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
840#if RENDER_LAYERS_AS_REGIONS
841 if (bounds.intersect(*mSnapshot->clipRect)) {
842 bounds.snapToPixelBoundaries();
843 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
844 if (!dirty.isEmpty()) {
845 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 }
847 }
848#endif
849}
850
Romain Guy54be1cd2011-06-13 19:04:27 -0700851void OpenGLRenderer::clearLayerRegions() {
852 const size_t count = mLayers.size();
853 if (count == 0) return;
854
855 if (!mSnapshot->isIgnored()) {
856 // Doing several glScissor/glClear here can negatively impact
857 // GPUs with a tiler architecture, instead we draw quads with
858 // the Clear blending mode
859
860 // The list contains bounds that have already been clipped
861 // against their initial clip rect, and the current clip
862 // is likely different so we need to disable clipping here
863 glDisable(GL_SCISSOR_TEST);
864
865 Vertex mesh[count * 6];
866 Vertex* vertex = mesh;
867
868 for (uint32_t i = 0; i < count; i++) {
869 Rect* bounds = mLayers.itemAt(i);
870
871 Vertex::set(vertex++, bounds->left, bounds->bottom);
872 Vertex::set(vertex++, bounds->left, bounds->top);
873 Vertex::set(vertex++, bounds->right, bounds->top);
874 Vertex::set(vertex++, bounds->left, bounds->bottom);
875 Vertex::set(vertex++, bounds->right, bounds->top);
876 Vertex::set(vertex++, bounds->right, bounds->bottom);
877
878 delete bounds;
879 }
880
881 setupDraw(false);
882 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
883 setupDrawBlending(true, SkXfermode::kClear_Mode);
884 setupDrawProgram();
885 setupDrawPureColorUniforms();
886 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
887
888 mCaches.unbindMeshBuffer();
889 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
890 gVertexStride, &mesh[0].position[0]);
891 glDrawArrays(GL_TRIANGLES, 0, count * 6);
892
893 glEnable(GL_SCISSOR_TEST);
894 } else {
895 for (uint32_t i = 0; i < count; i++) {
896 delete mLayers.itemAt(i);
897 }
898 }
899
900 mLayers.clear();
901}
902
Romain Guybd6b79b2010-06-26 00:13:53 -0700903///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700904// Transforms
905///////////////////////////////////////////////////////////////////////////////
906
907void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700908 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700909}
910
911void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700912 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700913}
914
915void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700916 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700917}
918
Romain Guy807daf72011-01-18 11:19:19 -0800919void OpenGLRenderer::skew(float sx, float sy) {
920 mSnapshot->transform->skew(sx, sy);
921}
922
Romain Guyf6a11b82010-06-23 17:47:49 -0700923void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700924 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700925}
926
927void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700928 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700929}
930
931void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700932 SkMatrix transform;
933 mSnapshot->transform->copyTo(transform);
934 transform.preConcat(*matrix);
935 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700936}
937
938///////////////////////////////////////////////////////////////////////////////
939// Clipping
940///////////////////////////////////////////////////////////////////////////////
941
Romain Guybb9524b2010-06-22 18:56:38 -0700942void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700943 Rect clip(*mSnapshot->clipRect);
944 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700945 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700946 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700947}
948
949const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700950 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700951}
952
Romain Guyc7d53492010-06-25 13:41:57 -0700953bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800954 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700955 return true;
956 }
957
Romain Guy1d83e192010-08-17 11:37:00 -0700958 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700959 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700960 r.snapToPixelBoundaries();
961
962 Rect clipRect(*mSnapshot->clipRect);
963 clipRect.snapToPixelBoundaries();
964
965 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700966}
967
Romain Guy079ba2c2010-07-16 14:12:24 -0700968bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
969 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700970 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700971 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700972 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700973 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700974}
975
Romain Guyf6a11b82010-06-23 17:47:49 -0700976///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800977// Drawing commands
978///////////////////////////////////////////////////////////////////////////////
979
Romain Guy54be1cd2011-06-13 19:04:27 -0700980void OpenGLRenderer::setupDraw(bool clear) {
981 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -0800982 if (mDirtyClip) {
983 setScissorFromClip();
984 }
985 mDescription.reset();
986 mSetShaderColor = false;
987 mColorSet = false;
988 mColorA = mColorR = mColorG = mColorB = 0.0f;
989 mTextureUnit = 0;
990 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800991 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800992}
993
994void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
995 mDescription.hasTexture = true;
996 mDescription.hasAlpha8Texture = isAlpha8;
997}
998
Romain Guyaa6c24c2011-04-28 18:40:04 -0700999void OpenGLRenderer::setupDrawWithExternalTexture() {
1000 mDescription.hasExternalTexture = true;
1001}
1002
Chet Haase5b0200b2011-04-13 17:58:08 -07001003void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001004 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001005}
1006
Romain Guyed6fcb02011-03-21 13:11:28 -07001007void OpenGLRenderer::setupDrawPoint(float pointSize) {
1008 mDescription.isPoint = true;
1009 mDescription.pointSize = pointSize;
1010}
1011
Romain Guy70ca14e2010-12-13 18:24:33 -08001012void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001013 setupDrawColor(color, (color >> 24) & 0xFF);
1014}
1015
1016void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1017 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001018 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1019 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001020 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001021 mColorR = a * ((color >> 16) & 0xFF);
1022 mColorG = a * ((color >> 8) & 0xFF);
1023 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001024 mColorSet = true;
1025 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1026}
1027
Romain Guy86568192010-12-14 15:55:39 -08001028void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1029 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001030 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1031 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001032 const float a = mColorA / 255.0f;
1033 mColorR = a * ((color >> 16) & 0xFF);
1034 mColorG = a * ((color >> 8) & 0xFF);
1035 mColorB = a * ((color ) & 0xFF);
1036 mColorSet = true;
1037 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1038}
1039
Romain Guy70ca14e2010-12-13 18:24:33 -08001040void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1041 mColorA = a;
1042 mColorR = r;
1043 mColorG = g;
1044 mColorB = b;
1045 mColorSet = true;
1046 mSetShaderColor = mDescription.setColor(r, g, b, a);
1047}
1048
Romain Guy86568192010-12-14 15:55:39 -08001049void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1050 mColorA = a;
1051 mColorR = r;
1052 mColorG = g;
1053 mColorB = b;
1054 mColorSet = true;
1055 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1056}
1057
Romain Guy70ca14e2010-12-13 18:24:33 -08001058void OpenGLRenderer::setupDrawShader() {
1059 if (mShader) {
1060 mShader->describe(mDescription, mCaches.extensions);
1061 }
1062}
1063
1064void OpenGLRenderer::setupDrawColorFilter() {
1065 if (mColorFilter) {
1066 mColorFilter->describe(mDescription, mCaches.extensions);
1067 }
1068}
1069
Romain Guyf09ef512011-05-27 11:43:46 -07001070void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1071 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1072 mColorA = 1.0f;
1073 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001074 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001075 }
1076}
1077
Romain Guy70ca14e2010-12-13 18:24:33 -08001078void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001079 // When the blending mode is kClear_Mode, we need to use a modulate color
1080 // argb=1,0,0,0
1081 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001082 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1083 mDescription, swapSrcDst);
1084}
1085
1086void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001087 // When the blending mode is kClear_Mode, we need to use a modulate color
1088 // argb=1,0,0,0
1089 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001090 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1091 mDescription, swapSrcDst);
1092}
1093
1094void OpenGLRenderer::setupDrawProgram() {
1095 useProgram(mCaches.programCache.get(mDescription));
1096}
1097
1098void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1099 mTrackDirtyRegions = false;
1100}
1101
1102void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1103 bool ignoreTransform) {
1104 mModelView.loadTranslate(left, top, 0.0f);
1105 if (!ignoreTransform) {
1106 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1107 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1108 } else {
1109 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1110 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1111 }
1112}
1113
Chet Haase8a5cc922011-04-26 07:28:09 -07001114void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1115 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001116}
1117
Romain Guy70ca14e2010-12-13 18:24:33 -08001118void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1119 bool ignoreTransform, bool ignoreModelView) {
1120 if (!ignoreModelView) {
1121 mModelView.loadTranslate(left, top, 0.0f);
1122 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001123 } else {
1124 mModelView.loadIdentity();
1125 }
Romain Guy86568192010-12-14 15:55:39 -08001126 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1127 if (!ignoreTransform) {
1128 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1129 if (mTrackDirtyRegions && dirty) {
1130 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1131 }
1132 } else {
1133 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1134 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1135 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001136}
1137
Romain Guyed6fcb02011-03-21 13:11:28 -07001138void OpenGLRenderer::setupDrawPointUniforms() {
1139 int slot = mCaches.currentProgram->getUniform("pointSize");
1140 glUniform1f(slot, mDescription.pointSize);
1141}
1142
Romain Guy70ca14e2010-12-13 18:24:33 -08001143void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001144 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001145 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1146 }
1147}
1148
Romain Guy86568192010-12-14 15:55:39 -08001149void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001150 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001151 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001152 }
1153}
1154
Romain Guy70ca14e2010-12-13 18:24:33 -08001155void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1156 if (mShader) {
1157 if (ignoreTransform) {
1158 mModelView.loadInverse(*mSnapshot->transform);
1159 }
1160 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1161 }
1162}
1163
Romain Guy8d0d4782010-12-14 20:13:35 -08001164void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1165 if (mShader) {
1166 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1167 }
1168}
1169
Romain Guy70ca14e2010-12-13 18:24:33 -08001170void OpenGLRenderer::setupDrawColorFilterUniforms() {
1171 if (mColorFilter) {
1172 mColorFilter->setupProgram(mCaches.currentProgram);
1173 }
1174}
1175
1176void OpenGLRenderer::setupDrawSimpleMesh() {
1177 mCaches.bindMeshBuffer();
1178 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1179 gMeshStride, 0);
1180}
1181
1182void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1183 bindTexture(texture);
1184 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1185
1186 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1187 glEnableVertexAttribArray(mTexCoordsSlot);
1188}
1189
Romain Guyaa6c24c2011-04-28 18:40:04 -07001190void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1191 bindExternalTexture(texture);
1192 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1193
1194 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1195 glEnableVertexAttribArray(mTexCoordsSlot);
1196}
1197
Romain Guy8f0095c2011-05-02 17:24:22 -07001198void OpenGLRenderer::setupDrawTextureTransform() {
1199 mDescription.hasTextureTransform = true;
1200}
1201
1202void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001203 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1204 GL_FALSE, &transform.data[0]);
1205}
1206
Romain Guy70ca14e2010-12-13 18:24:33 -08001207void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1208 if (!vertices) {
1209 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1210 } else {
1211 mCaches.unbindMeshBuffer();
1212 }
1213 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1214 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001215 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001216 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1217 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001218}
1219
Chet Haase5b0200b2011-04-13 17:58:08 -07001220void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1221 mCaches.unbindMeshBuffer();
1222 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1223 gVertexStride, vertices);
1224}
1225
1226/**
1227 * 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 -07001228 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1229 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1230 * attributes (one per vertex) are values from zero to one that tells the fragment
1231 * shader where the fragment is in relation to the line width/length overall; these values are
1232 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1233 * region of the line.
1234 * Note that we only pass down the width values in this setup function. The length coordinates
1235 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001236 */
Chet Haase99585ad2011-05-02 15:00:16 -07001237void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001238 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001239 mCaches.unbindMeshBuffer();
1240 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001241 gAAVertexStride, vertices);
1242 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1243 glEnableVertexAttribArray(widthSlot);
1244 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1245 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1246 glEnableVertexAttribArray(lengthSlot);
1247 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001248 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001249 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001250 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001251 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001252 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001253}
1254
Romain Guy70ca14e2010-12-13 18:24:33 -08001255void OpenGLRenderer::finishDrawTexture() {
1256 glDisableVertexAttribArray(mTexCoordsSlot);
1257}
1258
1259///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001260// Drawing
1261///////////////////////////////////////////////////////////////////////////////
1262
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001263bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1264 Rect& dirty, uint32_t level) {
1265 if (quickReject(0.0f, 0.0f, width, height)) {
1266 return false;
1267 }
1268
Romain Guy0fe478e2010-11-08 12:08:41 -08001269 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1270 // will be performed by the display list itself
1271 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001272 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001273 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001274
Chet Haasedaf98e92011-01-10 14:10:36 -08001275 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001276}
1277
Chet Haaseed30fd82011-04-22 16:18:45 -07001278void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1279 if (displayList) {
1280 displayList->output(*this, level);
1281 }
1282}
1283
Romain Guya168d732011-03-18 16:50:13 -07001284void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1285 int alpha;
1286 SkXfermode::Mode mode;
1287 getAlphaAndMode(paint, &alpha, &mode);
1288
Romain Guya168d732011-03-18 16:50:13 -07001289 float x = left;
1290 float y = top;
1291
Romain Guye3c26852011-07-25 16:36:01 -07001292 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001293 bool ignoreTransform = false;
1294 if (mSnapshot->transform->isPureTranslate()) {
1295 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1296 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1297 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001298 filter = GL_NEAREST;
Romain Guya168d732011-03-18 16:50:13 -07001299 }
1300
1301 setupDraw();
1302 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001303 if (paint) {
1304 setupDrawAlpha8Color(paint->getColor(), alpha);
1305 }
Romain Guya168d732011-03-18 16:50:13 -07001306 setupDrawColorFilter();
1307 setupDrawShader();
1308 setupDrawBlending(true, mode);
1309 setupDrawProgram();
1310 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001311
Romain Guya168d732011-03-18 16:50:13 -07001312 setupDrawTexture(texture->id);
Romain Guye3c26852011-07-25 16:36:01 -07001313 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1314 texture->setFilter(filter, filter);
1315
Romain Guya168d732011-03-18 16:50:13 -07001316 setupDrawPureColorUniforms();
1317 setupDrawColorFilterUniforms();
1318 setupDrawShaderUniforms();
1319 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1320
1321 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1322
1323 finishDrawTexture();
1324}
1325
Chet Haase5c13d892010-10-08 08:37:55 -07001326void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001327 const float right = left + bitmap->width();
1328 const float bottom = top + bitmap->height();
1329
1330 if (quickReject(left, top, right, bottom)) {
1331 return;
1332 }
1333
Romain Guy86568192010-12-14 15:55:39 -08001334 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001335 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001336 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001337 const AutoTexture autoCleanup(texture);
1338
Romain Guya168d732011-03-18 16:50:13 -07001339 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1340 drawAlphaBitmap(texture, left, top, paint);
1341 } else {
1342 drawTextureRect(left, top, right, bottom, texture, paint);
1343 }
Romain Guyce0537b2010-06-29 21:05:21 -07001344}
1345
Chet Haase5c13d892010-10-08 08:37:55 -07001346void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001347 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1348 const mat4 transform(*matrix);
1349 transform.mapRect(r);
1350
Romain Guy6926c722010-07-12 20:20:03 -07001351 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1352 return;
1353 }
1354
Romain Guy86568192010-12-14 15:55:39 -08001355 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001356 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001357 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001358 const AutoTexture autoCleanup(texture);
1359
Romain Guy5b3b3522010-10-27 18:57:51 -07001360 // This could be done in a cheaper way, all we need is pass the matrix
1361 // to the vertex shader. The save/restore is a bit overkill.
1362 save(SkCanvas::kMatrix_SaveFlag);
1363 concatMatrix(matrix);
1364 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1365 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001366}
1367
Romain Guy5a7b4662011-01-20 19:09:30 -08001368void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1369 float* vertices, int* colors, SkPaint* paint) {
1370 // TODO: Do a quickReject
1371 if (!vertices || mSnapshot->isIgnored()) {
1372 return;
1373 }
1374
1375 glActiveTexture(gTextureUnits[0]);
1376 Texture* texture = mCaches.textureCache.get(bitmap);
1377 if (!texture) return;
1378 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001379
1380 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1381 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001382
1383 int alpha;
1384 SkXfermode::Mode mode;
1385 getAlphaAndMode(paint, &alpha, &mode);
1386
Romain Guy5a7b4662011-01-20 19:09:30 -08001387 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001388
Romain Guyb18d2d02011-02-10 15:52:54 -08001389 float left = FLT_MAX;
1390 float top = FLT_MAX;
1391 float right = FLT_MIN;
1392 float bottom = FLT_MIN;
1393
1394#if RENDER_LAYERS_AS_REGIONS
1395 bool hasActiveLayer = hasLayer();
1396#else
1397 bool hasActiveLayer = false;
1398#endif
1399
Romain Guya566b7c2011-01-23 16:36:11 -08001400 // TODO: Support the colors array
1401 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001402 TextureVertex* vertex = mesh;
1403 for (int32_t y = 0; y < meshHeight; y++) {
1404 for (int32_t x = 0; x < meshWidth; x++) {
1405 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1406
1407 float u1 = float(x) / meshWidth;
1408 float u2 = float(x + 1) / meshWidth;
1409 float v1 = float(y) / meshHeight;
1410 float v2 = float(y + 1) / meshHeight;
1411
1412 int ax = i + (meshWidth + 1) * 2;
1413 int ay = ax + 1;
1414 int bx = i;
1415 int by = bx + 1;
1416 int cx = i + 2;
1417 int cy = cx + 1;
1418 int dx = i + (meshWidth + 1) * 2 + 2;
1419 int dy = dx + 1;
1420
1421 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1422 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1423 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1424
1425 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1426 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1427 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001428
1429#if RENDER_LAYERS_AS_REGIONS
1430 if (hasActiveLayer) {
1431 // TODO: This could be optimized to avoid unnecessary ops
1432 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1433 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1434 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1435 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1436 }
1437#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001438 }
1439 }
1440
Romain Guyb18d2d02011-02-10 15:52:54 -08001441#if RENDER_LAYERS_AS_REGIONS
1442 if (hasActiveLayer) {
1443 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1444 }
1445#endif
1446
Romain Guy5a7b4662011-01-20 19:09:30 -08001447 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1448 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001449 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001450}
1451
Romain Guy8ba548f2010-06-30 19:21:21 -07001452void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1453 float srcLeft, float srcTop, float srcRight, float srcBottom,
1454 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001455 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001456 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1457 return;
1458 }
1459
Romain Guy746b7402010-10-26 16:27:31 -07001460 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001461 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001462 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001463 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001464 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8ba548f2010-06-30 19:21:21 -07001465
Romain Guy8ba548f2010-06-30 19:21:21 -07001466 const float width = texture->width;
1467 const float height = texture->height;
1468
Romain Guy1e59f9d2011-05-26 18:39:34 -07001469 const float u1 = (srcLeft + 0.5f) / width;
1470 const float v1 = (srcTop + 0.5f) / height;
1471 const float u2 = (srcRight - 0.5f) / width;
1472 const float v2 = (srcBottom - 0.5f) / height;
Romain Guy8ba548f2010-06-30 19:21:21 -07001473
Romain Guy03750a02010-10-18 14:06:08 -07001474 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001475 resetDrawTextureTexCoords(u1, v1, u2, v2);
1476
Romain Guy03750a02010-10-18 14:06:08 -07001477 int alpha;
1478 SkXfermode::Mode mode;
1479 getAlphaAndMode(paint, &alpha, &mode);
1480
Romain Guy6620c6d2010-12-06 18:07:02 -08001481 if (mSnapshot->transform->isPureTranslate()) {
1482 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1483 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1484
Romain Guyb5014982011-07-28 15:39:12 -07001485 GLenum filter = GL_NEAREST;
1486 if (u1 > 0.0f || u2 < 1.0f || v1 > 0.0f || v2 < 1.0f) {
1487 filter = GL_LINEAR;
1488 }
1489 texture->setFilter(filter, filter, true);
1490
Romain Guy6620c6d2010-12-06 18:07:02 -08001491 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1492 texture->id, alpha / 255.0f, mode, texture->blend,
1493 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1494 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1495 } else {
Romain Guye3c26852011-07-25 16:36:01 -07001496 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyb5014982011-07-28 15:39:12 -07001497
Romain Guy6620c6d2010-12-06 18:07:02 -08001498 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1499 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1500 GL_TRIANGLE_STRIP, gMeshCount);
1501 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001502
1503 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1504}
1505
Romain Guy4aa90572010-09-26 18:40:37 -07001506void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001507 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001508 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001509 if (quickReject(left, top, right, bottom)) {
1510 return;
1511 }
1512
Romain Guy746b7402010-10-26 16:27:31 -07001513 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001514 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001515 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001516 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001517 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1518 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001519
1520 int alpha;
1521 SkXfermode::Mode mode;
1522 getAlphaAndMode(paint, &alpha, &mode);
1523
Romain Guy2728f962010-10-08 18:36:15 -07001524 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001525 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001526
Romain Guya5ef39a2010-12-03 16:48:20 -08001527 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001528 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001529#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001530 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001531 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001532 const float offsetX = left + mSnapshot->transform->getTranslateX();
1533 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001534 const size_t count = mesh->quads.size();
1535 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001536 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001537 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001538 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1539 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1540 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001541 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001542 dirtyLayer(left + bounds.left, top + bounds.top,
1543 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001544 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001545 }
1546 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001547#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001548
Romain Guy6620c6d2010-12-06 18:07:02 -08001549 if (pureTranslate) {
1550 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1551 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1552
1553 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1554 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1555 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1556 true, !mesh->hasEmptyQuads);
1557 } else {
1558 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1559 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1560 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1561 true, !mesh->hasEmptyQuads);
1562 }
Romain Guy054dc182010-10-15 17:55:25 -07001563 }
Romain Guyf7f93552010-07-08 19:17:03 -07001564}
1565
Chet Haase99ecdc42011-05-06 12:06:34 -07001566/**
Chet Haase858aa932011-05-12 09:06:00 -07001567 * This function uses a similar approach to that of AA lines in the drawLines() function.
1568 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1569 * shader to compute the translucency of the color, determined by whether a given pixel is
1570 * within that boundary region and how far into the region it is.
1571 */
1572void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001573 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001574 float inverseScaleX = 1.0f;
1575 float inverseScaleY = 1.0f;
1576 // The quad that we use needs to account for scaling.
1577 if (!mSnapshot->transform->isPureTranslate()) {
1578 Matrix4 *mat = mSnapshot->transform;
1579 float m00 = mat->data[Matrix4::kScaleX];
1580 float m01 = mat->data[Matrix4::kSkewY];
1581 float m02 = mat->data[2];
1582 float m10 = mat->data[Matrix4::kSkewX];
1583 float m11 = mat->data[Matrix4::kScaleX];
1584 float m12 = mat->data[6];
1585 float scaleX = sqrt(m00 * m00 + m01 * m01);
1586 float scaleY = sqrt(m10 * m10 + m11 * m11);
1587 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1588 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1589 }
1590
1591 setupDraw();
1592 setupDrawAALine();
1593 setupDrawColor(color);
1594 setupDrawColorFilter();
1595 setupDrawShader();
1596 setupDrawBlending(true, mode);
1597 setupDrawProgram();
1598 setupDrawModelViewIdentity(true);
1599 setupDrawColorUniforms();
1600 setupDrawColorFilterUniforms();
1601 setupDrawShaderIdentityUniforms();
1602
1603 AAVertex rects[4];
1604 AAVertex* aaVertices = &rects[0];
1605 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1606 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1607
1608 float boundarySizeX = .5 * inverseScaleX;
1609 float boundarySizeY = .5 * inverseScaleY;
1610
1611 // Adjust the rect by the AA boundary padding
1612 left -= boundarySizeX;
1613 right += boundarySizeX;
1614 top -= boundarySizeY;
1615 bottom += boundarySizeY;
1616
1617 float width = right - left;
1618 float height = bottom - top;
1619
1620 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1621 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1622 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1623 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1624 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1625 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1626 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1627
1628 if (!quickReject(left, top, right, bottom)) {
1629 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1630 AAVertex::set(aaVertices++, left, top, 1, 0);
1631 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1632 AAVertex::set(aaVertices++, right, top, 0, 0);
1633 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1634 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1635 }
1636}
1637
1638/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001639 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1640 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1641 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1642 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1643 * of the line. Hairlines are more involved because we need to account for transform scaling
1644 * to end up with a one-pixel-wide line in screen space..
1645 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1646 * in combination with values that we calculate and pass down in this method. The basic approach
1647 * is that the quad we create contains both the core line area plus a bounding area in which
1648 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1649 * proportion of the width and the length of a given segment is represented by the boundary
1650 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1651 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1652 * on the inside). This ends up giving the result we want, with pixels that are completely
1653 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1654 * how far into the boundary region they are, which is determined by shader interpolation.
1655 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001656void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1657 if (mSnapshot->isIgnored()) return;
1658
1659 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001660 // We use half the stroke width here because we're going to position the quad
1661 // corner vertices half of the width away from the line endpoints
1662 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001663 // A stroke width of 0 has a special meaning in Skia:
1664 // it draws a line 1 px wide regardless of current transform
1665 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001666 float inverseScaleX = 1.0f;
1667 float inverseScaleY = 1.0f;
1668 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001669 int alpha;
1670 SkXfermode::Mode mode;
1671 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001672 int verticesCount = count;
1673 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001674 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001675 verticesCount += (count - 4);
1676 }
1677
1678 if (isHairLine || isAA) {
1679 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1680 // the line on the screen should always be one pixel wide regardless of scale. For
1681 // AA lines, we only want one pixel of translucent boundary around the quad.
1682 if (!mSnapshot->transform->isPureTranslate()) {
1683 Matrix4 *mat = mSnapshot->transform;
1684 float m00 = mat->data[Matrix4::kScaleX];
1685 float m01 = mat->data[Matrix4::kSkewY];
1686 float m02 = mat->data[2];
1687 float m10 = mat->data[Matrix4::kSkewX];
1688 float m11 = mat->data[Matrix4::kScaleX];
1689 float m12 = mat->data[6];
1690 float scaleX = sqrt(m00*m00 + m01*m01);
1691 float scaleY = sqrt(m10*m10 + m11*m11);
1692 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1693 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1694 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1695 scaled = true;
1696 }
1697 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001698 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001699
1700 getAlphaAndMode(paint, &alpha, &mode);
1701 setupDraw();
1702 if (isAA) {
1703 setupDrawAALine();
1704 }
1705 setupDrawColor(paint->getColor(), alpha);
1706 setupDrawColorFilter();
1707 setupDrawShader();
1708 if (isAA) {
1709 setupDrawBlending(true, mode);
1710 } else {
1711 setupDrawBlending(mode);
1712 }
1713 setupDrawProgram();
1714 setupDrawModelViewIdentity(true);
1715 setupDrawColorUniforms();
1716 setupDrawColorFilterUniforms();
1717 setupDrawShaderIdentityUniforms();
1718
1719 if (isHairLine) {
1720 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001721 halfStrokeWidth = isAA? 1 : .5;
1722 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001723 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001724 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001725 }
1726 Vertex lines[verticesCount];
1727 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001728 AAVertex wLines[verticesCount];
1729 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001730 if (!isAA) {
1731 setupDrawVertices(vertices);
1732 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001733 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1734 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001735 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001736 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1737 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001738 // We will need to calculate the actual width proportion on each segment for
1739 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1740 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1741 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001742 }
1743
Chet Haase99ecdc42011-05-06 12:06:34 -07001744 AAVertex* prevAAVertex = NULL;
1745 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001746
Chet Haase99585ad2011-05-02 15:00:16 -07001747 int boundaryLengthSlot = -1;
1748 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001749 int boundaryWidthSlot = -1;
1750 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001751 for (int i = 0; i < count; i += 4) {
1752 // a = start point, b = end point
1753 vec2 a(points[i], points[i + 1]);
1754 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001755 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001756 float boundaryLengthProportion = 0;
1757 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001758
Chet Haase5b0200b2011-04-13 17:58:08 -07001759 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001760 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001761 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001762 if (isAA) {
1763 float wideningFactor;
1764 if (fabs(n.x) >= fabs(n.y)) {
1765 wideningFactor = fabs(1.0f / n.x);
1766 } else {
1767 wideningFactor = fabs(1.0f / n.y);
1768 }
1769 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001770 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001771 if (scaled) {
1772 n.x *= inverseScaleX;
1773 n.y *= inverseScaleY;
1774 }
1775 } else if (scaled) {
1776 // Extend n by .5 pixel on each side, post-transform
1777 vec2 extendedN = n.copyNormalized();
1778 extendedN /= 2;
1779 extendedN.x *= inverseScaleX;
1780 extendedN.y *= inverseScaleY;
1781 float extendedNLength = extendedN.length();
1782 // We need to set this value on the shader prior to drawing
1783 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1784 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001785 }
1786 float x = n.x;
1787 n.x = -n.y;
1788 n.y = x;
1789
Chet Haase99585ad2011-05-02 15:00:16 -07001790 // aa lines expand the endpoint vertices to encompass the AA boundary
1791 if (isAA) {
1792 vec2 abVector = (b - a);
1793 length = abVector.length();
1794 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001795 if (scaled) {
1796 abVector.x *= inverseScaleX;
1797 abVector.y *= inverseScaleY;
1798 float abLength = abVector.length();
1799 boundaryLengthProportion = abLength / (length + abLength);
1800 } else {
1801 boundaryLengthProportion = .5 / (length + 1);
1802 }
1803 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001804 a -= abVector;
1805 b += abVector;
1806 }
1807
Chet Haase5b0200b2011-04-13 17:58:08 -07001808 // Four corners of the rectangle defining a thick line
1809 vec2 p1 = a - n;
1810 vec2 p2 = a + n;
1811 vec2 p3 = b + n;
1812 vec2 p4 = b - n;
1813
Chet Haase99585ad2011-05-02 15:00:16 -07001814
Chet Haase5b0200b2011-04-13 17:58:08 -07001815 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1816 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1817 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1818 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1819
1820 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001821 if (!isAA) {
1822 if (prevVertex != NULL) {
1823 // Issue two repeat vertices to create degenerate triangles to bridge
1824 // between the previous line and the new one. This is necessary because
1825 // we are creating a single triangle_strip which will contain
1826 // potentially discontinuous line segments.
1827 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1828 Vertex::set(vertices++, p1.x, p1.y);
1829 generatedVerticesCount += 2;
1830 }
1831 Vertex::set(vertices++, p1.x, p1.y);
1832 Vertex::set(vertices++, p2.x, p2.y);
1833 Vertex::set(vertices++, p4.x, p4.y);
1834 Vertex::set(vertices++, p3.x, p3.y);
1835 prevVertex = vertices - 1;
1836 generatedVerticesCount += 4;
1837 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001838 if (!isHairLine && scaled) {
1839 // Must set width proportions per-segment for scaled non-hairlines to use the
1840 // correct AA boundary dimensions
1841 if (boundaryWidthSlot < 0) {
1842 boundaryWidthSlot =
1843 mCaches.currentProgram->getUniform("boundaryWidth");
1844 inverseBoundaryWidthSlot =
1845 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1846 }
1847 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1848 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1849 }
Chet Haase99585ad2011-05-02 15:00:16 -07001850 if (boundaryLengthSlot < 0) {
1851 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1852 inverseBoundaryLengthSlot =
1853 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1854 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001855 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1856 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001857
Chet Haase5b0200b2011-04-13 17:58:08 -07001858 if (prevAAVertex != NULL) {
1859 // Issue two repeat vertices to create degenerate triangles to bridge
1860 // between the previous line and the new one. This is necessary because
1861 // we are creating a single triangle_strip which will contain
1862 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001863 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1864 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1865 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001866 generatedVerticesCount += 2;
1867 }
Chet Haase99585ad2011-05-02 15:00:16 -07001868 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1869 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1870 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1871 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001872 prevAAVertex = aaVertices - 1;
1873 generatedVerticesCount += 4;
1874 }
Chet Haase99585ad2011-05-02 15:00:16 -07001875 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1876 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1877 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001878 }
1879 }
1880 if (generatedVerticesCount > 0) {
1881 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1882 }
1883}
1884
Romain Guyed6fcb02011-03-21 13:11:28 -07001885void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1886 if (mSnapshot->isIgnored()) return;
1887
1888 // TODO: The paint's cap style defines whether the points are square or circular
1889 // TODO: Handle AA for round points
1890
Chet Haase5b0200b2011-04-13 17:58:08 -07001891 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001892 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001893 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001894 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001895 if (isHairLine) {
1896 // Now that we know it's hairline, we can set the effective width, to be used later
1897 strokeWidth = 1.0f;
1898 }
1899 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001900 int alpha;
1901 SkXfermode::Mode mode;
1902 getAlphaAndMode(paint, &alpha, &mode);
1903
1904 int verticesCount = count >> 1;
1905 int generatedVerticesCount = 0;
1906
1907 TextureVertex pointsData[verticesCount];
1908 TextureVertex* vertex = &pointsData[0];
1909
1910 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001911 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001912 setupDrawColor(paint->getColor(), alpha);
1913 setupDrawColorFilter();
1914 setupDrawShader();
1915 setupDrawBlending(mode);
1916 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001917 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001918 setupDrawColorUniforms();
1919 setupDrawColorFilterUniforms();
1920 setupDrawPointUniforms();
1921 setupDrawShaderIdentityUniforms();
1922 setupDrawMesh(vertex);
1923
1924 for (int i = 0; i < count; i += 2) {
1925 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1926 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001927 float left = points[i] - halfWidth;
1928 float right = points[i] + halfWidth;
1929 float top = points[i + 1] - halfWidth;
1930 float bottom = points [i + 1] + halfWidth;
1931 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001932 }
1933
1934 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1935}
1936
Romain Guy85bf02f2010-06-22 13:11:24 -07001937void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001938 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001939 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001940
Romain Guyae88e5e2010-10-22 17:49:18 -07001941 Rect& clip(*mSnapshot->clipRect);
1942 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001943
Romain Guy3d58c032010-07-14 16:34:53 -07001944 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001945}
Romain Guy9d5316e2010-06-24 19:30:36 -07001946
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001947void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001948 if (!texture) return;
1949 const AutoTexture autoCleanup(texture);
1950
1951 const float x = left + texture->left - texture->offset;
1952 const float y = top + texture->top - texture->offset;
1953
1954 drawPathTexture(texture, x, y, paint);
1955}
1956
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001957void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1958 float rx, float ry, SkPaint* paint) {
1959 if (mSnapshot->isIgnored()) return;
1960
1961 glActiveTexture(gTextureUnits[0]);
1962 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1963 right - left, bottom - top, rx, ry, paint);
1964 drawShape(left, top, texture, paint);
1965}
1966
Romain Guy01d58e42011-01-19 21:54:02 -08001967void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1968 if (mSnapshot->isIgnored()) return;
1969
1970 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001971 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001972 drawShape(x - radius, y - radius, texture, paint);
1973}
Romain Guy01d58e42011-01-19 21:54:02 -08001974
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001975void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1976 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001977
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001978 glActiveTexture(gTextureUnits[0]);
1979 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1980 drawShape(left, top, texture, paint);
1981}
1982
Romain Guy8b2f5262011-01-23 16:15:02 -08001983void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1984 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1985 if (mSnapshot->isIgnored()) return;
1986
1987 if (fabs(sweepAngle) >= 360.0f) {
1988 drawOval(left, top, right, bottom, paint);
1989 return;
1990 }
1991
1992 glActiveTexture(gTextureUnits[0]);
1993 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1994 startAngle, sweepAngle, useCenter, paint);
1995 drawShape(left, top, texture, paint);
1996}
1997
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001998void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1999 SkPaint* paint) {
2000 if (mSnapshot->isIgnored()) return;
2001
2002 glActiveTexture(gTextureUnits[0]);
2003 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2004 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002005}
2006
Chet Haase5c13d892010-10-08 08:37:55 -07002007void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002008 if (p->getStyle() != SkPaint::kFill_Style) {
2009 drawRectAsShape(left, top, right, bottom, p);
2010 return;
2011 }
2012
Romain Guy6926c722010-07-12 20:20:03 -07002013 if (quickReject(left, top, right, bottom)) {
2014 return;
2015 }
2016
Romain Guy026c5e162010-06-28 17:12:22 -07002017 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002018 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002019 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2020 if (!isMode) {
2021 // Assume SRC_OVER
2022 mode = SkXfermode::kSrcOver_Mode;
2023 }
2024 } else {
2025 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002026 }
2027
Romain Guy026c5e162010-06-28 17:12:22 -07002028 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002029 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002030 drawAARect(left, top, right, bottom, color, mode);
2031 } else {
2032 drawColorRect(left, top, right, bottom, color, mode);
2033 }
Romain Guyc7d53492010-06-25 13:41:57 -07002034}
Romain Guy9d5316e2010-06-24 19:30:36 -07002035
Romain Guye8e62a42010-07-23 18:55:21 -07002036void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
2037 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08002038 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002039 return;
2040 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002041 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002042
Romain Guy67ffc362011-06-03 18:50:11 -07002043 // TODO: We should probably make a copy of the paint instead of modifying
2044 // it; modifying the paint will change its generationID the first
2045 // time, which might impact caches. More investigation needed to
2046 // see if it matters.
2047 // If we make a copy, then drawTextDecorations() should *not* make
2048 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002049 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002050#if RENDER_TEXT_AS_GLYPHS
2051 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2052#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002053
Romain Guya674ab72010-08-10 17:26:42 -07002054 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002055 switch (paint->getTextAlign()) {
2056 case SkPaint::kCenter_Align:
2057 length = paint->measureText(text, bytesCount);
2058 x -= length / 2.0f;
2059 break;
2060 case SkPaint::kRight_Align:
2061 length = paint->measureText(text, bytesCount);
2062 x -= length;
2063 break;
2064 default:
2065 break;
2066 }
2067
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002068 const float oldX = x;
2069 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002070 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2071 if (pureTranslate) {
2072 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2073 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2074 }
2075
Romain Guyb45c0c92010-08-26 20:35:23 -07002076 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2077 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002078 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002079
Romain Guy86568192010-12-14 15:55:39 -08002080 int alpha;
2081 SkXfermode::Mode mode;
2082 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002083
Romain Guy1e45aae2010-08-13 19:39:53 -07002084 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002085 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002086 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2087 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002088 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002089
Romain Guy740bf2b2011-04-26 15:33:10 -07002090 const float sx = oldX - shadow->left + mShadowDx;
2091 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002092
Romain Guy86568192010-12-14 15:55:39 -08002093 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002094 int shadowColor = mShadowColor;
2095 if (mShader) {
2096 shadowColor = 0xffffffff;
2097 }
Romain Guy86568192010-12-14 15:55:39 -08002098
2099 glActiveTexture(gTextureUnits[0]);
2100 setupDraw();
2101 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002102 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2103 setupDrawColorFilter();
2104 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002105 setupDrawBlending(true, mode);
2106 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002107 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002108 setupDrawTexture(shadow->id);
2109 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002110 setupDrawColorFilterUniforms();
2111 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002112 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2113
Romain Guy0a417492010-08-16 20:26:20 -07002114 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002115
Romain Guy86568192010-12-14 15:55:39 -08002116 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002117 }
2118
Romain Guyb146b122010-12-15 17:06:45 -08002119 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2120 return;
2121 }
2122
Romain Guy6620c6d2010-12-06 18:07:02 -08002123 // Pick the appropriate texture filtering
2124 bool linearFilter = mSnapshot->transform->changesBounds();
2125 if (pureTranslate && !linearFilter) {
2126 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2127 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002128
Romain Guy86568192010-12-14 15:55:39 -08002129 glActiveTexture(gTextureUnits[0]);
2130 setupDraw();
2131 setupDrawDirtyRegionsDisabled();
2132 setupDrawWithTexture(true);
2133 setupDrawAlpha8Color(paint->getColor(), alpha);
2134 setupDrawColorFilter();
2135 setupDrawShader();
2136 setupDrawBlending(true, mode);
2137 setupDrawProgram();
2138 setupDrawModelView(x, y, x, y, pureTranslate, true);
2139 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2140 setupDrawPureColorUniforms();
2141 setupDrawColorFilterUniforms();
2142 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002143
Romain Guy6620c6d2010-12-06 18:07:02 -08002144 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002145 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2146
2147#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002148 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002149#else
Romain Guyf219da52011-01-16 12:54:25 -08002150 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002151#endif
Romain Guy03750a02010-10-18 14:06:08 -07002152 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002153
2154 // Tell font renderer the locations of position and texture coord
2155 // attributes so it can bind its data properly
2156 int positionSlot = mCaches.currentProgram->position;
2157 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002158 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002159 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002160#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002161 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002162 if (!pureTranslate) {
2163 mSnapshot->transform->mapRect(bounds);
2164 }
Romain Guyf219da52011-01-16 12:54:25 -08002165 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002166 }
2167#endif
2168 }
Romain Guy694b5192010-07-21 21:33:20 -07002169
2170 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002171 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002172
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002173 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002174}
2175
Romain Guy7fbcc042010-08-04 15:40:07 -07002176void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002177 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002178
Romain Guy01d58e42011-01-19 21:54:02 -08002179 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002180
Romain Guyfb8b7632010-08-23 21:05:08 -07002181 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002182 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002183 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002184
Romain Guy8b55f372010-08-18 17:10:07 -07002185 const float x = texture->left - texture->offset;
2186 const float y = texture->top - texture->offset;
2187
Romain Guy01d58e42011-01-19 21:54:02 -08002188 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002189}
2190
Romain Guyada830f2011-01-13 12:13:20 -08002191void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2192 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002193 return;
2194 }
2195
2196 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002197
2198 int alpha;
2199 SkXfermode::Mode mode;
2200 getAlphaAndMode(paint, &alpha, &mode);
2201
Romain Guy9ace8f52011-07-07 20:50:11 -07002202 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002203
Romain Guyf219da52011-01-16 12:54:25 -08002204#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002205 if (!layer->region.isEmpty()) {
2206 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002207 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002208 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002209 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002210 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002211
Romain Guyc88e3572011-01-22 00:32:12 -08002212 setupDraw();
2213 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002214 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002215 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002216 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002217 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002218 setupDrawPureColorUniforms();
2219 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002220 setupDrawTexture(layer->getTexture());
2221 if (mSnapshot->transform->isPureTranslate()) {
2222 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2223 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2224
2225 layer->setFilter(GL_NEAREST, GL_NEAREST);
2226 setupDrawModelViewTranslate(x, y,
2227 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2228 } else {
2229 layer->setFilter(GL_LINEAR, GL_LINEAR);
2230 setupDrawModelViewTranslate(x, y,
2231 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2232 }
Romain Guyc88e3572011-01-22 00:32:12 -08002233 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002234
Romain Guyc88e3572011-01-22 00:32:12 -08002235 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2236 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002237
Romain Guyc88e3572011-01-22 00:32:12 -08002238 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002239
2240#if DEBUG_LAYERS_AS_REGIONS
2241 drawRegionRects(layer->region);
2242#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002243 }
Romain Guyf219da52011-01-16 12:54:25 -08002244 }
2245#else
Romain Guyada830f2011-01-13 12:13:20 -08002246 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2247 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002248#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002249}
2250
Romain Guy6926c722010-07-12 20:20:03 -07002251///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002252// Shaders
2253///////////////////////////////////////////////////////////////////////////////
2254
2255void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002256 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002257}
2258
Romain Guy06f96e22010-07-30 19:18:16 -07002259void OpenGLRenderer::setupShader(SkiaShader* shader) {
2260 mShader = shader;
2261 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002262 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002263 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002264}
2265
Romain Guyd27977d2010-07-14 19:18:51 -07002266///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002267// Color filters
2268///////////////////////////////////////////////////////////////////////////////
2269
2270void OpenGLRenderer::resetColorFilter() {
2271 mColorFilter = NULL;
2272}
2273
2274void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2275 mColorFilter = filter;
2276}
2277
2278///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002279// Drop shadow
2280///////////////////////////////////////////////////////////////////////////////
2281
2282void OpenGLRenderer::resetShadow() {
2283 mHasShadow = false;
2284}
2285
2286void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2287 mHasShadow = true;
2288 mShadowRadius = radius;
2289 mShadowDx = dx;
2290 mShadowDy = dy;
2291 mShadowColor = color;
2292}
2293
2294///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002295// Drawing implementation
2296///////////////////////////////////////////////////////////////////////////////
2297
Romain Guy01d58e42011-01-19 21:54:02 -08002298void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2299 float x, float y, SkPaint* paint) {
2300 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2301 return;
2302 }
2303
2304 int alpha;
2305 SkXfermode::Mode mode;
2306 getAlphaAndMode(paint, &alpha, &mode);
2307
2308 setupDraw();
2309 setupDrawWithTexture(true);
2310 setupDrawAlpha8Color(paint->getColor(), alpha);
2311 setupDrawColorFilter();
2312 setupDrawShader();
2313 setupDrawBlending(true, mode);
2314 setupDrawProgram();
2315 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2316 setupDrawTexture(texture->id);
2317 setupDrawPureColorUniforms();
2318 setupDrawColorFilterUniforms();
2319 setupDrawShaderUniforms();
2320 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2321
2322 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2323
2324 finishDrawTexture();
2325}
2326
Romain Guyf607bdc2010-09-10 19:20:06 -07002327// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002328#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2329#define kStdUnderline_Offset (1.0f / 9.0f)
2330#define kStdUnderline_Thickness (1.0f / 18.0f)
2331
2332void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2333 float x, float y, SkPaint* paint) {
2334 // Handle underline and strike-through
2335 uint32_t flags = paint->getFlags();
2336 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002337 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002338 float underlineWidth = length;
2339 // If length is > 0.0f, we already measured the text for the text alignment
2340 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002341 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002342 }
2343
2344 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002345 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002346 case SkPaint::kCenter_Align:
2347 offsetX = underlineWidth * 0.5f;
2348 break;
2349 case SkPaint::kRight_Align:
2350 offsetX = underlineWidth;
2351 break;
2352 default:
2353 break;
2354 }
2355
2356 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002357 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002358 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002359
Romain Guye20ecbd2010-09-22 19:49:04 -07002360 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002361 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002362
Romain Guyf6834472011-01-23 13:32:12 -08002363 int linesCount = 0;
2364 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2365 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2366
2367 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002368 float points[pointsCount];
2369 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002370
2371 if (flags & SkPaint::kUnderlineText_Flag) {
2372 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002373 points[currentPoint++] = left;
2374 points[currentPoint++] = top;
2375 points[currentPoint++] = left + underlineWidth;
2376 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002377 }
2378
2379 if (flags & SkPaint::kStrikeThruText_Flag) {
2380 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002381 points[currentPoint++] = left;
2382 points[currentPoint++] = top;
2383 points[currentPoint++] = left + underlineWidth;
2384 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002385 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002386
Romain Guy726aeba2011-06-01 14:52:00 -07002387 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002388
Romain Guy726aeba2011-06-01 14:52:00 -07002389 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002390 }
2391 }
2392}
2393
Romain Guy026c5e162010-06-28 17:12:22 -07002394void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002395 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002396 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002397 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002398 color |= 0x00ffffff;
2399 }
2400
Romain Guy70ca14e2010-12-13 18:24:33 -08002401 setupDraw();
2402 setupDrawColor(color);
2403 setupDrawShader();
2404 setupDrawColorFilter();
2405 setupDrawBlending(mode);
2406 setupDrawProgram();
2407 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2408 setupDrawColorUniforms();
2409 setupDrawShaderUniforms(ignoreTransform);
2410 setupDrawColorFilterUniforms();
2411 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002412
Romain Guyc95c8d62010-09-17 15:31:32 -07002413 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2414}
2415
Romain Guy82ba8142010-07-09 13:25:56 -07002416void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002417 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002418 int alpha;
2419 SkXfermode::Mode mode;
2420 getAlphaAndMode(paint, &alpha, &mode);
2421
Romain Guye3c26852011-07-25 16:36:01 -07002422 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002423
Romain Guy6620c6d2010-12-06 18:07:02 -08002424 if (mSnapshot->transform->isPureTranslate()) {
2425 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2426 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2427
Romain Guye3c26852011-07-25 16:36:01 -07002428 texture->setFilter(GL_NEAREST, GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002429 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2430 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2431 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2432 } else {
Romain Guye3c26852011-07-25 16:36:01 -07002433 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002434 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2435 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2436 GL_TRIANGLE_STRIP, gMeshCount);
2437 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002438}
2439
Romain Guybd6b79b2010-06-26 00:13:53 -07002440void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002441 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2442 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002443 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002444}
2445
2446void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002447 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002448 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002449 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002450
Romain Guy746b7402010-10-26 16:27:31 -07002451 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002452 setupDrawWithTexture();
2453 setupDrawColor(alpha, alpha, alpha, alpha);
2454 setupDrawColorFilter();
2455 setupDrawBlending(blend, mode, swapSrcDst);
2456 setupDrawProgram();
2457 if (!dirty) {
2458 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002459 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002460 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002461 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002462 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002463 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002464 }
Romain Guy86568192010-12-14 15:55:39 -08002465 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002466 setupDrawColorFilterUniforms();
2467 setupDrawTexture(texture);
2468 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002469
Romain Guy6820ac82010-09-15 18:11:50 -07002470 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002471
2472 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002473}
2474
Romain Guya5aed0d2010-09-09 14:42:43 -07002475void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002476 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002477 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2478 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002479 if (mode < SkXfermode::kPlus_Mode) {
2480 if (!mCaches.blend) {
2481 glEnable(GL_BLEND);
2482 }
Romain Guy82ba8142010-07-09 13:25:56 -07002483
Romain Guyf607bdc2010-09-10 19:20:06 -07002484 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2485 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002486
Romain Guya5aed0d2010-09-09 14:42:43 -07002487 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2488 glBlendFunc(sourceMode, destMode);
2489 mCaches.lastSrcMode = sourceMode;
2490 mCaches.lastDstMode = destMode;
2491 }
2492 } else {
2493 // These blend modes are not supported by OpenGL directly and have
2494 // to be implemented using shaders. Since the shader will perform
2495 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002496 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002497 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002498 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002499 }
2500
2501 if (mCaches.blend) {
2502 glDisable(GL_BLEND);
2503 }
2504 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002505 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002506 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002507 glDisable(GL_BLEND);
2508 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002509 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002510}
2511
Romain Guy889f8d12010-07-29 14:37:42 -07002512bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002513 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002514 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002515 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002516 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002517 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002518 }
Romain Guy6926c722010-07-12 20:20:03 -07002519 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002520}
2521
Romain Guy026c5e162010-06-28 17:12:22 -07002522void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002523 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002524 TextureVertex::setUV(v++, u1, v1);
2525 TextureVertex::setUV(v++, u2, v1);
2526 TextureVertex::setUV(v++, u1, v2);
2527 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002528}
2529
Chet Haase5c13d892010-10-08 08:37:55 -07002530void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002531 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002532 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002533 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2534 if (!isMode) {
2535 // Assume SRC_OVER
2536 *mode = SkXfermode::kSrcOver_Mode;
2537 }
2538 } else {
2539 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002540 }
2541
2542 // Skia draws using the color's alpha channel if < 255
2543 // Otherwise, it uses the paint's alpha
2544 int color = paint->getColor();
2545 *alpha = (color >> 24) & 0xFF;
2546 if (*alpha == 255) {
2547 *alpha = paint->getAlpha();
2548 }
2549 } else {
2550 *mode = SkXfermode::kSrcOver_Mode;
2551 *alpha = 255;
2552 }
Romain Guy026c5e162010-06-28 17:12:22 -07002553}
2554
Romain Guya5aed0d2010-09-09 14:42:43 -07002555SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002556 SkXfermode::Mode resultMode;
2557 if (!SkXfermode::AsMode(mode, &resultMode)) {
2558 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002559 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002560 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002561}
2562
Romain Guy9d5316e2010-06-24 19:30:36 -07002563}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002564}; // namespace android