blob: 45569ace79d511baea63ac8b197bc473ed9bec2a [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 Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
83 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
84 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
104 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guyfb8b7632010-08-23 21:05:08 -0700112OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700113 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700114 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700115 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800116 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700121
122 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700126 // The context has already been destroyed at this point, do not call
127 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guy87e2f7572012-09-24 11:37:12 -0700130void OpenGLRenderer::initProperties() {
131 char property[PROPERTY_VALUE_MAX];
132 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
133 mScissorOptimizationDisabled = !strcasecmp(property, "true");
134 INIT_LOGD(" Scissor optimization %s",
135 mScissorOptimizationDisabled ? "disabled" : "enabled");
136 } else {
137 INIT_LOGD(" Scissor optimization enabled");
138 }
Romain Guy13631f32012-01-30 17:41:55 -0800139}
140
141///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guyef359272013-01-31 19:07:29 -0800145void OpenGLRenderer::setName(const char* name) {
146 if (name) {
147 mName.setTo(name);
148 } else {
149 mName.clear();
150 }
151}
152
153const char* OpenGLRenderer::getName() const {
154 return mName.string();
155}
156
Romain Guy49c5fc02012-05-15 11:10:01 -0700157bool OpenGLRenderer::isDeferred() {
158 return false;
159}
160
Romain Guy85bf02f2010-06-22 13:11:24 -0700161void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700162 initViewport(width, height);
163
164 glDisable(GL_DITHER);
165 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
166
167 glEnableVertexAttribArray(Program::kBindingPosition);
168}
169
170void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700171 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700172
173 mWidth = width;
174 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700175
176 mFirstSnapshot->height = height;
177 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700178}
179
Romain Guy7c25aab2012-10-18 15:05:02 -0700180status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700181 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800182}
183
Romain Guyc3fedaf2013-01-29 17:26:25 -0800184status_t OpenGLRenderer::prepareDirty(float left, float top,
185 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800186 mCaches.clearGarbage();
187
Romain Guy8aef54f2010-09-01 15:13:49 -0700188 mSnapshot = new Snapshot(mFirstSnapshot,
189 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800190 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700191 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700192
Romain Guy7d7b5492011-01-24 16:33:45 -0800193 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700194 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700195
Romain Guy11cb6422012-09-21 00:39:43 -0700196 updateLayers();
197
Romain Guydcfc8362013-01-03 13:08:57 -0800198 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700199
Romain Guyddf74372012-05-22 14:07:07 -0700200 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800201
Romain Guy54c1a642012-09-27 17:55:46 -0700202 // Functors break the tiling extension in pretty spectacular ways
203 // This ensures we don't use tiling when a functor is going to be
204 // invoked during the frame
205 mSuppressTiling = mCaches.hasRegisteredFunctors();
206
Romain Guy2b7028e2012-09-19 17:25:38 -0700207 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700208 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700209
Romain Guy7c450aa2012-09-21 19:15:00 -0700210 debugOverdraw(true, true);
211
Romain Guy7c25aab2012-10-18 15:05:02 -0700212 return clear(left, top, right, bottom, opaque);
213}
214
Romain Guydcfc8362013-01-03 13:08:57 -0800215void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
216 // If we know that we are going to redraw the entire framebuffer,
217 // perform a discard to let the driver know we don't need to preserve
218 // the back buffer for this frame.
219 if (mCaches.extensions.hasDiscardFramebuffer() &&
220 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800221 const bool isFbo = getTargetFbo() == 0;
222 const GLenum attachments[] = {
223 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
224 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800225 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
226 }
227}
228
Romain Guy7c25aab2012-10-18 15:05:02 -0700229status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700230 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700231 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700232 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700233 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700234 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700235 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700236
Romain Guy7c25aab2012-10-18 15:05:02 -0700237 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700238 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700239}
240
241void OpenGLRenderer::syncState() {
242 glViewport(0, 0, mWidth, mHeight);
243
244 if (mCaches.blend) {
245 glEnable(GL_BLEND);
246 } else {
247 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700248 }
Romain Guybb9524b2010-06-22 18:56:38 -0700249}
250
Romain Guy57b52682012-09-20 17:38:46 -0700251void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700252 if (!mSuppressTiling) {
253 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800254 if (s->flags & Snapshot::kFlagFboTarget) {
255 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700256 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700257
Romain Guyc3fedaf2013-01-29 17:26:25 -0800258 startTiling(*clip, s->height, opaque);
259 }
260}
261
262void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
263 if (!mSuppressTiling) {
264 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
265 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700266 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700267}
268
269void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700270 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700271}
272
Romain Guyb025b9c2010-09-16 14:16:48 -0700273void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700274 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700275 endTiling();
276
Romain Guy11cb6422012-09-21 00:39:43 -0700277 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700278#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700279 GLenum status = GL_NO_ERROR;
280 while ((status = glGetError()) != GL_NO_ERROR) {
281 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
282 switch (status) {
283 case GL_INVALID_ENUM:
284 ALOGE(" GL_INVALID_ENUM");
285 break;
286 case GL_INVALID_VALUE:
287 ALOGE(" GL_INVALID_VALUE");
288 break;
289 case GL_INVALID_OPERATION:
290 ALOGE(" GL_INVALID_OPERATION");
291 break;
292 case GL_OUT_OF_MEMORY:
293 ALOGE(" Out of memory!");
294 break;
295 }
Romain Guya07105b2011-01-10 21:14:18 -0800296 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700297#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700298
Romain Guyc15008e2010-11-10 11:59:15 -0800299#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800300 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700301#else
302 if (mCaches.getDebugLevel() & kDebugMemory) {
303 mCaches.dumpMemoryUsage();
304 }
Romain Guyc15008e2010-11-10 11:59:15 -0800305#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700306 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700307}
308
Romain Guy6c319ca2011-01-11 14:29:25 -0800309void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700310 if (mCaches.currentProgram) {
311 if (mCaches.currentProgram->isInUse()) {
312 mCaches.currentProgram->remove();
313 mCaches.currentProgram = NULL;
314 }
315 }
Romain Guy50c0f092010-10-19 11:42:22 -0700316 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800317 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800318 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800319 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700320 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700321}
322
Romain Guy6c319ca2011-01-11 14:29:25 -0800323void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800324 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800325 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700326 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700327 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700328
Romain Guy3e263fa2011-12-12 16:47:48 -0800329 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
330
Chet Haase80250612012-08-15 13:46:54 -0700331 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700332 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800333 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700334 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700335
Romain Guya1d3c912011-12-13 14:55:06 -0800336 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700337
Romain Guy50c0f092010-10-19 11:42:22 -0700338 mCaches.blend = true;
339 glEnable(GL_BLEND);
340 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
341 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700342}
343
Romain Guy35643dd2012-09-18 15:40:58 -0700344void OpenGLRenderer::resumeAfterLayer() {
345 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
346 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
347 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700348 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700349
350 mCaches.resetScissor();
351 dirtyClip();
352}
353
Romain Guyba6be8a2012-04-23 18:22:09 -0700354void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700355 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700356}
357
358void OpenGLRenderer::attachFunctor(Functor* functor) {
359 mFunctors.add(functor);
360}
361
Romain Guy8f3b8e32012-03-27 16:33:45 -0700362status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
363 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700364 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700365
Romain Guyba6be8a2012-04-23 18:22:09 -0700366 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800367 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700368 SortedVector<Functor*> functors(mFunctors);
369 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370
Romain Guyba6be8a2012-04-23 18:22:09 -0700371 DrawGlInfo info;
372 info.clipLeft = 0;
373 info.clipTop = 0;
374 info.clipRight = 0;
375 info.clipBottom = 0;
376 info.isLayer = false;
377 info.width = 0;
378 info.height = 0;
379 memset(info.transform, 0, sizeof(float) * 16);
380
381 for (size_t i = 0; i < count; i++) {
382 Functor* f = functors.itemAt(i);
383 result |= (*f)(DrawGlInfo::kModeProcess, &info);
384
Chris Craikc2c95432012-04-25 15:13:52 -0700385 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700386 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
387 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700388 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700389
Chris Craikc2c95432012-04-25 15:13:52 -0700390 if (result & DrawGlInfo::kStatusInvoke) {
391 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700392 }
393 }
Chris Craikd15321b2012-11-28 14:45:04 -0800394 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700395 }
396
397 return result;
398}
399
400status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800401 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700402 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700403
Romain Guy8a4ac612012-07-17 17:32:48 -0700404 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800405 if (mDirtyClip) {
406 setScissorFromClip();
407 }
Romain Guyd643bb52011-03-01 14:55:21 -0800408
Romain Guy80911b82011-03-16 15:30:12 -0700409 Rect clip(*mSnapshot->clipRect);
410 clip.snapToPixelBoundaries();
411
Romain Guyd643bb52011-03-01 14:55:21 -0800412 // Since we don't know what the functor will draw, let's dirty
413 // tne entire clip region
414 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800415 dirtyLayerUnchecked(clip, getRegion());
416 }
Romain Guyd643bb52011-03-01 14:55:21 -0800417
Romain Guy08aa2cb2011-03-17 11:06:57 -0700418 DrawGlInfo info;
419 info.clipLeft = clip.left;
420 info.clipTop = clip.top;
421 info.clipRight = clip.right;
422 info.clipBottom = clip.bottom;
423 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700424 info.width = getSnapshot()->viewport.getWidth();
425 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700426 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700427
Chet Haase48659092012-05-31 15:21:51 -0700428 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800429
Romain Guy8f3b8e32012-03-27 16:33:45 -0700430 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700431 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800432 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700433
Chris Craik65924a32012-04-05 17:52:11 -0700434 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700436 }
Romain Guycabfcc12011-03-07 18:06:46 -0800437 }
438
Chet Haasedaf98e92011-01-10 14:10:36 -0800439 resume();
Romain Guy65549432012-03-26 16:45:05 -0700440 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800441}
442
Romain Guyf6a11b82010-06-23 17:47:49 -0700443///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700444// Debug
445///////////////////////////////////////////////////////////////////////////////
446
447void OpenGLRenderer::startMark(const char* name) const {
448 mCaches.startMark(0, name);
449}
450
451void OpenGLRenderer::endMark() const {
452 mCaches.endMark();
453}
454
455void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
456 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
457 if (clear) {
458 mCaches.disableScissor();
459 mCaches.stencil.clear();
460 }
461 if (enable) {
462 mCaches.stencil.enableDebugWrite();
463 } else {
464 mCaches.stencil.disable();
465 }
466 }
467}
468
469void OpenGLRenderer::renderOverdraw() {
470 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
471 const Rect* clip = mTilingSnapshot->clipRect;
472
473 mCaches.enableScissor();
474 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
475 clip->right - clip->left, clip->bottom - clip->top);
476
477 mCaches.stencil.enableDebugTest(2);
478 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
479 mCaches.stencil.enableDebugTest(3);
480 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
481 mCaches.stencil.enableDebugTest(4);
482 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
483 mCaches.stencil.enableDebugTest(4, true);
484 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
485 mCaches.stencil.disable();
486 }
487}
488
489///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700490// Layers
491///////////////////////////////////////////////////////////////////////////////
492
493bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
494 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
495 OpenGLRenderer* renderer = layer->renderer;
496 Rect& dirty = layer->dirtyRect;
497
Romain Guy7c450aa2012-09-21 19:15:00 -0700498 if (inFrame) {
499 endTiling();
500 debugOverdraw(false, false);
501 }
Romain Guy11cb6422012-09-21 00:39:43 -0700502
503 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
504 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
505 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
506 renderer->finish();
507
508 if (inFrame) {
509 resumeAfterLayer();
510 startTiling(mSnapshot);
511 }
512
513 dirty.setEmpty();
514 layer->deferredUpdateScheduled = false;
515 layer->renderer = NULL;
516 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800517 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700518
519 return true;
520 }
521
522 return false;
523}
524
525void OpenGLRenderer::updateLayers() {
526 int count = mLayerUpdates.size();
527 if (count > 0) {
528 startMark("Layer Updates");
529
530 // Note: it is very important to update the layers in reverse order
531 for (int i = count - 1; i >= 0; i--) {
532 Layer* layer = mLayerUpdates.itemAt(i);
533 updateLayer(layer, false);
534 mCaches.resourceCache.decrementRefcount(layer);
535 }
536 mLayerUpdates.clear();
537
538 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
539 endMark();
540 }
541}
542
543void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
544 if (layer) {
545 mLayerUpdates.push_back(layer);
546 mCaches.resourceCache.incrementRefcount(layer);
547 }
548}
549
550void OpenGLRenderer::clearLayerUpdates() {
551 size_t count = mLayerUpdates.size();
552 if (count > 0) {
553 mCaches.resourceCache.lock();
554 for (size_t i = 0; i < count; i++) {
555 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
556 }
557 mCaches.resourceCache.unlock();
558 mLayerUpdates.clear();
559 }
560}
561
562///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700563// State management
564///////////////////////////////////////////////////////////////////////////////
565
Romain Guybb9524b2010-06-22 18:56:38 -0700566int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700567 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700568}
569
570int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700571 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700572}
573
574void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700575 if (mSaveCount > 1) {
576 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700577 }
Romain Guybb9524b2010-06-22 18:56:38 -0700578}
579
580void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700581 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700582
Romain Guy8fb95422010-08-17 18:38:51 -0700583 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700584 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700585 }
Romain Guybb9524b2010-06-22 18:56:38 -0700586}
587
Romain Guy8aef54f2010-09-01 15:13:49 -0700588int OpenGLRenderer::saveSnapshot(int flags) {
589 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700590 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700591}
592
593bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700594 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700595 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700596 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700597
Romain Guybd6b79b2010-06-26 00:13:53 -0700598 sp<Snapshot> current = mSnapshot;
599 sp<Snapshot> previous = mSnapshot->previous;
600
Romain Guyeb993562010-10-05 18:14:38 -0700601 if (restoreOrtho) {
602 Rect& r = previous->viewport;
603 glViewport(r.left, r.top, r.right, r.bottom);
604 mOrthoMatrix.load(current->orthoMatrix);
605 }
606
Romain Guy8b55f372010-08-18 17:10:07 -0700607 mSaveCount--;
608 mSnapshot = previous;
609
Romain Guy2542d192010-08-18 11:47:12 -0700610 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700611 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700612 }
Romain Guy2542d192010-08-18 11:47:12 -0700613
Romain Guy5ec99242010-11-03 16:19:08 -0700614 if (restoreLayer) {
615 composeLayer(current, previous);
616 }
617
Romain Guy2542d192010-08-18 11:47:12 -0700618 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700619}
620
Romain Guyf6a11b82010-06-23 17:47:49 -0700621///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700622// Layers
623///////////////////////////////////////////////////////////////////////////////
624
625int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700626 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700627 const GLuint previousFbo = mSnapshot->fbo;
628 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700629
Romain Guyaf636eb2010-12-09 17:47:21 -0800630 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700631 int alpha = 255;
632 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700633
Romain Guye45362c2010-11-03 19:58:32 -0700634 if (p) {
635 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700636 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700637 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700638 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700639 }
Romain Guyd55a8612010-06-28 17:42:46 -0700640
Chet Haased48885a2012-08-28 17:43:28 -0700641 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700642 }
Romain Guyd55a8612010-06-28 17:42:46 -0700643
644 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700645}
646
647int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
648 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700649 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700650 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700651 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700652 SkPaint paint;
653 paint.setAlpha(alpha);
654 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700655 }
Romain Guyd55a8612010-06-28 17:42:46 -0700656}
Romain Guybd6b79b2010-06-26 00:13:53 -0700657
Romain Guy1c740bc2010-09-13 18:00:09 -0700658/**
659 * Layers are viewed by Skia are slightly different than layers in image editing
660 * programs (for instance.) When a layer is created, previously created layers
661 * and the frame buffer still receive every drawing command. For instance, if a
662 * layer is created and a shape intersecting the bounds of the layers and the
663 * framebuffer is draw, the shape will be drawn on both (unless the layer was
664 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
665 *
666 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
667 * texture. Unfortunately, this is inefficient as it requires every primitive to
668 * be drawn n + 1 times, where n is the number of active layers. In practice this
669 * means, for every primitive:
670 * - Switch active frame buffer
671 * - Change viewport, clip and projection matrix
672 * - Issue the drawing
673 *
674 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700675 * To avoid this, layers are implemented in a different way here, at least in the
676 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
677 * is set. When this flag is set we can redirect all drawing operations into a
678 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700679 *
680 * This implementation relies on the frame buffer being at least RGBA 8888. When
681 * a layer is created, only a texture is created, not an FBO. The content of the
682 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700683 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700684 * buffer and drawing continues as normal. This technique therefore treats the
685 * frame buffer as a scratch buffer for the layers.
686 *
687 * To compose the layers back onto the frame buffer, each layer texture
688 * (containing the original frame buffer data) is drawn as a simple quad over
689 * the frame buffer. The trick is that the quad is set as the composition
690 * destination in the blending equation, and the frame buffer becomes the source
691 * of the composition.
692 *
693 * Drawing layers with an alpha value requires an extra step before composition.
694 * An empty quad is drawn over the layer's region in the frame buffer. This quad
695 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
696 * quad is used to multiply the colors in the frame buffer. This is achieved by
697 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
698 * GL_ZERO, GL_SRC_ALPHA.
699 *
700 * Because glCopyTexImage2D() can be slow, an alternative implementation might
701 * be use to draw a single clipped layer. The implementation described above
702 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700703 *
704 * (1) The frame buffer is actually not cleared right away. To allow the GPU
705 * to potentially optimize series of calls to glCopyTexImage2D, the frame
706 * buffer is left untouched until the first drawing operation. Only when
707 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700708 */
Chet Haased48885a2012-08-28 17:43:28 -0700709bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
710 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700711 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700712 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700713
Romain Guyeb993562010-10-05 18:14:38 -0700714 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
715
Romain Guyf607bdc2010-09-10 19:20:06 -0700716 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700717 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700718 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700719 Rect untransformedBounds(bounds);
720 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700721
Chet Haased48885a2012-08-28 17:43:28 -0700722 // Layers only make sense if they are in the framebuffer's bounds
723 if (bounds.intersect(*mSnapshot->clipRect)) {
724 // We cannot work with sub-pixels in this case
725 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700726
Chet Haased48885a2012-08-28 17:43:28 -0700727 // When the layer is not an FBO, we may use glCopyTexImage so we
728 // need to make sure the layer does not extend outside the bounds
729 // of the framebuffer
730 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700731 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700732 } else if (fboLayer) {
733 clip.set(bounds);
734 mat4 inverse;
735 inverse.loadInverse(*mSnapshot->transform);
736 inverse.mapRect(clip);
737 clip.snapToPixelBoundaries();
738 if (clip.intersect(untransformedBounds)) {
739 clip.translate(-left, -top);
740 bounds.set(untransformedBounds);
741 } else {
742 clip.setEmpty();
743 }
Romain Guyad37cd32011-03-15 11:12:25 -0700744 }
Chet Haased48885a2012-08-28 17:43:28 -0700745 } else {
746 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700747 }
Romain Guybf434112010-09-16 14:40:17 -0700748
Romain Guy746b7402010-10-26 16:27:31 -0700749 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700750 bounds.getHeight() > mCaches.maxTextureSize ||
751 (fboLayer && clip.isEmpty())) {
752 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700753 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700754 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700755 }
756
757 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700758 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700759 return false;
760 }
Romain Guyf18fd992010-07-08 11:45:51 -0700761
Romain Guya1d3c912011-12-13 14:55:06 -0800762 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700763 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700764 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700765 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700766 }
767
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700769 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
771 bounds.getWidth() / float(layer->getWidth()), 0.0f);
772 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700773 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700774 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700775
Romain Guy8fb95422010-08-17 18:38:51 -0700776 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700777 mSnapshot->flags |= Snapshot::kFlagIsLayer;
778 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700779
Romain Guyeb993562010-10-05 18:14:38 -0700780 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700781 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700782 } else {
783 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800785 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 if (layer->isEmpty()) {
787 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700788 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 layer->getWidth(), layer->getHeight(), 0);
790 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800791 } else {
792 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700793 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800794 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700795
Romain Guy54be1cd2011-06-13 19:04:27 -0700796 // Enqueue the buffer coordinates to clear the corresponding region later
797 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700798 }
Romain Guyeb993562010-10-05 18:14:38 -0700799 }
Romain Guyf86ef572010-07-01 11:05:42 -0700800
Romain Guyd55a8612010-06-28 17:42:46 -0700801 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700802}
803
Chet Haased48885a2012-08-28 17:43:28 -0700804bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800805 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
Chet Haased48885a2012-08-28 17:43:28 -0700808 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800809 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
810 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700811 mSnapshot->fbo = layer->getFbo();
812 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
813 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
814 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
815 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700816 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700817
Romain Guy2b7028e2012-09-19 17:25:38 -0700818 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700819 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
822 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700823
824 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700825 if (layer->isEmpty()) {
826 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
827 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700828 }
829
830 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832
Romain Guyf735c8e2013-01-31 17:45:55 -0800833 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700834
835 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700836 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800837 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700838 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 glClear(GL_COLOR_BUFFER_BIT);
840
841 dirtyClip();
842
843 // Change the ortho projection
844 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
845 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
846
847 return true;
848}
849
Romain Guy1c740bc2010-09-13 18:00:09 -0700850/**
851 * Read the documentation of createLayer() before doing anything in this method.
852 */
Romain Guy1d83e192010-08-17 11:37:00 -0700853void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
854 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000855 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700856 return;
857 }
858
Romain Guy8ce00302013-01-15 18:51:42 -0800859 Layer* layer = current->layer;
860 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700861 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700862
863 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700864 endTiling();
865
Romain Guye0aa84b2012-04-03 19:30:26 -0700866 // Detach the texture from the FBO
867 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800868
869 layer->removeFbo(false);
870
Romain Guyeb993562010-10-05 18:14:38 -0700871 // Unbind current FBO and restore previous one
872 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700873 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700874
875 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700876 }
877
Romain Guy9ace8f52011-07-07 20:50:11 -0700878 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700879 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700880 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700881 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700882 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700883 }
884
Romain Guy03750a02010-10-18 14:06:08 -0700885 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700886
Romain Guya1d3c912011-12-13 14:55:06 -0800887 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700888
Romain Guy5b3b3522010-10-27 18:57:51 -0700889 // When the layer is stored in an FBO, we can save a bit of fillrate by
890 // drawing only the dirty region
891 if (fboLayer) {
892 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 if (layer->getColorFilter()) {
894 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800895 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700896 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700897 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800898 resetColorFilter();
899 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 } else if (!rect.isEmpty()) {
901 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
902 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700903 }
Romain Guy8b55f372010-08-18 17:10:07 -0700904
Romain Guy746b7402010-10-26 16:27:31 -0700905 dirtyClip();
906
Romain Guyeb993562010-10-05 18:14:38 -0700907 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700908 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700909 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700910 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700911 }
912}
913
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700916
917 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700919 setupDrawWithTexture();
920 } else {
921 setupDrawWithExternalTexture();
922 }
923 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700924 setupDrawColor(alpha, alpha, alpha, alpha);
925 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700927 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928 setupDrawPureColorUniforms();
929 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
931 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700932 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700934 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700935 if (mSnapshot->transform->isPureTranslate() &&
936 layer->getWidth() == (uint32_t) rect.getWidth() &&
937 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
939 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
940
Romain Guyd21b6e12011-11-30 20:21:23 -0800941 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
943 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800944 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
946 }
947 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700948 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
949
950 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
951
952 finishDrawTexture();
953}
954
Romain Guy5b3b3522010-10-27 18:57:51 -0700955void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700957 const Rect& texCoords = layer->texCoords;
958 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
959 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700960
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 float x = rect.left;
962 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700963 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700964 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700965 layer->getHeight() == (uint32_t) rect.getHeight();
966
967 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 // When we're swapping, the layer is already in screen coordinates
969 if (!swap) {
970 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
971 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
972 }
973
Romain Guyd21b6e12011-11-30 20:21:23 -0800974 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800976 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 }
978
979 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
980 layer->getTexture(), layer->getAlpha() / 255.0f,
981 layer->getMode(), layer->isBlend(),
982 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
983 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700984
Romain Guyaa6c24c2011-04-28 18:40:04 -0700985 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
986 } else {
987 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
988 drawTextureLayer(layer, rect);
989 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
990 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700991}
992
993void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700994 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700995 layer->setRegionAsRect();
996
Romain Guy40667672011-03-18 14:34:03 -0700997 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700998
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 layer->region.clear();
1000 return;
1001 }
1002
Romain Guy8a3957d2011-09-07 17:55:15 -07001003 // TODO: See LayerRenderer.cpp::generateMesh() for important
1004 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001005 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001006 size_t count;
1007 const android::Rect* rects = layer->region.getArray(&count);
1008
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 const float alpha = layer->getAlpha() / 255.0f;
1010 const float texX = 1.0f / float(layer->getWidth());
1011 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001012 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001013
Romain Guy8ce00302013-01-15 18:51:42 -08001014 setupDraw();
1015
1016 // We must get (and therefore bind) the region mesh buffer
1017 // after we setup drawing in case we need to mess with the
1018 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001019 TextureVertex* mesh = mCaches.getRegionMesh();
1020 GLsizei numQuads = 0;
1021
Romain Guy7230a742011-01-10 22:26:16 -08001022 setupDrawWithTexture();
1023 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001024 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001025 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001026 setupDrawProgram();
1027 setupDrawDirtyRegionsDisabled();
1028 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001029 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001030 setupDrawTexture(layer->getTexture());
1031 if (mSnapshot->transform->isPureTranslate()) {
1032 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1033 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1034
Romain Guyd21b6e12011-11-30 20:21:23 -08001035 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001036 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1037 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001038 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001039 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1040 }
Romain Guy15bc6432011-12-13 13:11:32 -08001041 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001042
1043 for (size_t i = 0; i < count; i++) {
1044 const android::Rect* r = &rects[i];
1045
1046 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001047 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001048 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001049 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001050
1051 // TODO: Reject quads outside of the clip
1052 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1053 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1054 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1055 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1056
1057 numQuads++;
1058
1059 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1060 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1061 numQuads = 0;
1062 mesh = mCaches.getRegionMesh();
1063 }
1064 }
1065
1066 if (numQuads > 0) {
1067 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1068 }
1069
Romain Guy7230a742011-01-10 22:26:16 -08001070 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001071
1072#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001073 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001074#endif
1075
1076 layer->region.clear();
1077 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001078}
1079
Romain Guy3a3133d2011-02-01 22:59:58 -08001080void OpenGLRenderer::drawRegionRects(const Region& region) {
1081#if DEBUG_LAYERS_AS_REGIONS
1082 size_t count;
1083 const android::Rect* rects = region.getArray(&count);
1084
1085 uint32_t colors[] = {
1086 0x7fff0000, 0x7f00ff00,
1087 0x7f0000ff, 0x7fff00ff,
1088 };
1089
1090 int offset = 0;
1091 int32_t top = rects[0].top;
1092
1093 for (size_t i = 0; i < count; i++) {
1094 if (top != rects[i].top) {
1095 offset ^= 0x2;
1096 top = rects[i].top;
1097 }
1098
1099 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1100 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1101 SkXfermode::kSrcOver_Mode);
1102 }
1103#endif
1104}
1105
Romain Guy8ce00302013-01-15 18:51:42 -08001106void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1107 SkXfermode::Mode mode, bool dirty) {
1108 int count = 0;
1109 Vector<float> rects;
1110
1111 SkRegion::Iterator it(region);
1112 while (!it.done()) {
1113 const SkIRect& r = it.rect();
1114 rects.push(r.fLeft);
1115 rects.push(r.fTop);
1116 rects.push(r.fRight);
1117 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001118 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001119 it.next();
1120 }
1121
1122 drawColorRects(rects.array(), count, color, mode, true, dirty);
1123}
1124
Romain Guy5b3b3522010-10-27 18:57:51 -07001125void OpenGLRenderer::dirtyLayer(const float left, const float top,
1126 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001127 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001128 Rect bounds(left, top, right, bottom);
1129 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001130 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001131 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001132}
1133
1134void OpenGLRenderer::dirtyLayer(const float left, const float top,
1135 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001136 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001137 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001138 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001139 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001140}
1141
1142void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001143 if (bounds.intersect(*mSnapshot->clipRect)) {
1144 bounds.snapToPixelBoundaries();
1145 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1146 if (!dirty.isEmpty()) {
1147 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001148 }
1149 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001150}
1151
Romain Guy54be1cd2011-06-13 19:04:27 -07001152void OpenGLRenderer::clearLayerRegions() {
1153 const size_t count = mLayers.size();
1154 if (count == 0) return;
1155
1156 if (!mSnapshot->isIgnored()) {
1157 // Doing several glScissor/glClear here can negatively impact
1158 // GPUs with a tiler architecture, instead we draw quads with
1159 // the Clear blending mode
1160
1161 // The list contains bounds that have already been clipped
1162 // against their initial clip rect, and the current clip
1163 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001164 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001165
1166 Vertex mesh[count * 6];
1167 Vertex* vertex = mesh;
1168
1169 for (uint32_t i = 0; i < count; i++) {
1170 Rect* bounds = mLayers.itemAt(i);
1171
1172 Vertex::set(vertex++, bounds->left, bounds->bottom);
1173 Vertex::set(vertex++, bounds->left, bounds->top);
1174 Vertex::set(vertex++, bounds->right, bounds->top);
1175 Vertex::set(vertex++, bounds->left, bounds->bottom);
1176 Vertex::set(vertex++, bounds->right, bounds->top);
1177 Vertex::set(vertex++, bounds->right, bounds->bottom);
1178
1179 delete bounds;
1180 }
1181
1182 setupDraw(false);
1183 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1184 setupDrawBlending(true, SkXfermode::kClear_Mode);
1185 setupDrawProgram();
1186 setupDrawPureColorUniforms();
1187 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001188 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001189
Romain Guy54be1cd2011-06-13 19:04:27 -07001190 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001191
1192 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001193 } else {
1194 for (uint32_t i = 0; i < count; i++) {
1195 delete mLayers.itemAt(i);
1196 }
1197 }
1198
1199 mLayers.clear();
1200}
1201
Romain Guybd6b79b2010-06-26 00:13:53 -07001202///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001203// Transforms
1204///////////////////////////////////////////////////////////////////////////////
1205
1206void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001207 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001208}
1209
1210void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001211 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001212}
1213
1214void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001215 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001216}
1217
Romain Guy807daf72011-01-18 11:19:19 -08001218void OpenGLRenderer::skew(float sx, float sy) {
1219 mSnapshot->transform->skew(sx, sy);
1220}
1221
Romain Guyf6a11b82010-06-23 17:47:49 -07001222void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001223 if (matrix) {
1224 mSnapshot->transform->load(*matrix);
1225 } else {
1226 mSnapshot->transform->loadIdentity();
1227 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001228}
1229
1230void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001231 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001232}
1233
1234void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001235 SkMatrix transform;
1236 mSnapshot->transform->copyTo(transform);
1237 transform.preConcat(*matrix);
1238 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001239}
1240
1241///////////////////////////////////////////////////////////////////////////////
1242// Clipping
1243///////////////////////////////////////////////////////////////////////////////
1244
Romain Guybb9524b2010-06-22 18:56:38 -07001245void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001246 Rect clip(*mSnapshot->clipRect);
1247 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001248
Romain Guy8a4ac612012-07-17 17:32:48 -07001249 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1250 clip.getWidth(), clip.getHeight())) {
1251 mDirtyClip = false;
1252 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001253}
1254
Romain Guy8ce00302013-01-15 18:51:42 -08001255void OpenGLRenderer::ensureStencilBuffer() {
1256 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1257 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1258 // just hope we have one when hasLayer() returns false.
1259 if (hasLayer()) {
1260 attachStencilBufferToLayer(mSnapshot->layer);
1261 }
1262}
1263
1264void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1265 // The layer's FBO is already bound when we reach this stage
1266 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001267 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1268 // is attached after we initiated tiling. We must turn it off,
1269 // attach the new render buffer then turn tiling back on
1270 endTiling();
1271
Romain Guy8ce00302013-01-15 18:51:42 -08001272 // TODO: See Layer::removeFbo(). The stencil renderbuffer should be cached
1273 GLuint buffer;
1274 glGenRenderbuffers(1, &buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001275
Romain Guy8ce00302013-01-15 18:51:42 -08001276 layer->setStencilRenderBuffer(buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001277 layer->bindStencilRenderBuffer();
1278 layer->allocateStencilRenderBuffer();
1279
1280 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001281
Romain Guyf735c8e2013-01-31 17:45:55 -08001282 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001283 }
1284}
1285
1286void OpenGLRenderer::setStencilFromClip() {
1287 if (!mCaches.debugOverdraw) {
1288 if (!mSnapshot->clipRegion->isEmpty()) {
1289 // NOTE: The order here is important, we must set dirtyClip to false
1290 // before any draw call to avoid calling back into this method
1291 mDirtyClip = false;
1292
1293 ensureStencilBuffer();
1294
1295 mCaches.stencil.enableWrite();
1296
1297 // Clear the stencil but first make sure we restrict drawing
1298 // to the region's bounds
1299 bool resetScissor = mCaches.enableScissor();
1300 if (resetScissor) {
1301 // The scissor was not set so we now need to update it
1302 setScissorFromClip();
1303 }
1304 mCaches.stencil.clear();
1305 if (resetScissor) mCaches.disableScissor();
1306
1307 // NOTE: We could use the region contour path to generate a smaller mesh
1308 // Since we are using the stencil we could use the red book path
1309 // drawing technique. It might increase bandwidth usage though.
1310
1311 // The last parameter is important: we are not drawing in the color buffer
1312 // so we don't want to dirty the current layer, if any
1313 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1314
1315 mCaches.stencil.enableTest();
1316 } else {
1317 mCaches.stencil.disable();
1318 }
1319 }
1320}
1321
Romain Guy9d5316e2010-06-24 19:30:36 -07001322const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001323 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001324}
1325
Romain Guy8a4ac612012-07-17 17:32:48 -07001326bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1327 if (mSnapshot->isIgnored()) {
1328 return true;
1329 }
1330
1331 Rect r(left, top, right, bottom);
1332 mSnapshot->transform->mapRect(r);
1333 r.snapToPixelBoundaries();
1334
1335 Rect clipRect(*mSnapshot->clipRect);
1336 clipRect.snapToPixelBoundaries();
1337
1338 return !clipRect.intersects(r);
1339}
1340
Romain Guy35643dd2012-09-18 15:40:58 -07001341bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1342 Rect& transformed, Rect& clip) {
1343 if (mSnapshot->isIgnored()) {
1344 return true;
1345 }
1346
1347 transformed.set(left, top, right, bottom);
1348 mSnapshot->transform->mapRect(transformed);
1349 transformed.snapToPixelBoundaries();
1350
1351 clip.set(*mSnapshot->clipRect);
1352 clip.snapToPixelBoundaries();
1353
1354 return !clip.intersects(transformed);
1355}
1356
Romain Guy672433d2013-01-04 19:05:13 -08001357bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1358 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001359 if (paint->getStyle() != SkPaint::kFill_Style) {
1360 float outset = paint->getStrokeWidth() * 0.5f;
1361 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1362 } else {
1363 return quickReject(left, top, right, bottom);
1364 }
1365}
1366
Romain Guyc7d53492010-06-25 13:41:57 -07001367bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001368 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001369 return true;
1370 }
1371
Romain Guy1d83e192010-08-17 11:37:00 -07001372 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001373 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001374 r.snapToPixelBoundaries();
1375
1376 Rect clipRect(*mSnapshot->clipRect);
1377 clipRect.snapToPixelBoundaries();
1378
Romain Guy586cae32012-07-13 15:28:31 -07001379 bool rejected = !clipRect.intersects(r);
1380 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001381 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001382 }
1383
1384 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001385}
1386
Romain Guy8ce00302013-01-15 18:51:42 -08001387void OpenGLRenderer::debugClip() {
1388#if DEBUG_CLIP_REGIONS
1389 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1390 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1391 }
1392#endif
1393}
1394
Romain Guy079ba2c2010-07-16 14:12:24 -07001395bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001396 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1397 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1398 if (clipped) {
1399 dirtyClip();
1400 }
1401 return !mSnapshot->clipRect->isEmpty();
1402 }
1403
1404 SkPath path;
1405 path.addRect(left, top, right, bottom);
1406
1407 return clipPath(&path, op);
1408}
1409
1410bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1411 SkMatrix transform;
1412 mSnapshot->transform->copyTo(transform);
1413
1414 SkPath transformed;
1415 path->transform(transform, &transformed);
1416
1417 SkRegion clip;
1418 if (!mSnapshot->clipRegion->isEmpty()) {
1419 clip.setRegion(*mSnapshot->clipRegion);
1420 } else {
1421 Rect* bounds = mSnapshot->clipRect;
1422 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1423 }
1424
1425 SkRegion region;
1426 region.setPath(transformed, clip);
1427
1428 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001429 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001430 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001431 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001432 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001433}
1434
Romain Guy735738c2012-12-03 12:34:51 -08001435bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001436 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1437 if (clipped) {
1438 dirtyClip();
1439 }
1440 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001441}
1442
Chet Haasea23eed82012-04-12 15:19:04 -07001443Rect* OpenGLRenderer::getClipRect() {
1444 return mSnapshot->clipRect;
1445}
1446
Romain Guyf6a11b82010-06-23 17:47:49 -07001447///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001448// Drawing commands
1449///////////////////////////////////////////////////////////////////////////////
1450
Romain Guy54be1cd2011-06-13 19:04:27 -07001451void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001452 // TODO: It would be best if we could do this before quickReject()
1453 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001454 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001455 // Make sure setScissor & setStencil happen at the beginning of
1456 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001457 if (mDirtyClip) {
1458 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001459 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001460 }
1461 mDescription.reset();
1462 mSetShaderColor = false;
1463 mColorSet = false;
1464 mColorA = mColorR = mColorG = mColorB = 0.0f;
1465 mTextureUnit = 0;
1466 mTrackDirtyRegions = true;
1467}
1468
1469void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1470 mDescription.hasTexture = true;
1471 mDescription.hasAlpha8Texture = isAlpha8;
1472}
1473
Romain Guyaa6c24c2011-04-28 18:40:04 -07001474void OpenGLRenderer::setupDrawWithExternalTexture() {
1475 mDescription.hasExternalTexture = true;
1476}
1477
Romain Guy15bc6432011-12-13 13:11:32 -08001478void OpenGLRenderer::setupDrawNoTexture() {
1479 mCaches.disbaleTexCoordsVertexArray();
1480}
1481
Chris Craik710f46d2012-09-17 17:25:49 -07001482void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001483 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001484}
1485
Chris Craik710f46d2012-09-17 17:25:49 -07001486void OpenGLRenderer::setupDrawVertexShape() {
1487 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001488}
1489
Romain Guyed6fcb02011-03-21 13:11:28 -07001490void OpenGLRenderer::setupDrawPoint(float pointSize) {
1491 mDescription.isPoint = true;
1492 mDescription.pointSize = pointSize;
1493}
1494
Romain Guy8d0d4782010-12-14 20:13:35 -08001495void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1496 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001497 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1498 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1499 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001500 mColorSet = true;
1501 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1502}
1503
Romain Guy86568192010-12-14 15:55:39 -08001504void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1505 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001506 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1507 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1508 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001509 mColorSet = true;
1510 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1511}
1512
Romain Guy41210632012-07-16 17:04:24 -07001513void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1514 mCaches.fontRenderer->describe(mDescription, paint);
1515}
1516
Romain Guy70ca14e2010-12-13 18:24:33 -08001517void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1518 mColorA = a;
1519 mColorR = r;
1520 mColorG = g;
1521 mColorB = b;
1522 mColorSet = true;
1523 mSetShaderColor = mDescription.setColor(r, g, b, a);
1524}
1525
1526void OpenGLRenderer::setupDrawShader() {
1527 if (mShader) {
1528 mShader->describe(mDescription, mCaches.extensions);
1529 }
1530}
1531
1532void OpenGLRenderer::setupDrawColorFilter() {
1533 if (mColorFilter) {
1534 mColorFilter->describe(mDescription, mCaches.extensions);
1535 }
1536}
1537
Romain Guyf09ef512011-05-27 11:43:46 -07001538void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1539 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1540 mColorA = 1.0f;
1541 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001542 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001543 }
1544}
1545
Romain Guy70ca14e2010-12-13 18:24:33 -08001546void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001547 // When the blending mode is kClear_Mode, we need to use a modulate color
1548 // argb=1,0,0,0
1549 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001550 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1551 mDescription, swapSrcDst);
1552}
1553
1554void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001555 // When the blending mode is kClear_Mode, we need to use a modulate color
1556 // argb=1,0,0,0
1557 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001558 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1559 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001560}
1561
1562void OpenGLRenderer::setupDrawProgram() {
1563 useProgram(mCaches.programCache.get(mDescription));
1564}
1565
1566void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1567 mTrackDirtyRegions = false;
1568}
1569
1570void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1571 bool ignoreTransform) {
1572 mModelView.loadTranslate(left, top, 0.0f);
1573 if (!ignoreTransform) {
1574 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1575 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1576 } else {
1577 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1578 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1579 }
1580}
1581
Chet Haase8a5cc922011-04-26 07:28:09 -07001582void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1583 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001584}
1585
Romain Guy70ca14e2010-12-13 18:24:33 -08001586void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1587 bool ignoreTransform, bool ignoreModelView) {
1588 if (!ignoreModelView) {
1589 mModelView.loadTranslate(left, top, 0.0f);
1590 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001591 } else {
1592 mModelView.loadIdentity();
1593 }
Romain Guy86568192010-12-14 15:55:39 -08001594 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1595 if (!ignoreTransform) {
1596 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1597 if (mTrackDirtyRegions && dirty) {
1598 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1599 }
1600 } else {
1601 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1602 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1603 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001604}
1605
Romain Guyed6fcb02011-03-21 13:11:28 -07001606void OpenGLRenderer::setupDrawPointUniforms() {
1607 int slot = mCaches.currentProgram->getUniform("pointSize");
1608 glUniform1f(slot, mDescription.pointSize);
1609}
1610
Romain Guy70ca14e2010-12-13 18:24:33 -08001611void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001612 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001613 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1614 }
1615}
1616
Romain Guy86568192010-12-14 15:55:39 -08001617void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001618 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001619 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001620 }
1621}
1622
Romain Guy70ca14e2010-12-13 18:24:33 -08001623void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1624 if (mShader) {
1625 if (ignoreTransform) {
1626 mModelView.loadInverse(*mSnapshot->transform);
1627 }
1628 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1629 }
1630}
1631
Romain Guy8d0d4782010-12-14 20:13:35 -08001632void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1633 if (mShader) {
1634 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1635 }
1636}
1637
Romain Guy70ca14e2010-12-13 18:24:33 -08001638void OpenGLRenderer::setupDrawColorFilterUniforms() {
1639 if (mColorFilter) {
1640 mColorFilter->setupProgram(mCaches.currentProgram);
1641 }
1642}
1643
Romain Guy41210632012-07-16 17:04:24 -07001644void OpenGLRenderer::setupDrawTextGammaUniforms() {
1645 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1646}
1647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001649 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001650 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001651 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001652}
1653
1654void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1655 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001656 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001657 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001658}
1659
Romain Guyaa6c24c2011-04-28 18:40:04 -07001660void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1661 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001662 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001663 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001664}
1665
Romain Guy8f0095c2011-05-02 17:24:22 -07001666void OpenGLRenderer::setupDrawTextureTransform() {
1667 mDescription.hasTextureTransform = true;
1668}
1669
1670void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001671 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1672 GL_FALSE, &transform.data[0]);
1673}
1674
Romain Guy70ca14e2010-12-13 18:24:33 -08001675void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001676 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001677 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001678 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001679 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001680 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001681 }
Romain Guyd71dd362011-12-12 19:03:35 -08001682
Chris Craikcb4d6002012-09-25 12:00:29 -07001683 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001684 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001685 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001686 }
1687
1688 mCaches.unbindIndicesBuffer();
1689}
1690
1691void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1692 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001693 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001694 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001695 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001696 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001697}
1698
Chet Haase5b0200b2011-04-13 17:58:08 -07001699void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001700 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001701 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001702 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001703}
1704
1705/**
1706 * 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 -07001707 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1708 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1709 * attributes (one per vertex) are values from zero to one that tells the fragment
1710 * shader where the fragment is in relation to the line width/length overall; these values are
1711 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1712 * region of the line.
1713 * Note that we only pass down the width values in this setup function. The length coordinates
1714 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001715 */
Chet Haase99585ad2011-05-02 15:00:16 -07001716void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001717 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001718 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001719 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001720 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001721 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001722
Romain Guy7b631422012-04-04 11:38:54 -07001723 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001724 glEnableVertexAttribArray(widthSlot);
1725 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001726
Romain Guy7b631422012-04-04 11:38:54 -07001727 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001728 glEnableVertexAttribArray(lengthSlot);
1729 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001730
Chet Haase5b0200b2011-04-13 17:58:08 -07001731 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001732 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001733}
1734
1735void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1736 glDisableVertexAttribArray(widthSlot);
1737 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001738}
1739
Romain Guy70ca14e2010-12-13 18:24:33 -08001740void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001741}
1742
1743///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001744// Drawing
1745///////////////////////////////////////////////////////////////////////////////
1746
Chet Haase1271e2c2012-04-20 09:54:27 -07001747status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001748 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001749
Romain Guy0fe478e2010-11-08 12:08:41 -08001750 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1751 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001752 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001753 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001754 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001755
Romain Guy65549432012-03-26 16:45:05 -07001756 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001757}
1758
Chet Haaseed30fd82011-04-22 16:18:45 -07001759void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1760 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001761 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001762 }
1763}
1764
Romain Guya168d732011-03-18 16:50:13 -07001765void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1766 int alpha;
1767 SkXfermode::Mode mode;
1768 getAlphaAndMode(paint, &alpha, &mode);
1769
Romain Guy886b2752013-01-04 12:26:18 -08001770 int color = paint != NULL ? paint->getColor() : 0;
1771
Romain Guya168d732011-03-18 16:50:13 -07001772 float x = left;
1773 float y = top;
1774
Romain Guy886b2752013-01-04 12:26:18 -08001775 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1776
Romain Guya168d732011-03-18 16:50:13 -07001777 bool ignoreTransform = false;
1778 if (mSnapshot->transform->isPureTranslate()) {
1779 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1780 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1781 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001782
1783 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001784 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001785 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001786 }
1787
Romain Guy886b2752013-01-04 12:26:18 -08001788 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1789 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1790 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001791}
1792
Chet Haase48659092012-05-31 15:21:51 -07001793status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001794 const float right = left + bitmap->width();
1795 const float bottom = top + bitmap->height();
1796
1797 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001798 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001799 }
1800
Romain Guya1d3c912011-12-13 14:55:06 -08001801 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001802 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001803 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001804 const AutoTexture autoCleanup(texture);
1805
Romain Guy211370f2012-02-01 16:10:55 -08001806 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001807 drawAlphaBitmap(texture, left, top, paint);
1808 } else {
1809 drawTextureRect(left, top, right, bottom, texture, paint);
1810 }
Chet Haase48659092012-05-31 15:21:51 -07001811
1812 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001813}
1814
Chet Haase48659092012-05-31 15:21:51 -07001815status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001816 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1817 const mat4 transform(*matrix);
1818 transform.mapRect(r);
1819
Romain Guy6926c722010-07-12 20:20:03 -07001820 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001821 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001822 }
1823
Romain Guya1d3c912011-12-13 14:55:06 -08001824 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001825 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001826 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001827 const AutoTexture autoCleanup(texture);
1828
Romain Guy5b3b3522010-10-27 18:57:51 -07001829 // This could be done in a cheaper way, all we need is pass the matrix
1830 // to the vertex shader. The save/restore is a bit overkill.
1831 save(SkCanvas::kMatrix_SaveFlag);
1832 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001833 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1834 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1835 } else {
1836 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1837 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001838 restore();
Chet Haase48659092012-05-31 15:21:51 -07001839
1840 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001841}
1842
Chet Haase48659092012-05-31 15:21:51 -07001843status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001844 const float right = left + bitmap->width();
1845 const float bottom = top + bitmap->height();
1846
1847 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001848 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001849 }
1850
1851 mCaches.activeTexture(0);
1852 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1853 const AutoTexture autoCleanup(texture);
1854
Romain Guy886b2752013-01-04 12:26:18 -08001855 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1856 drawAlphaBitmap(texture, left, top, paint);
1857 } else {
1858 drawTextureRect(left, top, right, bottom, texture, paint);
1859 }
Chet Haase48659092012-05-31 15:21:51 -07001860
1861 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001862}
1863
Chet Haase48659092012-05-31 15:21:51 -07001864status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001865 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001866 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001867 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001868 }
1869
Romain Guyb18d2d02011-02-10 15:52:54 -08001870 float left = FLT_MAX;
1871 float top = FLT_MAX;
1872 float right = FLT_MIN;
1873 float bottom = FLT_MIN;
1874
Romain Guya92bb4d2012-10-16 11:08:44 -07001875 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001876
Romain Guya566b7c2011-01-23 16:36:11 -08001877 // TODO: Support the colors array
1878 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001879 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001880
Romain Guy5a7b4662011-01-20 19:09:30 -08001881 for (int32_t y = 0; y < meshHeight; y++) {
1882 for (int32_t x = 0; x < meshWidth; x++) {
1883 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1884
1885 float u1 = float(x) / meshWidth;
1886 float u2 = float(x + 1) / meshWidth;
1887 float v1 = float(y) / meshHeight;
1888 float v2 = float(y + 1) / meshHeight;
1889
1890 int ax = i + (meshWidth + 1) * 2;
1891 int ay = ax + 1;
1892 int bx = i;
1893 int by = bx + 1;
1894 int cx = i + 2;
1895 int cy = cx + 1;
1896 int dx = i + (meshWidth + 1) * 2 + 2;
1897 int dy = dx + 1;
1898
1899 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1900 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1901 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1902
1903 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1904 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1905 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001906
Romain Guya92bb4d2012-10-16 11:08:44 -07001907 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1908 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1909 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1910 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001911 }
1912 }
1913
Romain Guya92bb4d2012-10-16 11:08:44 -07001914 if (quickReject(left, top, right, bottom)) {
1915 return DrawGlInfo::kStatusDone;
1916 }
1917
1918 mCaches.activeTexture(0);
1919 Texture* texture = mCaches.textureCache.get(bitmap);
1920 if (!texture) return DrawGlInfo::kStatusDone;
1921 const AutoTexture autoCleanup(texture);
1922
1923 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1924 texture->setFilter(FILTER(paint), true);
1925
1926 int alpha;
1927 SkXfermode::Mode mode;
1928 getAlphaAndMode(paint, &alpha, &mode);
1929
1930 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001931 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1932 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001933
Romain Guy5a7b4662011-01-20 19:09:30 -08001934 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1935 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001936 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001937
1938 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001939}
1940
Chet Haase48659092012-05-31 15:21:51 -07001941status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001942 float srcLeft, float srcTop, float srcRight, float srcBottom,
1943 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001944 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001945 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001946 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001947 }
1948
Romain Guya1d3c912011-12-13 14:55:06 -08001949 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001950 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001951 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001952 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001953
Romain Guy8ba548f2010-06-30 19:21:21 -07001954 const float width = texture->width;
1955 const float height = texture->height;
1956
Romain Guy68169722011-08-22 17:33:33 -07001957 const float u1 = fmax(0.0f, srcLeft / width);
1958 const float v1 = fmax(0.0f, srcTop / height);
1959 const float u2 = fmin(1.0f, srcRight / width);
1960 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001961
Romain Guy03750a02010-10-18 14:06:08 -07001962 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001963 resetDrawTextureTexCoords(u1, v1, u2, v2);
1964
Romain Guy03750a02010-10-18 14:06:08 -07001965 int alpha;
1966 SkXfermode::Mode mode;
1967 getAlphaAndMode(paint, &alpha, &mode);
1968
Romain Guyd21b6e12011-11-30 20:21:23 -08001969 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1970
Romain Guy886b2752013-01-04 12:26:18 -08001971 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1972 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001973
Romain Guy886b2752013-01-04 12:26:18 -08001974 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1975 // Apply a scale transform on the canvas only when a shader is in use
1976 // Skia handles the ratio between the dst and src rects as a scale factor
1977 // when a shader is set
1978 bool useScaleTransform = mShader && scaled;
1979 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001980
Romain Guy886b2752013-01-04 12:26:18 -08001981 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1982 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1983 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1984
1985 dstRight = x + (dstRight - dstLeft);
1986 dstBottom = y + (dstBottom - dstTop);
1987
1988 dstLeft = x;
1989 dstTop = y;
1990
1991 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1992 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001993 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001994 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001995 }
1996
1997 if (CC_UNLIKELY(useScaleTransform)) {
1998 save(SkCanvas::kMatrix_SaveFlag);
1999 translate(dstLeft, dstTop);
2000 scale(scaleX, scaleY);
2001
2002 dstLeft = 0.0f;
2003 dstTop = 0.0f;
2004
2005 dstRight = srcRight - srcLeft;
2006 dstBottom = srcBottom - srcTop;
2007 }
2008
2009 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2010 int color = paint ? paint->getColor() : 0;
2011 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2012 texture->id, paint != NULL, color, alpha, mode,
2013 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2014 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2015 } else {
2016 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2017 texture->id, alpha / 255.0f, mode, texture->blend,
2018 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2019 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2020 }
2021
2022 if (CC_UNLIKELY(useScaleTransform)) {
2023 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002024 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002025
2026 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002027
2028 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002029}
2030
Chet Haase48659092012-05-31 15:21:51 -07002031status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002032 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002033 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002034 int alpha;
2035 SkXfermode::Mode mode;
2036 getAlphaAndModeDirect(paint, &alpha, &mode);
2037
2038 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2039 left, top, right, bottom, alpha, mode);
2040}
2041
2042status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2043 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2044 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002045 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002046 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002047 }
2048
Romain Guybe6f9dc2012-07-16 12:41:17 -07002049 alpha *= mSnapshot->alpha;
2050
Romain Guya1d3c912011-12-13 14:55:06 -08002051 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002052 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002053 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002054 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002055 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2056 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002057
Romain Guy2728f962010-10-08 18:36:15 -07002058 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002059 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002060
Romain Guy211370f2012-02-01 16:10:55 -08002061 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002062 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002063 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002064 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002065 const float offsetX = left + mSnapshot->transform->getTranslateX();
2066 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002067 const size_t count = mesh->quads.size();
2068 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002069 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002070 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002071 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2072 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2073 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002074 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002075 dirtyLayer(left + bounds.left, top + bounds.top,
2076 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002077 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002078 }
2079 }
2080
Romain Guy211370f2012-02-01 16:10:55 -08002081 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002082 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2083 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2084
2085 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2086 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2087 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2088 true, !mesh->hasEmptyQuads);
2089 } else {
2090 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2091 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2092 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2093 true, !mesh->hasEmptyQuads);
2094 }
Romain Guy054dc182010-10-15 17:55:25 -07002095 }
Chet Haase48659092012-05-31 15:21:51 -07002096
2097 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002098}
2099
Chet Haase99ecdc42011-05-06 12:06:34 -07002100/**
Chris Craikcb4d6002012-09-25 12:00:29 -07002101 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2102 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2103 * screen space in all directions. However, instead of using a fragment shader to compute the
2104 * translucency of the color from its position, we simply use a varying parameter to define how far
2105 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2106 *
2107 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07002108 */
Chris Craikcb4d6002012-09-25 12:00:29 -07002109void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2110 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002111 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2112 bool isAA = paint->isAntiAlias();
2113
Chris Craik710f46d2012-09-17 17:25:49 -07002114 VertexBuffer vertexBuffer;
2115 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07002116 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07002117
Chris Craikbf09ffb2012-10-01 13:50:37 -07002118 if (!vertexBuffer.getSize()) {
2119 // no vertices to draw
2120 return;
2121 }
2122
Chris Craik710f46d2012-09-17 17:25:49 -07002123 setupDraw();
2124 setupDrawNoTexture();
2125 if (isAA) setupDrawAA();
2126 setupDrawVertexShape();
2127 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2128 setupDrawColorFilter();
2129 setupDrawShader();
2130 setupDrawBlending(isAA, mode);
2131 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002132 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07002133 setupDrawColorUniforms();
2134 setupDrawColorFilterUniforms();
2135 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002136
Chris Craik710f46d2012-09-17 17:25:49 -07002137 void* vertices = vertexBuffer.getBuffer();
2138 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002139 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002140 mCaches.resetTexCoordsVertexPointer();
2141 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002142
Chris Craik710f46d2012-09-17 17:25:49 -07002143 int alphaSlot = -1;
2144 if (isAA) {
2145 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2146 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002147
Chris Craik710f46d2012-09-17 17:25:49 -07002148 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002149 glEnableVertexAttribArray(alphaSlot);
2150 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002151 }
Romain Guy04299382012-07-18 17:15:41 -07002152
Chris Craikcb4d6002012-09-25 12:00:29 -07002153 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002154 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002155
Chris Craik710f46d2012-09-17 17:25:49 -07002156 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002157
Chris Craik710f46d2012-09-17 17:25:49 -07002158 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002159 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002160 }
Chet Haase858aa932011-05-12 09:06:00 -07002161}
2162
2163/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002164 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2165 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2166 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2167 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2168 * of the line. Hairlines are more involved because we need to account for transform scaling
2169 * to end up with a one-pixel-wide line in screen space..
2170 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2171 * in combination with values that we calculate and pass down in this method. The basic approach
2172 * is that the quad we create contains both the core line area plus a bounding area in which
2173 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2174 * proportion of the width and the length of a given segment is represented by the boundary
2175 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2176 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2177 * on the inside). This ends up giving the result we want, with pixels that are completely
2178 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2179 * how far into the boundary region they are, which is determined by shader interpolation.
2180 */
Chet Haase48659092012-05-31 15:21:51 -07002181status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2182 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002183
2184 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002185 // We use half the stroke width here because we're going to position the quad
2186 // corner vertices half of the width away from the line endpoints
2187 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002188 // A stroke width of 0 has a special meaning in Skia:
2189 // it draws a line 1 px wide regardless of current transform
2190 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002191
Chet Haase99ecdc42011-05-06 12:06:34 -07002192 float inverseScaleX = 1.0f;
2193 float inverseScaleY = 1.0f;
2194 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002195
Chet Haase8a5cc922011-04-26 07:28:09 -07002196 int alpha;
2197 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002198
Chet Haase8a5cc922011-04-26 07:28:09 -07002199 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002200 int verticesCount = count;
2201 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002202 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002203 verticesCount += (count - 4);
2204 }
2205
2206 if (isHairLine || isAA) {
2207 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2208 // the line on the screen should always be one pixel wide regardless of scale. For
2209 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002210 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002211 Matrix4 *mat = mSnapshot->transform;
2212 float m00 = mat->data[Matrix4::kScaleX];
2213 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002214 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002215 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002216
Romain Guy211370f2012-02-01 16:10:55 -08002217 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2218 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002219
Chet Haase99ecdc42011-05-06 12:06:34 -07002220 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2221 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002222
Chet Haase99ecdc42011-05-06 12:06:34 -07002223 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2224 scaled = true;
2225 }
2226 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002227 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002228
2229 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002230
2231 mCaches.enableScissor();
2232
Chet Haase8a5cc922011-04-26 07:28:09 -07002233 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002234 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002235 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002236 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002237 }
2238 setupDrawColor(paint->getColor(), alpha);
2239 setupDrawColorFilter();
2240 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002241 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002242 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002243 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002244 setupDrawColorUniforms();
2245 setupDrawColorFilterUniforms();
2246 setupDrawShaderIdentityUniforms();
2247
2248 if (isHairLine) {
2249 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002250 halfStrokeWidth = isAA? 1 : .5;
2251 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002252 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002253 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002254 }
Romain Guy7b631422012-04-04 11:38:54 -07002255
2256 int widthSlot;
2257 int lengthSlot;
2258
Chet Haase5b0200b2011-04-13 17:58:08 -07002259 Vertex lines[verticesCount];
2260 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002261
Chet Haase99585ad2011-05-02 15:00:16 -07002262 AAVertex wLines[verticesCount];
2263 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002264
Romain Guy211370f2012-02-01 16:10:55 -08002265 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002266 setupDrawVertices(vertices);
2267 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002268 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2269 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002270 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002271 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2272 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002273 // We will need to calculate the actual width proportion on each segment for
2274 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002275 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002276 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2277 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002278 }
2279
Chet Haase99ecdc42011-05-06 12:06:34 -07002280 AAVertex* prevAAVertex = NULL;
2281 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002282
Chet Haase99585ad2011-05-02 15:00:16 -07002283 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002284 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002285
Chet Haase5b0200b2011-04-13 17:58:08 -07002286 for (int i = 0; i < count; i += 4) {
2287 // a = start point, b = end point
2288 vec2 a(points[i], points[i + 1]);
2289 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002290
Chet Haase99585ad2011-05-02 15:00:16 -07002291 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002292 float boundaryLengthProportion = 0;
2293 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002294
Chet Haase5b0200b2011-04-13 17:58:08 -07002295 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002296 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002297 float x = n.x;
2298 n.x = -n.y;
2299 n.y = x;
2300
Chet Haase8a5cc922011-04-26 07:28:09 -07002301 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002302 if (isAA) {
2303 float wideningFactor;
2304 if (fabs(n.x) >= fabs(n.y)) {
2305 wideningFactor = fabs(1.0f / n.x);
2306 } else {
2307 wideningFactor = fabs(1.0f / n.y);
2308 }
2309 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002310 }
Romain Guy7b631422012-04-04 11:38:54 -07002311
Chet Haase99ecdc42011-05-06 12:06:34 -07002312 if (scaled) {
2313 n.x *= inverseScaleX;
2314 n.y *= inverseScaleY;
2315 }
2316 } else if (scaled) {
2317 // Extend n by .5 pixel on each side, post-transform
2318 vec2 extendedN = n.copyNormalized();
2319 extendedN /= 2;
2320 extendedN.x *= inverseScaleX;
2321 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002322
Chet Haase99ecdc42011-05-06 12:06:34 -07002323 float extendedNLength = extendedN.length();
2324 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002325 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002326 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002327 }
Romain Guy7b631422012-04-04 11:38:54 -07002328
Chet Haase99585ad2011-05-02 15:00:16 -07002329 // aa lines expand the endpoint vertices to encompass the AA boundary
2330 if (isAA) {
2331 vec2 abVector = (b - a);
2332 length = abVector.length();
2333 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002334
Chet Haase99ecdc42011-05-06 12:06:34 -07002335 if (scaled) {
2336 abVector.x *= inverseScaleX;
2337 abVector.y *= inverseScaleY;
2338 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002339 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002340 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002341 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002342 }
Romain Guy7b631422012-04-04 11:38:54 -07002343
Chet Haase99ecdc42011-05-06 12:06:34 -07002344 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002345 a -= abVector;
2346 b += abVector;
2347 }
2348
Chet Haase5b0200b2011-04-13 17:58:08 -07002349 // Four corners of the rectangle defining a thick line
2350 vec2 p1 = a - n;
2351 vec2 p2 = a + n;
2352 vec2 p3 = b + n;
2353 vec2 p4 = b - n;
2354
Chet Haase99585ad2011-05-02 15:00:16 -07002355
Chet Haase5b0200b2011-04-13 17:58:08 -07002356 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2357 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2358 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2359 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2360
Romain Guy04299382012-07-18 17:15:41 -07002361 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002362 if (!isAA) {
2363 if (prevVertex != NULL) {
2364 // Issue two repeat vertices to create degenerate triangles to bridge
2365 // between the previous line and the new one. This is necessary because
2366 // we are creating a single triangle_strip which will contain
2367 // potentially discontinuous line segments.
2368 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2369 Vertex::set(vertices++, p1.x, p1.y);
2370 generatedVerticesCount += 2;
2371 }
Romain Guy7b631422012-04-04 11:38:54 -07002372
Chet Haase5b0200b2011-04-13 17:58:08 -07002373 Vertex::set(vertices++, p1.x, p1.y);
2374 Vertex::set(vertices++, p2.x, p2.y);
2375 Vertex::set(vertices++, p4.x, p4.y);
2376 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002377
Chet Haase5b0200b2011-04-13 17:58:08 -07002378 prevVertex = vertices - 1;
2379 generatedVerticesCount += 4;
2380 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002381 if (!isHairLine && scaled) {
2382 // Must set width proportions per-segment for scaled non-hairlines to use the
2383 // correct AA boundary dimensions
2384 if (boundaryWidthSlot < 0) {
2385 boundaryWidthSlot =
2386 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002387 }
Romain Guy7b631422012-04-04 11:38:54 -07002388
Chet Haase99ecdc42011-05-06 12:06:34 -07002389 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002390 }
Romain Guy7b631422012-04-04 11:38:54 -07002391
Chet Haase99585ad2011-05-02 15:00:16 -07002392 if (boundaryLengthSlot < 0) {
2393 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002394 }
Romain Guy7b631422012-04-04 11:38:54 -07002395
Chet Haase99ecdc42011-05-06 12:06:34 -07002396 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002397
Chet Haase5b0200b2011-04-13 17:58:08 -07002398 if (prevAAVertex != NULL) {
2399 // Issue two repeat vertices to create degenerate triangles to bridge
2400 // between the previous line and the new one. This is necessary because
2401 // we are creating a single triangle_strip which will contain
2402 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002403 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2404 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2405 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002406 generatedVerticesCount += 2;
2407 }
Romain Guy7b631422012-04-04 11:38:54 -07002408
Chet Haase99585ad2011-05-02 15:00:16 -07002409 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2410 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2411 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2412 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002413
Chet Haase5b0200b2011-04-13 17:58:08 -07002414 prevAAVertex = aaVertices - 1;
2415 generatedVerticesCount += 4;
2416 }
Romain Guy7b631422012-04-04 11:38:54 -07002417
Chet Haase99585ad2011-05-02 15:00:16 -07002418 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2419 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2420 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002421 }
2422 }
Romain Guy7b631422012-04-04 11:38:54 -07002423
Chet Haase5b0200b2011-04-13 17:58:08 -07002424 if (generatedVerticesCount > 0) {
2425 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2426 }
Romain Guy7b631422012-04-04 11:38:54 -07002427
2428 if (isAA) {
2429 finishDrawAALine(widthSlot, lengthSlot);
2430 }
Chet Haase48659092012-05-31 15:21:51 -07002431
2432 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002433}
2434
Chet Haase48659092012-05-31 15:21:51 -07002435status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2436 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002437
2438 // TODO: The paint's cap style defines whether the points are square or circular
2439 // TODO: Handle AA for round points
2440
Chet Haase5b0200b2011-04-13 17:58:08 -07002441 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002442 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002443 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002444 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002445 if (isHairLine) {
2446 // Now that we know it's hairline, we can set the effective width, to be used later
2447 strokeWidth = 1.0f;
2448 }
2449 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002450 int alpha;
2451 SkXfermode::Mode mode;
2452 getAlphaAndMode(paint, &alpha, &mode);
2453
2454 int verticesCount = count >> 1;
2455 int generatedVerticesCount = 0;
2456
2457 TextureVertex pointsData[verticesCount];
2458 TextureVertex* vertex = &pointsData[0];
2459
Romain Guy04299382012-07-18 17:15:41 -07002460 // TODO: We should optimize this method to not generate vertices for points
2461 // that lie outside of the clip.
2462 mCaches.enableScissor();
2463
Romain Guyed6fcb02011-03-21 13:11:28 -07002464 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002465 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002466 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002467 setupDrawColor(paint->getColor(), alpha);
2468 setupDrawColorFilter();
2469 setupDrawShader();
2470 setupDrawBlending(mode);
2471 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002472 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002473 setupDrawColorUniforms();
2474 setupDrawColorFilterUniforms();
2475 setupDrawPointUniforms();
2476 setupDrawShaderIdentityUniforms();
2477 setupDrawMesh(vertex);
2478
2479 for (int i = 0; i < count; i += 2) {
2480 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2481 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002482
Chet Haase8a5cc922011-04-26 07:28:09 -07002483 float left = points[i] - halfWidth;
2484 float right = points[i] + halfWidth;
2485 float top = points[i + 1] - halfWidth;
2486 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002487
Chet Haase8a5cc922011-04-26 07:28:09 -07002488 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002489 }
2490
2491 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002492
2493 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002494}
2495
Chet Haase48659092012-05-31 15:21:51 -07002496status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002497 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002498 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002499
Romain Guyae88e5e2010-10-22 17:49:18 -07002500 Rect& clip(*mSnapshot->clipRect);
2501 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002502
Romain Guy3d58c032010-07-14 16:34:53 -07002503 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002504
2505 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002506}
Romain Guy9d5316e2010-06-24 19:30:36 -07002507
Chet Haase48659092012-05-31 15:21:51 -07002508status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2509 SkPaint* paint) {
2510 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002511 const AutoTexture autoCleanup(texture);
2512
2513 const float x = left + texture->left - texture->offset;
2514 const float y = top + texture->top - texture->offset;
2515
2516 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002517
2518 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002519}
2520
Chet Haase48659092012-05-31 15:21:51 -07002521status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002522 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002524 return DrawGlInfo::kStatusDone;
2525 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002526
Chris Craikcb4d6002012-09-25 12:00:29 -07002527 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002528 mCaches.activeTexture(0);
2529 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2530 right - left, bottom - top, rx, ry, p);
2531 return drawShape(left, top, texture, p);
2532 }
2533
2534 SkPath path;
2535 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002536 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2537 float outset = p->getStrokeWidth() / 2;
2538 rect.outset(outset, outset);
2539 rx += outset;
2540 ry += outset;
2541 }
Chris Craik710f46d2012-09-17 17:25:49 -07002542 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002543 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002544
2545 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002546}
2547
Chris Craik710f46d2012-09-17 17:25:49 -07002548status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002549 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2550 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002551 return DrawGlInfo::kStatusDone;
2552 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002553 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002554 mCaches.activeTexture(0);
2555 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2556 return drawShape(x - radius, y - radius, texture, p);
2557 }
2558
2559 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002560 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2561 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2562 } else {
2563 path.addCircle(x, y, radius);
2564 }
2565 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002566
2567 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002568}
Romain Guy01d58e42011-01-19 21:54:02 -08002569
Chet Haase48659092012-05-31 15:21:51 -07002570status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002571 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002572 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002573 return DrawGlInfo::kStatusDone;
2574 }
Romain Guy01d58e42011-01-19 21:54:02 -08002575
Chris Craikcb4d6002012-09-25 12:00:29 -07002576 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002577 mCaches.activeTexture(0);
2578 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2579 return drawShape(left, top, texture, p);
2580 }
2581
2582 SkPath path;
2583 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002584 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2585 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2586 }
Chris Craik710f46d2012-09-17 17:25:49 -07002587 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002588 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002589
2590 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002591}
2592
Chet Haase48659092012-05-31 15:21:51 -07002593status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002594 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2595 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2596 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002597 }
2598
Chris Craik780c1282012-10-04 14:10:49 -07002599 if (fabs(sweepAngle) >= 360.0f) {
2600 return drawOval(left, top, right, bottom, p);
2601 }
2602
2603 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002604 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2605 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002606 mCaches.activeTexture(0);
2607 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2608 startAngle, sweepAngle, useCenter, p);
2609 return drawShape(left, top, texture, p);
2610 }
2611
2612 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2613 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2614 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2615 }
2616
2617 SkPath path;
2618 if (useCenter) {
2619 path.moveTo(rect.centerX(), rect.centerY());
2620 }
2621 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2622 if (useCenter) {
2623 path.close();
2624 }
2625 drawConvexPath(path, p);
2626
2627 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002628}
2629
Romain Guycf8675e2012-10-02 12:32:25 -07002630// See SkPaintDefaults.h
2631#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2632
Chet Haase48659092012-05-31 15:21:51 -07002633status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002634 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002635 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002636 }
2637
Chris Craik710f46d2012-09-17 17:25:49 -07002638 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002639 // only fill style is supported by drawConvexPath, since others have to handle joins
2640 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2641 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2642 mCaches.activeTexture(0);
2643 const PathTexture* texture =
2644 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2645 return drawShape(left, top, texture, p);
2646 }
2647
2648 SkPath path;
2649 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2650 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2651 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2652 }
2653 path.addRect(rect);
2654 drawConvexPath(path, p);
2655
2656 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002657 }
2658
Romain Guy181d0a62011-06-09 18:52:38 -07002659 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002660 SkPath path;
2661 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002662 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002663 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002664 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002665 }
Chet Haase48659092012-05-31 15:21:51 -07002666
2667 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002668}
Romain Guy9d5316e2010-06-24 19:30:36 -07002669
Raph Levien416a8472012-07-19 22:48:17 -07002670void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2671 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2672 float x, float y) {
2673 mCaches.activeTexture(0);
2674
2675 // NOTE: The drop shadow will not perform gamma correction
2676 // if shader-based correction is enabled
2677 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2678 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2679 paint, text, bytesCount, count, mShadowRadius, positions);
2680 const AutoTexture autoCleanup(shadow);
2681
2682 const float sx = x - shadow->left + mShadowDx;
2683 const float sy = y - shadow->top + mShadowDy;
2684
2685 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2686 int shadowColor = mShadowColor;
2687 if (mShader) {
2688 shadowColor = 0xffffffff;
2689 }
2690
2691 setupDraw();
2692 setupDrawWithTexture(true);
2693 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2694 setupDrawColorFilter();
2695 setupDrawShader();
2696 setupDrawBlending(true, mode);
2697 setupDrawProgram();
2698 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2699 setupDrawTexture(shadow->id);
2700 setupDrawPureColorUniforms();
2701 setupDrawColorFilterUniforms();
2702 setupDrawShaderUniforms();
2703 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2704
2705 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2706}
2707
Chet Haase48659092012-05-31 15:21:51 -07002708status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002709 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002710 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002711 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002712 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002713 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002714
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 // NOTE: Skia does not support perspective transform on drawPosText yet
2716 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002717 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002718 }
2719
2720 float x = 0.0f;
2721 float y = 0.0f;
2722 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2723 if (pureTranslate) {
2724 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2725 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2726 }
2727
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002728 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002729 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002730
2731 int alpha;
2732 SkXfermode::Mode mode;
2733 getAlphaAndMode(paint, &alpha, &mode);
2734
Raph Levien416a8472012-07-19 22:48:17 -07002735 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002736 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2737 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002738 }
2739
Romain Guy671d6cf2012-01-18 12:39:17 -08002740 // Pick the appropriate texture filtering
2741 bool linearFilter = mSnapshot->transform->changesBounds();
2742 if (pureTranslate && !linearFilter) {
2743 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2744 }
2745
2746 mCaches.activeTexture(0);
2747 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002748 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 setupDrawDirtyRegionsDisabled();
2750 setupDrawWithTexture(true);
2751 setupDrawAlpha8Color(paint->getColor(), alpha);
2752 setupDrawColorFilter();
2753 setupDrawShader();
2754 setupDrawBlending(true, mode);
2755 setupDrawProgram();
2756 setupDrawModelView(x, y, x, y, pureTranslate, true);
2757 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2758 setupDrawPureColorUniforms();
2759 setupDrawColorFilterUniforms();
2760 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002761 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002762
2763 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2764 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2765
Romain Guy211370f2012-02-01 16:10:55 -08002766 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002767
2768 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2769 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002770 if (hasActiveLayer) {
2771 if (!pureTranslate) {
2772 mSnapshot->transform->mapRect(bounds);
2773 }
2774 dirtyLayerUnchecked(bounds, getRegion());
2775 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002776 }
Chet Haase48659092012-05-31 15:21:51 -07002777
2778 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002779}
2780
Romain Guyc2525952012-07-27 16:41:22 -07002781status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002782 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002783 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002784 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002785 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002786 }
2787
Chet Haasea1cff502012-02-21 13:43:44 -08002788 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002789 switch (paint->getTextAlign()) {
2790 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002791 x -= length / 2.0f;
2792 break;
2793 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002794 x -= length;
2795 break;
2796 default:
2797 break;
2798 }
2799
Romain Guycac5fd32011-12-01 20:08:50 -08002800 SkPaint::FontMetrics metrics;
2801 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002802 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002803 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002804 }
2805
Romain Guye3a9b242013-01-08 11:15:30 -08002806#if DEBUG_GLYPHS
2807 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2808 SkTypeface::UniqueID(paint->getTypeface()));
2809#endif
2810
2811 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2812 fontRenderer.setFont(paint, *mSnapshot->transform);
2813
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002814 const float oldX = x;
2815 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002816 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002817 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002818 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2819 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2820 }
2821
Romain Guy86568192010-12-14 15:55:39 -08002822 int alpha;
2823 SkXfermode::Mode mode;
2824 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002825
Romain Guy211370f2012-02-01 16:10:55 -08002826 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002827 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2828 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002829 }
2830
Romain Guy6620c6d2010-12-06 18:07:02 -08002831 // Pick the appropriate texture filtering
2832 bool linearFilter = mSnapshot->transform->changesBounds();
2833 if (pureTranslate && !linearFilter) {
2834 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2835 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002836
Romain Guy16c88082012-06-11 16:03:47 -07002837 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002838 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002839 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002840 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002841 setupDrawDirtyRegionsDisabled();
2842 setupDrawWithTexture(true);
2843 setupDrawAlpha8Color(paint->getColor(), alpha);
2844 setupDrawColorFilter();
2845 setupDrawShader();
2846 setupDrawBlending(true, mode);
2847 setupDrawProgram();
2848 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002849 // See comment above; the font renderer must use texture unit 0
2850 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002851 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2852 setupDrawPureColorUniforms();
2853 setupDrawColorFilterUniforms();
2854 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002855 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002856
Romain Guya3dc55f2012-09-28 13:55:44 -07002857 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2858 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002859 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2860
Romain Guy211370f2012-02-01 16:10:55 -08002861 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002862
Raph Levien996e57c2012-07-23 15:22:52 -07002863 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002864 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002865 SkPaint paintCopy(*paint);
2866 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2867 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002868 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002869 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002870 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002871 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002872 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002873
2874 if (status && hasActiveLayer) {
2875 if (!pureTranslate) {
2876 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002877 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002878 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002879 }
Romain Guy694b5192010-07-21 21:33:20 -07002880
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002881 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002882
2883 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002884}
2885
Chet Haase48659092012-05-31 15:21:51 -07002886status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002887 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002888 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2889 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002890 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002891 }
2892
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002893 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002894 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002895
2896 int alpha;
2897 SkXfermode::Mode mode;
2898 getAlphaAndMode(paint, &alpha, &mode);
2899
2900 mCaches.activeTexture(0);
2901 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002902 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002903 setupDrawDirtyRegionsDisabled();
2904 setupDrawWithTexture(true);
2905 setupDrawAlpha8Color(paint->getColor(), alpha);
2906 setupDrawColorFilter();
2907 setupDrawShader();
2908 setupDrawBlending(true, mode);
2909 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002910 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002911 setupDrawTexture(fontRenderer.getTexture(true));
2912 setupDrawPureColorUniforms();
2913 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002914 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002915 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002916
Romain Guy97771732012-02-28 18:17:02 -08002917 const Rect* clip = &mSnapshot->getLocalClip();
2918 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy03d58522012-02-24 17:54:07 -08002919
Romain Guy97771732012-02-28 18:17:02 -08002920 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002921
2922 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2923 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002924 if (hasActiveLayer) {
2925 mSnapshot->transform->mapRect(bounds);
2926 dirtyLayerUnchecked(bounds, getRegion());
2927 }
Romain Guy97771732012-02-28 18:17:02 -08002928 }
Chet Haase48659092012-05-31 15:21:51 -07002929
2930 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002931}
2932
Chet Haase48659092012-05-31 15:21:51 -07002933status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2934 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002935
Romain Guya1d3c912011-12-13 14:55:06 -08002936 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002937
Romain Guyfb8b7632010-08-23 21:05:08 -07002938 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002939 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002940 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002941
Romain Guy8b55f372010-08-18 17:10:07 -07002942 const float x = texture->left - texture->offset;
2943 const float y = texture->top - texture->offset;
2944
Romain Guy01d58e42011-01-19 21:54:02 -08002945 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002946
2947 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002948}
2949
Chet Haase48659092012-05-31 15:21:51 -07002950status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002951 if (!layer) {
2952 return DrawGlInfo::kStatusDone;
2953 }
2954
Romain Guyb2e2f242012-10-17 18:18:35 -07002955 mat4* transform = NULL;
2956 if (layer->isTextureLayer()) {
2957 transform = &layer->getTransform();
2958 if (!transform->isIdentity()) {
2959 save(0);
2960 mSnapshot->transform->multiply(*transform);
2961 }
2962 }
2963
Romain Guy35643dd2012-09-18 15:40:58 -07002964 Rect transformed;
2965 Rect clip;
2966 const bool rejected = quickRejectNoScissor(x, y,
2967 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2968
2969 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002970 if (transform && !transform->isIdentity()) {
2971 restore();
2972 }
Chet Haase48659092012-05-31 15:21:51 -07002973 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002974 }
2975
Romain Guy5bb3c732012-11-29 17:52:58 -08002976 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002977
Romain Guy87e2f7572012-09-24 11:37:12 -07002978 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002979 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002980
Romain Guy211370f2012-02-01 16:10:55 -08002981 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002982 SkiaColorFilter* oldFilter = mColorFilter;
2983 mColorFilter = layer->getColorFilter();
2984
Romain Guyc88e3572011-01-22 00:32:12 -08002985 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002986 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002987 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002988 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002989 setupDraw();
2990 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002991 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002992 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002993 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002994 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002995 setupDrawPureColorUniforms();
2996 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002997 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002998 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002999 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
3000 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003001
Romain Guyd21b6e12011-11-30 20:21:23 -08003002 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003003 setupDrawModelViewTranslate(tx, ty,
3004 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003005 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003006 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003007 setupDrawModelViewTranslate(x, y,
3008 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3009 }
Romain Guyc88e3572011-01-22 00:32:12 -08003010 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003011
Romain Guyc88e3572011-01-22 00:32:12 -08003012 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3013 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003014
Romain Guyc88e3572011-01-22 00:32:12 -08003015 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003016
3017#if DEBUG_LAYERS_AS_REGIONS
3018 drawRegionRects(layer->region);
3019#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003020 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003021
Romain Guye529ece2012-09-26 11:23:17 -07003022 mColorFilter = oldFilter;
3023
Romain Guy5bb3c732012-11-29 17:52:58 -08003024 if (layer->debugDrawUpdate) {
3025 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003026 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3027 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3028 }
Romain Guyf219da52011-01-16 12:54:25 -08003029 }
Chet Haase48659092012-05-31 15:21:51 -07003030
Romain Guyb2e2f242012-10-17 18:18:35 -07003031 if (transform && !transform->isIdentity()) {
3032 restore();
3033 }
3034
Chet Haase48659092012-05-31 15:21:51 -07003035 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003036}
3037
Romain Guy6926c722010-07-12 20:20:03 -07003038///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003039// Shaders
3040///////////////////////////////////////////////////////////////////////////////
3041
3042void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07003043 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003044}
3045
Romain Guy06f96e22010-07-30 19:18:16 -07003046void OpenGLRenderer::setupShader(SkiaShader* shader) {
3047 mShader = shader;
3048 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003049 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003050 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003051}
3052
Romain Guyd27977d2010-07-14 19:18:51 -07003053///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003054// Color filters
3055///////////////////////////////////////////////////////////////////////////////
3056
3057void OpenGLRenderer::resetColorFilter() {
3058 mColorFilter = NULL;
3059}
3060
3061void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
3062 mColorFilter = filter;
3063}
3064
3065///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003066// Drop shadow
3067///////////////////////////////////////////////////////////////////////////////
3068
3069void OpenGLRenderer::resetShadow() {
3070 mHasShadow = false;
3071}
3072
3073void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
3074 mHasShadow = true;
3075 mShadowRadius = radius;
3076 mShadowDx = dx;
3077 mShadowDy = dy;
3078 mShadowColor = color;
3079}
3080
3081///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003082// Draw filters
3083///////////////////////////////////////////////////////////////////////////////
3084
3085void OpenGLRenderer::resetPaintFilter() {
3086 mHasDrawFilter = false;
3087}
3088
3089void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
3090 mHasDrawFilter = true;
3091 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3092 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
3093}
3094
3095SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08003096 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08003097
3098 uint32_t flags = paint->getFlags();
3099
3100 mFilteredPaint = *paint;
3101 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
3102
3103 return &mFilteredPaint;
3104}
3105
3106///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003107// Drawing implementation
3108///////////////////////////////////////////////////////////////////////////////
3109
Romain Guy01d58e42011-01-19 21:54:02 -08003110void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3111 float x, float y, SkPaint* paint) {
3112 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3113 return;
3114 }
3115
3116 int alpha;
3117 SkXfermode::Mode mode;
3118 getAlphaAndMode(paint, &alpha, &mode);
3119
3120 setupDraw();
3121 setupDrawWithTexture(true);
3122 setupDrawAlpha8Color(paint->getColor(), alpha);
3123 setupDrawColorFilter();
3124 setupDrawShader();
3125 setupDrawBlending(true, mode);
3126 setupDrawProgram();
3127 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3128 setupDrawTexture(texture->id);
3129 setupDrawPureColorUniforms();
3130 setupDrawColorFilterUniforms();
3131 setupDrawShaderUniforms();
3132 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3133
3134 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3135
3136 finishDrawTexture();
3137}
3138
Romain Guyf607bdc2010-09-10 19:20:06 -07003139// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003140#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3141#define kStdUnderline_Offset (1.0f / 9.0f)
3142#define kStdUnderline_Thickness (1.0f / 18.0f)
3143
3144void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3145 float x, float y, SkPaint* paint) {
3146 // Handle underline and strike-through
3147 uint32_t flags = paint->getFlags();
3148 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003149 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003150 float underlineWidth = length;
3151 // If length is > 0.0f, we already measured the text for the text alignment
3152 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003153 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003154 }
3155
Romain Guy211370f2012-02-01 16:10:55 -08003156 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003157 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003158 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003159
Raph Levien8b4072d2012-07-30 15:50:00 -07003160 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003161 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003162
Romain Guyf6834472011-01-23 13:32:12 -08003163 int linesCount = 0;
3164 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3165 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3166
3167 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003168 float points[pointsCount];
3169 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003170
3171 if (flags & SkPaint::kUnderlineText_Flag) {
3172 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003173 points[currentPoint++] = left;
3174 points[currentPoint++] = top;
3175 points[currentPoint++] = left + underlineWidth;
3176 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003177 }
3178
3179 if (flags & SkPaint::kStrikeThruText_Flag) {
3180 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003181 points[currentPoint++] = left;
3182 points[currentPoint++] = top;
3183 points[currentPoint++] = left + underlineWidth;
3184 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003185 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003186
Romain Guy726aeba2011-06-01 14:52:00 -07003187 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003188
Romain Guy726aeba2011-06-01 14:52:00 -07003189 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003190 }
3191 }
3192}
3193
Romain Guy672433d2013-01-04 19:05:13 -08003194status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3195 if (mSnapshot->isIgnored()) {
3196 return DrawGlInfo::kStatusDone;
3197 }
3198
Romain Guy735738c2012-12-03 12:34:51 -08003199 int color = paint->getColor();
3200 // If a shader is set, preserve only the alpha
3201 if (mShader) {
3202 color |= 0x00ffffff;
3203 }
3204 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3205
3206 return drawColorRects(rects, count, color, mode);
3207}
3208
3209status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy8ce00302013-01-15 18:51:42 -08003210 SkXfermode::Mode mode, bool ignoreTransform, bool dirty) {
Romain Guy735738c2012-12-03 12:34:51 -08003211
Romain Guy672433d2013-01-04 19:05:13 -08003212 float left = FLT_MAX;
3213 float top = FLT_MAX;
3214 float right = FLT_MIN;
3215 float bottom = FLT_MIN;
3216
3217 int vertexCount = 0;
3218 Vertex mesh[count * 6];
3219 Vertex* vertex = mesh;
3220
Chris Craik2af46352012-11-26 18:30:17 -08003221 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003222 float l = rects[index + 0];
3223 float t = rects[index + 1];
3224 float r = rects[index + 2];
3225 float b = rects[index + 3];
3226
Romain Guy8ce00302013-01-15 18:51:42 -08003227 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003228 Vertex::set(vertex++, l, b);
3229 Vertex::set(vertex++, l, t);
3230 Vertex::set(vertex++, r, t);
3231 Vertex::set(vertex++, l, b);
3232 Vertex::set(vertex++, r, t);
3233 Vertex::set(vertex++, r, b);
3234
3235 vertexCount += 6;
3236
3237 left = fminf(left, l);
3238 top = fminf(top, t);
3239 right = fmaxf(right, r);
3240 bottom = fmaxf(bottom, b);
3241 }
3242 }
3243
Romain Guya362c692013-02-04 13:50:16 -08003244 if (count == 0 || quickReject(left, top, right, bottom)) {
3245 return DrawGlInfo::kStatusDone;
3246 }
Romain Guy672433d2013-01-04 19:05:13 -08003247
Romain Guy672433d2013-01-04 19:05:13 -08003248 setupDraw();
3249 setupDrawNoTexture();
3250 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3251 setupDrawShader();
3252 setupDrawColorFilter();
3253 setupDrawBlending(mode);
3254 setupDrawProgram();
3255 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003256 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003257 setupDrawColorUniforms();
3258 setupDrawShaderUniforms();
3259 setupDrawColorFilterUniforms();
3260 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3261
Romain Guy8ce00302013-01-15 18:51:42 -08003262 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003263 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3264 }
3265
3266 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3267
3268 return DrawGlInfo::kStatusDrew;
3269}
3270
Romain Guy026c5e162010-06-28 17:12:22 -07003271void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003272 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003273 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003274 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003275 color |= 0x00ffffff;
3276 }
3277
Romain Guy70ca14e2010-12-13 18:24:33 -08003278 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003279 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003280 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003281 setupDrawShader();
3282 setupDrawColorFilter();
3283 setupDrawBlending(mode);
3284 setupDrawProgram();
3285 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3286 setupDrawColorUniforms();
3287 setupDrawShaderUniforms(ignoreTransform);
3288 setupDrawColorFilterUniforms();
3289 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003290
Romain Guyc95c8d62010-09-17 15:31:32 -07003291 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3292}
3293
Romain Guy82ba8142010-07-09 13:25:56 -07003294void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003295 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003296 int alpha;
3297 SkXfermode::Mode mode;
3298 getAlphaAndMode(paint, &alpha, &mode);
3299
Romain Guyd21b6e12011-11-30 20:21:23 -08003300 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003301
Romain Guy211370f2012-02-01 16:10:55 -08003302 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003303 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3304 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3305
Romain Guyd21b6e12011-11-30 20:21:23 -08003306 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003307 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3308 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3309 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3310 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003311 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003312 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3313 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3314 GL_TRIANGLE_STRIP, gMeshCount);
3315 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003316}
3317
Romain Guybd6b79b2010-06-26 00:13:53 -07003318void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003319 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3320 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003321 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003322}
3323
3324void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003325 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003326 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003327 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003328
Romain Guy746b7402010-10-26 16:27:31 -07003329 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawWithTexture();
3331 setupDrawColor(alpha, alpha, alpha, alpha);
3332 setupDrawColorFilter();
3333 setupDrawBlending(blend, mode, swapSrcDst);
3334 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003335 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003336 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003337 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003338 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003339 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003340 }
Romain Guy886b2752013-01-04 12:26:18 -08003341 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003342 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003343 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003344 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003345
Romain Guy6820ac82010-09-15 18:11:50 -07003346 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003347
3348 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003349}
3350
Romain Guy886b2752013-01-04 12:26:18 -08003351void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3352 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3353 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3354 bool ignoreTransform, bool dirty) {
3355
3356 setupDraw();
3357 setupDrawWithTexture(true);
3358 if (hasColor) {
3359 setupDrawAlpha8Color(color, alpha);
3360 }
3361 setupDrawColorFilter();
3362 setupDrawShader();
3363 setupDrawBlending(true, mode);
3364 setupDrawProgram();
3365 if (!dirty) setupDrawDirtyRegionsDisabled();
3366 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3367 setupDrawTexture(texture);
3368 setupDrawPureColorUniforms();
3369 setupDrawColorFilterUniforms();
3370 setupDrawShaderUniforms();
3371 setupDrawMesh(vertices, texCoords);
3372
3373 glDrawArrays(drawMode, 0, elementsCount);
3374
3375 finishDrawTexture();
3376}
3377
Romain Guya5aed0d2010-09-09 14:42:43 -07003378void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003379 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003380 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003381
Romain Guy82ba8142010-07-09 13:25:56 -07003382 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003383 // These blend modes are not supported by OpenGL directly and have
3384 // to be implemented using shaders. Since the shader will perform
3385 // the blending, turn blending off here
3386 // If the blend mode cannot be implemented using shaders, fall
3387 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003388 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003389 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003390 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003391 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003392
Romain Guy82bc7a72012-01-03 14:13:39 -08003393 if (mCaches.blend) {
3394 glDisable(GL_BLEND);
3395 mCaches.blend = false;
3396 }
3397
3398 return;
3399 } else {
3400 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003401 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003402 }
3403
3404 if (!mCaches.blend) {
3405 glEnable(GL_BLEND);
3406 }
3407
3408 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3409 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3410
3411 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3412 glBlendFunc(sourceMode, destMode);
3413 mCaches.lastSrcMode = sourceMode;
3414 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003415 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003416 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003417 glDisable(GL_BLEND);
3418 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003419 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003420}
3421
Romain Guy889f8d12010-07-29 14:37:42 -07003422bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003423 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003424 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003425 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003426 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003427 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003428 }
Romain Guy6926c722010-07-12 20:20:03 -07003429 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003430}
3431
Romain Guy026c5e162010-06-28 17:12:22 -07003432void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003433 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003434 TextureVertex::setUV(v++, u1, v1);
3435 TextureVertex::setUV(v++, u2, v1);
3436 TextureVertex::setUV(v++, u1, v2);
3437 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003438}
3439
Chet Haase5c13d892010-10-08 08:37:55 -07003440void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003441 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003442 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003443}
3444
Romain Guy9d5316e2010-06-24 19:30:36 -07003445}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003446}; // namespace android