blob: 1c36a23fb86c910332f561f7f867e46d42addb03 [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"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700115 mDrawModifiers.mShader = NULL;
116 mDrawModifiers.mColorFilter = NULL;
117 mDrawModifiers.mHasShadow = false;
118 mDrawModifiers.mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700123 mFrameStarted = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700124
125 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700126}
127
Romain Guy85bf02f2010-06-22 13:11:24 -0700128OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700129 // The context has already been destroyed at this point, do not call
130 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700131}
132
Romain Guy87e2f7572012-09-24 11:37:12 -0700133void OpenGLRenderer::initProperties() {
134 char property[PROPERTY_VALUE_MAX];
135 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
136 mScissorOptimizationDisabled = !strcasecmp(property, "true");
137 INIT_LOGD(" Scissor optimization %s",
138 mScissorOptimizationDisabled ? "disabled" : "enabled");
139 } else {
140 INIT_LOGD(" Scissor optimization enabled");
141 }
Romain Guy13631f32012-01-30 17:41:55 -0800142}
143
144///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700145// Setup
146///////////////////////////////////////////////////////////////////////////////
147
Romain Guyef359272013-01-31 19:07:29 -0800148void OpenGLRenderer::setName(const char* name) {
149 if (name) {
150 mName.setTo(name);
151 } else {
152 mName.clear();
153 }
154}
155
156const char* OpenGLRenderer::getName() const {
157 return mName.string();
158}
159
Romain Guy49c5fc02012-05-15 11:10:01 -0700160bool OpenGLRenderer::isDeferred() {
161 return false;
162}
163
Romain Guy85bf02f2010-06-22 13:11:24 -0700164void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700165 initViewport(width, height);
166
167 glDisable(GL_DITHER);
168 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
169
170 glEnableVertexAttribArray(Program::kBindingPosition);
171}
172
173void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700174 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700175
176 mWidth = width;
177 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700178
179 mFirstSnapshot->height = height;
180 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700181}
182
Romain Guy96885eb2013-03-26 15:05:58 -0700183void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800184 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800185 mCaches.clearGarbage();
186
Romain Guy96885eb2013-03-26 15:05:58 -0700187 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700188 mSnapshot = new Snapshot(mFirstSnapshot,
189 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800190 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700191 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700192
Romain Guy7d7b5492011-01-24 16:33:45 -0800193 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700194 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700195}
196
197status_t OpenGLRenderer::startFrame() {
198 if (mFrameStarted) return DrawGlInfo::kStatusDone;
199 mFrameStarted = true;
200
Romain Guy41308e22012-10-22 20:02:43 -0700201 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700202
Romain Guy96885eb2013-03-26 15:05:58 -0700203 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700204
Romain Guy96885eb2013-03-26 15:05:58 -0700205 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800206
Romain Guy54c1a642012-09-27 17:55:46 -0700207 // Functors break the tiling extension in pretty spectacular ways
208 // This ensures we don't use tiling when a functor is going to be
209 // invoked during the frame
210 mSuppressTiling = mCaches.hasRegisteredFunctors();
211
Chris Craik5f803622013-03-21 14:39:04 -0700212 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700213
Romain Guy7c450aa2012-09-21 19:15:00 -0700214 debugOverdraw(true, true);
215
Romain Guy96885eb2013-03-26 15:05:58 -0700216 return clear(mTilingClip.left, mTilingClip.top,
217 mTilingClip.right, mTilingClip.bottom, mOpaque);
218}
219
220status_t OpenGLRenderer::prepare(bool opaque) {
221 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
222}
223
224status_t OpenGLRenderer::prepareDirty(float left, float top,
225 float right, float bottom, bool opaque) {
226 setupFrameState(left, top, right, bottom, opaque);
227
228 // Layer renderers will start the frame immediately
229 // The framebuffer renderer will first defer the display list
230 // for each layer and wait until the first drawing command
231 // to start the frame
232 if (mSnapshot->fbo == 0) {
233 syncState();
234 updateLayers();
235 } else {
236 return startFrame();
237 }
238
239 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700240}
241
Romain Guydcfc8362013-01-03 13:08:57 -0800242void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
243 // If we know that we are going to redraw the entire framebuffer,
244 // perform a discard to let the driver know we don't need to preserve
245 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800246 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800247 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800248 const bool isFbo = getTargetFbo() == 0;
249 const GLenum attachments[] = {
250 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
251 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800252 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
253 }
254}
255
Romain Guy7c25aab2012-10-18 15:05:02 -0700256status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700257 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700258 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700259 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700260 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700261 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700262 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700263
Romain Guy7c25aab2012-10-18 15:05:02 -0700264 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700265 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700266}
267
268void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700269 if (mCaches.blend) {
270 glEnable(GL_BLEND);
271 } else {
272 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
Romain Guy57b52682012-09-20 17:38:46 -0700276void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700277 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700278 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800279 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700280 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700281 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700282
Romain Guyc3fedaf2013-01-29 17:26:25 -0800283 startTiling(*clip, s->height, opaque);
284 }
285}
286
287void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
288 if (!mSuppressTiling) {
289 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800290 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292}
293
294void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700295 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700296}
297
Romain Guyb025b9c2010-09-16 14:16:48 -0700298void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700299 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700300 endTiling();
301
Romain Guyca89e2a2013-03-08 17:44:20 -0800302 // When finish() is invoked on FBO 0 we've reached the end
303 // of the current frame
304 if (getTargetFbo() == 0) {
305 mCaches.pathCache.trim();
306 }
307
Romain Guy11cb6422012-09-21 00:39:43 -0700308 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700309#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700310 GLenum status = GL_NO_ERROR;
311 while ((status = glGetError()) != GL_NO_ERROR) {
312 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
313 switch (status) {
314 case GL_INVALID_ENUM:
315 ALOGE(" GL_INVALID_ENUM");
316 break;
317 case GL_INVALID_VALUE:
318 ALOGE(" GL_INVALID_VALUE");
319 break;
320 case GL_INVALID_OPERATION:
321 ALOGE(" GL_INVALID_OPERATION");
322 break;
323 case GL_OUT_OF_MEMORY:
324 ALOGE(" Out of memory!");
325 break;
326 }
Romain Guya07105b2011-01-10 21:14:18 -0800327 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700328#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700329
Romain Guyc15008e2010-11-10 11:59:15 -0800330#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800331 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700332#else
333 if (mCaches.getDebugLevel() & kDebugMemory) {
334 mCaches.dumpMemoryUsage();
335 }
Romain Guyc15008e2010-11-10 11:59:15 -0800336#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700337 }
Romain Guy96885eb2013-03-26 15:05:58 -0700338
339 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700340}
341
Romain Guy6c319ca2011-01-11 14:29:25 -0800342void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700343 if (mCaches.currentProgram) {
344 if (mCaches.currentProgram->isInUse()) {
345 mCaches.currentProgram->remove();
346 mCaches.currentProgram = NULL;
347 }
348 }
Romain Guy50c0f092010-10-19 11:42:22 -0700349 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800350 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800351 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800352 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700353 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800357 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800358 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700359 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700360 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700361
Romain Guy3e263fa2011-12-12 16:47:48 -0800362 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
363
Chet Haase80250612012-08-15 13:46:54 -0700364 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700365 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800366 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700367 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700368
Romain Guya1d3c912011-12-13 14:55:06 -0800369 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700370
Romain Guy50c0f092010-10-19 11:42:22 -0700371 mCaches.blend = true;
372 glEnable(GL_BLEND);
373 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
374 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700375}
376
Romain Guy35643dd2012-09-18 15:40:58 -0700377void OpenGLRenderer::resumeAfterLayer() {
378 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
379 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
380 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700381 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700382
383 mCaches.resetScissor();
384 dirtyClip();
385}
386
Romain Guyba6be8a2012-04-23 18:22:09 -0700387void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700388 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700389}
390
391void OpenGLRenderer::attachFunctor(Functor* functor) {
392 mFunctors.add(functor);
393}
394
Romain Guy8f3b8e32012-03-27 16:33:45 -0700395status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
396 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700397 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700398
Romain Guyba6be8a2012-04-23 18:22:09 -0700399 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800400 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700401 SortedVector<Functor*> functors(mFunctors);
402 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700403
Romain Guyba6be8a2012-04-23 18:22:09 -0700404 DrawGlInfo info;
405 info.clipLeft = 0;
406 info.clipTop = 0;
407 info.clipRight = 0;
408 info.clipBottom = 0;
409 info.isLayer = false;
410 info.width = 0;
411 info.height = 0;
412 memset(info.transform, 0, sizeof(float) * 16);
413
414 for (size_t i = 0; i < count; i++) {
415 Functor* f = functors.itemAt(i);
416 result |= (*f)(DrawGlInfo::kModeProcess, &info);
417
Chris Craikc2c95432012-04-25 15:13:52 -0700418 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700419 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
420 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700421 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700422
Chris Craikc2c95432012-04-25 15:13:52 -0700423 if (result & DrawGlInfo::kStatusInvoke) {
424 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700425 }
426 }
Chris Craikd15321b2012-11-28 14:45:04 -0800427 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700428 }
429
430 return result;
431}
432
433status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800434 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700435 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700436
Romain Guy8a4ac612012-07-17 17:32:48 -0700437 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800438 if (mDirtyClip) {
439 setScissorFromClip();
440 }
Romain Guyd643bb52011-03-01 14:55:21 -0800441
Romain Guy80911b82011-03-16 15:30:12 -0700442 Rect clip(*mSnapshot->clipRect);
443 clip.snapToPixelBoundaries();
444
Romain Guyd643bb52011-03-01 14:55:21 -0800445 // Since we don't know what the functor will draw, let's dirty
446 // tne entire clip region
447 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800448 dirtyLayerUnchecked(clip, getRegion());
449 }
Romain Guyd643bb52011-03-01 14:55:21 -0800450
Romain Guy08aa2cb2011-03-17 11:06:57 -0700451 DrawGlInfo info;
452 info.clipLeft = clip.left;
453 info.clipTop = clip.top;
454 info.clipRight = clip.right;
455 info.clipBottom = clip.bottom;
456 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700457 info.width = getSnapshot()->viewport.getWidth();
458 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700459 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700460
Chet Haase48659092012-05-31 15:21:51 -0700461 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800462
Romain Guy8f3b8e32012-03-27 16:33:45 -0700463 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800465 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700466
Chris Craik65924a32012-04-05 17:52:11 -0700467 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700468 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700469 }
Romain Guycabfcc12011-03-07 18:06:46 -0800470 }
471
Chet Haasedaf98e92011-01-10 14:10:36 -0800472 resume();
Romain Guy65549432012-03-26 16:45:05 -0700473 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800474}
475
Romain Guyf6a11b82010-06-23 17:47:49 -0700476///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700477// Debug
478///////////////////////////////////////////////////////////////////////////////
479
Romain Guy0f6675332013-03-01 14:31:04 -0800480void OpenGLRenderer::eventMark(const char* name) const {
481 mCaches.eventMark(0, name);
482}
483
Romain Guy87e2f7572012-09-24 11:37:12 -0700484void OpenGLRenderer::startMark(const char* name) const {
485 mCaches.startMark(0, name);
486}
487
488void OpenGLRenderer::endMark() const {
489 mCaches.endMark();
490}
491
492void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
493 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
494 if (clear) {
495 mCaches.disableScissor();
496 mCaches.stencil.clear();
497 }
498 if (enable) {
499 mCaches.stencil.enableDebugWrite();
500 } else {
501 mCaches.stencil.disable();
502 }
503 }
504}
505
506void OpenGLRenderer::renderOverdraw() {
507 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700508 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700509
510 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700511 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700512 clip->right - clip->left, clip->bottom - clip->top);
513
514 mCaches.stencil.enableDebugTest(2);
515 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
516 mCaches.stencil.enableDebugTest(3);
517 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
518 mCaches.stencil.enableDebugTest(4);
519 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
520 mCaches.stencil.enableDebugTest(4, true);
521 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
522 mCaches.stencil.disable();
523 }
524}
525
526///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700527// Layers
528///////////////////////////////////////////////////////////////////////////////
529
530bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700531 if (layer->deferredUpdateScheduled && layer->renderer &&
532 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700533 Rect& dirty = layer->dirtyRect;
534
Romain Guy7c450aa2012-09-21 19:15:00 -0700535 if (inFrame) {
536 endTiling();
537 debugOverdraw(false, false);
538 }
Romain Guy11cb6422012-09-21 00:39:43 -0700539
Romain Guy96885eb2013-03-26 15:05:58 -0700540 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
541 OpenGLRenderer* renderer = layer->renderer;
542 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
543 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom,
544 !layer->isBlend());
545 renderer->drawDisplayList(layer->displayList, dirty,
546 DisplayList::kReplayFlag_ClipChildren);
547 renderer->finish();
548 } else {
549 layer->defer();
550 }
Romain Guy11cb6422012-09-21 00:39:43 -0700551
552 if (inFrame) {
553 resumeAfterLayer();
554 startTiling(mSnapshot);
555 }
556
Romain Guy96885eb2013-03-26 15:05:58 -0700557 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
558 dirty.setEmpty();
559 layer->renderer = NULL;
560 }
561
Romain Guy11cb6422012-09-21 00:39:43 -0700562 layer->deferredUpdateScheduled = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700563 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800564 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700565
566 return true;
567 }
568
569 return false;
570}
571
572void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700573 // If draw deferring is enabled this method will simply defer
574 // the display list of each individual layer. The layers remain
575 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700576 int count = mLayerUpdates.size();
577 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
579 startMark("Layer Updates");
580 } else {
581 startMark("Defer Layer Updates");
582 }
Romain Guy11cb6422012-09-21 00:39:43 -0700583
584 // Note: it is very important to update the layers in reverse order
585 for (int i = count - 1; i >= 0; i--) {
586 Layer* layer = mLayerUpdates.itemAt(i);
587 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700588 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
589 mCaches.resourceCache.decrementRefcount(layer);
590 }
Romain Guy11cb6422012-09-21 00:39:43 -0700591 }
Romain Guy11cb6422012-09-21 00:39:43 -0700592
Romain Guy96885eb2013-03-26 15:05:58 -0700593 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
594 mLayerUpdates.clear();
595 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
596 }
597 endMark();
598 }
599}
600
601void OpenGLRenderer::flushLayers() {
602 int count = mLayerUpdates.size();
603 if (count > 0) {
604 startMark("Apply Layer Updates");
605 char layerName[12];
606
607 // Note: it is very important to update the layers in reverse order
608 for (int i = count - 1; i >= 0; i--) {
609 sprintf(layerName, "Layer #%d", i);
610 startMark(layerName); {
611 Layer* layer = mLayerUpdates.itemAt(i);
612 layer->flush();
613 mCaches.resourceCache.decrementRefcount(layer);
614 }
615 endMark();
616 }
617
618 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700619 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700620
Romain Guy11cb6422012-09-21 00:39:43 -0700621 endMark();
622 }
623}
624
625void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
626 if (layer) {
627 mLayerUpdates.push_back(layer);
628 mCaches.resourceCache.incrementRefcount(layer);
629 }
630}
631
632void OpenGLRenderer::clearLayerUpdates() {
633 size_t count = mLayerUpdates.size();
634 if (count > 0) {
635 mCaches.resourceCache.lock();
636 for (size_t i = 0; i < count; i++) {
637 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
638 }
639 mCaches.resourceCache.unlock();
640 mLayerUpdates.clear();
641 }
642}
643
644///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700645// State management
646///////////////////////////////////////////////////////////////////////////////
647
Romain Guybb9524b2010-06-22 18:56:38 -0700648int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700649 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700650}
651
652int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700653 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700654}
655
656void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700657 if (mSaveCount > 1) {
658 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700659 }
Romain Guybb9524b2010-06-22 18:56:38 -0700660}
661
662void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700663 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700664
Romain Guy8fb95422010-08-17 18:38:51 -0700665 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700666 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700667 }
Romain Guybb9524b2010-06-22 18:56:38 -0700668}
669
Romain Guy8aef54f2010-09-01 15:13:49 -0700670int OpenGLRenderer::saveSnapshot(int flags) {
671 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700672 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700673}
674
675bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700676 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700677 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700678 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700679
Romain Guybd6b79b2010-06-26 00:13:53 -0700680 sp<Snapshot> current = mSnapshot;
681 sp<Snapshot> previous = mSnapshot->previous;
682
Romain Guyeb993562010-10-05 18:14:38 -0700683 if (restoreOrtho) {
684 Rect& r = previous->viewport;
685 glViewport(r.left, r.top, r.right, r.bottom);
686 mOrthoMatrix.load(current->orthoMatrix);
687 }
688
Romain Guy8b55f372010-08-18 17:10:07 -0700689 mSaveCount--;
690 mSnapshot = previous;
691
Romain Guy2542d192010-08-18 11:47:12 -0700692 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700693 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700694 }
Romain Guy2542d192010-08-18 11:47:12 -0700695
Romain Guy5ec99242010-11-03 16:19:08 -0700696 if (restoreLayer) {
697 composeLayer(current, previous);
698 }
699
Romain Guy2542d192010-08-18 11:47:12 -0700700 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700701}
702
Romain Guyf6a11b82010-06-23 17:47:49 -0700703///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700704// Layers
705///////////////////////////////////////////////////////////////////////////////
706
707int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800708 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700709 const GLuint previousFbo = mSnapshot->fbo;
710 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700711
Romain Guyaf636eb2010-12-09 17:47:21 -0800712 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700713 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700714 }
Romain Guyd55a8612010-06-28 17:42:46 -0700715
716 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700717}
718
Chris Craikd90144d2013-03-19 15:03:48 -0700719void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
720 const Rect untransformedBounds(bounds);
721
722 currentTransform().mapRect(bounds);
723
724 // Layers only make sense if they are in the framebuffer's bounds
725 if (bounds.intersect(*mSnapshot->clipRect)) {
726 // We cannot work with sub-pixels in this case
727 bounds.snapToPixelBoundaries();
728
729 // When the layer is not an FBO, we may use glCopyTexImage so we
730 // need to make sure the layer does not extend outside the bounds
731 // of the framebuffer
732 if (!bounds.intersect(mSnapshot->previous->viewport)) {
733 bounds.setEmpty();
734 } else if (fboLayer) {
735 clip.set(bounds);
736 mat4 inverse;
737 inverse.loadInverse(currentTransform());
738 inverse.mapRect(clip);
739 clip.snapToPixelBoundaries();
740 if (clip.intersect(untransformedBounds)) {
741 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
742 bounds.set(untransformedBounds);
743 } else {
744 clip.setEmpty();
745 }
746 }
747 } else {
748 bounds.setEmpty();
749 }
750}
751
752int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
753 int alpha, SkXfermode::Mode mode, int flags) {
754 const GLuint previousFbo = mSnapshot->fbo;
755 const int count = saveSnapshot(flags);
756
757 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
758 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
759 // operations will be able to store and restore the current clip and transform info, and
760 // quick rejection will be correct (for display lists)
761
762 Rect bounds(left, top, right, bottom);
763 Rect clip;
764 calculateLayerBoundsAndClip(bounds, clip, true);
765
766 if (!bounds.isEmpty() && !clip.isEmpty()) {
767 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
768 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
769 }
770 }
771
772 return count;
773}
774
775
Romain Guy1c740bc2010-09-13 18:00:09 -0700776/**
777 * Layers are viewed by Skia are slightly different than layers in image editing
778 * programs (for instance.) When a layer is created, previously created layers
779 * and the frame buffer still receive every drawing command. For instance, if a
780 * layer is created and a shape intersecting the bounds of the layers and the
781 * framebuffer is draw, the shape will be drawn on both (unless the layer was
782 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
783 *
784 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
785 * texture. Unfortunately, this is inefficient as it requires every primitive to
786 * be drawn n + 1 times, where n is the number of active layers. In practice this
787 * means, for every primitive:
788 * - Switch active frame buffer
789 * - Change viewport, clip and projection matrix
790 * - Issue the drawing
791 *
792 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700793 * To avoid this, layers are implemented in a different way here, at least in the
794 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
795 * is set. When this flag is set we can redirect all drawing operations into a
796 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700797 *
798 * This implementation relies on the frame buffer being at least RGBA 8888. When
799 * a layer is created, only a texture is created, not an FBO. The content of the
800 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700801 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700802 * buffer and drawing continues as normal. This technique therefore treats the
803 * frame buffer as a scratch buffer for the layers.
804 *
805 * To compose the layers back onto the frame buffer, each layer texture
806 * (containing the original frame buffer data) is drawn as a simple quad over
807 * the frame buffer. The trick is that the quad is set as the composition
808 * destination in the blending equation, and the frame buffer becomes the source
809 * of the composition.
810 *
811 * Drawing layers with an alpha value requires an extra step before composition.
812 * An empty quad is drawn over the layer's region in the frame buffer. This quad
813 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
814 * quad is used to multiply the colors in the frame buffer. This is achieved by
815 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
816 * GL_ZERO, GL_SRC_ALPHA.
817 *
818 * Because glCopyTexImage2D() can be slow, an alternative implementation might
819 * be use to draw a single clipped layer. The implementation described above
820 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700821 *
822 * (1) The frame buffer is actually not cleared right away. To allow the GPU
823 * to potentially optimize series of calls to glCopyTexImage2D, the frame
824 * buffer is left untouched until the first drawing operation. Only when
825 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700826 */
Chet Haased48885a2012-08-28 17:43:28 -0700827bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
828 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700829 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700830 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700831
Romain Guyeb993562010-10-05 18:14:38 -0700832 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
833
Romain Guyf607bdc2010-09-10 19:20:06 -0700834 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700835 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700836 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700837 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Romain Guybf434112010-09-16 14:40:17 -0700838
Romain Guy746b7402010-10-26 16:27:31 -0700839 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700840 bounds.getHeight() > mCaches.maxTextureSize ||
841 (fboLayer && clip.isEmpty())) {
842 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700843 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700844 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700845 }
846
847 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700848 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700849 return false;
850 }
Romain Guyf18fd992010-07-08 11:45:51 -0700851
Romain Guya1d3c912011-12-13 14:55:06 -0800852 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700853 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700854 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700855 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700856 }
857
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700859 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700860 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
861 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800862 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700863 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700864 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700865
Romain Guy8fb95422010-08-17 18:38:51 -0700866 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700867 mSnapshot->flags |= Snapshot::kFlagIsLayer;
868 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700869
Romain Guyeb993562010-10-05 18:14:38 -0700870 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700871 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700872 } else {
873 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700874 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800875 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700876 if (layer->isEmpty()) {
877 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700878 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 layer->getWidth(), layer->getHeight(), 0);
880 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800881 } else {
882 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700883 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800884 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700885
Romain Guy54be1cd2011-06-13 19:04:27 -0700886 // Enqueue the buffer coordinates to clear the corresponding region later
887 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700888 }
Romain Guyeb993562010-10-05 18:14:38 -0700889 }
Romain Guyf86ef572010-07-01 11:05:42 -0700890
Romain Guyd55a8612010-06-28 17:42:46 -0700891 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700892}
893
Chet Haased48885a2012-08-28 17:43:28 -0700894bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800895 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700897
Chet Haased48885a2012-08-28 17:43:28 -0700898 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800899 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
900 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700901 mSnapshot->fbo = layer->getFbo();
902 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
903 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
904 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
905 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700906 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700907
Romain Guy2b7028e2012-09-19 17:25:38 -0700908 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700909 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700910 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
912 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700913
914 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 if (layer->isEmpty()) {
916 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
917 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700918 }
919
920 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700922
Romain Guyf735c8e2013-01-31 17:45:55 -0800923 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700924
925 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700926 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800927 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700928 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700929 glClear(GL_COLOR_BUFFER_BIT);
930
931 dirtyClip();
932
933 // Change the ortho projection
934 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
935 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
936
937 return true;
938}
939
Romain Guy1c740bc2010-09-13 18:00:09 -0700940/**
941 * Read the documentation of createLayer() before doing anything in this method.
942 */
Romain Guy1d83e192010-08-17 11:37:00 -0700943void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
944 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000945 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700946 return;
947 }
948
Romain Guy8ce00302013-01-15 18:51:42 -0800949 Layer* layer = current->layer;
950 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700951 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700952
953 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700954 endTiling();
955
Romain Guye0aa84b2012-04-03 19:30:26 -0700956 // Detach the texture from the FBO
957 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800958
959 layer->removeFbo(false);
960
Romain Guyeb993562010-10-05 18:14:38 -0700961 // Unbind current FBO and restore previous one
962 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700963 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700964
965 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700966 }
967
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700969 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700970 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700972 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700973 }
974
Romain Guy03750a02010-10-18 14:06:08 -0700975 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700976
Romain Guya1d3c912011-12-13 14:55:06 -0800977 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700978
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 // When the layer is stored in an FBO, we can save a bit of fillrate by
980 // drawing only the dirty region
981 if (fboLayer) {
982 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700983 if (layer->getColorFilter()) {
984 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800985 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700986 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800988 resetColorFilter();
989 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700990 } else if (!rect.isEmpty()) {
991 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
992 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700993 }
Romain Guy8b55f372010-08-18 17:10:07 -0700994
Romain Guy746b7402010-10-26 16:27:31 -0700995 dirtyClip();
996
Romain Guyeb993562010-10-05 18:14:38 -0700997 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700998 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700999 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001000 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001001 }
1002}
1003
Romain Guyaa6c24c2011-04-28 18:40:04 -07001004void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001005 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001006
1007 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001008 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001009 setupDrawWithTexture();
1010 } else {
1011 setupDrawWithExternalTexture();
1012 }
1013 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001014 setupDrawColor(alpha, alpha, alpha, alpha);
1015 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001017 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001018 setupDrawPureColorUniforms();
1019 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001020 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1021 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001022 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001023 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001024 }
Romain Guy3b753822013-03-05 10:27:35 -08001025 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001026 layer->getWidth() == (uint32_t) rect.getWidth() &&
1027 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001028 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1029 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001030
Romain Guyd21b6e12011-11-30 20:21:23 -08001031 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001032 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1033 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001034 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001035 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1036 }
1037 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001038 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1039
1040 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1041
1042 finishDrawTexture();
1043}
1044
Romain Guy5b3b3522010-10-27 18:57:51 -07001045void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001046 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001047 const Rect& texCoords = layer->texCoords;
1048 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1049 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001050
Romain Guy9ace8f52011-07-07 20:50:11 -07001051 float x = rect.left;
1052 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001053 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001054 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001055 layer->getHeight() == (uint32_t) rect.getHeight();
1056
1057 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 // When we're swapping, the layer is already in screen coordinates
1059 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001060 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1061 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001062 }
1063
Romain Guyd21b6e12011-11-30 20:21:23 -08001064 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001065 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001066 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001067 }
1068
Chris Craike83569c2013-03-20 16:57:09 -07001069 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
1070 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001072 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001073 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1074 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001075
Romain Guyaa6c24c2011-04-28 18:40:04 -07001076 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1077 } else {
1078 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1079 drawTextureLayer(layer, rect);
1080 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1081 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001082}
1083
1084void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001085 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001086 layer->setRegionAsRect();
1087
Romain Guy40667672011-03-18 14:34:03 -07001088 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001089
Romain Guy5b3b3522010-10-27 18:57:51 -07001090 layer->region.clear();
1091 return;
1092 }
1093
Romain Guy8a3957d2011-09-07 17:55:15 -07001094 // TODO: See LayerRenderer.cpp::generateMesh() for important
1095 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001096 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001097 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001098 const android::Rect* rects;
1099 Region safeRegion;
1100 if (CC_LIKELY(hasRectToRectTransform())) {
1101 rects = layer->region.getArray(&count);
1102 } else {
1103 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1104 rects = safeRegion.getArray(&count);
1105 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001106
Chris Craike83569c2013-03-20 16:57:09 -07001107 const float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guy9ace8f52011-07-07 20:50:11 -07001108 const float texX = 1.0f / float(layer->getWidth());
1109 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001110 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001111
Romain Guy8ce00302013-01-15 18:51:42 -08001112 setupDraw();
1113
1114 // We must get (and therefore bind) the region mesh buffer
1115 // after we setup drawing in case we need to mess with the
1116 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001117 TextureVertex* mesh = mCaches.getRegionMesh();
1118 GLsizei numQuads = 0;
1119
Romain Guy7230a742011-01-10 22:26:16 -08001120 setupDrawWithTexture();
1121 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001122 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001123 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001124 setupDrawProgram();
1125 setupDrawDirtyRegionsDisabled();
1126 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001127 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001128 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001129 if (currentTransform().isPureTranslate()) {
1130 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1131 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001132
Romain Guyd21b6e12011-11-30 20:21:23 -08001133 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001134 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1135 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001136 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001137 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1138 }
Romain Guy15bc6432011-12-13 13:11:32 -08001139 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001140
1141 for (size_t i = 0; i < count; i++) {
1142 const android::Rect* r = &rects[i];
1143
1144 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001145 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001146 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001147 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001148
1149 // TODO: Reject quads outside of the clip
1150 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1151 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1152 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1153 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1154
1155 numQuads++;
1156
1157 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1158 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1159 numQuads = 0;
1160 mesh = mCaches.getRegionMesh();
1161 }
1162 }
1163
1164 if (numQuads > 0) {
1165 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1166 }
1167
Romain Guy7230a742011-01-10 22:26:16 -08001168 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001169
1170#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001171 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001172#endif
1173
1174 layer->region.clear();
1175 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001176}
1177
Romain Guy3a3133d2011-02-01 22:59:58 -08001178void OpenGLRenderer::drawRegionRects(const Region& region) {
1179#if DEBUG_LAYERS_AS_REGIONS
1180 size_t count;
1181 const android::Rect* rects = region.getArray(&count);
1182
1183 uint32_t colors[] = {
1184 0x7fff0000, 0x7f00ff00,
1185 0x7f0000ff, 0x7fff00ff,
1186 };
1187
1188 int offset = 0;
1189 int32_t top = rects[0].top;
1190
1191 for (size_t i = 0; i < count; i++) {
1192 if (top != rects[i].top) {
1193 offset ^= 0x2;
1194 top = rects[i].top;
1195 }
1196
1197 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1198 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1199 SkXfermode::kSrcOver_Mode);
1200 }
1201#endif
1202}
1203
Romain Guy8ce00302013-01-15 18:51:42 -08001204void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1205 SkXfermode::Mode mode, bool dirty) {
1206 int count = 0;
1207 Vector<float> rects;
1208
1209 SkRegion::Iterator it(region);
1210 while (!it.done()) {
1211 const SkIRect& r = it.rect();
1212 rects.push(r.fLeft);
1213 rects.push(r.fTop);
1214 rects.push(r.fRight);
1215 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001216 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001217 it.next();
1218 }
1219
Romain Guy3bbacf22013-02-06 16:51:04 -08001220 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001221}
1222
Romain Guy5b3b3522010-10-27 18:57:51 -07001223void OpenGLRenderer::dirtyLayer(const float left, const float top,
1224 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001225 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001226 Rect bounds(left, top, right, bottom);
1227 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001228 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001229 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001230}
1231
1232void OpenGLRenderer::dirtyLayer(const float left, const float top,
1233 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001234 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001235 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001236 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001237 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001238}
1239
1240void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001241 if (bounds.intersect(*mSnapshot->clipRect)) {
1242 bounds.snapToPixelBoundaries();
1243 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1244 if (!dirty.isEmpty()) {
1245 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 }
1247 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001248}
1249
Romain Guy54be1cd2011-06-13 19:04:27 -07001250void OpenGLRenderer::clearLayerRegions() {
1251 const size_t count = mLayers.size();
1252 if (count == 0) return;
1253
1254 if (!mSnapshot->isIgnored()) {
1255 // Doing several glScissor/glClear here can negatively impact
1256 // GPUs with a tiler architecture, instead we draw quads with
1257 // the Clear blending mode
1258
1259 // The list contains bounds that have already been clipped
1260 // against their initial clip rect, and the current clip
1261 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001262 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001263
1264 Vertex mesh[count * 6];
1265 Vertex* vertex = mesh;
1266
1267 for (uint32_t i = 0; i < count; i++) {
1268 Rect* bounds = mLayers.itemAt(i);
1269
1270 Vertex::set(vertex++, bounds->left, bounds->bottom);
1271 Vertex::set(vertex++, bounds->left, bounds->top);
1272 Vertex::set(vertex++, bounds->right, bounds->top);
1273 Vertex::set(vertex++, bounds->left, bounds->bottom);
1274 Vertex::set(vertex++, bounds->right, bounds->top);
1275 Vertex::set(vertex++, bounds->right, bounds->bottom);
1276
1277 delete bounds;
1278 }
Romain Guye67307c2013-02-11 18:01:20 -08001279 // We must clear the list of dirty rects before we
1280 // call setupDraw() to prevent stencil setup to do
1281 // the same thing again
1282 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001283
1284 setupDraw(false);
1285 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1286 setupDrawBlending(true, SkXfermode::kClear_Mode);
1287 setupDrawProgram();
1288 setupDrawPureColorUniforms();
1289 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001290 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001291
Romain Guy54be1cd2011-06-13 19:04:27 -07001292 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001293
1294 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001295 } else {
1296 for (uint32_t i = 0; i < count; i++) {
1297 delete mLayers.itemAt(i);
1298 }
Romain Guye67307c2013-02-11 18:01:20 -08001299 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001300 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001301}
1302
Romain Guybd6b79b2010-06-26 00:13:53 -07001303///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001304// State Deferral
1305///////////////////////////////////////////////////////////////////////////////
1306
Chris Craikff785832013-03-08 13:12:16 -08001307bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001308 const Rect& currentClip = *(mSnapshot->clipRect);
1309 const mat4& currentMatrix = *(mSnapshot->transform);
1310
Chris Craikff785832013-03-08 13:12:16 -08001311 if (stateDeferFlags & kStateDeferFlag_Draw) {
1312 // state has bounds initialized in local coordinates
1313 if (!state.mBounds.isEmpty()) {
1314 currentMatrix.mapRect(state.mBounds);
1315 if (!state.mBounds.intersect(currentClip)) {
1316 // quick rejected
1317 return true;
1318 }
1319 } else {
1320 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001321 }
Chris Craikff785832013-03-08 13:12:16 -08001322 state.mDrawModifiers = mDrawModifiers;
1323 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001324 }
1325
Chris Craikff785832013-03-08 13:12:16 -08001326 if (stateDeferFlags & kStateDeferFlag_Clip) {
1327 state.mClip.set(currentClip);
1328 } else {
1329 state.mClip.setEmpty();
1330 }
1331
1332 // transform always deferred
Chris Craikc3566d02013-02-04 16:16:33 -08001333 state.mMatrix.load(currentMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001334 return false;
1335}
1336
Chris Craikff785832013-03-08 13:12:16 -08001337void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, int stateDeferFlags) {
Romain Guy3b753822013-03-05 10:27:35 -08001338 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001339
Chris Craikff785832013-03-08 13:12:16 -08001340 if (stateDeferFlags & kStateDeferFlag_Draw) {
1341 mDrawModifiers = state.mDrawModifiers;
1342 mSnapshot->alpha = state.mAlpha;
1343 }
1344
Chris Craikd90144d2013-03-19 15:03:48 -07001345 if (!state.mClip.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001346 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1347 dirtyClip();
1348 }
Chris Craikc3566d02013-02-04 16:16:33 -08001349}
1350
1351///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001352// Transforms
1353///////////////////////////////////////////////////////////////////////////////
1354
1355void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001356 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001357}
1358
1359void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001360 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001361}
1362
1363void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001364 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001365}
1366
Romain Guy807daf72011-01-18 11:19:19 -08001367void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001368 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001369}
1370
Romain Guyf6a11b82010-06-23 17:47:49 -07001371void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001372 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001373 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001374 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001375 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001376 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001377}
1378
Chris Craikb98a0162013-02-21 11:30:22 -08001379bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001380 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001381}
1382
Romain Guyf6a11b82010-06-23 17:47:49 -07001383void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001384 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001385}
1386
1387void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001388 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001389 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001390 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001391 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001392}
1393
1394///////////////////////////////////////////////////////////////////////////////
1395// Clipping
1396///////////////////////////////////////////////////////////////////////////////
1397
Romain Guybb9524b2010-06-22 18:56:38 -07001398void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001399 Rect clip(*mSnapshot->clipRect);
1400 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001401
Romain Guy8a4ac612012-07-17 17:32:48 -07001402 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1403 clip.getWidth(), clip.getHeight())) {
1404 mDirtyClip = false;
1405 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001406}
1407
Romain Guy8ce00302013-01-15 18:51:42 -08001408void OpenGLRenderer::ensureStencilBuffer() {
1409 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1410 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1411 // just hope we have one when hasLayer() returns false.
1412 if (hasLayer()) {
1413 attachStencilBufferToLayer(mSnapshot->layer);
1414 }
1415}
1416
1417void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1418 // The layer's FBO is already bound when we reach this stage
1419 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001420 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1421 // is attached after we initiated tiling. We must turn it off,
1422 // attach the new render buffer then turn tiling back on
1423 endTiling();
1424
Romain Guy8d4aeb72013-02-12 16:08:55 -08001425 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001426 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001427 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001428
Romain Guyf735c8e2013-01-31 17:45:55 -08001429 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001430 }
1431}
1432
1433void OpenGLRenderer::setStencilFromClip() {
1434 if (!mCaches.debugOverdraw) {
1435 if (!mSnapshot->clipRegion->isEmpty()) {
1436 // NOTE: The order here is important, we must set dirtyClip to false
1437 // before any draw call to avoid calling back into this method
1438 mDirtyClip = false;
1439
1440 ensureStencilBuffer();
1441
1442 mCaches.stencil.enableWrite();
1443
1444 // Clear the stencil but first make sure we restrict drawing
1445 // to the region's bounds
1446 bool resetScissor = mCaches.enableScissor();
1447 if (resetScissor) {
1448 // The scissor was not set so we now need to update it
1449 setScissorFromClip();
1450 }
1451 mCaches.stencil.clear();
1452 if (resetScissor) mCaches.disableScissor();
1453
1454 // NOTE: We could use the region contour path to generate a smaller mesh
1455 // Since we are using the stencil we could use the red book path
1456 // drawing technique. It might increase bandwidth usage though.
1457
1458 // The last parameter is important: we are not drawing in the color buffer
1459 // so we don't want to dirty the current layer, if any
1460 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1461
1462 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001463
1464 // Draw the region used to generate the stencil if the appropriate debug
1465 // mode is enabled
1466 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1467 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1468 }
Romain Guy8ce00302013-01-15 18:51:42 -08001469 } else {
1470 mCaches.stencil.disable();
1471 }
1472 }
1473}
1474
Romain Guy9d5316e2010-06-24 19:30:36 -07001475const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001476 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001477}
1478
Romain Guy8a4ac612012-07-17 17:32:48 -07001479bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1480 if (mSnapshot->isIgnored()) {
1481 return true;
1482 }
1483
1484 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001485 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001486 r.snapToPixelBoundaries();
1487
1488 Rect clipRect(*mSnapshot->clipRect);
1489 clipRect.snapToPixelBoundaries();
1490
1491 return !clipRect.intersects(r);
1492}
1493
Romain Guy35643dd2012-09-18 15:40:58 -07001494bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1495 Rect& transformed, Rect& clip) {
1496 if (mSnapshot->isIgnored()) {
1497 return true;
1498 }
1499
1500 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001501 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001502 transformed.snapToPixelBoundaries();
1503
1504 clip.set(*mSnapshot->clipRect);
1505 clip.snapToPixelBoundaries();
1506
1507 return !clip.intersects(transformed);
1508}
1509
Romain Guy672433d2013-01-04 19:05:13 -08001510bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1511 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001512 if (paint->getStyle() != SkPaint::kFill_Style) {
1513 float outset = paint->getStrokeWidth() * 0.5f;
1514 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1515 } else {
1516 return quickReject(left, top, right, bottom);
1517 }
1518}
1519
Romain Guyc7d53492010-06-25 13:41:57 -07001520bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001521 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001522 return true;
1523 }
1524
Romain Guy1d83e192010-08-17 11:37:00 -07001525 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001526 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001527 r.snapToPixelBoundaries();
1528
1529 Rect clipRect(*mSnapshot->clipRect);
1530 clipRect.snapToPixelBoundaries();
1531
Romain Guy586cae32012-07-13 15:28:31 -07001532 bool rejected = !clipRect.intersects(r);
1533 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001534 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001535 }
1536
1537 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001538}
1539
Romain Guy8ce00302013-01-15 18:51:42 -08001540void OpenGLRenderer::debugClip() {
1541#if DEBUG_CLIP_REGIONS
1542 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1543 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1544 }
1545#endif
1546}
1547
Romain Guy079ba2c2010-07-16 14:12:24 -07001548bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001549 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001550 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1551 if (clipped) {
1552 dirtyClip();
1553 }
1554 return !mSnapshot->clipRect->isEmpty();
1555 }
1556
1557 SkPath path;
1558 path.addRect(left, top, right, bottom);
1559
1560 return clipPath(&path, op);
1561}
1562
1563bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1564 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001565 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001566
1567 SkPath transformed;
1568 path->transform(transform, &transformed);
1569
1570 SkRegion clip;
1571 if (!mSnapshot->clipRegion->isEmpty()) {
1572 clip.setRegion(*mSnapshot->clipRegion);
1573 } else {
1574 Rect* bounds = mSnapshot->clipRect;
1575 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1576 }
1577
1578 SkRegion region;
1579 region.setPath(transformed, clip);
1580
1581 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001582 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001583 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001584 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001585 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001586}
1587
Romain Guy735738c2012-12-03 12:34:51 -08001588bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001589 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1590 if (clipped) {
1591 dirtyClip();
1592 }
1593 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001594}
1595
Chet Haasea23eed82012-04-12 15:19:04 -07001596Rect* OpenGLRenderer::getClipRect() {
1597 return mSnapshot->clipRect;
1598}
1599
Romain Guyf6a11b82010-06-23 17:47:49 -07001600///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001601// Drawing commands
1602///////////////////////////////////////////////////////////////////////////////
1603
Romain Guy54be1cd2011-06-13 19:04:27 -07001604void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001605 // TODO: It would be best if we could do this before quickReject()
1606 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001607 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001608 // Make sure setScissor & setStencil happen at the beginning of
1609 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001610 if (mDirtyClip) {
1611 if (mCaches.scissorEnabled) {
1612 setScissorFromClip();
1613 }
Romain Guy8ce00302013-01-15 18:51:42 -08001614 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001615 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001616
Romain Guy70ca14e2010-12-13 18:24:33 -08001617 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001618
Romain Guy70ca14e2010-12-13 18:24:33 -08001619 mSetShaderColor = false;
1620 mColorSet = false;
1621 mColorA = mColorR = mColorG = mColorB = 0.0f;
1622 mTextureUnit = 0;
1623 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001624
1625 // Enable debug highlight when what we're about to draw is tested against
1626 // the stencil buffer and if stencil highlight debugging is on
1627 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1628 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1629 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001630}
1631
1632void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1633 mDescription.hasTexture = true;
1634 mDescription.hasAlpha8Texture = isAlpha8;
1635}
1636
Romain Guyff316ec2013-02-13 18:39:43 -08001637void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1638 mDescription.hasTexture = true;
1639 mDescription.hasColors = true;
1640 mDescription.hasAlpha8Texture = isAlpha8;
1641}
1642
Romain Guyaa6c24c2011-04-28 18:40:04 -07001643void OpenGLRenderer::setupDrawWithExternalTexture() {
1644 mDescription.hasExternalTexture = true;
1645}
1646
Romain Guy15bc6432011-12-13 13:11:32 -08001647void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001648 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001649}
1650
Chris Craik710f46d2012-09-17 17:25:49 -07001651void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001652 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001653}
1654
Romain Guyed6fcb02011-03-21 13:11:28 -07001655void OpenGLRenderer::setupDrawPoint(float pointSize) {
1656 mDescription.isPoint = true;
1657 mDescription.pointSize = pointSize;
1658}
1659
Romain Guy8d0d4782010-12-14 20:13:35 -08001660void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1661 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001662 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1663 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1664 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001665 mColorSet = true;
1666 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1667}
1668
Romain Guy86568192010-12-14 15:55:39 -08001669void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1670 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001671 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1672 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1673 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001674 mColorSet = true;
1675 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1676}
1677
Romain Guy41210632012-07-16 17:04:24 -07001678void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1679 mCaches.fontRenderer->describe(mDescription, paint);
1680}
1681
Romain Guy70ca14e2010-12-13 18:24:33 -08001682void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1683 mColorA = a;
1684 mColorR = r;
1685 mColorG = g;
1686 mColorB = b;
1687 mColorSet = true;
1688 mSetShaderColor = mDescription.setColor(r, g, b, a);
1689}
1690
1691void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001692 if (mDrawModifiers.mShader) {
1693 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001694 }
1695}
1696
1697void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001698 if (mDrawModifiers.mColorFilter) {
1699 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001700 }
1701}
1702
Romain Guyf09ef512011-05-27 11:43:46 -07001703void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1704 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1705 mColorA = 1.0f;
1706 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001707 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001708 }
1709}
1710
Romain Guy70ca14e2010-12-13 18:24:33 -08001711void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001712 // When the blending mode is kClear_Mode, we need to use a modulate color
1713 // argb=1,0,0,0
1714 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001715 bool blend = (mColorSet && mColorA < 1.0f) ||
1716 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1717 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001718}
1719
1720void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001721 // When the blending mode is kClear_Mode, we need to use a modulate color
1722 // argb=1,0,0,0
1723 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001724 blend |= (mColorSet && mColorA < 1.0f) ||
1725 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1726 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1727 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001728}
1729
1730void OpenGLRenderer::setupDrawProgram() {
1731 useProgram(mCaches.programCache.get(mDescription));
1732}
1733
1734void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1735 mTrackDirtyRegions = false;
1736}
1737
1738void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1739 bool ignoreTransform) {
1740 mModelView.loadTranslate(left, top, 0.0f);
1741 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001742 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1743 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001744 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001745 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001746 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1747 }
1748}
1749
Chet Haase8a5cc922011-04-26 07:28:09 -07001750void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001751 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001752}
1753
Romain Guy70ca14e2010-12-13 18:24:33 -08001754void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1755 bool ignoreTransform, bool ignoreModelView) {
1756 if (!ignoreModelView) {
1757 mModelView.loadTranslate(left, top, 0.0f);
1758 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001759 } else {
1760 mModelView.loadIdentity();
1761 }
Romain Guy86568192010-12-14 15:55:39 -08001762 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1763 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001764 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001765 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001766 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001767 }
1768 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001769 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001770 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1771 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001772}
1773
Romain Guyed6fcb02011-03-21 13:11:28 -07001774void OpenGLRenderer::setupDrawPointUniforms() {
1775 int slot = mCaches.currentProgram->getUniform("pointSize");
1776 glUniform1f(slot, mDescription.pointSize);
1777}
1778
Romain Guy70ca14e2010-12-13 18:24:33 -08001779void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001780 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001781 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1782 }
1783}
1784
Romain Guy86568192010-12-14 15:55:39 -08001785void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001786 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001787 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001788 }
1789}
1790
Romain Guy70ca14e2010-12-13 18:24:33 -08001791void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001792 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001793 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001794 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001795 }
Chris Craikc3566d02013-02-04 16:16:33 -08001796 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1797 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 }
1799}
1800
Romain Guy8d0d4782010-12-14 20:13:35 -08001801void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001802 if (mDrawModifiers.mShader) {
1803 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001804 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001805 }
1806}
1807
Romain Guy70ca14e2010-12-13 18:24:33 -08001808void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001809 if (mDrawModifiers.mColorFilter) {
1810 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001811 }
1812}
1813
Romain Guy41210632012-07-16 17:04:24 -07001814void OpenGLRenderer::setupDrawTextGammaUniforms() {
1815 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1816}
1817
Romain Guy70ca14e2010-12-13 18:24:33 -08001818void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001819 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001820 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001821 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001822}
1823
1824void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001825 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001826 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001827 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001828}
1829
Romain Guyaa6c24c2011-04-28 18:40:04 -07001830void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1831 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001832 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001833 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001834}
1835
Romain Guy8f0095c2011-05-02 17:24:22 -07001836void OpenGLRenderer::setupDrawTextureTransform() {
1837 mDescription.hasTextureTransform = true;
1838}
1839
1840void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001841 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1842 GL_FALSE, &transform.data[0]);
1843}
1844
Romain Guy70ca14e2010-12-13 18:24:33 -08001845void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001846 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001847 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001848 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001849 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001850 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 }
Romain Guyd71dd362011-12-12 19:03:35 -08001852
Chris Craikcb4d6002012-09-25 12:00:29 -07001853 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001854 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001855 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001856 }
1857
1858 mCaches.unbindIndicesBuffer();
1859}
1860
Romain Guyff316ec2013-02-13 18:39:43 -08001861void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1862 bool force = mCaches.unbindMeshBuffer();
1863 GLsizei stride = sizeof(ColorTextureVertex);
1864
1865 mCaches.bindPositionVertexPointer(force, vertices, stride);
1866 if (mCaches.currentProgram->texCoords >= 0) {
1867 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1868 }
1869 int slot = mCaches.currentProgram->getAttrib("colors");
1870 if (slot >= 0) {
1871 glEnableVertexAttribArray(slot);
1872 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1873 }
1874
1875 mCaches.unbindIndicesBuffer();
1876}
1877
Romain Guy15bc6432011-12-13 13:11:32 -08001878void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1879 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001880 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001881 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001882 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001883 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001884}
1885
Chet Haase5b0200b2011-04-13 17:58:08 -07001886void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001887 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001888 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001889 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001890}
1891
Romain Guy70ca14e2010-12-13 18:24:33 -08001892void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001893}
1894
1895///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001896// Drawing
1897///////////////////////////////////////////////////////////////////////////////
1898
Chris Craikff785832013-03-08 13:12:16 -08001899status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1900 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001901 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1902 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001903 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001904 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -07001905 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001906 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1907 displayList->replay(replayStruct, 0);
1908 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001909 }
1910
1911 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001912 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1913 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001914
1915 flushLayers();
1916 startFrame();
1917
Chris Craikff785832013-03-08 13:12:16 -08001918 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001919 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001920
Romain Guy65549432012-03-26 16:45:05 -07001921 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001922}
1923
Chris Craikc3566d02013-02-04 16:16:33 -08001924void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001925 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001926 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001927 }
1928}
1929
Romain Guya168d732011-03-18 16:50:13 -07001930void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1931 int alpha;
1932 SkXfermode::Mode mode;
1933 getAlphaAndMode(paint, &alpha, &mode);
1934
Romain Guy886b2752013-01-04 12:26:18 -08001935 int color = paint != NULL ? paint->getColor() : 0;
1936
Romain Guya168d732011-03-18 16:50:13 -07001937 float x = left;
1938 float y = top;
1939
Romain Guy886b2752013-01-04 12:26:18 -08001940 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1941
Romain Guya168d732011-03-18 16:50:13 -07001942 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001943 if (currentTransform().isPureTranslate()) {
1944 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1945 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001946 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001947
1948 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001949 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001950 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001951 }
1952
Romain Guy886b2752013-01-04 12:26:18 -08001953 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1954 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1955 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001956}
1957
Chet Haase48659092012-05-31 15:21:51 -07001958status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07001959 const float right = left + bitmap->width();
1960 const float bottom = top + bitmap->height();
1961
1962 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001963 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07001964 }
1965
Romain Guya1d3c912011-12-13 14:55:06 -08001966 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001967 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001968 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001969 const AutoTexture autoCleanup(texture);
1970
Romain Guy211370f2012-02-01 16:10:55 -08001971 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001972 drawAlphaBitmap(texture, left, top, paint);
1973 } else {
1974 drawTextureRect(left, top, right, bottom, texture, paint);
1975 }
Chet Haase48659092012-05-31 15:21:51 -07001976
1977 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001978}
1979
Chet Haase48659092012-05-31 15:21:51 -07001980status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001981 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1982 const mat4 transform(*matrix);
1983 transform.mapRect(r);
1984
Romain Guy6926c72e2010-07-12 20:20:03 -07001985 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001986 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07001987 }
1988
Romain Guya1d3c912011-12-13 14:55:06 -08001989 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001990 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001991 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001992 const AutoTexture autoCleanup(texture);
1993
Romain Guy5b3b3522010-10-27 18:57:51 -07001994 // This could be done in a cheaper way, all we need is pass the matrix
1995 // to the vertex shader. The save/restore is a bit overkill.
1996 save(SkCanvas::kMatrix_SaveFlag);
1997 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001998 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1999 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2000 } else {
2001 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2002 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002003 restore();
Chet Haase48659092012-05-31 15:21:51 -07002004
2005 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002006}
2007
Chet Haase48659092012-05-31 15:21:51 -07002008status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002009 const float right = left + bitmap->width();
2010 const float bottom = top + bitmap->height();
2011
2012 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002013 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002014 }
2015
2016 mCaches.activeTexture(0);
2017 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2018 const AutoTexture autoCleanup(texture);
2019
Romain Guy886b2752013-01-04 12:26:18 -08002020 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2021 drawAlphaBitmap(texture, left, top, paint);
2022 } else {
2023 drawTextureRect(left, top, right, bottom, texture, paint);
2024 }
Chet Haase48659092012-05-31 15:21:51 -07002025
2026 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002027}
2028
Chet Haase48659092012-05-31 15:21:51 -07002029status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002030 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002031 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002032 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002033 }
2034
Romain Guyb18d2d02011-02-10 15:52:54 -08002035 float left = FLT_MAX;
2036 float top = FLT_MAX;
2037 float right = FLT_MIN;
2038 float bottom = FLT_MIN;
2039
Romain Guya92bb4d2012-10-16 11:08:44 -07002040 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002041
Romain Guyff316ec2013-02-13 18:39:43 -08002042 ColorTextureVertex mesh[count];
2043 ColorTextureVertex* vertex = mesh;
2044
2045 bool cleanupColors = false;
2046 if (!colors) {
2047 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2048 colors = new int[colorsCount];
2049 memset(colors, 0xff, colorsCount * sizeof(int));
2050 cleanupColors = true;
2051 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002052
Romain Guy5a7b4662011-01-20 19:09:30 -08002053 for (int32_t y = 0; y < meshHeight; y++) {
2054 for (int32_t x = 0; x < meshWidth; x++) {
2055 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2056
2057 float u1 = float(x) / meshWidth;
2058 float u2 = float(x + 1) / meshWidth;
2059 float v1 = float(y) / meshHeight;
2060 float v2 = float(y + 1) / meshHeight;
2061
2062 int ax = i + (meshWidth + 1) * 2;
2063 int ay = ax + 1;
2064 int bx = i;
2065 int by = bx + 1;
2066 int cx = i + 2;
2067 int cy = cx + 1;
2068 int dx = i + (meshWidth + 1) * 2 + 2;
2069 int dy = dx + 1;
2070
Romain Guyff316ec2013-02-13 18:39:43 -08002071 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2072 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2073 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002074
Romain Guyff316ec2013-02-13 18:39:43 -08002075 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2076 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2077 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002078
Romain Guya92bb4d2012-10-16 11:08:44 -07002079 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2080 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2081 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2082 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002083 }
2084 }
2085
Romain Guya92bb4d2012-10-16 11:08:44 -07002086 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002087 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002088 return DrawGlInfo::kStatusDone;
2089 }
2090
2091 mCaches.activeTexture(0);
2092 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002093 if (!texture) {
2094 if (cleanupColors) delete[] colors;
2095 return DrawGlInfo::kStatusDone;
2096 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002097 const AutoTexture autoCleanup(texture);
2098
2099 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2100 texture->setFilter(FILTER(paint), true);
2101
2102 int alpha;
2103 SkXfermode::Mode mode;
2104 getAlphaAndMode(paint, &alpha, &mode);
2105
Romain Guyff316ec2013-02-13 18:39:43 -08002106 float a = alpha / 255.0f;
2107
Romain Guya92bb4d2012-10-16 11:08:44 -07002108 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002109 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002110 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002111
Romain Guyff316ec2013-02-13 18:39:43 -08002112 setupDraw();
2113 setupDrawWithTextureAndColor();
2114 setupDrawColor(a, a, a, a);
2115 setupDrawColorFilter();
2116 setupDrawBlending(true, mode, false);
2117 setupDrawProgram();
2118 setupDrawDirtyRegionsDisabled();
2119 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2120 setupDrawTexture(texture->id);
2121 setupDrawPureColorUniforms();
2122 setupDrawColorFilterUniforms();
2123 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2124
2125 glDrawArrays(GL_TRIANGLES, 0, count);
2126
2127 finishDrawTexture();
2128
2129 int slot = mCaches.currentProgram->getAttrib("colors");
2130 if (slot >= 0) {
2131 glDisableVertexAttribArray(slot);
2132 }
2133
2134 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002135
2136 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002137}
2138
Chet Haase48659092012-05-31 15:21:51 -07002139status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002140 float srcLeft, float srcTop, float srcRight, float srcBottom,
2141 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002142 SkPaint* paint) {
Romain Guy6926c72e2010-07-12 20:20:03 -07002143 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002144 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07002145 }
2146
Romain Guya1d3c912011-12-13 14:55:06 -08002147 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002148 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002149 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002150 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002151
Romain Guy8ba548f2010-06-30 19:21:21 -07002152 const float width = texture->width;
2153 const float height = texture->height;
2154
Romain Guy68169722011-08-22 17:33:33 -07002155 const float u1 = fmax(0.0f, srcLeft / width);
2156 const float v1 = fmax(0.0f, srcTop / height);
2157 const float u2 = fmin(1.0f, srcRight / width);
2158 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002159
Romain Guy03750a02010-10-18 14:06:08 -07002160 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002161 resetDrawTextureTexCoords(u1, v1, u2, v2);
2162
Romain Guy03750a02010-10-18 14:06:08 -07002163 int alpha;
2164 SkXfermode::Mode mode;
2165 getAlphaAndMode(paint, &alpha, &mode);
2166
Romain Guyd21b6e12011-11-30 20:21:23 -08002167 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2168
Romain Guy886b2752013-01-04 12:26:18 -08002169 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2170 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002171
Romain Guy886b2752013-01-04 12:26:18 -08002172 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2173 // Apply a scale transform on the canvas only when a shader is in use
2174 // Skia handles the ratio between the dst and src rects as a scale factor
2175 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002176 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002177 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002178
Romain Guy3b753822013-03-05 10:27:35 -08002179 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2180 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2181 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002182
2183 dstRight = x + (dstRight - dstLeft);
2184 dstBottom = y + (dstBottom - dstTop);
2185
2186 dstLeft = x;
2187 dstTop = y;
2188
2189 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2190 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002191 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002192 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002193 }
2194
2195 if (CC_UNLIKELY(useScaleTransform)) {
2196 save(SkCanvas::kMatrix_SaveFlag);
2197 translate(dstLeft, dstTop);
2198 scale(scaleX, scaleY);
2199
2200 dstLeft = 0.0f;
2201 dstTop = 0.0f;
2202
2203 dstRight = srcRight - srcLeft;
2204 dstBottom = srcBottom - srcTop;
2205 }
2206
2207 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2208 int color = paint ? paint->getColor() : 0;
2209 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2210 texture->id, paint != NULL, color, alpha, mode,
2211 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2212 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2213 } else {
2214 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2215 texture->id, alpha / 255.0f, mode, texture->blend,
2216 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2217 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2218 }
2219
2220 if (CC_UNLIKELY(useScaleTransform)) {
2221 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002222 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002223
2224 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002225
2226 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002227}
2228
Chet Haase48659092012-05-31 15:21:51 -07002229status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002230 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002231 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002232 int alpha;
2233 SkXfermode::Mode mode;
2234 getAlphaAndModeDirect(paint, &alpha, &mode);
2235
2236 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2237 left, top, right, bottom, alpha, mode);
2238}
2239
2240status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2241 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2242 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c72e2010-07-12 20:20:03 -07002243 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002244 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07002245 }
2246
Romain Guybe6f9dc2012-07-16 12:41:17 -07002247 alpha *= mSnapshot->alpha;
2248
Romain Guy2728f962010-10-08 18:36:15 -07002249 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002250 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002251
Romain Guy211370f2012-02-01 16:10:55 -08002252 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002253 mCaches.activeTexture(0);
2254 Texture* texture = mCaches.textureCache.get(bitmap);
2255 if (!texture) return DrawGlInfo::kStatusDone;
2256 const AutoTexture autoCleanup(texture);
2257 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2258 texture->setFilter(GL_LINEAR, true);
2259
Romain Guy3b753822013-03-05 10:27:35 -08002260 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002261 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002262 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002263 const float offsetX = left + currentTransform().getTranslateX();
2264 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002265 const size_t count = mesh->quads.size();
2266 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002267 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002268 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002269 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2270 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2271 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002272 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002273 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002274 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002275 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002276 }
2277 }
2278
Romain Guy211370f2012-02-01 16:10:55 -08002279 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002280 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2281 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002282
2283 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2284 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2285 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2286 true, !mesh->hasEmptyQuads);
2287 } else {
2288 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2289 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2290 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2291 true, !mesh->hasEmptyQuads);
2292 }
Romain Guy054dc182010-10-15 17:55:25 -07002293 }
Chet Haase48659092012-05-31 15:21:51 -07002294
2295 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002296}
2297
Chris Craik65cd6122012-12-10 17:56:27 -08002298status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2299 bool useOffset) {
2300 if (!vertexBuffer.getSize()) {
2301 // no vertices to draw
2302 return DrawGlInfo::kStatusDone;
2303 }
2304
Chris Craikcb4d6002012-09-25 12:00:29 -07002305 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002306 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2307 bool isAA = paint->isAntiAlias();
2308
Chris Craik710f46d2012-09-17 17:25:49 -07002309 setupDraw();
2310 setupDrawNoTexture();
2311 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002312 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2313 setupDrawColorFilter();
2314 setupDrawShader();
2315 setupDrawBlending(isAA, mode);
2316 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002317 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002318 setupDrawColorUniforms();
2319 setupDrawColorFilterUniforms();
2320 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002321
Chris Craik710f46d2012-09-17 17:25:49 -07002322 void* vertices = vertexBuffer.getBuffer();
2323 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002324 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002325 mCaches.resetTexCoordsVertexPointer();
2326 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002327
Chris Craik710f46d2012-09-17 17:25:49 -07002328 int alphaSlot = -1;
2329 if (isAA) {
2330 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2331 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002332
Chris Craik710f46d2012-09-17 17:25:49 -07002333 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002334 glEnableVertexAttribArray(alphaSlot);
2335 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002336 }
Romain Guy04299382012-07-18 17:15:41 -07002337
Chris Craik710f46d2012-09-17 17:25:49 -07002338 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002339
Chris Craik710f46d2012-09-17 17:25:49 -07002340 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002341 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002342 }
Chris Craik65cd6122012-12-10 17:56:27 -08002343
2344 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002345}
2346
2347/**
Chris Craik65cd6122012-12-10 17:56:27 -08002348 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2349 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2350 * screen space in all directions. However, instead of using a fragment shader to compute the
2351 * translucency of the color from its position, we simply use a varying parameter to define how far
2352 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2353 *
2354 * Doesn't yet support joins, caps, or path effects.
2355 */
2356status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2357 VertexBuffer vertexBuffer;
2358 // TODO: try clipping large paths to viewport
2359 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2360
Romain Guyc46d07a2013-03-15 19:06:39 -07002361 if (hasLayer()) {
2362 SkRect bounds = path.getBounds();
2363 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2364 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2365 }
Chris Craik65cd6122012-12-10 17:56:27 -08002366
2367 return drawVertexBuffer(vertexBuffer, paint);
2368}
2369
2370/**
2371 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2372 * and additional geometry for defining an alpha slope perimeter.
2373 *
2374 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2375 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2376 * in-shader alpha region, but found it to be taxing on some GPUs.
2377 *
2378 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2379 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002380 */
Chet Haase48659092012-05-31 15:21:51 -07002381status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002382 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002383
Chris Craik65cd6122012-12-10 17:56:27 -08002384 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002385
Chris Craik65cd6122012-12-10 17:56:27 -08002386 VertexBuffer buffer;
2387 SkRect bounds;
2388 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002389
2390 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2391 return DrawGlInfo::kStatusDone;
2392 }
2393
Romain Guy3b753822013-03-05 10:27:35 -08002394 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002395
Chris Craik65cd6122012-12-10 17:56:27 -08002396 bool useOffset = !paint->isAntiAlias();
2397 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002398}
2399
Chet Haase48659092012-05-31 15:21:51 -07002400status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2401 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002402
2403 // TODO: The paint's cap style defines whether the points are square or circular
2404 // TODO: Handle AA for round points
2405
Chet Haase5b0200b2011-04-13 17:58:08 -07002406 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002407 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002408 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002409 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002410 if (isHairLine) {
2411 // Now that we know it's hairline, we can set the effective width, to be used later
2412 strokeWidth = 1.0f;
2413 }
2414 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002415
Romain Guyed6fcb02011-03-21 13:11:28 -07002416 int alpha;
2417 SkXfermode::Mode mode;
2418 getAlphaAndMode(paint, &alpha, &mode);
2419
2420 int verticesCount = count >> 1;
2421 int generatedVerticesCount = 0;
2422
2423 TextureVertex pointsData[verticesCount];
2424 TextureVertex* vertex = &pointsData[0];
2425
Romain Guy04299382012-07-18 17:15:41 -07002426 // TODO: We should optimize this method to not generate vertices for points
2427 // that lie outside of the clip.
2428 mCaches.enableScissor();
2429
Romain Guyed6fcb02011-03-21 13:11:28 -07002430 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002431 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002432 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002433 setupDrawColor(paint->getColor(), alpha);
2434 setupDrawColorFilter();
2435 setupDrawShader();
2436 setupDrawBlending(mode);
2437 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002438 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002439 setupDrawColorUniforms();
2440 setupDrawColorFilterUniforms();
2441 setupDrawPointUniforms();
2442 setupDrawShaderIdentityUniforms();
2443 setupDrawMesh(vertex);
2444
2445 for (int i = 0; i < count; i += 2) {
2446 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2447 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002448
Chet Haase8a5cc922011-04-26 07:28:09 -07002449 float left = points[i] - halfWidth;
2450 float right = points[i] + halfWidth;
2451 float top = points[i + 1] - halfWidth;
2452 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002453
Romain Guy3b753822013-03-05 10:27:35 -08002454 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002455 }
2456
2457 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002458
2459 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002460}
2461
Chet Haase48659092012-05-31 15:21:51 -07002462status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002463 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002464 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002465
Romain Guyae88e5e2010-10-22 17:49:18 -07002466 Rect& clip(*mSnapshot->clipRect);
2467 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002468
Romain Guy3d58c032010-07-14 16:34:53 -07002469 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002470
2471 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002472}
Romain Guy9d5316e2010-06-24 19:30:36 -07002473
Chet Haase48659092012-05-31 15:21:51 -07002474status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2475 SkPaint* paint) {
2476 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002477 const AutoTexture autoCleanup(texture);
2478
2479 const float x = left + texture->left - texture->offset;
2480 const float y = top + texture->top - texture->offset;
2481
2482 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002483
2484 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002485}
2486
Chet Haase48659092012-05-31 15:21:51 -07002487status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002488 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002489 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2490 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002491 return DrawGlInfo::kStatusDone;
2492 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002493
Chris Craikcb4d6002012-09-25 12:00:29 -07002494 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002495 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002496 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002497 right - left, bottom - top, rx, ry, p);
2498 return drawShape(left, top, texture, p);
2499 }
2500
2501 SkPath path;
2502 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002503 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2504 float outset = p->getStrokeWidth() / 2;
2505 rect.outset(outset, outset);
2506 rx += outset;
2507 ry += outset;
2508 }
Chris Craik710f46d2012-09-17 17:25:49 -07002509 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002510 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002511}
2512
Chris Craik710f46d2012-09-17 17:25:49 -07002513status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002514 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002515 x + radius, y + radius, p) ||
2516 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002517 return DrawGlInfo::kStatusDone;
2518 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002519 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002520 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002521 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002522 return drawShape(x - radius, y - radius, texture, p);
2523 }
2524
2525 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002526 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2527 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2528 } else {
2529 path.addCircle(x, y, radius);
2530 }
Chris Craik65cd6122012-12-10 17:56:27 -08002531 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002532}
Romain Guy01d58e42011-01-19 21:54:02 -08002533
Chet Haase48659092012-05-31 15:21:51 -07002534status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002535 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002536 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2537 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002538 return DrawGlInfo::kStatusDone;
2539 }
Romain Guy01d58e42011-01-19 21:54:02 -08002540
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002542 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002543 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002544 return drawShape(left, top, texture, p);
2545 }
2546
2547 SkPath path;
2548 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002549 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2550 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2551 }
Chris Craik710f46d2012-09-17 17:25:49 -07002552 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002553 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002554}
2555
Chet Haase48659092012-05-31 15:21:51 -07002556status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002557 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002558 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2559 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002560 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002561 }
2562
Chris Craik780c1282012-10-04 14:10:49 -07002563 if (fabs(sweepAngle) >= 360.0f) {
2564 return drawOval(left, top, right, bottom, p);
2565 }
2566
2567 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002568 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002569 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002570 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002571 startAngle, sweepAngle, useCenter, p);
2572 return drawShape(left, top, texture, p);
2573 }
2574
2575 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2576 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2577 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2578 }
2579
2580 SkPath path;
2581 if (useCenter) {
2582 path.moveTo(rect.centerX(), rect.centerY());
2583 }
2584 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2585 if (useCenter) {
2586 path.close();
2587 }
Chris Craik65cd6122012-12-10 17:56:27 -08002588 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002589}
2590
Romain Guycf8675e2012-10-02 12:32:25 -07002591// See SkPaintDefaults.h
2592#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2593
Chet Haase48659092012-05-31 15:21:51 -07002594status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002595 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2596 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002597 return DrawGlInfo::kStatusDone;
Romain Guy6926c72e2010-07-12 20:20:03 -07002598 }
2599
Chris Craik710f46d2012-09-17 17:25:49 -07002600 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002601 // only fill style is supported by drawConvexPath, since others have to handle joins
2602 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2603 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2604 mCaches.activeTexture(0);
2605 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002606 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002607 return drawShape(left, top, texture, p);
2608 }
2609
2610 SkPath path;
2611 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2612 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2613 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2614 }
2615 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002616 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002617 }
2618
Romain Guy3b753822013-03-05 10:27:35 -08002619 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002620 SkPath path;
2621 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002622 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002623 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002624 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002625 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002626 }
Romain Guyc7d53492010-06-25 13:41:57 -07002627}
Romain Guy9d5316e2010-06-24 19:30:36 -07002628
Raph Levien416a8472012-07-19 22:48:17 -07002629void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2630 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2631 float x, float y) {
2632 mCaches.activeTexture(0);
2633
2634 // NOTE: The drop shadow will not perform gamma correction
2635 // if shader-based correction is enabled
2636 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2637 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002638 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002639 const AutoTexture autoCleanup(shadow);
2640
Chris Craikc3566d02013-02-04 16:16:33 -08002641 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2642 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002643
Chris Craikc3566d02013-02-04 16:16:33 -08002644 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2645 int shadowColor = mDrawModifiers.mShadowColor;
2646 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002647 shadowColor = 0xffffffff;
2648 }
2649
2650 setupDraw();
2651 setupDrawWithTexture(true);
2652 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2653 setupDrawColorFilter();
2654 setupDrawShader();
2655 setupDrawBlending(true, mode);
2656 setupDrawProgram();
2657 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2658 setupDrawTexture(shadow->id);
2659 setupDrawPureColorUniforms();
2660 setupDrawColorFilterUniforms();
2661 setupDrawShaderUniforms();
2662 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2663
2664 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2665}
2666
Romain Guy768bffc2013-02-27 13:50:45 -08002667bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2668 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2669 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2670}
2671
Romain Guy257ae352013-03-20 16:31:12 -07002672class TextSetupFunctor: public Functor {
2673public:
2674 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2675 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2676 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2677 alpha(alpha), mode(mode), paint(paint) {
2678 }
2679 ~TextSetupFunctor() { }
2680
2681 status_t operator ()(int what, void* data) {
2682 renderer.setupDraw();
2683 renderer.setupDrawTextGamma(paint);
2684 renderer.setupDrawDirtyRegionsDisabled();
2685 renderer.setupDrawWithTexture(true);
2686 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2687 renderer.setupDrawColorFilter();
2688 renderer.setupDrawShader();
2689 renderer.setupDrawBlending(true, mode);
2690 renderer.setupDrawProgram();
2691 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2692 // Calling setupDrawTexture with the name 0 will enable the
2693 // uv attributes and increase the texture unit count
2694 // texture binding will be performed by the font renderer as
2695 // needed
2696 renderer.setupDrawTexture(0);
2697 renderer.setupDrawPureColorUniforms();
2698 renderer.setupDrawColorFilterUniforms();
2699 renderer.setupDrawShaderUniforms(pureTranslate);
2700 renderer.setupDrawTextGammaUniforms();
2701
2702 return NO_ERROR;
2703 }
2704
2705 OpenGLRenderer& renderer;
2706 float x;
2707 float y;
2708 bool pureTranslate;
2709 int alpha;
2710 SkXfermode::Mode mode;
2711 SkPaint* paint;
2712};
2713
Chet Haase48659092012-05-31 15:21:51 -07002714status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002715 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002716 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002717 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002718 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002719
Romain Guy671d6cf2012-01-18 12:39:17 -08002720 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002721 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002722 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002723 }
2724
2725 float x = 0.0f;
2726 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002727 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002728 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002729 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2730 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002731 }
2732
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002733 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002734 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002735
2736 int alpha;
2737 SkXfermode::Mode mode;
2738 getAlphaAndMode(paint, &alpha, &mode);
2739
Chris Craikc3566d02013-02-04 16:16:33 -08002740 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002741 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2742 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002743 }
2744
Romain Guy671d6cf2012-01-18 12:39:17 -08002745 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002746 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 if (pureTranslate && !linearFilter) {
2748 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2749 }
Romain Guy257ae352013-03-20 16:31:12 -07002750 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002751
2752 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2753 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2754
Romain Guy211370f2012-02-01 16:10:55 -08002755 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002756
Romain Guy257ae352013-03-20 16:31:12 -07002757 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002758 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002759 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002760 if (hasActiveLayer) {
2761 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002762 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002763 }
2764 dirtyLayerUnchecked(bounds, getRegion());
2765 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002766 }
Chet Haase48659092012-05-31 15:21:51 -07002767
2768 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002769}
2770
Romain Guy624234f2013-03-05 16:43:31 -08002771mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2772 mat4 fontTransform;
2773 if (CC_LIKELY(transform.isPureTranslate())) {
2774 fontTransform = mat4::identity();
2775 } else {
2776 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002777 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002778 } else {
2779 float sx, sy;
2780 currentTransform().decomposeScale(sx, sy);
2781 fontTransform.loadScale(sx, sy, 1.0f);
2782 }
2783 }
2784 return fontTransform;
2785}
2786
Romain Guyc2525952012-07-27 16:41:22 -07002787status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002788 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002789 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002790 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002791 }
2792
Chet Haasea1cff502012-02-21 13:43:44 -08002793 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002794 switch (paint->getTextAlign()) {
2795 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002796 x -= length / 2.0f;
2797 break;
2798 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002799 x -= length;
2800 break;
2801 default:
2802 break;
2803 }
2804
Romain Guycac5fd32011-12-01 20:08:50 -08002805 SkPaint::FontMetrics metrics;
2806 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002807 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002808 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002809 }
2810
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002811 const float oldX = x;
2812 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002813
2814 const mat4& transform = currentTransform();
2815 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002816
Romain Guy211370f2012-02-01 16:10:55 -08002817 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002818 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2819 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002820 }
2821
Romain Guy86568192010-12-14 15:55:39 -08002822 int alpha;
2823 SkXfermode::Mode mode;
2824 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002825
Romain Guyc74f45a2013-02-26 19:10:14 -08002826 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2827
Chris Craikc3566d02013-02-04 16:16:33 -08002828 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002829 fontRenderer.setFont(paint, mat4::identity());
2830 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2831 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002832 }
2833
Romain Guy19d4dd82013-03-04 11:14:26 -08002834 const bool hasActiveLayer = hasLayer();
2835
Romain Guy624234f2013-03-05 16:43:31 -08002836 // We only pass a partial transform to the font renderer. That partial
2837 // matrix defines how glyphs are rasterized. Typically we want glyphs
2838 // to be rasterized at their final size on screen, which means the partial
2839 // matrix needs to take the scale factor into account.
2840 // When a partial matrix is used to transform glyphs during rasterization,
2841 // the mesh is generated with the inverse transform (in the case of scale,
2842 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2843 // apply the full transform matrix at draw time in the vertex shader.
2844 // Applying the full matrix in the shader is the easiest way to handle
2845 // rotation and perspective and allows us to always generated quads in the
2846 // font renderer which greatly simplifies the code, clipping in particular.
2847 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002848 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002849
Romain Guy6620c6d2010-12-06 18:07:02 -08002850 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002851 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002852 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002853
Romain Guy624234f2013-03-05 16:43:31 -08002854 // TODO: Implement better clipping for scaled/rotated text
2855 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002856 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2857
Raph Levien996e57c2012-07-23 15:22:52 -07002858 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002859 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guya3dc55f2012-09-28 13:55:44 -07002860 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002861 SkPaint paintCopy(*paint);
2862 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2863 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002864 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002865 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002866 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002867 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002868 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002869
2870 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002871 if (!pureTranslate) {
2872 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002873 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002874 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002875 }
Romain Guy694b5192010-07-21 21:33:20 -07002876
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002877 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002878
2879 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002880}
2881
Chet Haase48659092012-05-31 15:21:51 -07002882status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002883 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002884 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002885 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002886 }
2887
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002888 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002889 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002890 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002891
2892 int alpha;
2893 SkXfermode::Mode mode;
2894 getAlphaAndMode(paint, &alpha, &mode);
2895
Romain Guy03d58522012-02-24 17:54:07 -08002896 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002897 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002898 setupDrawDirtyRegionsDisabled();
2899 setupDrawWithTexture(true);
2900 setupDrawAlpha8Color(paint->getColor(), alpha);
2901 setupDrawColorFilter();
2902 setupDrawShader();
2903 setupDrawBlending(true, mode);
2904 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002905 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002906 // Calling setupDrawTexture with the name 0 will enable the
2907 // uv attributes and increase the texture unit count
2908 // texture binding will be performed by the font renderer as
2909 // needed
2910 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002911 setupDrawPureColorUniforms();
2912 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002913 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002914 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002915
Romain Guy97771732012-02-28 18:17:02 -08002916 const Rect* clip = &mSnapshot->getLocalClip();
2917 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 -08002918
Romain Guy97771732012-02-28 18:17:02 -08002919 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002920
2921 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2922 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002923 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002924 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002925 dirtyLayerUnchecked(bounds, getRegion());
2926 }
Romain Guy97771732012-02-28 18:17:02 -08002927 }
Chet Haase48659092012-05-31 15:21:51 -07002928
2929 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002930}
2931
Chet Haase48659092012-05-31 15:21:51 -07002932status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2933 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002934
Romain Guya1d3c912011-12-13 14:55:06 -08002935 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002936
Romain Guyfb8b7632010-08-23 21:05:08 -07002937 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002938 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002939 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002940
Romain Guy8b55f372010-08-18 17:10:07 -07002941 const float x = texture->left - texture->offset;
2942 const float y = texture->top - texture->offset;
2943
Romain Guy01d58e42011-01-19 21:54:02 -08002944 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002945
2946 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002947}
2948
Chris Craika08f95c2013-03-15 17:24:33 -07002949status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002950 if (!layer) {
2951 return DrawGlInfo::kStatusDone;
2952 }
2953
Romain Guyb2e2f242012-10-17 18:18:35 -07002954 mat4* transform = NULL;
2955 if (layer->isTextureLayer()) {
2956 transform = &layer->getTransform();
2957 if (!transform->isIdentity()) {
2958 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002959 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002960 }
2961 }
2962
Romain Guy35643dd2012-09-18 15:40:58 -07002963 Rect transformed;
2964 Rect clip;
2965 const bool rejected = quickRejectNoScissor(x, y,
2966 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2967
2968 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002969 if (transform && !transform->isIdentity()) {
2970 restore();
2971 }
Chet Haase48659092012-05-31 15:21:51 -07002972 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002973 }
2974
Romain Guy5bb3c732012-11-29 17:52:58 -08002975 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002976
Romain Guy87e2f7572012-09-24 11:37:12 -07002977 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002978 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002979
Romain Guy211370f2012-02-01 16:10:55 -08002980 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002981 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2982 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002983
Romain Guyc88e3572011-01-22 00:32:12 -08002984 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002985 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002986 } else if (layer->mesh) {
Chris Craika08f95c2013-03-15 17:24:33 -07002987 const float a = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyc88e3572011-01-22 00:32:12 -08002988 setupDraw();
2989 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002990 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002991 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002992 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002993 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002994 setupDrawPureColorUniforms();
2995 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002996 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002997 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2998 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2999 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003000
Romain Guyd21b6e12011-11-30 20:21:23 -08003001 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003002 setupDrawModelViewTranslate(tx, ty,
3003 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003004 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003005 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003006 setupDrawModelViewTranslate(x, y,
3007 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3008 }
Romain Guyc88e3572011-01-22 00:32:12 -08003009 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003010
Romain Guyc88e3572011-01-22 00:32:12 -08003011 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3012 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003013
Romain Guyc88e3572011-01-22 00:32:12 -08003014 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003015
3016#if DEBUG_LAYERS_AS_REGIONS
3017 drawRegionRects(layer->region);
3018#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003019 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003020
Chris Craikc3566d02013-02-04 16:16:33 -08003021 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003022
Romain Guy5bb3c732012-11-29 17:52:58 -08003023 if (layer->debugDrawUpdate) {
3024 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003025 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3026 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3027 }
Romain Guyf219da52011-01-16 12:54:25 -08003028 }
Chet Haase48659092012-05-31 15:21:51 -07003029
Romain Guyb2e2f242012-10-17 18:18:35 -07003030 if (transform && !transform->isIdentity()) {
3031 restore();
3032 }
3033
Chet Haase48659092012-05-31 15:21:51 -07003034 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003035}
3036
Romain Guy6926c72e2010-07-12 20:20:03 -07003037///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003038// Shaders
3039///////////////////////////////////////////////////////////////////////////////
3040
3041void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003042 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003043}
3044
Romain Guy06f96e22010-07-30 19:18:16 -07003045void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003046 mDrawModifiers.mShader = shader;
3047 if (mDrawModifiers.mShader) {
3048 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003049 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003050}
3051
Romain Guyd27977d2010-07-14 19:18:51 -07003052///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003053// Color filters
3054///////////////////////////////////////////////////////////////////////////////
3055
3056void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003057 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003058}
3059
3060void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003061 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003062}
3063
3064///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003065// Drop shadow
3066///////////////////////////////////////////////////////////////////////////////
3067
3068void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003069 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003070}
3071
3072void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003073 mDrawModifiers.mHasShadow = true;
3074 mDrawModifiers.mShadowRadius = radius;
3075 mDrawModifiers.mShadowDx = dx;
3076 mDrawModifiers.mShadowDy = dy;
3077 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003078}
3079
3080///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003081// Draw filters
3082///////////////////////////////////////////////////////////////////////////////
3083
3084void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003085 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08003086}
3087
3088void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003089 mDrawModifiers.mHasDrawFilter = true;
3090 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3091 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003092}
3093
Chris Craika08f95c2013-03-15 17:24:33 -07003094SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003095 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003096 return paint;
3097 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003098
3099 uint32_t flags = paint->getFlags();
3100
3101 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003102 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3103 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003104
3105 return &mFilteredPaint;
3106}
3107
3108///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c72e2010-07-12 20:20:03 -07003109// Drawing implementation
3110///////////////////////////////////////////////////////////////////////////////
3111
Romain Guy01d58e42011-01-19 21:54:02 -08003112void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3113 float x, float y, SkPaint* paint) {
3114 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3115 return;
3116 }
3117
3118 int alpha;
3119 SkXfermode::Mode mode;
3120 getAlphaAndMode(paint, &alpha, &mode);
3121
3122 setupDraw();
3123 setupDrawWithTexture(true);
3124 setupDrawAlpha8Color(paint->getColor(), alpha);
3125 setupDrawColorFilter();
3126 setupDrawShader();
3127 setupDrawBlending(true, mode);
3128 setupDrawProgram();
3129 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3130 setupDrawTexture(texture->id);
3131 setupDrawPureColorUniforms();
3132 setupDrawColorFilterUniforms();
3133 setupDrawShaderUniforms();
3134 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3135
3136 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3137
3138 finishDrawTexture();
3139}
3140
Romain Guyf607bdc2010-09-10 19:20:06 -07003141// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003142#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3143#define kStdUnderline_Offset (1.0f / 9.0f)
3144#define kStdUnderline_Thickness (1.0f / 18.0f)
3145
3146void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3147 float x, float y, SkPaint* paint) {
3148 // Handle underline and strike-through
3149 uint32_t flags = paint->getFlags();
3150 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003151 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003152 float underlineWidth = length;
3153 // If length is > 0.0f, we already measured the text for the text alignment
3154 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003155 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003156 }
3157
Romain Guy211370f2012-02-01 16:10:55 -08003158 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003159 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003160 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003161
Raph Levien8b4072d2012-07-30 15:50:00 -07003162 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003163 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003164
Romain Guyf6834472011-01-23 13:32:12 -08003165 int linesCount = 0;
3166 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3167 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3168
3169 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003170 float points[pointsCount];
3171 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003172
3173 if (flags & SkPaint::kUnderlineText_Flag) {
3174 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003175 points[currentPoint++] = left;
3176 points[currentPoint++] = top;
3177 points[currentPoint++] = left + underlineWidth;
3178 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003179 }
3180
3181 if (flags & SkPaint::kStrikeThruText_Flag) {
3182 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003183 points[currentPoint++] = left;
3184 points[currentPoint++] = top;
3185 points[currentPoint++] = left + underlineWidth;
3186 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003187 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003188
Romain Guy726aeba2011-06-01 14:52:00 -07003189 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003190
Romain Guy726aeba2011-06-01 14:52:00 -07003191 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003192 }
3193 }
3194}
3195
Romain Guy672433d2013-01-04 19:05:13 -08003196status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3197 if (mSnapshot->isIgnored()) {
3198 return DrawGlInfo::kStatusDone;
3199 }
3200
Romain Guy735738c2012-12-03 12:34:51 -08003201 int color = paint->getColor();
3202 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003203 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003204 color |= 0x00ffffff;
3205 }
3206 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3207
3208 return drawColorRects(rects, count, color, mode);
3209}
3210
3211status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003212 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003213 if (count == 0) {
3214 return DrawGlInfo::kStatusDone;
3215 }
Romain Guy735738c2012-12-03 12:34:51 -08003216
Romain Guy672433d2013-01-04 19:05:13 -08003217 float left = FLT_MAX;
3218 float top = FLT_MAX;
3219 float right = FLT_MIN;
3220 float bottom = FLT_MIN;
3221
3222 int vertexCount = 0;
3223 Vertex mesh[count * 6];
3224 Vertex* vertex = mesh;
3225
Chris Craik2af46352012-11-26 18:30:17 -08003226 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003227 float l = rects[index + 0];
3228 float t = rects[index + 1];
3229 float r = rects[index + 2];
3230 float b = rects[index + 3];
3231
Romain Guy3b753822013-03-05 10:27:35 -08003232 Vertex::set(vertex++, l, b);
3233 Vertex::set(vertex++, l, t);
3234 Vertex::set(vertex++, r, t);
3235 Vertex::set(vertex++, l, b);
3236 Vertex::set(vertex++, r, t);
3237 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003238
Romain Guy3b753822013-03-05 10:27:35 -08003239 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003240
Romain Guy3b753822013-03-05 10:27:35 -08003241 left = fminf(left, l);
3242 top = fminf(top, t);
3243 right = fmaxf(right, r);
3244 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003245 }
3246
Romain Guy3b753822013-03-05 10:27:35 -08003247 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003248 return DrawGlInfo::kStatusDone;
3249 }
Romain Guy672433d2013-01-04 19:05:13 -08003250
Romain Guy672433d2013-01-04 19:05:13 -08003251 setupDraw();
3252 setupDrawNoTexture();
3253 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3254 setupDrawShader();
3255 setupDrawColorFilter();
3256 setupDrawBlending(mode);
3257 setupDrawProgram();
3258 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003259 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003260 setupDrawColorUniforms();
3261 setupDrawShaderUniforms();
3262 setupDrawColorFilterUniforms();
3263 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3264
Romain Guy8ce00302013-01-15 18:51:42 -08003265 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003266 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003267 }
3268
3269 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3270
3271 return DrawGlInfo::kStatusDrew;
3272}
3273
Romain Guy026c5e162010-06-28 17:12:22 -07003274void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003275 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003276 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003277 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003278 color |= 0x00ffffff;
3279 }
3280
Romain Guy70ca14e2010-12-13 18:24:33 -08003281 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003282 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003283 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003284 setupDrawShader();
3285 setupDrawColorFilter();
3286 setupDrawBlending(mode);
3287 setupDrawProgram();
3288 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3289 setupDrawColorUniforms();
3290 setupDrawShaderUniforms(ignoreTransform);
3291 setupDrawColorFilterUniforms();
3292 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003293
Romain Guyc95c8d62010-09-17 15:31:32 -07003294 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3295}
3296
Romain Guy82ba8142010-07-09 13:25:56 -07003297void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003298 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003299 int alpha;
3300 SkXfermode::Mode mode;
3301 getAlphaAndMode(paint, &alpha, &mode);
3302
Romain Guyd21b6e12011-11-30 20:21:23 -08003303 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003304
Romain Guy3b753822013-03-05 10:27:35 -08003305 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3306 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3307 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003308
Romain Guyd21b6e12011-11-30 20:21:23 -08003309 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003310 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3311 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3312 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3313 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003314 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003315 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3316 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3317 GL_TRIANGLE_STRIP, gMeshCount);
3318 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003319}
3320
Romain Guybd6b79b2010-06-26 00:13:53 -07003321void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003322 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3323 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003324 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003325}
3326
3327void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003328 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003329 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003330 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003331
Romain Guy746b7402010-10-26 16:27:31 -07003332 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003333 setupDrawWithTexture();
3334 setupDrawColor(alpha, alpha, alpha, alpha);
3335 setupDrawColorFilter();
3336 setupDrawBlending(blend, mode, swapSrcDst);
3337 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003338 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003339 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003340 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003341 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003342 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003343 }
Romain Guy886b2752013-01-04 12:26:18 -08003344 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003345 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003346 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003347 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003348
Romain Guy6820ac82010-09-15 18:11:50 -07003349 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003350
3351 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003352}
3353
Romain Guy886b2752013-01-04 12:26:18 -08003354void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3355 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3356 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3357 bool ignoreTransform, bool dirty) {
3358
3359 setupDraw();
3360 setupDrawWithTexture(true);
3361 if (hasColor) {
3362 setupDrawAlpha8Color(color, alpha);
3363 }
3364 setupDrawColorFilter();
3365 setupDrawShader();
3366 setupDrawBlending(true, mode);
3367 setupDrawProgram();
3368 if (!dirty) setupDrawDirtyRegionsDisabled();
3369 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3370 setupDrawTexture(texture);
3371 setupDrawPureColorUniforms();
3372 setupDrawColorFilterUniforms();
3373 setupDrawShaderUniforms();
3374 setupDrawMesh(vertices, texCoords);
3375
3376 glDrawArrays(drawMode, 0, elementsCount);
3377
3378 finishDrawTexture();
3379}
3380
Romain Guya5aed0d2010-09-09 14:42:43 -07003381void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003382 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003383 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003384
Romain Guy82ba8142010-07-09 13:25:56 -07003385 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003386 // These blend modes are not supported by OpenGL directly and have
3387 // to be implemented using shaders. Since the shader will perform
3388 // the blending, turn blending off here
3389 // If the blend mode cannot be implemented using shaders, fall
3390 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003391 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003392 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003393 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003394 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003395
Romain Guy82bc7a72012-01-03 14:13:39 -08003396 if (mCaches.blend) {
3397 glDisable(GL_BLEND);
3398 mCaches.blend = false;
3399 }
3400
3401 return;
3402 } else {
3403 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003404 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003405 }
3406
3407 if (!mCaches.blend) {
3408 glEnable(GL_BLEND);
3409 }
3410
3411 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3412 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3413
3414 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3415 glBlendFunc(sourceMode, destMode);
3416 mCaches.lastSrcMode = sourceMode;
3417 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003418 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003419 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003420 glDisable(GL_BLEND);
3421 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003422 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003423}
3424
Romain Guy889f8d12010-07-29 14:37:42 -07003425bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003426 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003427 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003428 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003429 mCaches.currentProgram = program;
Romain Guy6926c72e2010-07-12 20:20:03 -07003430 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003431 }
Romain Guy6926c72e2010-07-12 20:20:03 -07003432 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003433}
3434
Romain Guy026c5e162010-06-28 17:12:22 -07003435void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003436 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003437 TextureVertex::setUV(v++, u1, v1);
3438 TextureVertex::setUV(v++, u2, v1);
3439 TextureVertex::setUV(v++, u1, v2);
3440 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003441}
3442
Chet Haase5c13d892010-10-08 08:37:55 -07003443void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003444 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003445 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003446}
3447
Romain Guy9d5316e2010-06-24 19:30:36 -07003448}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003449}; // namespace android