blob: 9f0a38b31cfd645f35b8fbf34162f792990a414a [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 Guy49c5fc02012-05-15 11:10:01 -0700145bool OpenGLRenderer::isDeferred() {
146 return false;
147}
148
Romain Guy85bf02f2010-06-22 13:11:24 -0700149void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700150 initViewport(width, height);
151
152 glDisable(GL_DITHER);
153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155 glEnableVertexAttribArray(Program::kBindingPosition);
156}
157
158void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700159 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700160
161 mWidth = width;
162 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700163
164 mFirstSnapshot->height = height;
165 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy7c25aab2012-10-18 15:05:02 -0700168status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700169 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800170}
171
Romain Guyc3fedaf2013-01-29 17:26:25 -0800172status_t OpenGLRenderer::prepareDirty(float left, float top,
173 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800174 mCaches.clearGarbage();
175
Romain Guy8aef54f2010-09-01 15:13:49 -0700176 mSnapshot = new Snapshot(mFirstSnapshot,
177 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800178 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700179 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180
Romain Guy7d7b5492011-01-24 16:33:45 -0800181 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700182 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700183
Romain Guy11cb6422012-09-21 00:39:43 -0700184 updateLayers();
185
Romain Guydcfc8362013-01-03 13:08:57 -0800186 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700187
Romain Guyddf74372012-05-22 14:07:07 -0700188 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800189
Romain Guy54c1a642012-09-27 17:55:46 -0700190 // Functors break the tiling extension in pretty spectacular ways
191 // This ensures we don't use tiling when a functor is going to be
192 // invoked during the frame
193 mSuppressTiling = mCaches.hasRegisteredFunctors();
194
Romain Guy2b7028e2012-09-19 17:25:38 -0700195 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700196 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700197
Romain Guy7c450aa2012-09-21 19:15:00 -0700198 debugOverdraw(true, true);
199
Romain Guy7c25aab2012-10-18 15:05:02 -0700200 return clear(left, top, right, bottom, opaque);
201}
202
Romain Guydcfc8362013-01-03 13:08:57 -0800203void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
204 // If we know that we are going to redraw the entire framebuffer,
205 // perform a discard to let the driver know we don't need to preserve
206 // the back buffer for this frame.
207 if (mCaches.extensions.hasDiscardFramebuffer() &&
208 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800209 const bool isFbo = getTargetFbo() == 0;
210 const GLenum attachments[] = {
211 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
212 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800213 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
214 }
215}
216
Romain Guy7c25aab2012-10-18 15:05:02 -0700217status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700218 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700219 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700220 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700221 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700222 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700223 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700224
Romain Guy7c25aab2012-10-18 15:05:02 -0700225 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700226 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700227}
228
229void OpenGLRenderer::syncState() {
230 glViewport(0, 0, mWidth, mHeight);
231
232 if (mCaches.blend) {
233 glEnable(GL_BLEND);
234 } else {
235 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700236 }
Romain Guybb9524b2010-06-22 18:56:38 -0700237}
238
Romain Guy57b52682012-09-20 17:38:46 -0700239void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700240 if (!mSuppressTiling) {
241 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800242 if (s->flags & Snapshot::kFlagFboTarget) {
243 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700244 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700245
Romain Guyc3fedaf2013-01-29 17:26:25 -0800246 startTiling(*clip, s->height, opaque);
247 }
248}
249
250void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
251 if (!mSuppressTiling) {
252 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
253 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700254 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700255}
256
257void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700258 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700259}
260
Romain Guyb025b9c2010-09-16 14:16:48 -0700261void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700262 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700263 endTiling();
264
Romain Guy11cb6422012-09-21 00:39:43 -0700265 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700266#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700267 GLenum status = GL_NO_ERROR;
268 while ((status = glGetError()) != GL_NO_ERROR) {
269 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
270 switch (status) {
271 case GL_INVALID_ENUM:
272 ALOGE(" GL_INVALID_ENUM");
273 break;
274 case GL_INVALID_VALUE:
275 ALOGE(" GL_INVALID_VALUE");
276 break;
277 case GL_INVALID_OPERATION:
278 ALOGE(" GL_INVALID_OPERATION");
279 break;
280 case GL_OUT_OF_MEMORY:
281 ALOGE(" Out of memory!");
282 break;
283 }
Romain Guya07105b2011-01-10 21:14:18 -0800284 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700285#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700286
Romain Guyc15008e2010-11-10 11:59:15 -0800287#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800288 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700289#else
290 if (mCaches.getDebugLevel() & kDebugMemory) {
291 mCaches.dumpMemoryUsage();
292 }
Romain Guyc15008e2010-11-10 11:59:15 -0800293#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700294 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700295}
296
Romain Guy6c319ca2011-01-11 14:29:25 -0800297void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700298 if (mCaches.currentProgram) {
299 if (mCaches.currentProgram->isInUse()) {
300 mCaches.currentProgram->remove();
301 mCaches.currentProgram = NULL;
302 }
303 }
Romain Guy50c0f092010-10-19 11:42:22 -0700304 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800305 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800306 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800307 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700308 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700309}
310
Romain Guy6c319ca2011-01-11 14:29:25 -0800311void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800312 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800313 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700314 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700315 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700316
Romain Guy3e263fa2011-12-12 16:47:48 -0800317 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
318
Chet Haase80250612012-08-15 13:46:54 -0700319 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700320 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800321 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700322 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700323
Romain Guya1d3c912011-12-13 14:55:06 -0800324 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700325
Romain Guy50c0f092010-10-19 11:42:22 -0700326 mCaches.blend = true;
327 glEnable(GL_BLEND);
328 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
329 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700330}
331
Romain Guy35643dd2012-09-18 15:40:58 -0700332void OpenGLRenderer::resumeAfterLayer() {
333 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
334 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
335 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700336 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700337
338 mCaches.resetScissor();
339 dirtyClip();
340}
341
Romain Guyba6be8a2012-04-23 18:22:09 -0700342void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700343 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700344}
345
346void OpenGLRenderer::attachFunctor(Functor* functor) {
347 mFunctors.add(functor);
348}
349
Romain Guy8f3b8e32012-03-27 16:33:45 -0700350status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
351 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700352 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700353
Romain Guyba6be8a2012-04-23 18:22:09 -0700354 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800355 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700356 SortedVector<Functor*> functors(mFunctors);
357 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700358
Romain Guyba6be8a2012-04-23 18:22:09 -0700359 DrawGlInfo info;
360 info.clipLeft = 0;
361 info.clipTop = 0;
362 info.clipRight = 0;
363 info.clipBottom = 0;
364 info.isLayer = false;
365 info.width = 0;
366 info.height = 0;
367 memset(info.transform, 0, sizeof(float) * 16);
368
369 for (size_t i = 0; i < count; i++) {
370 Functor* f = functors.itemAt(i);
371 result |= (*f)(DrawGlInfo::kModeProcess, &info);
372
Chris Craikc2c95432012-04-25 15:13:52 -0700373 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700374 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
375 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700376 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700377
Chris Craikc2c95432012-04-25 15:13:52 -0700378 if (result & DrawGlInfo::kStatusInvoke) {
379 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700380 }
381 }
Chris Craikd15321b2012-11-28 14:45:04 -0800382 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700383 }
384
385 return result;
386}
387
388status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800389 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700390 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700391
Romain Guy8a4ac612012-07-17 17:32:48 -0700392 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800393 if (mDirtyClip) {
394 setScissorFromClip();
395 }
Romain Guyd643bb52011-03-01 14:55:21 -0800396
Romain Guy80911b82011-03-16 15:30:12 -0700397 Rect clip(*mSnapshot->clipRect);
398 clip.snapToPixelBoundaries();
399
Romain Guyd643bb52011-03-01 14:55:21 -0800400 // Since we don't know what the functor will draw, let's dirty
401 // tne entire clip region
402 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800403 dirtyLayerUnchecked(clip, getRegion());
404 }
Romain Guyd643bb52011-03-01 14:55:21 -0800405
Romain Guy08aa2cb2011-03-17 11:06:57 -0700406 DrawGlInfo info;
407 info.clipLeft = clip.left;
408 info.clipTop = clip.top;
409 info.clipRight = clip.right;
410 info.clipBottom = clip.bottom;
411 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700412 info.width = getSnapshot()->viewport.getWidth();
413 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700414 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700415
Chet Haase48659092012-05-31 15:21:51 -0700416 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800417
Romain Guy8f3b8e32012-03-27 16:33:45 -0700418 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700419 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800420 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700421
Chris Craik65924a32012-04-05 17:52:11 -0700422 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700423 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700424 }
Romain Guycabfcc12011-03-07 18:06:46 -0800425 }
426
Chet Haasedaf98e92011-01-10 14:10:36 -0800427 resume();
Romain Guy65549432012-03-26 16:45:05 -0700428 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800429}
430
Romain Guyf6a11b82010-06-23 17:47:49 -0700431///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700432// Debug
433///////////////////////////////////////////////////////////////////////////////
434
435void OpenGLRenderer::startMark(const char* name) const {
436 mCaches.startMark(0, name);
437}
438
439void OpenGLRenderer::endMark() const {
440 mCaches.endMark();
441}
442
443void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
444 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
445 if (clear) {
446 mCaches.disableScissor();
447 mCaches.stencil.clear();
448 }
449 if (enable) {
450 mCaches.stencil.enableDebugWrite();
451 } else {
452 mCaches.stencil.disable();
453 }
454 }
455}
456
457void OpenGLRenderer::renderOverdraw() {
458 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
459 const Rect* clip = mTilingSnapshot->clipRect;
460
461 mCaches.enableScissor();
462 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
463 clip->right - clip->left, clip->bottom - clip->top);
464
465 mCaches.stencil.enableDebugTest(2);
466 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
467 mCaches.stencil.enableDebugTest(3);
468 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
469 mCaches.stencil.enableDebugTest(4);
470 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
471 mCaches.stencil.enableDebugTest(4, true);
472 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
473 mCaches.stencil.disable();
474 }
475}
476
477///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700478// Layers
479///////////////////////////////////////////////////////////////////////////////
480
481bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
482 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
483 OpenGLRenderer* renderer = layer->renderer;
484 Rect& dirty = layer->dirtyRect;
485
Romain Guy7c450aa2012-09-21 19:15:00 -0700486 if (inFrame) {
487 endTiling();
488 debugOverdraw(false, false);
489 }
Romain Guy11cb6422012-09-21 00:39:43 -0700490
491 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
492 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
493 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
494 renderer->finish();
495
496 if (inFrame) {
497 resumeAfterLayer();
498 startTiling(mSnapshot);
499 }
500
501 dirty.setEmpty();
502 layer->deferredUpdateScheduled = false;
503 layer->renderer = NULL;
504 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800505 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700506
507 return true;
508 }
509
510 return false;
511}
512
513void OpenGLRenderer::updateLayers() {
514 int count = mLayerUpdates.size();
515 if (count > 0) {
516 startMark("Layer Updates");
517
518 // Note: it is very important to update the layers in reverse order
519 for (int i = count - 1; i >= 0; i--) {
520 Layer* layer = mLayerUpdates.itemAt(i);
521 updateLayer(layer, false);
522 mCaches.resourceCache.decrementRefcount(layer);
523 }
524 mLayerUpdates.clear();
525
526 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
527 endMark();
528 }
529}
530
531void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
532 if (layer) {
533 mLayerUpdates.push_back(layer);
534 mCaches.resourceCache.incrementRefcount(layer);
535 }
536}
537
538void OpenGLRenderer::clearLayerUpdates() {
539 size_t count = mLayerUpdates.size();
540 if (count > 0) {
541 mCaches.resourceCache.lock();
542 for (size_t i = 0; i < count; i++) {
543 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
544 }
545 mCaches.resourceCache.unlock();
546 mLayerUpdates.clear();
547 }
548}
549
550///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700551// State management
552///////////////////////////////////////////////////////////////////////////////
553
Romain Guybb9524b2010-06-22 18:56:38 -0700554int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700555 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700556}
557
558int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700559 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700560}
561
562void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700563 if (mSaveCount > 1) {
564 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700565 }
Romain Guybb9524b2010-06-22 18:56:38 -0700566}
567
568void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700569 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700570
Romain Guy8fb95422010-08-17 18:38:51 -0700571 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700572 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700573 }
Romain Guybb9524b2010-06-22 18:56:38 -0700574}
575
Romain Guy8aef54f2010-09-01 15:13:49 -0700576int OpenGLRenderer::saveSnapshot(int flags) {
577 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700578 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700579}
580
581bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700582 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700583 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700584 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700585
Romain Guybd6b79b2010-06-26 00:13:53 -0700586 sp<Snapshot> current = mSnapshot;
587 sp<Snapshot> previous = mSnapshot->previous;
588
Romain Guyeb993562010-10-05 18:14:38 -0700589 if (restoreOrtho) {
590 Rect& r = previous->viewport;
591 glViewport(r.left, r.top, r.right, r.bottom);
592 mOrthoMatrix.load(current->orthoMatrix);
593 }
594
Romain Guy8b55f372010-08-18 17:10:07 -0700595 mSaveCount--;
596 mSnapshot = previous;
597
Romain Guy2542d192010-08-18 11:47:12 -0700598 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700599 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700600 }
Romain Guy2542d192010-08-18 11:47:12 -0700601
Romain Guy5ec99242010-11-03 16:19:08 -0700602 if (restoreLayer) {
603 composeLayer(current, previous);
604 }
605
Romain Guy2542d192010-08-18 11:47:12 -0700606 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700607}
608
Romain Guyf6a11b82010-06-23 17:47:49 -0700609///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700610// Layers
611///////////////////////////////////////////////////////////////////////////////
612
613int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700614 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700615 const GLuint previousFbo = mSnapshot->fbo;
616 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700617
Romain Guyaf636eb2010-12-09 17:47:21 -0800618 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700619 int alpha = 255;
620 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700621
Romain Guye45362c2010-11-03 19:58:32 -0700622 if (p) {
623 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700624 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700625 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700626 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700627 }
Romain Guyd55a8612010-06-28 17:42:46 -0700628
Chet Haased48885a2012-08-28 17:43:28 -0700629 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700630 }
Romain Guyd55a8612010-06-28 17:42:46 -0700631
632 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700633}
634
635int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
636 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700637 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700638 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700639 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700640 SkPaint paint;
641 paint.setAlpha(alpha);
642 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700643 }
Romain Guyd55a8612010-06-28 17:42:46 -0700644}
Romain Guybd6b79b2010-06-26 00:13:53 -0700645
Romain Guy1c740bc2010-09-13 18:00:09 -0700646/**
647 * Layers are viewed by Skia are slightly different than layers in image editing
648 * programs (for instance.) When a layer is created, previously created layers
649 * and the frame buffer still receive every drawing command. For instance, if a
650 * layer is created and a shape intersecting the bounds of the layers and the
651 * framebuffer is draw, the shape will be drawn on both (unless the layer was
652 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
653 *
654 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
655 * texture. Unfortunately, this is inefficient as it requires every primitive to
656 * be drawn n + 1 times, where n is the number of active layers. In practice this
657 * means, for every primitive:
658 * - Switch active frame buffer
659 * - Change viewport, clip and projection matrix
660 * - Issue the drawing
661 *
662 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700663 * To avoid this, layers are implemented in a different way here, at least in the
664 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
665 * is set. When this flag is set we can redirect all drawing operations into a
666 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700667 *
668 * This implementation relies on the frame buffer being at least RGBA 8888. When
669 * a layer is created, only a texture is created, not an FBO. The content of the
670 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700671 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700672 * buffer and drawing continues as normal. This technique therefore treats the
673 * frame buffer as a scratch buffer for the layers.
674 *
675 * To compose the layers back onto the frame buffer, each layer texture
676 * (containing the original frame buffer data) is drawn as a simple quad over
677 * the frame buffer. The trick is that the quad is set as the composition
678 * destination in the blending equation, and the frame buffer becomes the source
679 * of the composition.
680 *
681 * Drawing layers with an alpha value requires an extra step before composition.
682 * An empty quad is drawn over the layer's region in the frame buffer. This quad
683 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
684 * quad is used to multiply the colors in the frame buffer. This is achieved by
685 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
686 * GL_ZERO, GL_SRC_ALPHA.
687 *
688 * Because glCopyTexImage2D() can be slow, an alternative implementation might
689 * be use to draw a single clipped layer. The implementation described above
690 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700691 *
692 * (1) The frame buffer is actually not cleared right away. To allow the GPU
693 * to potentially optimize series of calls to glCopyTexImage2D, the frame
694 * buffer is left untouched until the first drawing operation. Only when
695 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700696 */
Chet Haased48885a2012-08-28 17:43:28 -0700697bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
698 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700699 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700700 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700701
Romain Guyeb993562010-10-05 18:14:38 -0700702 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
703
Romain Guyf607bdc2010-09-10 19:20:06 -0700704 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700705 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700706 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700707 Rect untransformedBounds(bounds);
708 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700709
Chet Haased48885a2012-08-28 17:43:28 -0700710 // Layers only make sense if they are in the framebuffer's bounds
711 if (bounds.intersect(*mSnapshot->clipRect)) {
712 // We cannot work with sub-pixels in this case
713 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700714
Chet Haased48885a2012-08-28 17:43:28 -0700715 // When the layer is not an FBO, we may use glCopyTexImage so we
716 // need to make sure the layer does not extend outside the bounds
717 // of the framebuffer
718 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700719 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700720 } else if (fboLayer) {
721 clip.set(bounds);
722 mat4 inverse;
723 inverse.loadInverse(*mSnapshot->transform);
724 inverse.mapRect(clip);
725 clip.snapToPixelBoundaries();
726 if (clip.intersect(untransformedBounds)) {
727 clip.translate(-left, -top);
728 bounds.set(untransformedBounds);
729 } else {
730 clip.setEmpty();
731 }
Romain Guyad37cd32011-03-15 11:12:25 -0700732 }
Chet Haased48885a2012-08-28 17:43:28 -0700733 } else {
734 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700735 }
Romain Guybf434112010-09-16 14:40:17 -0700736
Romain Guy746b7402010-10-26 16:27:31 -0700737 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700738 bounds.getHeight() > mCaches.maxTextureSize ||
739 (fboLayer && clip.isEmpty())) {
740 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700741 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700742 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700743 }
744
745 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700746 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700747 return false;
748 }
Romain Guyf18fd992010-07-08 11:45:51 -0700749
Romain Guya1d3c912011-12-13 14:55:06 -0800750 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700751 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700752 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700753 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700754 }
755
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700757 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
759 bounds.getWidth() / float(layer->getWidth()), 0.0f);
760 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700761 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700762 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700763
Romain Guy8fb95422010-08-17 18:38:51 -0700764 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700765 mSnapshot->flags |= Snapshot::kFlagIsLayer;
766 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700767
Romain Guyeb993562010-10-05 18:14:38 -0700768 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700769 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700770 } else {
771 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800773 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 if (layer->isEmpty()) {
775 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700776 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700777 layer->getWidth(), layer->getHeight(), 0);
778 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800779 } else {
780 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700781 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800782 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700783
Romain Guy54be1cd2011-06-13 19:04:27 -0700784 // Enqueue the buffer coordinates to clear the corresponding region later
785 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700786 }
Romain Guyeb993562010-10-05 18:14:38 -0700787 }
Romain Guyf86ef572010-07-01 11:05:42 -0700788
Romain Guyd55a8612010-06-28 17:42:46 -0700789 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700790}
791
Chet Haased48885a2012-08-28 17:43:28 -0700792bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800793 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700795
Chet Haased48885a2012-08-28 17:43:28 -0700796 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800797 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
798 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700799 mSnapshot->fbo = layer->getFbo();
800 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
801 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
802 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
803 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700804 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700805
Romain Guy2b7028e2012-09-19 17:25:38 -0700806 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700807 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700808 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
810 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700811
812 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700813 if (layer->isEmpty()) {
814 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
815 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700816 }
817
818 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700819 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
Romain Guyf735c8e2013-01-31 17:45:55 -0800821 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700822
823 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700824 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800825 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700827 glClear(GL_COLOR_BUFFER_BIT);
828
829 dirtyClip();
830
831 // Change the ortho projection
832 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
833 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
834
835 return true;
836}
837
Romain Guy1c740bc2010-09-13 18:00:09 -0700838/**
839 * Read the documentation of createLayer() before doing anything in this method.
840 */
Romain Guy1d83e192010-08-17 11:37:00 -0700841void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
842 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000843 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700844 return;
845 }
846
Romain Guy8ce00302013-01-15 18:51:42 -0800847 Layer* layer = current->layer;
848 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700850
851 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700852 endTiling();
853
Romain Guye0aa84b2012-04-03 19:30:26 -0700854 // Detach the texture from the FBO
855 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800856
857 layer->removeFbo(false);
858
Romain Guyeb993562010-10-05 18:14:38 -0700859 // Unbind current FBO and restore previous one
860 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700861 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700862
863 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700864 }
865
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700867 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700871 }
872
Romain Guy03750a02010-10-18 14:06:08 -0700873 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700874
Romain Guya1d3c912011-12-13 14:55:06 -0800875 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700876
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 // When the layer is stored in an FBO, we can save a bit of fillrate by
878 // drawing only the dirty region
879 if (fboLayer) {
880 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 if (layer->getColorFilter()) {
882 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800883 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700884 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800886 resetColorFilter();
887 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 } else if (!rect.isEmpty()) {
889 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
890 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700891 }
Romain Guy8b55f372010-08-18 17:10:07 -0700892
Romain Guy746b7402010-10-26 16:27:31 -0700893 dirtyClip();
894
Romain Guyeb993562010-10-05 18:14:38 -0700895 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700896 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700897 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700898 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700899 }
900}
901
Romain Guyaa6c24c2011-04-28 18:40:04 -0700902void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700904
905 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700907 setupDrawWithTexture();
908 } else {
909 setupDrawWithExternalTexture();
910 }
911 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700912 setupDrawColor(alpha, alpha, alpha, alpha);
913 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700914 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700915 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700916 setupDrawPureColorUniforms();
917 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
919 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700922 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700923 if (mSnapshot->transform->isPureTranslate() &&
924 layer->getWidth() == (uint32_t) rect.getWidth() &&
925 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
927 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
928
Romain Guyd21b6e12011-11-30 20:21:23 -0800929 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
931 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800932 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
934 }
935 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
937
938 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
939
940 finishDrawTexture();
941}
942
Romain Guy5b3b3522010-10-27 18:57:51 -0700943void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700944 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700945 const Rect& texCoords = layer->texCoords;
946 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
947 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700948
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 float x = rect.left;
950 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700951 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700952 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700953 layer->getHeight() == (uint32_t) rect.getHeight();
954
955 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 // When we're swapping, the layer is already in screen coordinates
957 if (!swap) {
958 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
959 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
960 }
961
Romain Guyd21b6e12011-11-30 20:21:23 -0800962 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800964 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 }
966
967 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
968 layer->getTexture(), layer->getAlpha() / 255.0f,
969 layer->getMode(), layer->isBlend(),
970 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
971 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700972
Romain Guyaa6c24c2011-04-28 18:40:04 -0700973 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
974 } else {
975 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
976 drawTextureLayer(layer, rect);
977 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
978 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700979}
980
981void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700982 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700983 layer->setRegionAsRect();
984
Romain Guy40667672011-03-18 14:34:03 -0700985 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700986
Romain Guy5b3b3522010-10-27 18:57:51 -0700987 layer->region.clear();
988 return;
989 }
990
Romain Guy8a3957d2011-09-07 17:55:15 -0700991 // TODO: See LayerRenderer.cpp::generateMesh() for important
992 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800993 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700994 size_t count;
995 const android::Rect* rects = layer->region.getArray(&count);
996
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 const float alpha = layer->getAlpha() / 255.0f;
998 const float texX = 1.0f / float(layer->getWidth());
999 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001000 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001001
Romain Guy8ce00302013-01-15 18:51:42 -08001002 setupDraw();
1003
1004 // We must get (and therefore bind) the region mesh buffer
1005 // after we setup drawing in case we need to mess with the
1006 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 TextureVertex* mesh = mCaches.getRegionMesh();
1008 GLsizei numQuads = 0;
1009
Romain Guy7230a742011-01-10 22:26:16 -08001010 setupDrawWithTexture();
1011 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001012 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001013 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001014 setupDrawProgram();
1015 setupDrawDirtyRegionsDisabled();
1016 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001017 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 setupDrawTexture(layer->getTexture());
1019 if (mSnapshot->transform->isPureTranslate()) {
1020 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1021 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1022
Romain Guyd21b6e12011-11-30 20:21:23 -08001023 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001024 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1025 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001026 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001027 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1028 }
Romain Guy15bc6432011-12-13 13:11:32 -08001029 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001030
1031 for (size_t i = 0; i < count; i++) {
1032 const android::Rect* r = &rects[i];
1033
1034 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001035 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001036 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001037 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001038
1039 // TODO: Reject quads outside of the clip
1040 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1041 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1042 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1043 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1044
1045 numQuads++;
1046
1047 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1048 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1049 numQuads = 0;
1050 mesh = mCaches.getRegionMesh();
1051 }
1052 }
1053
1054 if (numQuads > 0) {
1055 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1056 }
1057
Romain Guy7230a742011-01-10 22:26:16 -08001058 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001059
1060#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001061 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001062#endif
1063
1064 layer->region.clear();
1065 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001066}
1067
Romain Guy3a3133d2011-02-01 22:59:58 -08001068void OpenGLRenderer::drawRegionRects(const Region& region) {
1069#if DEBUG_LAYERS_AS_REGIONS
1070 size_t count;
1071 const android::Rect* rects = region.getArray(&count);
1072
1073 uint32_t colors[] = {
1074 0x7fff0000, 0x7f00ff00,
1075 0x7f0000ff, 0x7fff00ff,
1076 };
1077
1078 int offset = 0;
1079 int32_t top = rects[0].top;
1080
1081 for (size_t i = 0; i < count; i++) {
1082 if (top != rects[i].top) {
1083 offset ^= 0x2;
1084 top = rects[i].top;
1085 }
1086
1087 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1088 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1089 SkXfermode::kSrcOver_Mode);
1090 }
1091#endif
1092}
1093
Romain Guy8ce00302013-01-15 18:51:42 -08001094void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1095 SkXfermode::Mode mode, bool dirty) {
1096 int count = 0;
1097 Vector<float> rects;
1098
1099 SkRegion::Iterator it(region);
1100 while (!it.done()) {
1101 const SkIRect& r = it.rect();
1102 rects.push(r.fLeft);
1103 rects.push(r.fTop);
1104 rects.push(r.fRight);
1105 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001106 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001107 it.next();
1108 }
1109
1110 drawColorRects(rects.array(), count, color, mode, true, dirty);
1111}
1112
Romain Guy5b3b3522010-10-27 18:57:51 -07001113void OpenGLRenderer::dirtyLayer(const float left, const float top,
1114 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001115 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001116 Rect bounds(left, top, right, bottom);
1117 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001118 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001119 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001120}
1121
1122void OpenGLRenderer::dirtyLayer(const float left, const float top,
1123 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001124 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001125 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001126 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001127 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001128}
1129
1130void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001131 if (bounds.intersect(*mSnapshot->clipRect)) {
1132 bounds.snapToPixelBoundaries();
1133 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1134 if (!dirty.isEmpty()) {
1135 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001136 }
1137 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001138}
1139
Romain Guy54be1cd2011-06-13 19:04:27 -07001140void OpenGLRenderer::clearLayerRegions() {
1141 const size_t count = mLayers.size();
1142 if (count == 0) return;
1143
1144 if (!mSnapshot->isIgnored()) {
1145 // Doing several glScissor/glClear here can negatively impact
1146 // GPUs with a tiler architecture, instead we draw quads with
1147 // the Clear blending mode
1148
1149 // The list contains bounds that have already been clipped
1150 // against their initial clip rect, and the current clip
1151 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001152 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001153
1154 Vertex mesh[count * 6];
1155 Vertex* vertex = mesh;
1156
1157 for (uint32_t i = 0; i < count; i++) {
1158 Rect* bounds = mLayers.itemAt(i);
1159
1160 Vertex::set(vertex++, bounds->left, bounds->bottom);
1161 Vertex::set(vertex++, bounds->left, bounds->top);
1162 Vertex::set(vertex++, bounds->right, bounds->top);
1163 Vertex::set(vertex++, bounds->left, bounds->bottom);
1164 Vertex::set(vertex++, bounds->right, bounds->top);
1165 Vertex::set(vertex++, bounds->right, bounds->bottom);
1166
1167 delete bounds;
1168 }
1169
1170 setupDraw(false);
1171 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1172 setupDrawBlending(true, SkXfermode::kClear_Mode);
1173 setupDrawProgram();
1174 setupDrawPureColorUniforms();
1175 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001176 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001177
Romain Guy54be1cd2011-06-13 19:04:27 -07001178 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001179
1180 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001181 } else {
1182 for (uint32_t i = 0; i < count; i++) {
1183 delete mLayers.itemAt(i);
1184 }
1185 }
1186
1187 mLayers.clear();
1188}
1189
Romain Guybd6b79b2010-06-26 00:13:53 -07001190///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001191// Transforms
1192///////////////////////////////////////////////////////////////////////////////
1193
1194void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001195 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001196}
1197
1198void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001199 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001200}
1201
1202void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001203 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001204}
1205
Romain Guy807daf72011-01-18 11:19:19 -08001206void OpenGLRenderer::skew(float sx, float sy) {
1207 mSnapshot->transform->skew(sx, sy);
1208}
1209
Romain Guyf6a11b82010-06-23 17:47:49 -07001210void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001211 if (matrix) {
1212 mSnapshot->transform->load(*matrix);
1213 } else {
1214 mSnapshot->transform->loadIdentity();
1215 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001216}
1217
1218void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001219 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001220}
1221
1222void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001223 SkMatrix transform;
1224 mSnapshot->transform->copyTo(transform);
1225 transform.preConcat(*matrix);
1226 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001227}
1228
1229///////////////////////////////////////////////////////////////////////////////
1230// Clipping
1231///////////////////////////////////////////////////////////////////////////////
1232
Romain Guybb9524b2010-06-22 18:56:38 -07001233void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001234 Rect clip(*mSnapshot->clipRect);
1235 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001236
Romain Guy8a4ac612012-07-17 17:32:48 -07001237 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1238 clip.getWidth(), clip.getHeight())) {
1239 mDirtyClip = false;
1240 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001241}
1242
Romain Guy8ce00302013-01-15 18:51:42 -08001243void OpenGLRenderer::ensureStencilBuffer() {
1244 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1245 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1246 // just hope we have one when hasLayer() returns false.
1247 if (hasLayer()) {
1248 attachStencilBufferToLayer(mSnapshot->layer);
1249 }
1250}
1251
1252void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1253 // The layer's FBO is already bound when we reach this stage
1254 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001255 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1256 // is attached after we initiated tiling. We must turn it off,
1257 // attach the new render buffer then turn tiling back on
1258 endTiling();
1259
Romain Guy8ce00302013-01-15 18:51:42 -08001260 // TODO: See Layer::removeFbo(). The stencil renderbuffer should be cached
1261 GLuint buffer;
1262 glGenRenderbuffers(1, &buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001263
Romain Guy8ce00302013-01-15 18:51:42 -08001264 layer->setStencilRenderBuffer(buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001265 layer->bindStencilRenderBuffer();
1266 layer->allocateStencilRenderBuffer();
1267
1268 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001269
Romain Guyf735c8e2013-01-31 17:45:55 -08001270 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001271 }
1272}
1273
1274void OpenGLRenderer::setStencilFromClip() {
1275 if (!mCaches.debugOverdraw) {
1276 if (!mSnapshot->clipRegion->isEmpty()) {
1277 // NOTE: The order here is important, we must set dirtyClip to false
1278 // before any draw call to avoid calling back into this method
1279 mDirtyClip = false;
1280
1281 ensureStencilBuffer();
1282
1283 mCaches.stencil.enableWrite();
1284
1285 // Clear the stencil but first make sure we restrict drawing
1286 // to the region's bounds
1287 bool resetScissor = mCaches.enableScissor();
1288 if (resetScissor) {
1289 // The scissor was not set so we now need to update it
1290 setScissorFromClip();
1291 }
1292 mCaches.stencil.clear();
1293 if (resetScissor) mCaches.disableScissor();
1294
1295 // NOTE: We could use the region contour path to generate a smaller mesh
1296 // Since we are using the stencil we could use the red book path
1297 // drawing technique. It might increase bandwidth usage though.
1298
1299 // The last parameter is important: we are not drawing in the color buffer
1300 // so we don't want to dirty the current layer, if any
1301 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1302
1303 mCaches.stencil.enableTest();
1304 } else {
1305 mCaches.stencil.disable();
1306 }
1307 }
1308}
1309
Romain Guy9d5316e2010-06-24 19:30:36 -07001310const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001311 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001312}
1313
Romain Guy8a4ac612012-07-17 17:32:48 -07001314bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1315 if (mSnapshot->isIgnored()) {
1316 return true;
1317 }
1318
1319 Rect r(left, top, right, bottom);
1320 mSnapshot->transform->mapRect(r);
1321 r.snapToPixelBoundaries();
1322
1323 Rect clipRect(*mSnapshot->clipRect);
1324 clipRect.snapToPixelBoundaries();
1325
1326 return !clipRect.intersects(r);
1327}
1328
Romain Guy35643dd2012-09-18 15:40:58 -07001329bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1330 Rect& transformed, Rect& clip) {
1331 if (mSnapshot->isIgnored()) {
1332 return true;
1333 }
1334
1335 transformed.set(left, top, right, bottom);
1336 mSnapshot->transform->mapRect(transformed);
1337 transformed.snapToPixelBoundaries();
1338
1339 clip.set(*mSnapshot->clipRect);
1340 clip.snapToPixelBoundaries();
1341
1342 return !clip.intersects(transformed);
1343}
1344
Romain Guy672433d2013-01-04 19:05:13 -08001345bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1346 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001347 if (paint->getStyle() != SkPaint::kFill_Style) {
1348 float outset = paint->getStrokeWidth() * 0.5f;
1349 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1350 } else {
1351 return quickReject(left, top, right, bottom);
1352 }
1353}
1354
Romain Guyc7d53492010-06-25 13:41:57 -07001355bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001356 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001357 return true;
1358 }
1359
Romain Guy1d83e192010-08-17 11:37:00 -07001360 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001361 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001362 r.snapToPixelBoundaries();
1363
1364 Rect clipRect(*mSnapshot->clipRect);
1365 clipRect.snapToPixelBoundaries();
1366
Romain Guy586cae32012-07-13 15:28:31 -07001367 bool rejected = !clipRect.intersects(r);
1368 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001369 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001370 }
1371
1372 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001373}
1374
Romain Guy8ce00302013-01-15 18:51:42 -08001375void OpenGLRenderer::debugClip() {
1376#if DEBUG_CLIP_REGIONS
1377 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1378 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1379 }
1380#endif
1381}
1382
Romain Guy079ba2c2010-07-16 14:12:24 -07001383bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001384 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1385 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1386 if (clipped) {
1387 dirtyClip();
1388 }
1389 return !mSnapshot->clipRect->isEmpty();
1390 }
1391
1392 SkPath path;
1393 path.addRect(left, top, right, bottom);
1394
1395 return clipPath(&path, op);
1396}
1397
1398bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1399 SkMatrix transform;
1400 mSnapshot->transform->copyTo(transform);
1401
1402 SkPath transformed;
1403 path->transform(transform, &transformed);
1404
1405 SkRegion clip;
1406 if (!mSnapshot->clipRegion->isEmpty()) {
1407 clip.setRegion(*mSnapshot->clipRegion);
1408 } else {
1409 Rect* bounds = mSnapshot->clipRect;
1410 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1411 }
1412
1413 SkRegion region;
1414 region.setPath(transformed, clip);
1415
1416 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001417 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001418 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001419 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001420 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001421}
1422
Romain Guy735738c2012-12-03 12:34:51 -08001423bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001424 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1425 if (clipped) {
1426 dirtyClip();
1427 }
1428 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001429}
1430
Chet Haasea23eed82012-04-12 15:19:04 -07001431Rect* OpenGLRenderer::getClipRect() {
1432 return mSnapshot->clipRect;
1433}
1434
Romain Guyf6a11b82010-06-23 17:47:49 -07001435///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001436// Drawing commands
1437///////////////////////////////////////////////////////////////////////////////
1438
Romain Guy54be1cd2011-06-13 19:04:27 -07001439void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001440 // TODO: It would be best if we could do this before quickReject()
1441 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001442 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001443 // Make sure setScissor & setStencil happen at the beginning of
1444 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001445 if (mDirtyClip) {
1446 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001447 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001448 }
1449 mDescription.reset();
1450 mSetShaderColor = false;
1451 mColorSet = false;
1452 mColorA = mColorR = mColorG = mColorB = 0.0f;
1453 mTextureUnit = 0;
1454 mTrackDirtyRegions = true;
1455}
1456
1457void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1458 mDescription.hasTexture = true;
1459 mDescription.hasAlpha8Texture = isAlpha8;
1460}
1461
Romain Guyaa6c24c2011-04-28 18:40:04 -07001462void OpenGLRenderer::setupDrawWithExternalTexture() {
1463 mDescription.hasExternalTexture = true;
1464}
1465
Romain Guy15bc6432011-12-13 13:11:32 -08001466void OpenGLRenderer::setupDrawNoTexture() {
1467 mCaches.disbaleTexCoordsVertexArray();
1468}
1469
Chris Craik710f46d2012-09-17 17:25:49 -07001470void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001471 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001472}
1473
Chris Craik710f46d2012-09-17 17:25:49 -07001474void OpenGLRenderer::setupDrawVertexShape() {
1475 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001476}
1477
Romain Guyed6fcb02011-03-21 13:11:28 -07001478void OpenGLRenderer::setupDrawPoint(float pointSize) {
1479 mDescription.isPoint = true;
1480 mDescription.pointSize = pointSize;
1481}
1482
Romain Guy8d0d4782010-12-14 20:13:35 -08001483void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1484 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001485 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1486 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1487 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001488 mColorSet = true;
1489 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1490}
1491
Romain Guy86568192010-12-14 15:55:39 -08001492void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1493 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001494 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1495 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1496 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001497 mColorSet = true;
1498 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1499}
1500
Romain Guy41210632012-07-16 17:04:24 -07001501void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1502 mCaches.fontRenderer->describe(mDescription, paint);
1503}
1504
Romain Guy70ca14e2010-12-13 18:24:33 -08001505void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1506 mColorA = a;
1507 mColorR = r;
1508 mColorG = g;
1509 mColorB = b;
1510 mColorSet = true;
1511 mSetShaderColor = mDescription.setColor(r, g, b, a);
1512}
1513
1514void OpenGLRenderer::setupDrawShader() {
1515 if (mShader) {
1516 mShader->describe(mDescription, mCaches.extensions);
1517 }
1518}
1519
1520void OpenGLRenderer::setupDrawColorFilter() {
1521 if (mColorFilter) {
1522 mColorFilter->describe(mDescription, mCaches.extensions);
1523 }
1524}
1525
Romain Guyf09ef512011-05-27 11:43:46 -07001526void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1527 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1528 mColorA = 1.0f;
1529 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001530 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001531 }
1532}
1533
Romain Guy70ca14e2010-12-13 18:24:33 -08001534void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001535 // When the blending mode is kClear_Mode, we need to use a modulate color
1536 // argb=1,0,0,0
1537 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001538 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1539 mDescription, swapSrcDst);
1540}
1541
1542void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001543 // When the blending mode is kClear_Mode, we need to use a modulate color
1544 // argb=1,0,0,0
1545 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001546 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1547 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001548}
1549
1550void OpenGLRenderer::setupDrawProgram() {
1551 useProgram(mCaches.programCache.get(mDescription));
1552}
1553
1554void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1555 mTrackDirtyRegions = false;
1556}
1557
1558void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1559 bool ignoreTransform) {
1560 mModelView.loadTranslate(left, top, 0.0f);
1561 if (!ignoreTransform) {
1562 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1563 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1564 } else {
1565 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1566 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1567 }
1568}
1569
Chet Haase8a5cc922011-04-26 07:28:09 -07001570void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1571 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001572}
1573
Romain Guy70ca14e2010-12-13 18:24:33 -08001574void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1575 bool ignoreTransform, bool ignoreModelView) {
1576 if (!ignoreModelView) {
1577 mModelView.loadTranslate(left, top, 0.0f);
1578 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001579 } else {
1580 mModelView.loadIdentity();
1581 }
Romain Guy86568192010-12-14 15:55:39 -08001582 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1583 if (!ignoreTransform) {
1584 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1585 if (mTrackDirtyRegions && dirty) {
1586 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1587 }
1588 } else {
1589 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1590 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1591 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001592}
1593
Romain Guyed6fcb02011-03-21 13:11:28 -07001594void OpenGLRenderer::setupDrawPointUniforms() {
1595 int slot = mCaches.currentProgram->getUniform("pointSize");
1596 glUniform1f(slot, mDescription.pointSize);
1597}
1598
Romain Guy70ca14e2010-12-13 18:24:33 -08001599void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001600 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001601 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1602 }
1603}
1604
Romain Guy86568192010-12-14 15:55:39 -08001605void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001606 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001607 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001608 }
1609}
1610
Romain Guy70ca14e2010-12-13 18:24:33 -08001611void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1612 if (mShader) {
1613 if (ignoreTransform) {
1614 mModelView.loadInverse(*mSnapshot->transform);
1615 }
1616 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1617 }
1618}
1619
Romain Guy8d0d4782010-12-14 20:13:35 -08001620void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1621 if (mShader) {
1622 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1623 }
1624}
1625
Romain Guy70ca14e2010-12-13 18:24:33 -08001626void OpenGLRenderer::setupDrawColorFilterUniforms() {
1627 if (mColorFilter) {
1628 mColorFilter->setupProgram(mCaches.currentProgram);
1629 }
1630}
1631
Romain Guy41210632012-07-16 17:04:24 -07001632void OpenGLRenderer::setupDrawTextGammaUniforms() {
1633 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1634}
1635
Romain Guy70ca14e2010-12-13 18:24:33 -08001636void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001637 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001638 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001639 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001640}
1641
1642void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1643 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001644 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001645 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001646}
1647
Romain Guyaa6c24c2011-04-28 18:40:04 -07001648void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1649 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001650 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001651 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001652}
1653
Romain Guy8f0095c2011-05-02 17:24:22 -07001654void OpenGLRenderer::setupDrawTextureTransform() {
1655 mDescription.hasTextureTransform = true;
1656}
1657
1658void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001659 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1660 GL_FALSE, &transform.data[0]);
1661}
1662
Romain Guy70ca14e2010-12-13 18:24:33 -08001663void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001664 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001665 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001666 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001667 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001668 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001669 }
Romain Guyd71dd362011-12-12 19:03:35 -08001670
Chris Craikcb4d6002012-09-25 12:00:29 -07001671 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001672 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001673 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001674 }
1675
1676 mCaches.unbindIndicesBuffer();
1677}
1678
1679void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1680 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001681 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001682 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001683 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001684 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001685}
1686
Chet Haase5b0200b2011-04-13 17:58:08 -07001687void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001688 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001689 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001690 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001691}
1692
1693/**
1694 * 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 -07001695 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1696 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1697 * attributes (one per vertex) are values from zero to one that tells the fragment
1698 * shader where the fragment is in relation to the line width/length overall; these values are
1699 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1700 * region of the line.
1701 * Note that we only pass down the width values in this setup function. The length coordinates
1702 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001703 */
Chet Haase99585ad2011-05-02 15:00:16 -07001704void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001705 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001706 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001707 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001708 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001709 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001710
Romain Guy7b631422012-04-04 11:38:54 -07001711 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001712 glEnableVertexAttribArray(widthSlot);
1713 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001714
Romain Guy7b631422012-04-04 11:38:54 -07001715 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001716 glEnableVertexAttribArray(lengthSlot);
1717 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001718
Chet Haase5b0200b2011-04-13 17:58:08 -07001719 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001720 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001721}
1722
1723void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1724 glDisableVertexAttribArray(widthSlot);
1725 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001726}
1727
Romain Guy70ca14e2010-12-13 18:24:33 -08001728void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001729}
1730
1731///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001732// Drawing
1733///////////////////////////////////////////////////////////////////////////////
1734
Chet Haase1271e2c2012-04-20 09:54:27 -07001735status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001736 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001737
Romain Guy0fe478e2010-11-08 12:08:41 -08001738 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1739 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001740 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001741 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001742 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001743
Romain Guy65549432012-03-26 16:45:05 -07001744 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001745}
1746
Chet Haaseed30fd82011-04-22 16:18:45 -07001747void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1748 if (displayList) {
Chris Craik2af46352012-11-26 18:30:17 -08001749 displayList->output(level);
Chet Haaseed30fd82011-04-22 16:18:45 -07001750 }
1751}
1752
Romain Guya168d732011-03-18 16:50:13 -07001753void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1754 int alpha;
1755 SkXfermode::Mode mode;
1756 getAlphaAndMode(paint, &alpha, &mode);
1757
Romain Guy886b2752013-01-04 12:26:18 -08001758 int color = paint != NULL ? paint->getColor() : 0;
1759
Romain Guya168d732011-03-18 16:50:13 -07001760 float x = left;
1761 float y = top;
1762
Romain Guy886b2752013-01-04 12:26:18 -08001763 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1764
Romain Guya168d732011-03-18 16:50:13 -07001765 bool ignoreTransform = false;
1766 if (mSnapshot->transform->isPureTranslate()) {
1767 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1768 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1769 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001770
1771 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001772 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001773 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001774 }
1775
Romain Guy886b2752013-01-04 12:26:18 -08001776 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1777 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1778 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001779}
1780
Chet Haase48659092012-05-31 15:21:51 -07001781status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001782 const float right = left + bitmap->width();
1783 const float bottom = top + bitmap->height();
1784
1785 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001786 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001787 }
1788
Romain Guya1d3c912011-12-13 14:55:06 -08001789 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001790 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001791 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001792 const AutoTexture autoCleanup(texture);
1793
Romain Guy211370f2012-02-01 16:10:55 -08001794 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001795 drawAlphaBitmap(texture, left, top, paint);
1796 } else {
1797 drawTextureRect(left, top, right, bottom, texture, paint);
1798 }
Chet Haase48659092012-05-31 15:21:51 -07001799
1800 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001801}
1802
Chet Haase48659092012-05-31 15:21:51 -07001803status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001804 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1805 const mat4 transform(*matrix);
1806 transform.mapRect(r);
1807
Romain Guy6926c722010-07-12 20:20:03 -07001808 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001809 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001810 }
1811
Romain Guya1d3c912011-12-13 14:55:06 -08001812 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001813 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001814 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001815 const AutoTexture autoCleanup(texture);
1816
Romain Guy5b3b3522010-10-27 18:57:51 -07001817 // This could be done in a cheaper way, all we need is pass the matrix
1818 // to the vertex shader. The save/restore is a bit overkill.
1819 save(SkCanvas::kMatrix_SaveFlag);
1820 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001821 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1822 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1823 } else {
1824 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1825 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001826 restore();
Chet Haase48659092012-05-31 15:21:51 -07001827
1828 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001829}
1830
Chet Haase48659092012-05-31 15:21:51 -07001831status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001832 const float right = left + bitmap->width();
1833 const float bottom = top + bitmap->height();
1834
1835 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001836 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001837 }
1838
1839 mCaches.activeTexture(0);
1840 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1841 const AutoTexture autoCleanup(texture);
1842
Romain Guy886b2752013-01-04 12:26:18 -08001843 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1844 drawAlphaBitmap(texture, left, top, paint);
1845 } else {
1846 drawTextureRect(left, top, right, bottom, texture, paint);
1847 }
Chet Haase48659092012-05-31 15:21:51 -07001848
1849 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001850}
1851
Chet Haase48659092012-05-31 15:21:51 -07001852status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001853 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001854 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001855 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001856 }
1857
Romain Guyb18d2d02011-02-10 15:52:54 -08001858 float left = FLT_MAX;
1859 float top = FLT_MAX;
1860 float right = FLT_MIN;
1861 float bottom = FLT_MIN;
1862
Romain Guya92bb4d2012-10-16 11:08:44 -07001863 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001864
Romain Guya566b7c2011-01-23 16:36:11 -08001865 // TODO: Support the colors array
1866 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001867 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001868
Romain Guy5a7b4662011-01-20 19:09:30 -08001869 for (int32_t y = 0; y < meshHeight; y++) {
1870 for (int32_t x = 0; x < meshWidth; x++) {
1871 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1872
1873 float u1 = float(x) / meshWidth;
1874 float u2 = float(x + 1) / meshWidth;
1875 float v1 = float(y) / meshHeight;
1876 float v2 = float(y + 1) / meshHeight;
1877
1878 int ax = i + (meshWidth + 1) * 2;
1879 int ay = ax + 1;
1880 int bx = i;
1881 int by = bx + 1;
1882 int cx = i + 2;
1883 int cy = cx + 1;
1884 int dx = i + (meshWidth + 1) * 2 + 2;
1885 int dy = dx + 1;
1886
1887 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1888 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1889 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1890
1891 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1892 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1893 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001894
Romain Guya92bb4d2012-10-16 11:08:44 -07001895 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1896 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1897 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1898 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001899 }
1900 }
1901
Romain Guya92bb4d2012-10-16 11:08:44 -07001902 if (quickReject(left, top, right, bottom)) {
1903 return DrawGlInfo::kStatusDone;
1904 }
1905
1906 mCaches.activeTexture(0);
1907 Texture* texture = mCaches.textureCache.get(bitmap);
1908 if (!texture) return DrawGlInfo::kStatusDone;
1909 const AutoTexture autoCleanup(texture);
1910
1911 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1912 texture->setFilter(FILTER(paint), true);
1913
1914 int alpha;
1915 SkXfermode::Mode mode;
1916 getAlphaAndMode(paint, &alpha, &mode);
1917
1918 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001919 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1920 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001921
Romain Guy5a7b4662011-01-20 19:09:30 -08001922 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1923 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001924 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001925
1926 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001927}
1928
Chet Haase48659092012-05-31 15:21:51 -07001929status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001930 float srcLeft, float srcTop, float srcRight, float srcBottom,
1931 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001932 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001933 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001934 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001935 }
1936
Romain Guya1d3c912011-12-13 14:55:06 -08001937 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001938 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001939 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001940 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001941
Romain Guy8ba548f2010-06-30 19:21:21 -07001942 const float width = texture->width;
1943 const float height = texture->height;
1944
Romain Guy68169722011-08-22 17:33:33 -07001945 const float u1 = fmax(0.0f, srcLeft / width);
1946 const float v1 = fmax(0.0f, srcTop / height);
1947 const float u2 = fmin(1.0f, srcRight / width);
1948 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001949
Romain Guy03750a02010-10-18 14:06:08 -07001950 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001951 resetDrawTextureTexCoords(u1, v1, u2, v2);
1952
Romain Guy03750a02010-10-18 14:06:08 -07001953 int alpha;
1954 SkXfermode::Mode mode;
1955 getAlphaAndMode(paint, &alpha, &mode);
1956
Romain Guyd21b6e12011-11-30 20:21:23 -08001957 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1958
Romain Guy886b2752013-01-04 12:26:18 -08001959 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1960 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001961
Romain Guy886b2752013-01-04 12:26:18 -08001962 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1963 // Apply a scale transform on the canvas only when a shader is in use
1964 // Skia handles the ratio between the dst and src rects as a scale factor
1965 // when a shader is set
1966 bool useScaleTransform = mShader && scaled;
1967 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001968
Romain Guy886b2752013-01-04 12:26:18 -08001969 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1970 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1971 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1972
1973 dstRight = x + (dstRight - dstLeft);
1974 dstBottom = y + (dstBottom - dstTop);
1975
1976 dstLeft = x;
1977 dstTop = y;
1978
1979 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1980 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001981 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001982 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001983 }
1984
1985 if (CC_UNLIKELY(useScaleTransform)) {
1986 save(SkCanvas::kMatrix_SaveFlag);
1987 translate(dstLeft, dstTop);
1988 scale(scaleX, scaleY);
1989
1990 dstLeft = 0.0f;
1991 dstTop = 0.0f;
1992
1993 dstRight = srcRight - srcLeft;
1994 dstBottom = srcBottom - srcTop;
1995 }
1996
1997 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1998 int color = paint ? paint->getColor() : 0;
1999 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2000 texture->id, paint != NULL, color, alpha, mode,
2001 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2002 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2003 } else {
2004 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2005 texture->id, alpha / 255.0f, mode, texture->blend,
2006 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2007 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2008 }
2009
2010 if (CC_UNLIKELY(useScaleTransform)) {
2011 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002012 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002013
2014 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002015
2016 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002017}
2018
Chet Haase48659092012-05-31 15:21:51 -07002019status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002020 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002021 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002022 int alpha;
2023 SkXfermode::Mode mode;
2024 getAlphaAndModeDirect(paint, &alpha, &mode);
2025
2026 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2027 left, top, right, bottom, alpha, mode);
2028}
2029
2030status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2031 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2032 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002033 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002034 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002035 }
2036
Romain Guybe6f9dc2012-07-16 12:41:17 -07002037 alpha *= mSnapshot->alpha;
2038
Romain Guya1d3c912011-12-13 14:55:06 -08002039 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002040 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002041 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002042 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002043 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2044 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002045
Romain Guy2728f962010-10-08 18:36:15 -07002046 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002047 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002048
Romain Guy211370f2012-02-01 16:10:55 -08002049 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002050 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002051 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002052 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002053 const float offsetX = left + mSnapshot->transform->getTranslateX();
2054 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002055 const size_t count = mesh->quads.size();
2056 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002057 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002058 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002059 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2060 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2061 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002062 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002063 dirtyLayer(left + bounds.left, top + bounds.top,
2064 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002065 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002066 }
2067 }
2068
Romain Guy211370f2012-02-01 16:10:55 -08002069 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002070 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2071 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2072
2073 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2074 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2075 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2076 true, !mesh->hasEmptyQuads);
2077 } else {
2078 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2079 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2080 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2081 true, !mesh->hasEmptyQuads);
2082 }
Romain Guy054dc182010-10-15 17:55:25 -07002083 }
Chet Haase48659092012-05-31 15:21:51 -07002084
2085 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002086}
2087
Chet Haase99ecdc42011-05-06 12:06:34 -07002088/**
Chris Craikcb4d6002012-09-25 12:00:29 -07002089 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2090 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2091 * screen space in all directions. However, instead of using a fragment shader to compute the
2092 * translucency of the color from its position, we simply use a varying parameter to define how far
2093 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2094 *
2095 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07002096 */
Chris Craikcb4d6002012-09-25 12:00:29 -07002097void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2098 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002099 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2100 bool isAA = paint->isAntiAlias();
2101
Chris Craik710f46d2012-09-17 17:25:49 -07002102 VertexBuffer vertexBuffer;
2103 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07002104 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07002105
Chris Craikbf09ffb2012-10-01 13:50:37 -07002106 if (!vertexBuffer.getSize()) {
2107 // no vertices to draw
2108 return;
2109 }
2110
Chris Craik710f46d2012-09-17 17:25:49 -07002111 setupDraw();
2112 setupDrawNoTexture();
2113 if (isAA) setupDrawAA();
2114 setupDrawVertexShape();
2115 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2116 setupDrawColorFilter();
2117 setupDrawShader();
2118 setupDrawBlending(isAA, mode);
2119 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002120 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07002121 setupDrawColorUniforms();
2122 setupDrawColorFilterUniforms();
2123 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002124
Chris Craik710f46d2012-09-17 17:25:49 -07002125 void* vertices = vertexBuffer.getBuffer();
2126 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002127 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002128 mCaches.resetTexCoordsVertexPointer();
2129 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002130
Chris Craik710f46d2012-09-17 17:25:49 -07002131 int alphaSlot = -1;
2132 if (isAA) {
2133 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2134 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002135
Chris Craik710f46d2012-09-17 17:25:49 -07002136 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002137 glEnableVertexAttribArray(alphaSlot);
2138 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002139 }
Romain Guy04299382012-07-18 17:15:41 -07002140
Chris Craikcb4d6002012-09-25 12:00:29 -07002141 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002142 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002143
Chris Craik710f46d2012-09-17 17:25:49 -07002144 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002145
Chris Craik710f46d2012-09-17 17:25:49 -07002146 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002147 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002148 }
Chet Haase858aa932011-05-12 09:06:00 -07002149}
2150
2151/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002152 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2153 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2154 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2155 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2156 * of the line. Hairlines are more involved because we need to account for transform scaling
2157 * to end up with a one-pixel-wide line in screen space..
2158 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2159 * in combination with values that we calculate and pass down in this method. The basic approach
2160 * is that the quad we create contains both the core line area plus a bounding area in which
2161 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2162 * proportion of the width and the length of a given segment is represented by the boundary
2163 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2164 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2165 * on the inside). This ends up giving the result we want, with pixels that are completely
2166 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2167 * how far into the boundary region they are, which is determined by shader interpolation.
2168 */
Chet Haase48659092012-05-31 15:21:51 -07002169status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2170 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002171
2172 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002173 // We use half the stroke width here because we're going to position the quad
2174 // corner vertices half of the width away from the line endpoints
2175 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002176 // A stroke width of 0 has a special meaning in Skia:
2177 // it draws a line 1 px wide regardless of current transform
2178 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002179
Chet Haase99ecdc42011-05-06 12:06:34 -07002180 float inverseScaleX = 1.0f;
2181 float inverseScaleY = 1.0f;
2182 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002183
Chet Haase8a5cc922011-04-26 07:28:09 -07002184 int alpha;
2185 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002186
Chet Haase8a5cc922011-04-26 07:28:09 -07002187 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002188 int verticesCount = count;
2189 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002190 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002191 verticesCount += (count - 4);
2192 }
2193
2194 if (isHairLine || isAA) {
2195 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2196 // the line on the screen should always be one pixel wide regardless of scale. For
2197 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002198 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002199 Matrix4 *mat = mSnapshot->transform;
2200 float m00 = mat->data[Matrix4::kScaleX];
2201 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002202 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002203 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002204
Romain Guy211370f2012-02-01 16:10:55 -08002205 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2206 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002207
Chet Haase99ecdc42011-05-06 12:06:34 -07002208 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2209 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002210
Chet Haase99ecdc42011-05-06 12:06:34 -07002211 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2212 scaled = true;
2213 }
2214 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002215 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002216
2217 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002218
2219 mCaches.enableScissor();
2220
Chet Haase8a5cc922011-04-26 07:28:09 -07002221 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002222 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002223 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002224 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002225 }
2226 setupDrawColor(paint->getColor(), alpha);
2227 setupDrawColorFilter();
2228 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002229 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002230 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002231 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002232 setupDrawColorUniforms();
2233 setupDrawColorFilterUniforms();
2234 setupDrawShaderIdentityUniforms();
2235
2236 if (isHairLine) {
2237 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002238 halfStrokeWidth = isAA? 1 : .5;
2239 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002240 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002241 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002242 }
Romain Guy7b631422012-04-04 11:38:54 -07002243
2244 int widthSlot;
2245 int lengthSlot;
2246
Chet Haase5b0200b2011-04-13 17:58:08 -07002247 Vertex lines[verticesCount];
2248 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002249
Chet Haase99585ad2011-05-02 15:00:16 -07002250 AAVertex wLines[verticesCount];
2251 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002252
Romain Guy211370f2012-02-01 16:10:55 -08002253 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002254 setupDrawVertices(vertices);
2255 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002256 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2257 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002258 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002259 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2260 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002261 // We will need to calculate the actual width proportion on each segment for
2262 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002263 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002264 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2265 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002266 }
2267
Chet Haase99ecdc42011-05-06 12:06:34 -07002268 AAVertex* prevAAVertex = NULL;
2269 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002270
Chet Haase99585ad2011-05-02 15:00:16 -07002271 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002272 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002273
Chet Haase5b0200b2011-04-13 17:58:08 -07002274 for (int i = 0; i < count; i += 4) {
2275 // a = start point, b = end point
2276 vec2 a(points[i], points[i + 1]);
2277 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002278
Chet Haase99585ad2011-05-02 15:00:16 -07002279 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002280 float boundaryLengthProportion = 0;
2281 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002282
Chet Haase5b0200b2011-04-13 17:58:08 -07002283 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002284 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002285 float x = n.x;
2286 n.x = -n.y;
2287 n.y = x;
2288
Chet Haase8a5cc922011-04-26 07:28:09 -07002289 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002290 if (isAA) {
2291 float wideningFactor;
2292 if (fabs(n.x) >= fabs(n.y)) {
2293 wideningFactor = fabs(1.0f / n.x);
2294 } else {
2295 wideningFactor = fabs(1.0f / n.y);
2296 }
2297 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002298 }
Romain Guy7b631422012-04-04 11:38:54 -07002299
Chet Haase99ecdc42011-05-06 12:06:34 -07002300 if (scaled) {
2301 n.x *= inverseScaleX;
2302 n.y *= inverseScaleY;
2303 }
2304 } else if (scaled) {
2305 // Extend n by .5 pixel on each side, post-transform
2306 vec2 extendedN = n.copyNormalized();
2307 extendedN /= 2;
2308 extendedN.x *= inverseScaleX;
2309 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002310
Chet Haase99ecdc42011-05-06 12:06:34 -07002311 float extendedNLength = extendedN.length();
2312 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002313 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002314 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002315 }
Romain Guy7b631422012-04-04 11:38:54 -07002316
Chet Haase99585ad2011-05-02 15:00:16 -07002317 // aa lines expand the endpoint vertices to encompass the AA boundary
2318 if (isAA) {
2319 vec2 abVector = (b - a);
2320 length = abVector.length();
2321 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002322
Chet Haase99ecdc42011-05-06 12:06:34 -07002323 if (scaled) {
2324 abVector.x *= inverseScaleX;
2325 abVector.y *= inverseScaleY;
2326 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002327 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002328 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002329 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002330 }
Romain Guy7b631422012-04-04 11:38:54 -07002331
Chet Haase99ecdc42011-05-06 12:06:34 -07002332 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002333 a -= abVector;
2334 b += abVector;
2335 }
2336
Chet Haase5b0200b2011-04-13 17:58:08 -07002337 // Four corners of the rectangle defining a thick line
2338 vec2 p1 = a - n;
2339 vec2 p2 = a + n;
2340 vec2 p3 = b + n;
2341 vec2 p4 = b - n;
2342
Chet Haase99585ad2011-05-02 15:00:16 -07002343
Chet Haase5b0200b2011-04-13 17:58:08 -07002344 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2345 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2346 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2347 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2348
Romain Guy04299382012-07-18 17:15:41 -07002349 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002350 if (!isAA) {
2351 if (prevVertex != NULL) {
2352 // Issue two repeat vertices to create degenerate triangles to bridge
2353 // between the previous line and the new one. This is necessary because
2354 // we are creating a single triangle_strip which will contain
2355 // potentially discontinuous line segments.
2356 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2357 Vertex::set(vertices++, p1.x, p1.y);
2358 generatedVerticesCount += 2;
2359 }
Romain Guy7b631422012-04-04 11:38:54 -07002360
Chet Haase5b0200b2011-04-13 17:58:08 -07002361 Vertex::set(vertices++, p1.x, p1.y);
2362 Vertex::set(vertices++, p2.x, p2.y);
2363 Vertex::set(vertices++, p4.x, p4.y);
2364 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002365
Chet Haase5b0200b2011-04-13 17:58:08 -07002366 prevVertex = vertices - 1;
2367 generatedVerticesCount += 4;
2368 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002369 if (!isHairLine && scaled) {
2370 // Must set width proportions per-segment for scaled non-hairlines to use the
2371 // correct AA boundary dimensions
2372 if (boundaryWidthSlot < 0) {
2373 boundaryWidthSlot =
2374 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002375 }
Romain Guy7b631422012-04-04 11:38:54 -07002376
Chet Haase99ecdc42011-05-06 12:06:34 -07002377 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002378 }
Romain Guy7b631422012-04-04 11:38:54 -07002379
Chet Haase99585ad2011-05-02 15:00:16 -07002380 if (boundaryLengthSlot < 0) {
2381 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002382 }
Romain Guy7b631422012-04-04 11:38:54 -07002383
Chet Haase99ecdc42011-05-06 12:06:34 -07002384 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002385
Chet Haase5b0200b2011-04-13 17:58:08 -07002386 if (prevAAVertex != NULL) {
2387 // Issue two repeat vertices to create degenerate triangles to bridge
2388 // between the previous line and the new one. This is necessary because
2389 // we are creating a single triangle_strip which will contain
2390 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002391 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2392 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2393 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002394 generatedVerticesCount += 2;
2395 }
Romain Guy7b631422012-04-04 11:38:54 -07002396
Chet Haase99585ad2011-05-02 15:00:16 -07002397 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2398 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2399 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2400 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002401
Chet Haase5b0200b2011-04-13 17:58:08 -07002402 prevAAVertex = aaVertices - 1;
2403 generatedVerticesCount += 4;
2404 }
Romain Guy7b631422012-04-04 11:38:54 -07002405
Chet Haase99585ad2011-05-02 15:00:16 -07002406 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2407 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2408 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002409 }
2410 }
Romain Guy7b631422012-04-04 11:38:54 -07002411
Chet Haase5b0200b2011-04-13 17:58:08 -07002412 if (generatedVerticesCount > 0) {
2413 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2414 }
Romain Guy7b631422012-04-04 11:38:54 -07002415
2416 if (isAA) {
2417 finishDrawAALine(widthSlot, lengthSlot);
2418 }
Chet Haase48659092012-05-31 15:21:51 -07002419
2420 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002421}
2422
Chet Haase48659092012-05-31 15:21:51 -07002423status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2424 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002425
2426 // TODO: The paint's cap style defines whether the points are square or circular
2427 // TODO: Handle AA for round points
2428
Chet Haase5b0200b2011-04-13 17:58:08 -07002429 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002430 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002431 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002432 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002433 if (isHairLine) {
2434 // Now that we know it's hairline, we can set the effective width, to be used later
2435 strokeWidth = 1.0f;
2436 }
2437 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002438 int alpha;
2439 SkXfermode::Mode mode;
2440 getAlphaAndMode(paint, &alpha, &mode);
2441
2442 int verticesCount = count >> 1;
2443 int generatedVerticesCount = 0;
2444
2445 TextureVertex pointsData[verticesCount];
2446 TextureVertex* vertex = &pointsData[0];
2447
Romain Guy04299382012-07-18 17:15:41 -07002448 // TODO: We should optimize this method to not generate vertices for points
2449 // that lie outside of the clip.
2450 mCaches.enableScissor();
2451
Romain Guyed6fcb02011-03-21 13:11:28 -07002452 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002453 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002454 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002455 setupDrawColor(paint->getColor(), alpha);
2456 setupDrawColorFilter();
2457 setupDrawShader();
2458 setupDrawBlending(mode);
2459 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002460 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002461 setupDrawColorUniforms();
2462 setupDrawColorFilterUniforms();
2463 setupDrawPointUniforms();
2464 setupDrawShaderIdentityUniforms();
2465 setupDrawMesh(vertex);
2466
2467 for (int i = 0; i < count; i += 2) {
2468 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2469 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002470
Chet Haase8a5cc922011-04-26 07:28:09 -07002471 float left = points[i] - halfWidth;
2472 float right = points[i] + halfWidth;
2473 float top = points[i + 1] - halfWidth;
2474 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002475
Chet Haase8a5cc922011-04-26 07:28:09 -07002476 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002477 }
2478
2479 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002480
2481 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002482}
2483
Chet Haase48659092012-05-31 15:21:51 -07002484status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002485 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002486 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002487
Romain Guyae88e5e2010-10-22 17:49:18 -07002488 Rect& clip(*mSnapshot->clipRect);
2489 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
Romain Guy3d58c032010-07-14 16:34:53 -07002491 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002492
2493 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002494}
Romain Guy9d5316e2010-06-24 19:30:36 -07002495
Chet Haase48659092012-05-31 15:21:51 -07002496status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2497 SkPaint* paint) {
2498 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002499 const AutoTexture autoCleanup(texture);
2500
2501 const float x = left + texture->left - texture->offset;
2502 const float y = top + texture->top - texture->offset;
2503
2504 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002505
2506 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002507}
2508
Chet Haase48659092012-05-31 15:21:51 -07002509status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002510 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002511 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002512 return DrawGlInfo::kStatusDone;
2513 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002514
Chris Craikcb4d6002012-09-25 12:00:29 -07002515 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002516 mCaches.activeTexture(0);
2517 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2518 right - left, bottom - top, rx, ry, p);
2519 return drawShape(left, top, texture, p);
2520 }
2521
2522 SkPath path;
2523 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002524 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2525 float outset = p->getStrokeWidth() / 2;
2526 rect.outset(outset, outset);
2527 rx += outset;
2528 ry += outset;
2529 }
Chris Craik710f46d2012-09-17 17:25:49 -07002530 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002531 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002532
2533 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002534}
2535
Chris Craik710f46d2012-09-17 17:25:49 -07002536status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002537 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2538 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002539 return DrawGlInfo::kStatusDone;
2540 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002542 mCaches.activeTexture(0);
2543 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2544 return drawShape(x - radius, y - radius, texture, p);
2545 }
2546
2547 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002548 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2549 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2550 } else {
2551 path.addCircle(x, y, radius);
2552 }
2553 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002554
2555 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002556}
Romain Guy01d58e42011-01-19 21:54:02 -08002557
Chet Haase48659092012-05-31 15:21:51 -07002558status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002559 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002560 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002561 return DrawGlInfo::kStatusDone;
2562 }
Romain Guy01d58e42011-01-19 21:54:02 -08002563
Chris Craikcb4d6002012-09-25 12:00:29 -07002564 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002565 mCaches.activeTexture(0);
2566 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2567 return drawShape(left, top, texture, p);
2568 }
2569
2570 SkPath path;
2571 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002572 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2573 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2574 }
Chris Craik710f46d2012-09-17 17:25:49 -07002575 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002576 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002577
2578 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002579}
2580
Chet Haase48659092012-05-31 15:21:51 -07002581status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002582 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2583 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2584 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002585 }
2586
Chris Craik780c1282012-10-04 14:10:49 -07002587 if (fabs(sweepAngle) >= 360.0f) {
2588 return drawOval(left, top, right, bottom, p);
2589 }
2590
2591 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002592 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2593 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002594 mCaches.activeTexture(0);
2595 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2596 startAngle, sweepAngle, useCenter, p);
2597 return drawShape(left, top, texture, p);
2598 }
2599
2600 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2601 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2602 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2603 }
2604
2605 SkPath path;
2606 if (useCenter) {
2607 path.moveTo(rect.centerX(), rect.centerY());
2608 }
2609 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2610 if (useCenter) {
2611 path.close();
2612 }
2613 drawConvexPath(path, p);
2614
2615 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002616}
2617
Romain Guycf8675e2012-10-02 12:32:25 -07002618// See SkPaintDefaults.h
2619#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2620
Chet Haase48659092012-05-31 15:21:51 -07002621status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002622 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002623 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002624 }
2625
Chris Craik710f46d2012-09-17 17:25:49 -07002626 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002627 // only fill style is supported by drawConvexPath, since others have to handle joins
2628 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2629 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2630 mCaches.activeTexture(0);
2631 const PathTexture* texture =
2632 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2633 return drawShape(left, top, texture, p);
2634 }
2635
2636 SkPath path;
2637 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2638 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2639 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2640 }
2641 path.addRect(rect);
2642 drawConvexPath(path, p);
2643
2644 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002645 }
2646
Romain Guy181d0a62011-06-09 18:52:38 -07002647 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002648 SkPath path;
2649 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002650 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002651 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002652 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002653 }
Chet Haase48659092012-05-31 15:21:51 -07002654
2655 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002656}
Romain Guy9d5316e2010-06-24 19:30:36 -07002657
Raph Levien416a8472012-07-19 22:48:17 -07002658void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2659 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2660 float x, float y) {
2661 mCaches.activeTexture(0);
2662
2663 // NOTE: The drop shadow will not perform gamma correction
2664 // if shader-based correction is enabled
2665 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2666 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2667 paint, text, bytesCount, count, mShadowRadius, positions);
2668 const AutoTexture autoCleanup(shadow);
2669
2670 const float sx = x - shadow->left + mShadowDx;
2671 const float sy = y - shadow->top + mShadowDy;
2672
2673 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2674 int shadowColor = mShadowColor;
2675 if (mShader) {
2676 shadowColor = 0xffffffff;
2677 }
2678
2679 setupDraw();
2680 setupDrawWithTexture(true);
2681 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2682 setupDrawColorFilter();
2683 setupDrawShader();
2684 setupDrawBlending(true, mode);
2685 setupDrawProgram();
2686 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2687 setupDrawTexture(shadow->id);
2688 setupDrawPureColorUniforms();
2689 setupDrawColorFilterUniforms();
2690 setupDrawShaderUniforms();
2691 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2692
2693 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2694}
2695
Chet Haase48659092012-05-31 15:21:51 -07002696status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002697 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002698 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002699 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002700 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002701 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002702
Romain Guy671d6cf2012-01-18 12:39:17 -08002703 // NOTE: Skia does not support perspective transform on drawPosText yet
2704 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002705 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002706 }
2707
2708 float x = 0.0f;
2709 float y = 0.0f;
2710 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2711 if (pureTranslate) {
2712 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2713 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2714 }
2715
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002716 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002717 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002718
2719 int alpha;
2720 SkXfermode::Mode mode;
2721 getAlphaAndMode(paint, &alpha, &mode);
2722
Raph Levien416a8472012-07-19 22:48:17 -07002723 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002724 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2725 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002726 }
2727
Romain Guy671d6cf2012-01-18 12:39:17 -08002728 // Pick the appropriate texture filtering
2729 bool linearFilter = mSnapshot->transform->changesBounds();
2730 if (pureTranslate && !linearFilter) {
2731 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2732 }
2733
2734 mCaches.activeTexture(0);
2735 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002736 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002737 setupDrawDirtyRegionsDisabled();
2738 setupDrawWithTexture(true);
2739 setupDrawAlpha8Color(paint->getColor(), alpha);
2740 setupDrawColorFilter();
2741 setupDrawShader();
2742 setupDrawBlending(true, mode);
2743 setupDrawProgram();
2744 setupDrawModelView(x, y, x, y, pureTranslate, true);
2745 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2746 setupDrawPureColorUniforms();
2747 setupDrawColorFilterUniforms();
2748 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002749 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002750
2751 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2752 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2753
Romain Guy211370f2012-02-01 16:10:55 -08002754 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002755
2756 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2757 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002758 if (hasActiveLayer) {
2759 if (!pureTranslate) {
2760 mSnapshot->transform->mapRect(bounds);
2761 }
2762 dirtyLayerUnchecked(bounds, getRegion());
2763 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002764 }
Chet Haase48659092012-05-31 15:21:51 -07002765
2766 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002767}
2768
Romain Guyc2525952012-07-27 16:41:22 -07002769status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002770 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002771 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002772 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002773 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002774 }
2775
Chet Haasea1cff502012-02-21 13:43:44 -08002776 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002777 switch (paint->getTextAlign()) {
2778 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002779 x -= length / 2.0f;
2780 break;
2781 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002782 x -= length;
2783 break;
2784 default:
2785 break;
2786 }
2787
Romain Guycac5fd32011-12-01 20:08:50 -08002788 SkPaint::FontMetrics metrics;
2789 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002790 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002791 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002792 }
2793
Romain Guye3a9b242013-01-08 11:15:30 -08002794#if DEBUG_GLYPHS
2795 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2796 SkTypeface::UniqueID(paint->getTypeface()));
2797#endif
2798
2799 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2800 fontRenderer.setFont(paint, *mSnapshot->transform);
2801
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002802 const float oldX = x;
2803 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002804 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002805 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002806 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2807 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2808 }
2809
Romain Guy86568192010-12-14 15:55:39 -08002810 int alpha;
2811 SkXfermode::Mode mode;
2812 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002813
Romain Guy211370f2012-02-01 16:10:55 -08002814 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002815 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2816 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002817 }
2818
Romain Guy6620c6d2010-12-06 18:07:02 -08002819 // Pick the appropriate texture filtering
2820 bool linearFilter = mSnapshot->transform->changesBounds();
2821 if (pureTranslate && !linearFilter) {
2822 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2823 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002824
Romain Guy16c88082012-06-11 16:03:47 -07002825 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002826 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002827 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002828 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002829 setupDrawDirtyRegionsDisabled();
2830 setupDrawWithTexture(true);
2831 setupDrawAlpha8Color(paint->getColor(), alpha);
2832 setupDrawColorFilter();
2833 setupDrawShader();
2834 setupDrawBlending(true, mode);
2835 setupDrawProgram();
2836 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002837 // See comment above; the font renderer must use texture unit 0
2838 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002839 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2840 setupDrawPureColorUniforms();
2841 setupDrawColorFilterUniforms();
2842 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002843 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002844
Romain Guya3dc55f2012-09-28 13:55:44 -07002845 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2846 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002847 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2848
Romain Guy211370f2012-02-01 16:10:55 -08002849 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002850
Raph Levien996e57c2012-07-23 15:22:52 -07002851 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002852 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002853 SkPaint paintCopy(*paint);
2854 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2855 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002856 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002857 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002858 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002859 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002860 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002861
2862 if (status && hasActiveLayer) {
2863 if (!pureTranslate) {
2864 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002865 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002866 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002867 }
Romain Guy694b5192010-07-21 21:33:20 -07002868
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002869 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002870
2871 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002872}
2873
Chet Haase48659092012-05-31 15:21:51 -07002874status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002875 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002876 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2877 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002878 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002879 }
2880
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002881 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002882 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002883
2884 int alpha;
2885 SkXfermode::Mode mode;
2886 getAlphaAndMode(paint, &alpha, &mode);
2887
2888 mCaches.activeTexture(0);
2889 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002890 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002891 setupDrawDirtyRegionsDisabled();
2892 setupDrawWithTexture(true);
2893 setupDrawAlpha8Color(paint->getColor(), alpha);
2894 setupDrawColorFilter();
2895 setupDrawShader();
2896 setupDrawBlending(true, mode);
2897 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002898 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002899 setupDrawTexture(fontRenderer.getTexture(true));
2900 setupDrawPureColorUniforms();
2901 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002902 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002903 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002904
Romain Guy97771732012-02-28 18:17:02 -08002905 const Rect* clip = &mSnapshot->getLocalClip();
2906 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 -08002907
Romain Guy97771732012-02-28 18:17:02 -08002908 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002909
2910 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2911 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002912 if (hasActiveLayer) {
2913 mSnapshot->transform->mapRect(bounds);
2914 dirtyLayerUnchecked(bounds, getRegion());
2915 }
Romain Guy97771732012-02-28 18:17:02 -08002916 }
Chet Haase48659092012-05-31 15:21:51 -07002917
2918 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002919}
2920
Chet Haase48659092012-05-31 15:21:51 -07002921status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2922 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002923
Romain Guya1d3c912011-12-13 14:55:06 -08002924 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002925
Romain Guyfb8b7632010-08-23 21:05:08 -07002926 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002927 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002928 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002929
Romain Guy8b55f372010-08-18 17:10:07 -07002930 const float x = texture->left - texture->offset;
2931 const float y = texture->top - texture->offset;
2932
Romain Guy01d58e42011-01-19 21:54:02 -08002933 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002934
2935 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002936}
2937
Chet Haase48659092012-05-31 15:21:51 -07002938status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002939 if (!layer) {
2940 return DrawGlInfo::kStatusDone;
2941 }
2942
Romain Guyb2e2f242012-10-17 18:18:35 -07002943 mat4* transform = NULL;
2944 if (layer->isTextureLayer()) {
2945 transform = &layer->getTransform();
2946 if (!transform->isIdentity()) {
2947 save(0);
2948 mSnapshot->transform->multiply(*transform);
2949 }
2950 }
2951
Romain Guy35643dd2012-09-18 15:40:58 -07002952 Rect transformed;
2953 Rect clip;
2954 const bool rejected = quickRejectNoScissor(x, y,
2955 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2956
2957 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002958 if (transform && !transform->isIdentity()) {
2959 restore();
2960 }
Chet Haase48659092012-05-31 15:21:51 -07002961 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002962 }
2963
Romain Guy5bb3c732012-11-29 17:52:58 -08002964 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002965
Romain Guy87e2f7572012-09-24 11:37:12 -07002966 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002967 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002968
Romain Guy211370f2012-02-01 16:10:55 -08002969 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002970 SkiaColorFilter* oldFilter = mColorFilter;
2971 mColorFilter = layer->getColorFilter();
2972
Romain Guyc88e3572011-01-22 00:32:12 -08002973 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002974 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002975 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002976 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002977 setupDraw();
2978 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002979 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002980 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002981 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002982 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002983 setupDrawPureColorUniforms();
2984 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002985 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002986 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002987 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2988 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002989
Romain Guyd21b6e12011-11-30 20:21:23 -08002990 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002991 setupDrawModelViewTranslate(tx, ty,
2992 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002993 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002994 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002995 setupDrawModelViewTranslate(x, y,
2996 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2997 }
Romain Guyc88e3572011-01-22 00:32:12 -08002998 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002999
Romain Guyc88e3572011-01-22 00:32:12 -08003000 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3001 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003002
Romain Guyc88e3572011-01-22 00:32:12 -08003003 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003004
3005#if DEBUG_LAYERS_AS_REGIONS
3006 drawRegionRects(layer->region);
3007#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003008 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003009
Romain Guye529ece2012-09-26 11:23:17 -07003010 mColorFilter = oldFilter;
3011
Romain Guy5bb3c732012-11-29 17:52:58 -08003012 if (layer->debugDrawUpdate) {
3013 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003014 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3015 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3016 }
Romain Guyf219da52011-01-16 12:54:25 -08003017 }
Chet Haase48659092012-05-31 15:21:51 -07003018
Romain Guyb2e2f242012-10-17 18:18:35 -07003019 if (transform && !transform->isIdentity()) {
3020 restore();
3021 }
3022
Chet Haase48659092012-05-31 15:21:51 -07003023 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003024}
3025
Romain Guy6926c722010-07-12 20:20:03 -07003026///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003027// Shaders
3028///////////////////////////////////////////////////////////////////////////////
3029
3030void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07003031 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003032}
3033
Romain Guy06f96e22010-07-30 19:18:16 -07003034void OpenGLRenderer::setupShader(SkiaShader* shader) {
3035 mShader = shader;
3036 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003037 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003038 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003039}
3040
Romain Guyd27977d2010-07-14 19:18:51 -07003041///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003042// Color filters
3043///////////////////////////////////////////////////////////////////////////////
3044
3045void OpenGLRenderer::resetColorFilter() {
3046 mColorFilter = NULL;
3047}
3048
3049void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
3050 mColorFilter = filter;
3051}
3052
3053///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003054// Drop shadow
3055///////////////////////////////////////////////////////////////////////////////
3056
3057void OpenGLRenderer::resetShadow() {
3058 mHasShadow = false;
3059}
3060
3061void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
3062 mHasShadow = true;
3063 mShadowRadius = radius;
3064 mShadowDx = dx;
3065 mShadowDy = dy;
3066 mShadowColor = color;
3067}
3068
3069///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003070// Draw filters
3071///////////////////////////////////////////////////////////////////////////////
3072
3073void OpenGLRenderer::resetPaintFilter() {
3074 mHasDrawFilter = false;
3075}
3076
3077void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
3078 mHasDrawFilter = true;
3079 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3080 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
3081}
3082
3083SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08003084 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08003085
3086 uint32_t flags = paint->getFlags();
3087
3088 mFilteredPaint = *paint;
3089 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
3090
3091 return &mFilteredPaint;
3092}
3093
3094///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003095// Drawing implementation
3096///////////////////////////////////////////////////////////////////////////////
3097
Romain Guy01d58e42011-01-19 21:54:02 -08003098void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3099 float x, float y, SkPaint* paint) {
3100 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3101 return;
3102 }
3103
3104 int alpha;
3105 SkXfermode::Mode mode;
3106 getAlphaAndMode(paint, &alpha, &mode);
3107
3108 setupDraw();
3109 setupDrawWithTexture(true);
3110 setupDrawAlpha8Color(paint->getColor(), alpha);
3111 setupDrawColorFilter();
3112 setupDrawShader();
3113 setupDrawBlending(true, mode);
3114 setupDrawProgram();
3115 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3116 setupDrawTexture(texture->id);
3117 setupDrawPureColorUniforms();
3118 setupDrawColorFilterUniforms();
3119 setupDrawShaderUniforms();
3120 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3121
3122 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3123
3124 finishDrawTexture();
3125}
3126
Romain Guyf607bdc2010-09-10 19:20:06 -07003127// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003128#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3129#define kStdUnderline_Offset (1.0f / 9.0f)
3130#define kStdUnderline_Thickness (1.0f / 18.0f)
3131
3132void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3133 float x, float y, SkPaint* paint) {
3134 // Handle underline and strike-through
3135 uint32_t flags = paint->getFlags();
3136 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003137 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003138 float underlineWidth = length;
3139 // If length is > 0.0f, we already measured the text for the text alignment
3140 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003141 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003142 }
3143
Romain Guy211370f2012-02-01 16:10:55 -08003144 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003145 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003146 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003147
Raph Levien8b4072d2012-07-30 15:50:00 -07003148 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003149 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003150
Romain Guyf6834472011-01-23 13:32:12 -08003151 int linesCount = 0;
3152 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3153 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3154
3155 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003156 float points[pointsCount];
3157 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003158
3159 if (flags & SkPaint::kUnderlineText_Flag) {
3160 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003161 points[currentPoint++] = left;
3162 points[currentPoint++] = top;
3163 points[currentPoint++] = left + underlineWidth;
3164 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003165 }
3166
3167 if (flags & SkPaint::kStrikeThruText_Flag) {
3168 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003169 points[currentPoint++] = left;
3170 points[currentPoint++] = top;
3171 points[currentPoint++] = left + underlineWidth;
3172 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003173 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003174
Romain Guy726aeba2011-06-01 14:52:00 -07003175 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003176
Romain Guy726aeba2011-06-01 14:52:00 -07003177 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003178 }
3179 }
3180}
3181
Romain Guy672433d2013-01-04 19:05:13 -08003182status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3183 if (mSnapshot->isIgnored()) {
3184 return DrawGlInfo::kStatusDone;
3185 }
3186
Romain Guy735738c2012-12-03 12:34:51 -08003187 int color = paint->getColor();
3188 // If a shader is set, preserve only the alpha
3189 if (mShader) {
3190 color |= 0x00ffffff;
3191 }
3192 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3193
3194 return drawColorRects(rects, count, color, mode);
3195}
3196
3197status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy8ce00302013-01-15 18:51:42 -08003198 SkXfermode::Mode mode, bool ignoreTransform, bool dirty) {
Romain Guy735738c2012-12-03 12:34:51 -08003199
Romain Guy672433d2013-01-04 19:05:13 -08003200 float left = FLT_MAX;
3201 float top = FLT_MAX;
3202 float right = FLT_MIN;
3203 float bottom = FLT_MIN;
3204
3205 int vertexCount = 0;
3206 Vertex mesh[count * 6];
3207 Vertex* vertex = mesh;
3208
Chris Craik2af46352012-11-26 18:30:17 -08003209 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003210 float l = rects[index + 0];
3211 float t = rects[index + 1];
3212 float r = rects[index + 2];
3213 float b = rects[index + 3];
3214
Romain Guy8ce00302013-01-15 18:51:42 -08003215 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003216 Vertex::set(vertex++, l, b);
3217 Vertex::set(vertex++, l, t);
3218 Vertex::set(vertex++, r, t);
3219 Vertex::set(vertex++, l, b);
3220 Vertex::set(vertex++, r, t);
3221 Vertex::set(vertex++, r, b);
3222
3223 vertexCount += 6;
3224
3225 left = fminf(left, l);
3226 top = fminf(top, t);
3227 right = fmaxf(right, r);
3228 bottom = fmaxf(bottom, b);
3229 }
3230 }
3231
3232 if (count == 0) return DrawGlInfo::kStatusDone;
3233
Romain Guy672433d2013-01-04 19:05:13 -08003234 setupDraw();
3235 setupDrawNoTexture();
3236 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3237 setupDrawShader();
3238 setupDrawColorFilter();
3239 setupDrawBlending(mode);
3240 setupDrawProgram();
3241 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003242 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003243 setupDrawColorUniforms();
3244 setupDrawShaderUniforms();
3245 setupDrawColorFilterUniforms();
3246 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3247
Romain Guy8ce00302013-01-15 18:51:42 -08003248 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003249 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3250 }
3251
3252 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3253
3254 return DrawGlInfo::kStatusDrew;
3255}
3256
Romain Guy026c5e162010-06-28 17:12:22 -07003257void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003258 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003259 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003260 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003261 color |= 0x00ffffff;
3262 }
3263
Romain Guy70ca14e2010-12-13 18:24:33 -08003264 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003265 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003266 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDrawShader();
3268 setupDrawColorFilter();
3269 setupDrawBlending(mode);
3270 setupDrawProgram();
3271 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3272 setupDrawColorUniforms();
3273 setupDrawShaderUniforms(ignoreTransform);
3274 setupDrawColorFilterUniforms();
3275 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003276
Romain Guyc95c8d62010-09-17 15:31:32 -07003277 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3278}
3279
Romain Guy82ba8142010-07-09 13:25:56 -07003280void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003281 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003282 int alpha;
3283 SkXfermode::Mode mode;
3284 getAlphaAndMode(paint, &alpha, &mode);
3285
Romain Guyd21b6e12011-11-30 20:21:23 -08003286 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003287
Romain Guy211370f2012-02-01 16:10:55 -08003288 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003289 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3290 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3291
Romain Guyd21b6e12011-11-30 20:21:23 -08003292 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003293 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3294 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3295 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3296 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003297 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003298 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3299 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3300 GL_TRIANGLE_STRIP, gMeshCount);
3301 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003302}
3303
Romain Guybd6b79b2010-06-26 00:13:53 -07003304void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003305 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3306 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003307 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003308}
3309
3310void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003311 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003312 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003313 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003314
Romain Guy746b7402010-10-26 16:27:31 -07003315 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003316 setupDrawWithTexture();
3317 setupDrawColor(alpha, alpha, alpha, alpha);
3318 setupDrawColorFilter();
3319 setupDrawBlending(blend, mode, swapSrcDst);
3320 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003321 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003322 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003323 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003324 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003325 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003326 }
Romain Guy886b2752013-01-04 12:26:18 -08003327 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003328 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003329 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003331
Romain Guy6820ac82010-09-15 18:11:50 -07003332 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003333
3334 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003335}
3336
Romain Guy886b2752013-01-04 12:26:18 -08003337void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3338 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3339 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3340 bool ignoreTransform, bool dirty) {
3341
3342 setupDraw();
3343 setupDrawWithTexture(true);
3344 if (hasColor) {
3345 setupDrawAlpha8Color(color, alpha);
3346 }
3347 setupDrawColorFilter();
3348 setupDrawShader();
3349 setupDrawBlending(true, mode);
3350 setupDrawProgram();
3351 if (!dirty) setupDrawDirtyRegionsDisabled();
3352 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3353 setupDrawTexture(texture);
3354 setupDrawPureColorUniforms();
3355 setupDrawColorFilterUniforms();
3356 setupDrawShaderUniforms();
3357 setupDrawMesh(vertices, texCoords);
3358
3359 glDrawArrays(drawMode, 0, elementsCount);
3360
3361 finishDrawTexture();
3362}
3363
Romain Guya5aed0d2010-09-09 14:42:43 -07003364void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003365 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003366 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003367
Romain Guy82ba8142010-07-09 13:25:56 -07003368 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003369 // These blend modes are not supported by OpenGL directly and have
3370 // to be implemented using shaders. Since the shader will perform
3371 // the blending, turn blending off here
3372 // If the blend mode cannot be implemented using shaders, fall
3373 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003374 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003375 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003376 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003377 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003378
Romain Guy82bc7a72012-01-03 14:13:39 -08003379 if (mCaches.blend) {
3380 glDisable(GL_BLEND);
3381 mCaches.blend = false;
3382 }
3383
3384 return;
3385 } else {
3386 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003387 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003388 }
3389
3390 if (!mCaches.blend) {
3391 glEnable(GL_BLEND);
3392 }
3393
3394 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3395 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3396
3397 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3398 glBlendFunc(sourceMode, destMode);
3399 mCaches.lastSrcMode = sourceMode;
3400 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003401 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003402 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003403 glDisable(GL_BLEND);
3404 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003405 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003406}
3407
Romain Guy889f8d12010-07-29 14:37:42 -07003408bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003409 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003410 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003411 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003412 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003413 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003414 }
Romain Guy6926c722010-07-12 20:20:03 -07003415 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003416}
3417
Romain Guy026c5e162010-06-28 17:12:22 -07003418void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003419 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003420 TextureVertex::setUV(v++, u1, v1);
3421 TextureVertex::setUV(v++, u2, v1);
3422 TextureVertex::setUV(v++, u1, v2);
3423 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003424}
3425
Chet Haase5c13d892010-10-08 08:37:55 -07003426void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003427 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003428 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003429}
3430
Romain Guy9d5316e2010-06-24 19:30:36 -07003431}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003432}; // namespace android