blob: 910195360d7300a82482200ea6fca0093e16bfcf [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 Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700114
Romain Guyac670c02010-07-27 17:39:27 -0700115 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
116
Romain Guyae5575b2010-07-29 18:48:04 -0700117 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700118}
119
Romain Guy85bf02f2010-06-22 13:11:24 -0700120OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700121 // The context has already been destroyed at this point, do not call
122 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guyf6a11b82010-06-23 17:47:49 -0700125///////////////////////////////////////////////////////////////////////////////
126// Setup
127///////////////////////////////////////////////////////////////////////////////
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129void OpenGLRenderer::setViewport(int width, int 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
Romain Guy3e263fa2011-12-12 16:47:48 -0800138 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800139 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800140 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800141
Romain Guy3e263fa2011-12-12 16:47:48 -0800142 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700143}
144
Romain Guy6b7bd242010-10-06 19:49:23 -0700145void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800146 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
147}
148
149void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800150 mCaches.clearGarbage();
151
Romain Guy8aef54f2010-09-01 15:13:49 -0700152 mSnapshot = new Snapshot(mFirstSnapshot,
153 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800154 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700155 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700156
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800158 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800159
Romain Guy7d7b5492011-01-24 16:33:45 -0800160 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700164 glClear(GL_COLOR_BUFFER_BIT);
165 }
Romain Guybb9524b2010-06-22 18:56:38 -0700166}
167
Romain Guyb025b9c2010-09-16 14:16:48 -0700168void OpenGLRenderer::finish() {
169#if DEBUG_OPENGL
170 GLenum status = GL_NO_ERROR;
171 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000172 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800173 switch (status) {
174 case GL_OUT_OF_MEMORY:
175 LOGE(" OpenGLRenderer is out of memory!");
176 break;
177 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700178 }
179#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800180#if DEBUG_MEMORY_USAGE
181 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800182#else
183 if (mCaches.getDebugLevel() & kDebugMemory) {
184 mCaches.dumpMemoryUsage();
185 }
Romain Guyc15008e2010-11-10 11:59:15 -0800186#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700187}
188
Romain Guy6c319ca2011-01-11 14:29:25 -0800189void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700190 if (mCaches.currentProgram) {
191 if (mCaches.currentProgram->isInUse()) {
192 mCaches.currentProgram->remove();
193 mCaches.currentProgram = NULL;
194 }
195 }
Romain Guy50c0f092010-10-19 11:42:22 -0700196 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800197 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800198 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800199 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700200}
201
Romain Guy6c319ca2011-01-11 14:29:25 -0800202void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800203 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
204
205 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800206 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
207
Romain Guyda8532c2010-08-31 11:50:35 -0700208 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800209 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700210 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700211
Romain Guya1d3c912011-12-13 14:55:06 -0800212 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800213 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700214
Romain Guy50c0f092010-10-19 11:42:22 -0700215 mCaches.blend = true;
216 glEnable(GL_BLEND);
217 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
218 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700219}
220
Romain Guycabfcc12011-03-07 18:06:46 -0800221bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800222 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800223 if (mDirtyClip) {
224 setScissorFromClip();
225 }
Romain Guyd643bb52011-03-01 14:55:21 -0800226
Romain Guy80911b82011-03-16 15:30:12 -0700227 Rect clip(*mSnapshot->clipRect);
228 clip.snapToPixelBoundaries();
229
Romain Guyd643bb52011-03-01 14:55:21 -0800230#if RENDER_LAYERS_AS_REGIONS
231 // Since we don't know what the functor will draw, let's dirty
232 // tne entire clip region
233 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800234 dirtyLayerUnchecked(clip, getRegion());
235 }
236#endif
237
Romain Guy08aa2cb2011-03-17 11:06:57 -0700238 DrawGlInfo info;
239 info.clipLeft = clip.left;
240 info.clipTop = clip.top;
241 info.clipRight = clip.right;
242 info.clipBottom = clip.bottom;
243 info.isLayer = hasLayer();
244 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700245
Romain Guy08aa2cb2011-03-17 11:06:57 -0700246 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800247
248 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700249 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800250 dirty.unionWith(localDirty);
251 }
252
Chet Haasedaf98e92011-01-10 14:10:36 -0800253 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800254 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800255}
256
Romain Guyf6a11b82010-06-23 17:47:49 -0700257///////////////////////////////////////////////////////////////////////////////
258// State management
259///////////////////////////////////////////////////////////////////////////////
260
Romain Guybb9524b2010-06-22 18:56:38 -0700261int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700262 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700263}
264
265int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700266 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
269void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700270 if (mSaveCount > 1) {
271 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700272 }
Romain Guybb9524b2010-06-22 18:56:38 -0700273}
274
275void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700276 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700277
Romain Guy8fb95422010-08-17 18:38:51 -0700278 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700279 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700280 }
Romain Guybb9524b2010-06-22 18:56:38 -0700281}
282
Romain Guy8aef54f2010-09-01 15:13:49 -0700283int OpenGLRenderer::saveSnapshot(int flags) {
284 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700285 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700286}
287
288bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700289 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700290 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700291 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700292
Romain Guybd6b79b2010-06-26 00:13:53 -0700293 sp<Snapshot> current = mSnapshot;
294 sp<Snapshot> previous = mSnapshot->previous;
295
Romain Guyeb993562010-10-05 18:14:38 -0700296 if (restoreOrtho) {
297 Rect& r = previous->viewport;
298 glViewport(r.left, r.top, r.right, r.bottom);
299 mOrthoMatrix.load(current->orthoMatrix);
300 }
301
Romain Guy8b55f372010-08-18 17:10:07 -0700302 mSaveCount--;
303 mSnapshot = previous;
304
Romain Guy2542d192010-08-18 11:47:12 -0700305 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700306 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700307 }
Romain Guy2542d192010-08-18 11:47:12 -0700308
Romain Guy5ec99242010-11-03 16:19:08 -0700309 if (restoreLayer) {
310 composeLayer(current, previous);
311 }
312
Romain Guy2542d192010-08-18 11:47:12 -0700313 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700314}
315
Romain Guyf6a11b82010-06-23 17:47:49 -0700316///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700317// Layers
318///////////////////////////////////////////////////////////////////////////////
319
320int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700321 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700322 const GLuint previousFbo = mSnapshot->fbo;
323 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700324
Romain Guyaf636eb2010-12-09 17:47:21 -0800325 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700326 int alpha = 255;
327 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700328
Romain Guye45362c2010-11-03 19:58:32 -0700329 if (p) {
330 alpha = p->getAlpha();
331 if (!mCaches.extensions.hasFramebufferFetch()) {
332 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
333 if (!isMode) {
334 // Assume SRC_OVER
335 mode = SkXfermode::kSrcOver_Mode;
336 }
337 } else {
338 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700339 }
340 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700341 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700342 }
Romain Guyd55a8612010-06-28 17:42:46 -0700343
Romain Guydbc26d22010-10-11 17:58:29 -0700344 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
345 }
Romain Guyd55a8612010-06-28 17:42:46 -0700346
347 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700348}
349
350int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
351 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700352 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700353 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700354 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700355 SkPaint paint;
356 paint.setAlpha(alpha);
357 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700358 }
Romain Guyd55a8612010-06-28 17:42:46 -0700359}
Romain Guybd6b79b2010-06-26 00:13:53 -0700360
Romain Guy1c740bc2010-09-13 18:00:09 -0700361/**
362 * Layers are viewed by Skia are slightly different than layers in image editing
363 * programs (for instance.) When a layer is created, previously created layers
364 * and the frame buffer still receive every drawing command. For instance, if a
365 * layer is created and a shape intersecting the bounds of the layers and the
366 * framebuffer is draw, the shape will be drawn on both (unless the layer was
367 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
368 *
369 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
370 * texture. Unfortunately, this is inefficient as it requires every primitive to
371 * be drawn n + 1 times, where n is the number of active layers. In practice this
372 * means, for every primitive:
373 * - Switch active frame buffer
374 * - Change viewport, clip and projection matrix
375 * - Issue the drawing
376 *
377 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700378 * To avoid this, layers are implemented in a different way here, at least in the
379 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
380 * is set. When this flag is set we can redirect all drawing operations into a
381 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700382 *
383 * This implementation relies on the frame buffer being at least RGBA 8888. When
384 * a layer is created, only a texture is created, not an FBO. The content of the
385 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700386 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700387 * buffer and drawing continues as normal. This technique therefore treats the
388 * frame buffer as a scratch buffer for the layers.
389 *
390 * To compose the layers back onto the frame buffer, each layer texture
391 * (containing the original frame buffer data) is drawn as a simple quad over
392 * the frame buffer. The trick is that the quad is set as the composition
393 * destination in the blending equation, and the frame buffer becomes the source
394 * of the composition.
395 *
396 * Drawing layers with an alpha value requires an extra step before composition.
397 * An empty quad is drawn over the layer's region in the frame buffer. This quad
398 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
399 * quad is used to multiply the colors in the frame buffer. This is achieved by
400 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
401 * GL_ZERO, GL_SRC_ALPHA.
402 *
403 * Because glCopyTexImage2D() can be slow, an alternative implementation might
404 * be use to draw a single clipped layer. The implementation described above
405 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700406 *
407 * (1) The frame buffer is actually not cleared right away. To allow the GPU
408 * to potentially optimize series of calls to glCopyTexImage2D, the frame
409 * buffer is left untouched until the first drawing operation. Only when
410 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700411 */
Romain Guyd55a8612010-06-28 17:42:46 -0700412bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700413 float right, float bottom, int alpha, SkXfermode::Mode mode,
414 int flags, GLuint previousFbo) {
415 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700416 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700417
Romain Guyeb993562010-10-05 18:14:38 -0700418 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
419
Romain Guyf607bdc2010-09-10 19:20:06 -0700420 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700421 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700422 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700423 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700424
Romain Guyeb993562010-10-05 18:14:38 -0700425 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700426 if (bounds.intersect(*snapshot->clipRect)) {
427 // We cannot work with sub-pixels in this case
428 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700429
Romain Guyad37cd32011-03-15 11:12:25 -0700430 // When the layer is not an FBO, we may use glCopyTexImage so we
431 // need to make sure the layer does not extend outside the bounds
432 // of the framebuffer
433 if (!bounds.intersect(snapshot->previous->viewport)) {
434 bounds.setEmpty();
435 }
436 } else {
437 bounds.setEmpty();
438 }
Romain Guyeb993562010-10-05 18:14:38 -0700439 }
Romain Guybf434112010-09-16 14:40:17 -0700440
Romain Guy746b7402010-10-26 16:27:31 -0700441 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
442 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800443 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700444 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700445 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700446 }
447
448 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800449 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700450 return false;
451 }
Romain Guyf18fd992010-07-08 11:45:51 -0700452
Romain Guya1d3c912011-12-13 14:55:06 -0800453 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700454 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700455 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700456 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700457 }
458
Romain Guy9ace8f52011-07-07 20:50:11 -0700459 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700460 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700461 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
462 bounds.getWidth() / float(layer->getWidth()), 0.0f);
463 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700464
Romain Guy8fb95422010-08-17 18:38:51 -0700465 // Save the layer in the snapshot
466 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700467 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700468
Romain Guyeb993562010-10-05 18:14:38 -0700469 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700470 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700471 } else {
472 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700473 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800474 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700475 if (layer->isEmpty()) {
476 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
477 bounds.left, snapshot->height - bounds.bottom,
478 layer->getWidth(), layer->getHeight(), 0);
479 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800480 } else {
481 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
482 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
483 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700484
Romain Guy54be1cd2011-06-13 19:04:27 -0700485 // Enqueue the buffer coordinates to clear the corresponding region later
486 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700487 }
Romain Guyeb993562010-10-05 18:14:38 -0700488 }
Romain Guyf86ef572010-07-01 11:05:42 -0700489
Romain Guyd55a8612010-06-28 17:42:46 -0700490 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700491}
492
Romain Guy5b3b3522010-10-27 18:57:51 -0700493bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
494 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700495 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700496
497#if RENDER_LAYERS_AS_REGIONS
498 snapshot->region = &snapshot->layer->region;
499 snapshot->flags |= Snapshot::kFlagFboTarget;
500#endif
501
502 Rect clip(bounds);
503 snapshot->transform->mapRect(clip);
504 clip.intersect(*snapshot->clipRect);
505 clip.snapToPixelBoundaries();
506 clip.intersect(snapshot->previous->viewport);
507
508 mat4 inverse;
509 inverse.loadInverse(*mSnapshot->transform);
510
511 inverse.mapRect(clip);
512 clip.snapToPixelBoundaries();
513 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700514 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700515
516 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700517 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700519 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
520 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
521 snapshot->height = bounds.getHeight();
522 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
523 snapshot->orthoMatrix.load(mOrthoMatrix);
524
525 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700526 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
527 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700528
529 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700530 if (layer->isEmpty()) {
531 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
532 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700533 }
534
535 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700536 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700537
538#if DEBUG_LAYERS_AS_REGIONS
539 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
540 if (status != GL_FRAMEBUFFER_COMPLETE) {
541 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
542
543 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700544 layer->deleteTexture();
545 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700546
547 delete layer;
548
549 return false;
550 }
551#endif
552
553 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800554 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700555 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700556 glClear(GL_COLOR_BUFFER_BIT);
557
558 dirtyClip();
559
560 // Change the ortho projection
561 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
562 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
563
564 return true;
565}
566
Romain Guy1c740bc2010-09-13 18:00:09 -0700567/**
568 * Read the documentation of createLayer() before doing anything in this method.
569 */
Romain Guy1d83e192010-08-17 11:37:00 -0700570void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
571 if (!current->layer) {
572 LOGE("Attempting to compose a layer that does not exist");
573 return;
574 }
575
Romain Guy5b3b3522010-10-27 18:57:51 -0700576 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700577
578 if (fboLayer) {
579 // Unbind current FBO and restore previous one
580 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
581 }
582
Romain Guy1d83e192010-08-17 11:37:00 -0700583 Layer* layer = current->layer;
584 const Rect& rect = layer->layer;
585
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700587 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700589 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700591 }
592
Romain Guy03750a02010-10-18 14:06:08 -0700593 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700594
Romain Guya1d3c912011-12-13 14:55:06 -0800595 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700596
Romain Guy5b3b3522010-10-27 18:57:51 -0700597 // When the layer is stored in an FBO, we can save a bit of fillrate by
598 // drawing only the dirty region
599 if (fboLayer) {
600 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700601 if (layer->getColorFilter()) {
602 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800603 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800606 resetColorFilter();
607 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700608 } else if (!rect.isEmpty()) {
609 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
610 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700611 }
Romain Guy8b55f372010-08-18 17:10:07 -0700612
Romain Guyeb993562010-10-05 18:14:38 -0700613 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800614 // Note: No need to use glDiscardFramebufferEXT() since we never
615 // create/compose layers that are not on screen with this
616 // code path
617 // See LayerRenderer::destroyLayer(Layer*)
618
Romain Guyeb993562010-10-05 18:14:38 -0700619 // Detach the texture from the FBO
620 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
621 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
622 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
623
624 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
625 mCaches.fboCache.put(current->fbo);
626 }
627
Romain Guy746b7402010-10-26 16:27:31 -0700628 dirtyClip();
629
Romain Guyeb993562010-10-05 18:14:38 -0700630 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700631 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700632 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700633 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700634 delete layer;
635 }
636}
637
Romain Guyaa6c24c2011-04-28 18:40:04 -0700638void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700639 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640
Romain Guy302a9df2011-08-16 13:55:02 -0700641 mat4& transform = layer->getTransform();
642 if (!transform.isIdentity()) {
643 save(0);
644 mSnapshot->transform->multiply(transform);
645 }
646
Romain Guyaa6c24c2011-04-28 18:40:04 -0700647 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700648 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700649 setupDrawWithTexture();
650 } else {
651 setupDrawWithExternalTexture();
652 }
653 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700654 setupDrawColor(alpha, alpha, alpha, alpha);
655 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700656 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 setupDrawPureColorUniforms();
659 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
661 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700662 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700664 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700665 if (mSnapshot->transform->isPureTranslate() &&
666 layer->getWidth() == (uint32_t) rect.getWidth() &&
667 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
669 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
670
Romain Guyd21b6e12011-11-30 20:21:23 -0800671 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700672 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
673 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800674 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700675 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
676 }
677 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700678 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
679
680 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
681
682 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700683
684 if (!transform.isIdentity()) {
685 restore();
686 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700687}
688
Romain Guy5b3b3522010-10-27 18:57:51 -0700689void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700691 const Rect& texCoords = layer->texCoords;
692 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
693 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700694
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 float x = rect.left;
696 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700697 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700698 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700699 layer->getHeight() == (uint32_t) rect.getHeight();
700
701 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 // When we're swapping, the layer is already in screen coordinates
703 if (!swap) {
704 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
705 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
706 }
707
Romain Guyd21b6e12011-11-30 20:21:23 -0800708 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700709 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800710 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 }
712
713 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
714 layer->getTexture(), layer->getAlpha() / 255.0f,
715 layer->getMode(), layer->isBlend(),
716 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
717 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700718
Romain Guyaa6c24c2011-04-28 18:40:04 -0700719 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
720 } else {
721 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
722 drawTextureLayer(layer, rect);
723 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
724 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700725}
726
727void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
728#if RENDER_LAYERS_AS_REGIONS
729 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700730 layer->setRegionAsRect();
731
Romain Guy40667672011-03-18 14:34:03 -0700732 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700733
Romain Guy5b3b3522010-10-27 18:57:51 -0700734 layer->region.clear();
735 return;
736 }
737
Romain Guy8a3957d2011-09-07 17:55:15 -0700738 // TODO: See LayerRenderer.cpp::generateMesh() for important
739 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 if (!layer->region.isEmpty()) {
741 size_t count;
742 const android::Rect* rects = layer->region.getArray(&count);
743
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 const float alpha = layer->getAlpha() / 255.0f;
745 const float texX = 1.0f / float(layer->getWidth());
746 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800747 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
749 TextureVertex* mesh = mCaches.getRegionMesh();
750 GLsizei numQuads = 0;
751
Romain Guy7230a742011-01-10 22:26:16 -0800752 setupDraw();
753 setupDrawWithTexture();
754 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800755 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800757 setupDrawProgram();
758 setupDrawDirtyRegionsDisabled();
759 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800760 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 setupDrawTexture(layer->getTexture());
762 if (mSnapshot->transform->isPureTranslate()) {
763 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
764 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
765
Romain Guyd21b6e12011-11-30 20:21:23 -0800766 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
768 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800769 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
771 }
Romain Guy15bc6432011-12-13 13:11:32 -0800772 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700773
774 for (size_t i = 0; i < count; i++) {
775 const android::Rect* r = &rects[i];
776
777 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800778 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700779 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800780 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700781
782 // TODO: Reject quads outside of the clip
783 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
784 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
785 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
786 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
787
788 numQuads++;
789
790 if (numQuads >= REGION_MESH_QUAD_COUNT) {
791 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
792 numQuads = 0;
793 mesh = mCaches.getRegionMesh();
794 }
795 }
796
797 if (numQuads > 0) {
798 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
799 }
800
Romain Guy7230a742011-01-10 22:26:16 -0800801 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700802
803#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800804 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700805#endif
806
807 layer->region.clear();
808 }
809#else
810 composeLayerRect(layer, rect);
811#endif
812}
813
Romain Guy3a3133d2011-02-01 22:59:58 -0800814void OpenGLRenderer::drawRegionRects(const Region& region) {
815#if DEBUG_LAYERS_AS_REGIONS
816 size_t count;
817 const android::Rect* rects = region.getArray(&count);
818
819 uint32_t colors[] = {
820 0x7fff0000, 0x7f00ff00,
821 0x7f0000ff, 0x7fff00ff,
822 };
823
824 int offset = 0;
825 int32_t top = rects[0].top;
826
827 for (size_t i = 0; i < count; i++) {
828 if (top != rects[i].top) {
829 offset ^= 0x2;
830 top = rects[i].top;
831 }
832
833 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
834 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
835 SkXfermode::kSrcOver_Mode);
836 }
837#endif
838}
839
Romain Guy5b3b3522010-10-27 18:57:51 -0700840void OpenGLRenderer::dirtyLayer(const float left, const float top,
841 const float right, const float bottom, const mat4 transform) {
842#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800843 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 Rect bounds(left, top, right, bottom);
845 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800846 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 }
848#endif
849}
850
851void OpenGLRenderer::dirtyLayer(const float left, const float top,
852 const float right, const float bottom) {
853#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800854 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800856 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800857 }
858#endif
859}
860
861void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
862#if RENDER_LAYERS_AS_REGIONS
863 if (bounds.intersect(*mSnapshot->clipRect)) {
864 bounds.snapToPixelBoundaries();
865 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
866 if (!dirty.isEmpty()) {
867 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700868 }
869 }
870#endif
871}
872
Romain Guy54be1cd2011-06-13 19:04:27 -0700873void OpenGLRenderer::clearLayerRegions() {
874 const size_t count = mLayers.size();
875 if (count == 0) return;
876
877 if (!mSnapshot->isIgnored()) {
878 // Doing several glScissor/glClear here can negatively impact
879 // GPUs with a tiler architecture, instead we draw quads with
880 // the Clear blending mode
881
882 // The list contains bounds that have already been clipped
883 // against their initial clip rect, and the current clip
884 // is likely different so we need to disable clipping here
885 glDisable(GL_SCISSOR_TEST);
886
887 Vertex mesh[count * 6];
888 Vertex* vertex = mesh;
889
890 for (uint32_t i = 0; i < count; i++) {
891 Rect* bounds = mLayers.itemAt(i);
892
893 Vertex::set(vertex++, bounds->left, bounds->bottom);
894 Vertex::set(vertex++, bounds->left, bounds->top);
895 Vertex::set(vertex++, bounds->right, bounds->top);
896 Vertex::set(vertex++, bounds->left, bounds->bottom);
897 Vertex::set(vertex++, bounds->right, bounds->top);
898 Vertex::set(vertex++, bounds->right, bounds->bottom);
899
900 delete bounds;
901 }
902
903 setupDraw(false);
904 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
905 setupDrawBlending(true, SkXfermode::kClear_Mode);
906 setupDrawProgram();
907 setupDrawPureColorUniforms();
908 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800909 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700910
Romain Guy54be1cd2011-06-13 19:04:27 -0700911 glDrawArrays(GL_TRIANGLES, 0, count * 6);
912
913 glEnable(GL_SCISSOR_TEST);
914 } else {
915 for (uint32_t i = 0; i < count; i++) {
916 delete mLayers.itemAt(i);
917 }
918 }
919
920 mLayers.clear();
921}
922
Romain Guybd6b79b2010-06-26 00:13:53 -0700923///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700924// Transforms
925///////////////////////////////////////////////////////////////////////////////
926
927void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700928 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700929}
930
931void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700932 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700933}
934
935void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700937}
938
Romain Guy807daf72011-01-18 11:19:19 -0800939void OpenGLRenderer::skew(float sx, float sy) {
940 mSnapshot->transform->skew(sx, sy);
941}
942
Romain Guyf6a11b82010-06-23 17:47:49 -0700943void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700944 if (matrix) {
945 mSnapshot->transform->load(*matrix);
946 } else {
947 mSnapshot->transform->loadIdentity();
948 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700949}
950
951void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700952 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700953}
954
955void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700956 SkMatrix transform;
957 mSnapshot->transform->copyTo(transform);
958 transform.preConcat(*matrix);
959 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700960}
961
962///////////////////////////////////////////////////////////////////////////////
963// Clipping
964///////////////////////////////////////////////////////////////////////////////
965
Romain Guybb9524b2010-06-22 18:56:38 -0700966void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700967 Rect clip(*mSnapshot->clipRect);
968 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800969
970 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
971 clip.getWidth(), clip.getHeight());
972
Romain Guy746b7402010-10-26 16:27:31 -0700973 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700974}
975
976const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700977 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700978}
979
Romain Guyc7d53492010-06-25 13:41:57 -0700980bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800981 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700982 return true;
983 }
984
Romain Guy1d83e192010-08-17 11:37:00 -0700985 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700986 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700987 r.snapToPixelBoundaries();
988
989 Rect clipRect(*mSnapshot->clipRect);
990 clipRect.snapToPixelBoundaries();
991
992 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700993}
994
Romain Guy079ba2c2010-07-16 14:12:24 -0700995bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
996 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700997 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700998 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700999 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001000 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001001}
1002
Romain Guyf6a11b82010-06-23 17:47:49 -07001003///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001004// Drawing commands
1005///////////////////////////////////////////////////////////////////////////////
1006
Romain Guy54be1cd2011-06-13 19:04:27 -07001007void OpenGLRenderer::setupDraw(bool clear) {
1008 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001009 if (mDirtyClip) {
1010 setScissorFromClip();
1011 }
1012 mDescription.reset();
1013 mSetShaderColor = false;
1014 mColorSet = false;
1015 mColorA = mColorR = mColorG = mColorB = 0.0f;
1016 mTextureUnit = 0;
1017 mTrackDirtyRegions = true;
1018}
1019
1020void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1021 mDescription.hasTexture = true;
1022 mDescription.hasAlpha8Texture = isAlpha8;
1023}
1024
Romain Guyaa6c24c2011-04-28 18:40:04 -07001025void OpenGLRenderer::setupDrawWithExternalTexture() {
1026 mDescription.hasExternalTexture = true;
1027}
1028
Romain Guy15bc6432011-12-13 13:11:32 -08001029void OpenGLRenderer::setupDrawNoTexture() {
1030 mCaches.disbaleTexCoordsVertexArray();
1031}
1032
Chet Haase5b0200b2011-04-13 17:58:08 -07001033void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001034 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001035}
1036
Romain Guyed6fcb02011-03-21 13:11:28 -07001037void OpenGLRenderer::setupDrawPoint(float pointSize) {
1038 mDescription.isPoint = true;
1039 mDescription.pointSize = pointSize;
1040}
1041
Romain Guy70ca14e2010-12-13 18:24:33 -08001042void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001043 setupDrawColor(color, (color >> 24) & 0xFF);
1044}
1045
1046void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1047 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001048 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1049 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001050 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001051 mColorR = a * ((color >> 16) & 0xFF);
1052 mColorG = a * ((color >> 8) & 0xFF);
1053 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001054 mColorSet = true;
1055 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1056}
1057
Romain Guy86568192010-12-14 15:55:39 -08001058void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1059 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001060 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1061 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001062 const float a = mColorA / 255.0f;
1063 mColorR = a * ((color >> 16) & 0xFF);
1064 mColorG = a * ((color >> 8) & 0xFF);
1065 mColorB = a * ((color ) & 0xFF);
1066 mColorSet = true;
1067 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1068}
1069
Romain Guy70ca14e2010-12-13 18:24:33 -08001070void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1071 mColorA = a;
1072 mColorR = r;
1073 mColorG = g;
1074 mColorB = b;
1075 mColorSet = true;
1076 mSetShaderColor = mDescription.setColor(r, g, b, a);
1077}
1078
Romain Guy86568192010-12-14 15:55:39 -08001079void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1080 mColorA = a;
1081 mColorR = r;
1082 mColorG = g;
1083 mColorB = b;
1084 mColorSet = true;
1085 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1086}
1087
Romain Guy70ca14e2010-12-13 18:24:33 -08001088void OpenGLRenderer::setupDrawShader() {
1089 if (mShader) {
1090 mShader->describe(mDescription, mCaches.extensions);
1091 }
1092}
1093
1094void OpenGLRenderer::setupDrawColorFilter() {
1095 if (mColorFilter) {
1096 mColorFilter->describe(mDescription, mCaches.extensions);
1097 }
1098}
1099
Romain Guyf09ef512011-05-27 11:43:46 -07001100void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1101 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1102 mColorA = 1.0f;
1103 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001104 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001105 }
1106}
1107
Romain Guy70ca14e2010-12-13 18:24:33 -08001108void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001109 // When the blending mode is kClear_Mode, we need to use a modulate color
1110 // argb=1,0,0,0
1111 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001112 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1113 mDescription, swapSrcDst);
1114}
1115
1116void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001117 // When the blending mode is kClear_Mode, we need to use a modulate color
1118 // argb=1,0,0,0
1119 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001120 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1121 mDescription, swapSrcDst);
1122}
1123
1124void OpenGLRenderer::setupDrawProgram() {
1125 useProgram(mCaches.programCache.get(mDescription));
1126}
1127
1128void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1129 mTrackDirtyRegions = false;
1130}
1131
1132void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1133 bool ignoreTransform) {
1134 mModelView.loadTranslate(left, top, 0.0f);
1135 if (!ignoreTransform) {
1136 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1137 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1138 } else {
1139 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1140 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1141 }
1142}
1143
Chet Haase8a5cc922011-04-26 07:28:09 -07001144void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1145 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001146}
1147
Romain Guy70ca14e2010-12-13 18:24:33 -08001148void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1149 bool ignoreTransform, bool ignoreModelView) {
1150 if (!ignoreModelView) {
1151 mModelView.loadTranslate(left, top, 0.0f);
1152 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001153 } else {
1154 mModelView.loadIdentity();
1155 }
Romain Guy86568192010-12-14 15:55:39 -08001156 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1157 if (!ignoreTransform) {
1158 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1159 if (mTrackDirtyRegions && dirty) {
1160 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1161 }
1162 } else {
1163 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1164 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1165 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001166}
1167
Romain Guyed6fcb02011-03-21 13:11:28 -07001168void OpenGLRenderer::setupDrawPointUniforms() {
1169 int slot = mCaches.currentProgram->getUniform("pointSize");
1170 glUniform1f(slot, mDescription.pointSize);
1171}
1172
Romain Guy70ca14e2010-12-13 18:24:33 -08001173void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001174 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001175 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1176 }
1177}
1178
Romain Guy86568192010-12-14 15:55:39 -08001179void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001180 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001181 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001182 }
1183}
1184
Romain Guy70ca14e2010-12-13 18:24:33 -08001185void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1186 if (mShader) {
1187 if (ignoreTransform) {
1188 mModelView.loadInverse(*mSnapshot->transform);
1189 }
1190 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1191 }
1192}
1193
Romain Guy8d0d4782010-12-14 20:13:35 -08001194void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1195 if (mShader) {
1196 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1197 }
1198}
1199
Romain Guy70ca14e2010-12-13 18:24:33 -08001200void OpenGLRenderer::setupDrawColorFilterUniforms() {
1201 if (mColorFilter) {
1202 mColorFilter->setupProgram(mCaches.currentProgram);
1203 }
1204}
1205
1206void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001207 bool force = mCaches.bindMeshBuffer();
1208 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001209 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001210}
1211
1212void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1213 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001214 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001215 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001216}
1217
Romain Guyaa6c24c2011-04-28 18:40:04 -07001218void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1219 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001220 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001221 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001222}
1223
Romain Guy8f0095c2011-05-02 17:24:22 -07001224void OpenGLRenderer::setupDrawTextureTransform() {
1225 mDescription.hasTextureTransform = true;
1226}
1227
1228void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001229 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1230 GL_FALSE, &transform.data[0]);
1231}
1232
Romain Guy70ca14e2010-12-13 18:24:33 -08001233void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001234 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001235 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001236 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001237 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001238 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001239 }
Romain Guyd71dd362011-12-12 19:03:35 -08001240
Romain Guyf3a910b42011-12-12 20:35:21 -08001241 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001242 if (mCaches.currentProgram->texCoords >= 0) {
1243 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1244 }
1245
1246 mCaches.unbindIndicesBuffer();
1247}
1248
1249void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1250 bool force = mCaches.unbindMeshBuffer();
1251 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1252 if (mCaches.currentProgram->texCoords >= 0) {
1253 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001254 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001255}
1256
Chet Haase5b0200b2011-04-13 17:58:08 -07001257void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001258 bool force = mCaches.unbindMeshBuffer();
1259 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1260 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001261 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001262}
1263
1264/**
1265 * 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 -07001266 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1267 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1268 * attributes (one per vertex) are values from zero to one that tells the fragment
1269 * shader where the fragment is in relation to the line width/length overall; these values are
1270 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1271 * region of the line.
1272 * Note that we only pass down the width values in this setup function. The length coordinates
1273 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001274 */
Chet Haase99585ad2011-05-02 15:00:16 -07001275void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001276 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001277 bool force = mCaches.unbindMeshBuffer();
1278 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1279 vertices, gAAVertexStride);
1280 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001281 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001282
Chet Haase99585ad2011-05-02 15:00:16 -07001283 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1284 glEnableVertexAttribArray(widthSlot);
1285 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001286
Chet Haase99585ad2011-05-02 15:00:16 -07001287 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1288 glEnableVertexAttribArray(lengthSlot);
1289 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001290
Chet Haase5b0200b2011-04-13 17:58:08 -07001291 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001292 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001293
Chet Haase99585ad2011-05-02 15:00:16 -07001294 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001295 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001296 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001297}
1298
Romain Guy70ca14e2010-12-13 18:24:33 -08001299void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001300}
1301
1302///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001303// Drawing
1304///////////////////////////////////////////////////////////////////////////////
1305
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001306bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1307 Rect& dirty, uint32_t level) {
1308 if (quickReject(0.0f, 0.0f, width, height)) {
1309 return false;
1310 }
1311
Romain Guy0fe478e2010-11-08 12:08:41 -08001312 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1313 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001314 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001315 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001316 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001317
Chet Haasedaf98e92011-01-10 14:10:36 -08001318 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001319}
1320
Chet Haaseed30fd82011-04-22 16:18:45 -07001321void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1322 if (displayList) {
1323 displayList->output(*this, level);
1324 }
1325}
1326
Romain Guya168d732011-03-18 16:50:13 -07001327void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1328 int alpha;
1329 SkXfermode::Mode mode;
1330 getAlphaAndMode(paint, &alpha, &mode);
1331
Romain Guya168d732011-03-18 16:50:13 -07001332 float x = left;
1333 float y = top;
1334
Romain Guye3c26852011-07-25 16:36:01 -07001335 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001336 bool ignoreTransform = false;
1337 if (mSnapshot->transform->isPureTranslate()) {
1338 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1339 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1340 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001341 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001342 } else {
1343 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001344 }
1345
1346 setupDraw();
1347 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001348 if (paint) {
1349 setupDrawAlpha8Color(paint->getColor(), alpha);
1350 }
Romain Guya168d732011-03-18 16:50:13 -07001351 setupDrawColorFilter();
1352 setupDrawShader();
1353 setupDrawBlending(true, mode);
1354 setupDrawProgram();
1355 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001356
Romain Guya168d732011-03-18 16:50:13 -07001357 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001358 texture->setWrap(GL_CLAMP_TO_EDGE);
1359 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001360
Romain Guya168d732011-03-18 16:50:13 -07001361 setupDrawPureColorUniforms();
1362 setupDrawColorFilterUniforms();
1363 setupDrawShaderUniforms();
1364 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1365
1366 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1367
1368 finishDrawTexture();
1369}
1370
Chet Haase5c13d892010-10-08 08:37:55 -07001371void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001372 const float right = left + bitmap->width();
1373 const float bottom = top + bitmap->height();
1374
1375 if (quickReject(left, top, right, bottom)) {
1376 return;
1377 }
1378
Romain Guya1d3c912011-12-13 14:55:06 -08001379 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001380 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001381 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001382 const AutoTexture autoCleanup(texture);
1383
Romain Guya168d732011-03-18 16:50:13 -07001384 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1385 drawAlphaBitmap(texture, left, top, paint);
1386 } else {
1387 drawTextureRect(left, top, right, bottom, texture, paint);
1388 }
Romain Guyce0537b2010-06-29 21:05:21 -07001389}
1390
Chet Haase5c13d892010-10-08 08:37:55 -07001391void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001392 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1393 const mat4 transform(*matrix);
1394 transform.mapRect(r);
1395
Romain Guy6926c722010-07-12 20:20:03 -07001396 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1397 return;
1398 }
1399
Romain Guya1d3c912011-12-13 14:55:06 -08001400 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001401 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001402 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001403 const AutoTexture autoCleanup(texture);
1404
Romain Guy5b3b3522010-10-27 18:57:51 -07001405 // This could be done in a cheaper way, all we need is pass the matrix
1406 // to the vertex shader. The save/restore is a bit overkill.
1407 save(SkCanvas::kMatrix_SaveFlag);
1408 concatMatrix(matrix);
1409 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1410 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001411}
1412
Romain Guy5a7b4662011-01-20 19:09:30 -08001413void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1414 float* vertices, int* colors, SkPaint* paint) {
1415 // TODO: Do a quickReject
1416 if (!vertices || mSnapshot->isIgnored()) {
1417 return;
1418 }
1419
Romain Guya1d3c912011-12-13 14:55:06 -08001420 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001421 Texture* texture = mCaches.textureCache.get(bitmap);
1422 if (!texture) return;
1423 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001424
Romain Guyd21b6e12011-11-30 20:21:23 -08001425 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1426 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001427
1428 int alpha;
1429 SkXfermode::Mode mode;
1430 getAlphaAndMode(paint, &alpha, &mode);
1431
Romain Guy5a7b4662011-01-20 19:09:30 -08001432 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001433
Romain Guyb18d2d02011-02-10 15:52:54 -08001434 float left = FLT_MAX;
1435 float top = FLT_MAX;
1436 float right = FLT_MIN;
1437 float bottom = FLT_MIN;
1438
1439#if RENDER_LAYERS_AS_REGIONS
1440 bool hasActiveLayer = hasLayer();
1441#else
1442 bool hasActiveLayer = false;
1443#endif
1444
Romain Guya566b7c2011-01-23 16:36:11 -08001445 // TODO: Support the colors array
1446 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001447 TextureVertex* vertex = mesh;
1448 for (int32_t y = 0; y < meshHeight; y++) {
1449 for (int32_t x = 0; x < meshWidth; x++) {
1450 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1451
1452 float u1 = float(x) / meshWidth;
1453 float u2 = float(x + 1) / meshWidth;
1454 float v1 = float(y) / meshHeight;
1455 float v2 = float(y + 1) / meshHeight;
1456
1457 int ax = i + (meshWidth + 1) * 2;
1458 int ay = ax + 1;
1459 int bx = i;
1460 int by = bx + 1;
1461 int cx = i + 2;
1462 int cy = cx + 1;
1463 int dx = i + (meshWidth + 1) * 2 + 2;
1464 int dy = dx + 1;
1465
1466 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1467 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1468 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1469
1470 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1471 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1472 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001473
1474#if RENDER_LAYERS_AS_REGIONS
1475 if (hasActiveLayer) {
1476 // TODO: This could be optimized to avoid unnecessary ops
1477 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1478 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1479 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1480 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1481 }
1482#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001483 }
1484 }
1485
Romain Guyb18d2d02011-02-10 15:52:54 -08001486#if RENDER_LAYERS_AS_REGIONS
1487 if (hasActiveLayer) {
1488 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1489 }
1490#endif
1491
Romain Guy5a7b4662011-01-20 19:09:30 -08001492 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1493 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001494 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001495}
1496
Romain Guy8ba548f2010-06-30 19:21:21 -07001497void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1498 float srcLeft, float srcTop, float srcRight, float srcBottom,
1499 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001500 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001501 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1502 return;
1503 }
1504
Romain Guya1d3c912011-12-13 14:55:06 -08001505 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001506 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001507 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001508 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001509
Romain Guy8ba548f2010-06-30 19:21:21 -07001510 const float width = texture->width;
1511 const float height = texture->height;
1512
Romain Guy68169722011-08-22 17:33:33 -07001513 const float u1 = fmax(0.0f, srcLeft / width);
1514 const float v1 = fmax(0.0f, srcTop / height);
1515 const float u2 = fmin(1.0f, srcRight / width);
1516 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001517
Romain Guy03750a02010-10-18 14:06:08 -07001518 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001519 resetDrawTextureTexCoords(u1, v1, u2, v2);
1520
Romain Guy03750a02010-10-18 14:06:08 -07001521 int alpha;
1522 SkXfermode::Mode mode;
1523 getAlphaAndMode(paint, &alpha, &mode);
1524
Romain Guyd21b6e12011-11-30 20:21:23 -08001525 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1526
Romain Guy6620c6d2010-12-06 18:07:02 -08001527 if (mSnapshot->transform->isPureTranslate()) {
1528 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1529 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1530
Romain Guyb5014982011-07-28 15:39:12 -07001531 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001532 // Enable linear filtering if the source rectangle is scaled
1533 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001534 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001535 }
Romain Guyb5014982011-07-28 15:39:12 -07001536
Romain Guyd21b6e12011-11-30 20:21:23 -08001537 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001538 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1539 texture->id, alpha / 255.0f, mode, texture->blend,
1540 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1541 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1542 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001543 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001544 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1545 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1546 GL_TRIANGLE_STRIP, gMeshCount);
1547 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001548
1549 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1550}
1551
Romain Guy4aa90572010-09-26 18:40:37 -07001552void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001553 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001554 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001555 if (quickReject(left, top, right, bottom)) {
1556 return;
1557 }
1558
Romain Guya1d3c912011-12-13 14:55:06 -08001559 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001560 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001561 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001562 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001563 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1564 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001565
1566 int alpha;
1567 SkXfermode::Mode mode;
1568 getAlphaAndMode(paint, &alpha, &mode);
1569
Romain Guy2728f962010-10-08 18:36:15 -07001570 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001571 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001572
Romain Guya5ef39a2010-12-03 16:48:20 -08001573 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001574 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001575#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001576 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001577 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001578 const float offsetX = left + mSnapshot->transform->getTranslateX();
1579 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001580 const size_t count = mesh->quads.size();
1581 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001582 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001583 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001584 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1585 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1586 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001587 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001588 dirtyLayer(left + bounds.left, top + bounds.top,
1589 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001590 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001591 }
1592 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001593#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001594
Romain Guy6620c6d2010-12-06 18:07:02 -08001595 if (pureTranslate) {
1596 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1597 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1598
1599 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1600 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1601 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1602 true, !mesh->hasEmptyQuads);
1603 } else {
1604 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1605 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1606 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1607 true, !mesh->hasEmptyQuads);
1608 }
Romain Guy054dc182010-10-15 17:55:25 -07001609 }
Romain Guyf7f93552010-07-08 19:17:03 -07001610}
1611
Chet Haase99ecdc42011-05-06 12:06:34 -07001612/**
Chet Haase858aa932011-05-12 09:06:00 -07001613 * This function uses a similar approach to that of AA lines in the drawLines() function.
1614 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1615 * shader to compute the translucency of the color, determined by whether a given pixel is
1616 * within that boundary region and how far into the region it is.
1617 */
1618void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001619 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001620 float inverseScaleX = 1.0f;
1621 float inverseScaleY = 1.0f;
1622 // The quad that we use needs to account for scaling.
1623 if (!mSnapshot->transform->isPureTranslate()) {
1624 Matrix4 *mat = mSnapshot->transform;
1625 float m00 = mat->data[Matrix4::kScaleX];
1626 float m01 = mat->data[Matrix4::kSkewY];
1627 float m02 = mat->data[2];
1628 float m10 = mat->data[Matrix4::kSkewX];
1629 float m11 = mat->data[Matrix4::kScaleX];
1630 float m12 = mat->data[6];
1631 float scaleX = sqrt(m00 * m00 + m01 * m01);
1632 float scaleY = sqrt(m10 * m10 + m11 * m11);
1633 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1634 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1635 }
1636
1637 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001638 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001639 setupDrawAALine();
1640 setupDrawColor(color);
1641 setupDrawColorFilter();
1642 setupDrawShader();
1643 setupDrawBlending(true, mode);
1644 setupDrawProgram();
1645 setupDrawModelViewIdentity(true);
1646 setupDrawColorUniforms();
1647 setupDrawColorFilterUniforms();
1648 setupDrawShaderIdentityUniforms();
1649
1650 AAVertex rects[4];
1651 AAVertex* aaVertices = &rects[0];
1652 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1653 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1654
1655 float boundarySizeX = .5 * inverseScaleX;
1656 float boundarySizeY = .5 * inverseScaleY;
1657
1658 // Adjust the rect by the AA boundary padding
1659 left -= boundarySizeX;
1660 right += boundarySizeX;
1661 top -= boundarySizeY;
1662 bottom += boundarySizeY;
1663
1664 float width = right - left;
1665 float height = bottom - top;
1666
1667 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1668 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1669 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1670 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1671 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1672 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1673 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1674
1675 if (!quickReject(left, top, right, bottom)) {
1676 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1677 AAVertex::set(aaVertices++, left, top, 1, 0);
1678 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1679 AAVertex::set(aaVertices++, right, top, 0, 0);
1680 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1681 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1682 }
1683}
1684
1685/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001686 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1687 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1688 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1689 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1690 * of the line. Hairlines are more involved because we need to account for transform scaling
1691 * to end up with a one-pixel-wide line in screen space..
1692 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1693 * in combination with values that we calculate and pass down in this method. The basic approach
1694 * is that the quad we create contains both the core line area plus a bounding area in which
1695 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1696 * proportion of the width and the length of a given segment is represented by the boundary
1697 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1698 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1699 * on the inside). This ends up giving the result we want, with pixels that are completely
1700 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1701 * how far into the boundary region they are, which is determined by shader interpolation.
1702 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001703void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1704 if (mSnapshot->isIgnored()) return;
1705
1706 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001707 // We use half the stroke width here because we're going to position the quad
1708 // corner vertices half of the width away from the line endpoints
1709 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001710 // A stroke width of 0 has a special meaning in Skia:
1711 // it draws a line 1 px wide regardless of current transform
1712 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001713 float inverseScaleX = 1.0f;
1714 float inverseScaleY = 1.0f;
1715 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001716 int alpha;
1717 SkXfermode::Mode mode;
1718 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001719 int verticesCount = count;
1720 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001721 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001722 verticesCount += (count - 4);
1723 }
1724
1725 if (isHairLine || isAA) {
1726 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1727 // the line on the screen should always be one pixel wide regardless of scale. For
1728 // AA lines, we only want one pixel of translucent boundary around the quad.
1729 if (!mSnapshot->transform->isPureTranslate()) {
1730 Matrix4 *mat = mSnapshot->transform;
1731 float m00 = mat->data[Matrix4::kScaleX];
1732 float m01 = mat->data[Matrix4::kSkewY];
1733 float m02 = mat->data[2];
1734 float m10 = mat->data[Matrix4::kSkewX];
1735 float m11 = mat->data[Matrix4::kScaleX];
1736 float m12 = mat->data[6];
1737 float scaleX = sqrt(m00*m00 + m01*m01);
1738 float scaleY = sqrt(m10*m10 + m11*m11);
1739 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1740 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1741 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1742 scaled = true;
1743 }
1744 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001745 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001746
1747 getAlphaAndMode(paint, &alpha, &mode);
1748 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001749 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001750 if (isAA) {
1751 setupDrawAALine();
1752 }
1753 setupDrawColor(paint->getColor(), alpha);
1754 setupDrawColorFilter();
1755 setupDrawShader();
1756 if (isAA) {
1757 setupDrawBlending(true, mode);
1758 } else {
1759 setupDrawBlending(mode);
1760 }
1761 setupDrawProgram();
1762 setupDrawModelViewIdentity(true);
1763 setupDrawColorUniforms();
1764 setupDrawColorFilterUniforms();
1765 setupDrawShaderIdentityUniforms();
1766
1767 if (isHairLine) {
1768 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001769 halfStrokeWidth = isAA? 1 : .5;
1770 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001771 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001772 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001773 }
1774 Vertex lines[verticesCount];
1775 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001776 AAVertex wLines[verticesCount];
1777 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001778 if (!isAA) {
1779 setupDrawVertices(vertices);
1780 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001781 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1782 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001783 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001784 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1785 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001786 // We will need to calculate the actual width proportion on each segment for
1787 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1788 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1789 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001790 }
1791
Chet Haase99ecdc42011-05-06 12:06:34 -07001792 AAVertex* prevAAVertex = NULL;
1793 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001794
Chet Haase99585ad2011-05-02 15:00:16 -07001795 int boundaryLengthSlot = -1;
1796 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 int boundaryWidthSlot = -1;
1798 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001799 for (int i = 0; i < count; i += 4) {
1800 // a = start point, b = end point
1801 vec2 a(points[i], points[i + 1]);
1802 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001803 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001804 float boundaryLengthProportion = 0;
1805 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001806
Chet Haase5b0200b2011-04-13 17:58:08 -07001807 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001808 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001809 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001810 if (isAA) {
1811 float wideningFactor;
1812 if (fabs(n.x) >= fabs(n.y)) {
1813 wideningFactor = fabs(1.0f / n.x);
1814 } else {
1815 wideningFactor = fabs(1.0f / n.y);
1816 }
1817 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001818 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001819 if (scaled) {
1820 n.x *= inverseScaleX;
1821 n.y *= inverseScaleY;
1822 }
1823 } else if (scaled) {
1824 // Extend n by .5 pixel on each side, post-transform
1825 vec2 extendedN = n.copyNormalized();
1826 extendedN /= 2;
1827 extendedN.x *= inverseScaleX;
1828 extendedN.y *= inverseScaleY;
1829 float extendedNLength = extendedN.length();
1830 // We need to set this value on the shader prior to drawing
1831 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1832 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001833 }
1834 float x = n.x;
1835 n.x = -n.y;
1836 n.y = x;
1837
Chet Haase99585ad2011-05-02 15:00:16 -07001838 // aa lines expand the endpoint vertices to encompass the AA boundary
1839 if (isAA) {
1840 vec2 abVector = (b - a);
1841 length = abVector.length();
1842 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001843 if (scaled) {
1844 abVector.x *= inverseScaleX;
1845 abVector.y *= inverseScaleY;
1846 float abLength = abVector.length();
1847 boundaryLengthProportion = abLength / (length + abLength);
1848 } else {
1849 boundaryLengthProportion = .5 / (length + 1);
1850 }
1851 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001852 a -= abVector;
1853 b += abVector;
1854 }
1855
Chet Haase5b0200b2011-04-13 17:58:08 -07001856 // Four corners of the rectangle defining a thick line
1857 vec2 p1 = a - n;
1858 vec2 p2 = a + n;
1859 vec2 p3 = b + n;
1860 vec2 p4 = b - n;
1861
Chet Haase99585ad2011-05-02 15:00:16 -07001862
Chet Haase5b0200b2011-04-13 17:58:08 -07001863 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1864 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1865 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1866 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1867
1868 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001869 if (!isAA) {
1870 if (prevVertex != NULL) {
1871 // Issue two repeat vertices to create degenerate triangles to bridge
1872 // between the previous line and the new one. This is necessary because
1873 // we are creating a single triangle_strip which will contain
1874 // potentially discontinuous line segments.
1875 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1876 Vertex::set(vertices++, p1.x, p1.y);
1877 generatedVerticesCount += 2;
1878 }
1879 Vertex::set(vertices++, p1.x, p1.y);
1880 Vertex::set(vertices++, p2.x, p2.y);
1881 Vertex::set(vertices++, p4.x, p4.y);
1882 Vertex::set(vertices++, p3.x, p3.y);
1883 prevVertex = vertices - 1;
1884 generatedVerticesCount += 4;
1885 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001886 if (!isHairLine && scaled) {
1887 // Must set width proportions per-segment for scaled non-hairlines to use the
1888 // correct AA boundary dimensions
1889 if (boundaryWidthSlot < 0) {
1890 boundaryWidthSlot =
1891 mCaches.currentProgram->getUniform("boundaryWidth");
1892 inverseBoundaryWidthSlot =
1893 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1894 }
1895 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1896 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1897 }
Chet Haase99585ad2011-05-02 15:00:16 -07001898 if (boundaryLengthSlot < 0) {
1899 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1900 inverseBoundaryLengthSlot =
1901 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1902 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001903 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1904 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001905
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 if (prevAAVertex != NULL) {
1907 // Issue two repeat vertices to create degenerate triangles to bridge
1908 // between the previous line and the new one. This is necessary because
1909 // we are creating a single triangle_strip which will contain
1910 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001911 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1912 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1913 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001914 generatedVerticesCount += 2;
1915 }
Chet Haase99585ad2011-05-02 15:00:16 -07001916 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1917 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1918 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1919 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001920 prevAAVertex = aaVertices - 1;
1921 generatedVerticesCount += 4;
1922 }
Chet Haase99585ad2011-05-02 15:00:16 -07001923 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1924 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1925 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001926 }
1927 }
1928 if (generatedVerticesCount > 0) {
1929 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1930 }
1931}
1932
Romain Guyed6fcb02011-03-21 13:11:28 -07001933void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1934 if (mSnapshot->isIgnored()) return;
1935
1936 // TODO: The paint's cap style defines whether the points are square or circular
1937 // TODO: Handle AA for round points
1938
Chet Haase5b0200b2011-04-13 17:58:08 -07001939 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001940 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001941 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001942 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001943 if (isHairLine) {
1944 // Now that we know it's hairline, we can set the effective width, to be used later
1945 strokeWidth = 1.0f;
1946 }
1947 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001948 int alpha;
1949 SkXfermode::Mode mode;
1950 getAlphaAndMode(paint, &alpha, &mode);
1951
1952 int verticesCount = count >> 1;
1953 int generatedVerticesCount = 0;
1954
1955 TextureVertex pointsData[verticesCount];
1956 TextureVertex* vertex = &pointsData[0];
1957
1958 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001959 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001960 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001961 setupDrawColor(paint->getColor(), alpha);
1962 setupDrawColorFilter();
1963 setupDrawShader();
1964 setupDrawBlending(mode);
1965 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001966 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001967 setupDrawColorUniforms();
1968 setupDrawColorFilterUniforms();
1969 setupDrawPointUniforms();
1970 setupDrawShaderIdentityUniforms();
1971 setupDrawMesh(vertex);
1972
1973 for (int i = 0; i < count; i += 2) {
1974 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1975 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001976 float left = points[i] - halfWidth;
1977 float right = points[i] + halfWidth;
1978 float top = points[i + 1] - halfWidth;
1979 float bottom = points [i + 1] + halfWidth;
1980 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001981 }
1982
1983 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1984}
1985
Romain Guy85bf02f2010-06-22 13:11:24 -07001986void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001987 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001988 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001989
Romain Guyae88e5e2010-10-22 17:49:18 -07001990 Rect& clip(*mSnapshot->clipRect);
1991 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001992
Romain Guy3d58c032010-07-14 16:34:53 -07001993 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001994}
Romain Guy9d5316e2010-06-24 19:30:36 -07001995
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001996void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001997 if (!texture) return;
1998 const AutoTexture autoCleanup(texture);
1999
2000 const float x = left + texture->left - texture->offset;
2001 const float y = top + texture->top - texture->offset;
2002
2003 drawPathTexture(texture, x, y, paint);
2004}
2005
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002006void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2007 float rx, float ry, SkPaint* paint) {
2008 if (mSnapshot->isIgnored()) return;
2009
Romain Guya1d3c912011-12-13 14:55:06 -08002010 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002011 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2012 right - left, bottom - top, rx, ry, paint);
2013 drawShape(left, top, texture, paint);
2014}
2015
Romain Guy01d58e42011-01-19 21:54:02 -08002016void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2017 if (mSnapshot->isIgnored()) return;
2018
Romain Guya1d3c912011-12-13 14:55:06 -08002019 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002020 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002021 drawShape(x - radius, y - radius, texture, paint);
2022}
Romain Guy01d58e42011-01-19 21:54:02 -08002023
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002024void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2025 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002026
Romain Guya1d3c912011-12-13 14:55:06 -08002027 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002028 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2029 drawShape(left, top, texture, paint);
2030}
2031
Romain Guy8b2f5262011-01-23 16:15:02 -08002032void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2033 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2034 if (mSnapshot->isIgnored()) return;
2035
2036 if (fabs(sweepAngle) >= 360.0f) {
2037 drawOval(left, top, right, bottom, paint);
2038 return;
2039 }
2040
Romain Guya1d3c912011-12-13 14:55:06 -08002041 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002042 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2043 startAngle, sweepAngle, useCenter, paint);
2044 drawShape(left, top, texture, paint);
2045}
2046
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002047void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2048 SkPaint* paint) {
2049 if (mSnapshot->isIgnored()) return;
2050
Romain Guya1d3c912011-12-13 14:55:06 -08002051 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002052 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2053 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002054}
2055
Chet Haase5c13d892010-10-08 08:37:55 -07002056void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002057 if (p->getStyle() != SkPaint::kFill_Style) {
2058 drawRectAsShape(left, top, right, bottom, p);
2059 return;
2060 }
2061
Romain Guy6926c722010-07-12 20:20:03 -07002062 if (quickReject(left, top, right, bottom)) {
2063 return;
2064 }
2065
Romain Guy026c5e162010-06-28 17:12:22 -07002066 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002067 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002068 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2069 if (!isMode) {
2070 // Assume SRC_OVER
2071 mode = SkXfermode::kSrcOver_Mode;
2072 }
2073 } else {
2074 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002075 }
2076
Romain Guy026c5e162010-06-28 17:12:22 -07002077 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002078 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002079 drawAARect(left, top, right, bottom, color, mode);
2080 } else {
2081 drawColorRect(left, top, right, bottom, color, mode);
2082 }
Romain Guyc7d53492010-06-25 13:41:57 -07002083}
Romain Guy9d5316e2010-06-24 19:30:36 -07002084
Romain Guye8e62a42010-07-23 18:55:21 -07002085void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002086 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002087 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002088 return;
2089 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002090 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002091
Romain Guy8f9a9f62011-12-05 11:53:26 -08002092 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002093
Romain Guye8e62a42010-07-23 18:55:21 -07002094 switch (paint->getTextAlign()) {
2095 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002096 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002097 x -= length / 2.0f;
2098 break;
2099 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002100 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002101 x -= length;
2102 break;
2103 default:
2104 break;
2105 }
2106
Romain Guycac5fd32011-12-01 20:08:50 -08002107 SkPaint::FontMetrics metrics;
2108 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002109 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002110 if (quickReject(x, y + metrics.fTop,
2111 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2112 return;
2113 }
2114
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002115 const float oldX = x;
2116 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002117 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2118 if (pureTranslate) {
2119 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2120 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2121 }
2122
Romain Guyb45c0c92010-08-26 20:35:23 -07002123 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002124#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002125 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002126#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002127 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002128 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002129
Romain Guy86568192010-12-14 15:55:39 -08002130 int alpha;
2131 SkXfermode::Mode mode;
2132 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002133
Romain Guy1e45aae2010-08-13 19:39:53 -07002134 if (mHasShadow) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002135 mCaches.activeTexture(0);
2136
Romain Guyb45c0c92010-08-26 20:35:23 -07002137 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002138 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2139 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002140 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002141
Romain Guy740bf2b2011-04-26 15:33:10 -07002142 const float sx = oldX - shadow->left + mShadowDx;
2143 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002144
Romain Guy86568192010-12-14 15:55:39 -08002145 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002146 int shadowColor = mShadowColor;
2147 if (mShader) {
2148 shadowColor = 0xffffffff;
2149 }
Romain Guy86568192010-12-14 15:55:39 -08002150
Romain Guy86568192010-12-14 15:55:39 -08002151 setupDraw();
2152 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002153 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2154 setupDrawColorFilter();
2155 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002156 setupDrawBlending(true, mode);
2157 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002158 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002159 setupDrawTexture(shadow->id);
2160 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002161 setupDrawColorFilterUniforms();
2162 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002163 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2164
Romain Guy0a417492010-08-16 20:26:20 -07002165 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002166 }
2167
Romain Guyb146b122010-12-15 17:06:45 -08002168 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2169 return;
2170 }
2171
Romain Guy6620c6d2010-12-06 18:07:02 -08002172 // Pick the appropriate texture filtering
2173 bool linearFilter = mSnapshot->transform->changesBounds();
2174 if (pureTranslate && !linearFilter) {
2175 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2176 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002177
Romain Guya1d3c912011-12-13 14:55:06 -08002178 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002179 setupDraw();
2180 setupDrawDirtyRegionsDisabled();
2181 setupDrawWithTexture(true);
2182 setupDrawAlpha8Color(paint->getColor(), alpha);
2183 setupDrawColorFilter();
2184 setupDrawShader();
2185 setupDrawBlending(true, mode);
2186 setupDrawProgram();
2187 setupDrawModelView(x, y, x, y, pureTranslate, true);
2188 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2189 setupDrawPureColorUniforms();
2190 setupDrawColorFilterUniforms();
2191 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002192
Romain Guy6620c6d2010-12-06 18:07:02 -08002193 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002194 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2195
2196#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002197 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002198#else
Romain Guyf219da52011-01-16 12:54:25 -08002199 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002200#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002201
Romain Guy6620c6d2010-12-06 18:07:02 -08002202 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002203 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002204#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002205 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002206 if (!pureTranslate) {
2207 mSnapshot->transform->mapRect(bounds);
2208 }
Romain Guyf219da52011-01-16 12:54:25 -08002209 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002210 }
2211#endif
2212 }
Romain Guy694b5192010-07-21 21:33:20 -07002213
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002214 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002215}
2216
Romain Guy7fbcc042010-08-04 15:40:07 -07002217void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002218 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002219
Romain Guya1d3c912011-12-13 14:55:06 -08002220 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002221
Romain Guyfb8b7632010-08-23 21:05:08 -07002222 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002223 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002224 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002225
Romain Guy8b55f372010-08-18 17:10:07 -07002226 const float x = texture->left - texture->offset;
2227 const float y = texture->top - texture->offset;
2228
Romain Guy01d58e42011-01-19 21:54:02 -08002229 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002230}
2231
Romain Guyada830f2011-01-13 12:13:20 -08002232void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2233 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002234 return;
2235 }
2236
Romain Guya1d3c912011-12-13 14:55:06 -08002237 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002238
2239 int alpha;
2240 SkXfermode::Mode mode;
2241 getAlphaAndMode(paint, &alpha, &mode);
2242
Romain Guy9ace8f52011-07-07 20:50:11 -07002243 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002244
Romain Guyf219da52011-01-16 12:54:25 -08002245#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002246 if (!layer->region.isEmpty()) {
2247 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002248 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002249 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002250 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002251 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002252
Romain Guyc88e3572011-01-22 00:32:12 -08002253 setupDraw();
2254 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002255 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002256 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002257 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002258 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002259 setupDrawPureColorUniforms();
2260 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002261 setupDrawTexture(layer->getTexture());
2262 if (mSnapshot->transform->isPureTranslate()) {
2263 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2264 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2265
Romain Guyd21b6e12011-11-30 20:21:23 -08002266 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002267 setupDrawModelViewTranslate(x, y,
2268 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2269 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002270 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002271 setupDrawModelViewTranslate(x, y,
2272 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2273 }
Romain Guyc88e3572011-01-22 00:32:12 -08002274 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002275
Romain Guyc88e3572011-01-22 00:32:12 -08002276 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2277 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002278
Romain Guyc88e3572011-01-22 00:32:12 -08002279 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002280
2281#if DEBUG_LAYERS_AS_REGIONS
2282 drawRegionRects(layer->region);
2283#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002284 }
Romain Guyf219da52011-01-16 12:54:25 -08002285 }
2286#else
Romain Guyada830f2011-01-13 12:13:20 -08002287 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2288 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002289#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002290}
2291
Romain Guy6926c722010-07-12 20:20:03 -07002292///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002293// Shaders
2294///////////////////////////////////////////////////////////////////////////////
2295
2296void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002297 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002298}
2299
Romain Guy06f96e22010-07-30 19:18:16 -07002300void OpenGLRenderer::setupShader(SkiaShader* shader) {
2301 mShader = shader;
2302 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002303 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002304 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002305}
2306
Romain Guyd27977d2010-07-14 19:18:51 -07002307///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002308// Color filters
2309///////////////////////////////////////////////////////////////////////////////
2310
2311void OpenGLRenderer::resetColorFilter() {
2312 mColorFilter = NULL;
2313}
2314
2315void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2316 mColorFilter = filter;
2317}
2318
2319///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002320// Drop shadow
2321///////////////////////////////////////////////////////////////////////////////
2322
2323void OpenGLRenderer::resetShadow() {
2324 mHasShadow = false;
2325}
2326
2327void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2328 mHasShadow = true;
2329 mShadowRadius = radius;
2330 mShadowDx = dx;
2331 mShadowDy = dy;
2332 mShadowColor = color;
2333}
2334
2335///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002336// Drawing implementation
2337///////////////////////////////////////////////////////////////////////////////
2338
Romain Guy01d58e42011-01-19 21:54:02 -08002339void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2340 float x, float y, SkPaint* paint) {
2341 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2342 return;
2343 }
2344
2345 int alpha;
2346 SkXfermode::Mode mode;
2347 getAlphaAndMode(paint, &alpha, &mode);
2348
2349 setupDraw();
2350 setupDrawWithTexture(true);
2351 setupDrawAlpha8Color(paint->getColor(), alpha);
2352 setupDrawColorFilter();
2353 setupDrawShader();
2354 setupDrawBlending(true, mode);
2355 setupDrawProgram();
2356 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2357 setupDrawTexture(texture->id);
2358 setupDrawPureColorUniforms();
2359 setupDrawColorFilterUniforms();
2360 setupDrawShaderUniforms();
2361 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2362
2363 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2364
2365 finishDrawTexture();
2366}
2367
Romain Guyf607bdc2010-09-10 19:20:06 -07002368// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002369#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2370#define kStdUnderline_Offset (1.0f / 9.0f)
2371#define kStdUnderline_Thickness (1.0f / 18.0f)
2372
2373void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2374 float x, float y, SkPaint* paint) {
2375 // Handle underline and strike-through
2376 uint32_t flags = paint->getFlags();
2377 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002378 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002379 float underlineWidth = length;
2380 // If length is > 0.0f, we already measured the text for the text alignment
2381 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002382 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002383 }
2384
2385 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002386 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002387 case SkPaint::kCenter_Align:
2388 offsetX = underlineWidth * 0.5f;
2389 break;
2390 case SkPaint::kRight_Align:
2391 offsetX = underlineWidth;
2392 break;
2393 default:
2394 break;
2395 }
2396
2397 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002398 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002399 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002400
Romain Guye20ecbd2010-09-22 19:49:04 -07002401 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002402 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002403
Romain Guyf6834472011-01-23 13:32:12 -08002404 int linesCount = 0;
2405 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2406 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2407
2408 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002409 float points[pointsCount];
2410 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002411
2412 if (flags & SkPaint::kUnderlineText_Flag) {
2413 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002414 points[currentPoint++] = left;
2415 points[currentPoint++] = top;
2416 points[currentPoint++] = left + underlineWidth;
2417 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002418 }
2419
2420 if (flags & SkPaint::kStrikeThruText_Flag) {
2421 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002422 points[currentPoint++] = left;
2423 points[currentPoint++] = top;
2424 points[currentPoint++] = left + underlineWidth;
2425 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002426 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002427
Romain Guy726aeba2011-06-01 14:52:00 -07002428 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002429
Romain Guy726aeba2011-06-01 14:52:00 -07002430 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002431 }
2432 }
2433}
2434
Romain Guy026c5e162010-06-28 17:12:22 -07002435void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002436 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002437 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002438 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002439 color |= 0x00ffffff;
2440 }
2441
Romain Guy70ca14e2010-12-13 18:24:33 -08002442 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002443 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002444 setupDrawColor(color);
2445 setupDrawShader();
2446 setupDrawColorFilter();
2447 setupDrawBlending(mode);
2448 setupDrawProgram();
2449 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2450 setupDrawColorUniforms();
2451 setupDrawShaderUniforms(ignoreTransform);
2452 setupDrawColorFilterUniforms();
2453 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002454
Romain Guyc95c8d62010-09-17 15:31:32 -07002455 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2456}
2457
Romain Guy82ba8142010-07-09 13:25:56 -07002458void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002459 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002460 int alpha;
2461 SkXfermode::Mode mode;
2462 getAlphaAndMode(paint, &alpha, &mode);
2463
Romain Guyd21b6e12011-11-30 20:21:23 -08002464 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002465
Romain Guy6620c6d2010-12-06 18:07:02 -08002466 if (mSnapshot->transform->isPureTranslate()) {
2467 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2468 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2469
Romain Guyd21b6e12011-11-30 20:21:23 -08002470 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002471 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2472 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2473 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2474 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002475 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002476 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2477 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2478 GL_TRIANGLE_STRIP, gMeshCount);
2479 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002480}
2481
Romain Guybd6b79b2010-06-26 00:13:53 -07002482void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002483 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2484 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002485 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002486}
2487
2488void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002489 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002490 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002491 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002492
Romain Guy746b7402010-10-26 16:27:31 -07002493 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002494 setupDrawWithTexture();
2495 setupDrawColor(alpha, alpha, alpha, alpha);
2496 setupDrawColorFilter();
2497 setupDrawBlending(blend, mode, swapSrcDst);
2498 setupDrawProgram();
2499 if (!dirty) {
2500 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002501 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002502 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002503 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002504 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002505 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002506 }
Romain Guy86568192010-12-14 15:55:39 -08002507 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002508 setupDrawColorFilterUniforms();
2509 setupDrawTexture(texture);
2510 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002511
Romain Guy6820ac82010-09-15 18:11:50 -07002512 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002513
2514 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002515}
2516
Romain Guya5aed0d2010-09-09 14:42:43 -07002517void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002518 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002519 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2520 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002521 // These blend modes are not supported by OpenGL directly and have
2522 // to be implemented using shaders. Since the shader will perform
2523 // the blending, turn blending off here
2524 // If the blend mode cannot be implemented using shaders, fall
2525 // back to the default SrcOver blend mode instead
2526 if (mode > SkXfermode::kScreen_Mode) {
Romain Guy746b7402010-10-26 16:27:31 -07002527 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002528 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002529 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002530
Romain Guy82bc7a72012-01-03 14:13:39 -08002531 if (mCaches.blend) {
2532 glDisable(GL_BLEND);
2533 mCaches.blend = false;
2534 }
2535
2536 return;
2537 } else {
2538 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002539 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002540 }
2541
2542 if (!mCaches.blend) {
2543 glEnable(GL_BLEND);
2544 }
2545
2546 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2547 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2548
2549 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2550 glBlendFunc(sourceMode, destMode);
2551 mCaches.lastSrcMode = sourceMode;
2552 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002553 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002554 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002555 glDisable(GL_BLEND);
2556 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002557 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002558}
2559
Romain Guy889f8d12010-07-29 14:37:42 -07002560bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002561 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002562 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002563 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002564 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002565 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002566 }
Romain Guy6926c722010-07-12 20:20:03 -07002567 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002568}
2569
Romain Guy026c5e162010-06-28 17:12:22 -07002570void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002571 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002572 TextureVertex::setUV(v++, u1, v1);
2573 TextureVertex::setUV(v++, u2, v1);
2574 TextureVertex::setUV(v++, u1, v2);
2575 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002576}
2577
Chet Haase5c13d892010-10-08 08:37:55 -07002578void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002579 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002580 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002581
2582 // Skia draws using the color's alpha channel if < 255
2583 // Otherwise, it uses the paint's alpha
2584 int color = paint->getColor();
2585 *alpha = (color >> 24) & 0xFF;
2586 if (*alpha == 255) {
2587 *alpha = paint->getAlpha();
2588 }
2589 } else {
2590 *mode = SkXfermode::kSrcOver_Mode;
2591 *alpha = 255;
2592 }
Romain Guy026c5e162010-06-28 17:12:22 -07002593}
2594
Romain Guya5aed0d2010-09-09 14:42:43 -07002595SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002596 SkXfermode::Mode resultMode;
2597 if (!SkXfermode::AsMode(mode, &resultMode)) {
2598 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002599 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002600 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002601}
2602
Romain Guy9d5316e2010-06-24 19:30:36 -07002603}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002604}; // namespace android