blob: aeabe3664d4625e6e785f7bf091c849173d0663f [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) {
Chris Craik408eb122013-03-26 18:55:15 -0700434 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
435
Chet Haasedaf98e92011-01-10 14:10:36 -0800436 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700437 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700438
Romain Guy8a4ac612012-07-17 17:32:48 -0700439 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800440 if (mDirtyClip) {
441 setScissorFromClip();
442 }
Romain Guyd643bb52011-03-01 14:55:21 -0800443
Romain Guy80911b82011-03-16 15:30:12 -0700444 Rect clip(*mSnapshot->clipRect);
445 clip.snapToPixelBoundaries();
446
Romain Guyd643bb52011-03-01 14:55:21 -0800447 // Since we don't know what the functor will draw, let's dirty
448 // tne entire clip region
449 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800450 dirtyLayerUnchecked(clip, getRegion());
451 }
Romain Guyd643bb52011-03-01 14:55:21 -0800452
Romain Guy08aa2cb2011-03-17 11:06:57 -0700453 DrawGlInfo info;
454 info.clipLeft = clip.left;
455 info.clipTop = clip.top;
456 info.clipRight = clip.right;
457 info.clipBottom = clip.bottom;
458 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700459 info.width = getSnapshot()->viewport.getWidth();
460 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700461 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700462
Chet Haase48659092012-05-31 15:21:51 -0700463 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800464
Romain Guy8f3b8e32012-03-27 16:33:45 -0700465 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700466 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800467 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700468
Chris Craik65924a32012-04-05 17:52:11 -0700469 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700470 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700471 }
Romain Guycabfcc12011-03-07 18:06:46 -0800472 }
473
Chet Haasedaf98e92011-01-10 14:10:36 -0800474 resume();
Romain Guy65549432012-03-26 16:45:05 -0700475 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800476}
477
Romain Guyf6a11b82010-06-23 17:47:49 -0700478///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700479// Debug
480///////////////////////////////////////////////////////////////////////////////
481
Romain Guy0f667532013-03-01 14:31:04 -0800482void OpenGLRenderer::eventMark(const char* name) const {
483 mCaches.eventMark(0, name);
484}
485
Romain Guy87e2f7572012-09-24 11:37:12 -0700486void OpenGLRenderer::startMark(const char* name) const {
487 mCaches.startMark(0, name);
488}
489
490void OpenGLRenderer::endMark() const {
491 mCaches.endMark();
492}
493
494void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
495 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
496 if (clear) {
497 mCaches.disableScissor();
498 mCaches.stencil.clear();
499 }
500 if (enable) {
501 mCaches.stencil.enableDebugWrite();
502 } else {
503 mCaches.stencil.disable();
504 }
505 }
506}
507
508void OpenGLRenderer::renderOverdraw() {
509 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700510 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700511
512 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700513 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700514 clip->right - clip->left, clip->bottom - clip->top);
515
516 mCaches.stencil.enableDebugTest(2);
517 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
518 mCaches.stencil.enableDebugTest(3);
519 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
520 mCaches.stencil.enableDebugTest(4);
521 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
522 mCaches.stencil.enableDebugTest(4, true);
523 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
524 mCaches.stencil.disable();
525 }
526}
527
528///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700529// Layers
530///////////////////////////////////////////////////////////////////////////////
531
532bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700533 if (layer->deferredUpdateScheduled && layer->renderer &&
534 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700535 Rect& dirty = layer->dirtyRect;
536
Romain Guy7c450aa2012-09-21 19:15:00 -0700537 if (inFrame) {
538 endTiling();
539 debugOverdraw(false, false);
540 }
Romain Guy11cb6422012-09-21 00:39:43 -0700541
Romain Guy96885eb2013-03-26 15:05:58 -0700542 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
543 OpenGLRenderer* renderer = layer->renderer;
544 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
545 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom,
546 !layer->isBlend());
547 renderer->drawDisplayList(layer->displayList, dirty,
548 DisplayList::kReplayFlag_ClipChildren);
549 renderer->finish();
550 } else {
551 layer->defer();
552 }
Romain Guy11cb6422012-09-21 00:39:43 -0700553
554 if (inFrame) {
555 resumeAfterLayer();
556 startTiling(mSnapshot);
557 }
558
Romain Guy96885eb2013-03-26 15:05:58 -0700559 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
560 dirty.setEmpty();
561 layer->renderer = NULL;
562 }
563
Romain Guy11cb6422012-09-21 00:39:43 -0700564 layer->deferredUpdateScheduled = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700565 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800566 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700567
568 return true;
569 }
570
571 return false;
572}
573
574void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700575 // If draw deferring is enabled this method will simply defer
576 // the display list of each individual layer. The layers remain
577 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700578 int count = mLayerUpdates.size();
579 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700580 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
581 startMark("Layer Updates");
582 } else {
583 startMark("Defer Layer Updates");
584 }
Romain Guy11cb6422012-09-21 00:39:43 -0700585
586 // Note: it is very important to update the layers in reverse order
587 for (int i = count - 1; i >= 0; i--) {
588 Layer* layer = mLayerUpdates.itemAt(i);
589 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700590 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
591 mCaches.resourceCache.decrementRefcount(layer);
592 }
Romain Guy11cb6422012-09-21 00:39:43 -0700593 }
Romain Guy11cb6422012-09-21 00:39:43 -0700594
Romain Guy96885eb2013-03-26 15:05:58 -0700595 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
596 mLayerUpdates.clear();
597 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
598 }
599 endMark();
600 }
601}
602
603void OpenGLRenderer::flushLayers() {
604 int count = mLayerUpdates.size();
605 if (count > 0) {
606 startMark("Apply Layer Updates");
607 char layerName[12];
608
609 // Note: it is very important to update the layers in reverse order
610 for (int i = count - 1; i >= 0; i--) {
611 sprintf(layerName, "Layer #%d", i);
612 startMark(layerName); {
613 Layer* layer = mLayerUpdates.itemAt(i);
614 layer->flush();
615 mCaches.resourceCache.decrementRefcount(layer);
616 }
617 endMark();
618 }
619
620 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700621 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700622
Romain Guy11cb6422012-09-21 00:39:43 -0700623 endMark();
624 }
625}
626
627void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
628 if (layer) {
629 mLayerUpdates.push_back(layer);
630 mCaches.resourceCache.incrementRefcount(layer);
631 }
632}
633
634void OpenGLRenderer::clearLayerUpdates() {
635 size_t count = mLayerUpdates.size();
636 if (count > 0) {
637 mCaches.resourceCache.lock();
638 for (size_t i = 0; i < count; i++) {
639 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
640 }
641 mCaches.resourceCache.unlock();
642 mLayerUpdates.clear();
643 }
644}
645
646///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700647// State management
648///////////////////////////////////////////////////////////////////////////////
649
Romain Guybb9524b2010-06-22 18:56:38 -0700650int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700651 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700652}
653
654int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700655 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700656}
657
658void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700659 if (mSaveCount > 1) {
660 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700661 }
Romain Guybb9524b2010-06-22 18:56:38 -0700662}
663
664void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700665 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700666
Romain Guy8fb95422010-08-17 18:38:51 -0700667 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700668 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700669 }
Romain Guybb9524b2010-06-22 18:56:38 -0700670}
671
Romain Guy8aef54f2010-09-01 15:13:49 -0700672int OpenGLRenderer::saveSnapshot(int flags) {
673 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700674 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700675}
676
677bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700678 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700679 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700680 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700681
Romain Guybd6b79b2010-06-26 00:13:53 -0700682 sp<Snapshot> current = mSnapshot;
683 sp<Snapshot> previous = mSnapshot->previous;
684
Romain Guyeb993562010-10-05 18:14:38 -0700685 if (restoreOrtho) {
686 Rect& r = previous->viewport;
687 glViewport(r.left, r.top, r.right, r.bottom);
688 mOrthoMatrix.load(current->orthoMatrix);
689 }
690
Romain Guy8b55f372010-08-18 17:10:07 -0700691 mSaveCount--;
692 mSnapshot = previous;
693
Romain Guy2542d192010-08-18 11:47:12 -0700694 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700695 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700696 }
Romain Guy2542d192010-08-18 11:47:12 -0700697
Romain Guy5ec99242010-11-03 16:19:08 -0700698 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700699 endMark(); // Savelayer
700 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700701 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700702 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700703 }
704
Romain Guy2542d192010-08-18 11:47:12 -0700705 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700706}
707
Romain Guyf6a11b82010-06-23 17:47:49 -0700708///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700709// Layers
710///////////////////////////////////////////////////////////////////////////////
711
712int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800713 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700714 const GLuint previousFbo = mSnapshot->fbo;
715 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700716
Romain Guyaf636eb2010-12-09 17:47:21 -0800717 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700718 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700719 }
Romain Guyd55a8612010-06-28 17:42:46 -0700720
721 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700722}
723
Chris Craikd90144d2013-03-19 15:03:48 -0700724void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
725 const Rect untransformedBounds(bounds);
726
727 currentTransform().mapRect(bounds);
728
729 // Layers only make sense if they are in the framebuffer's bounds
730 if (bounds.intersect(*mSnapshot->clipRect)) {
731 // We cannot work with sub-pixels in this case
732 bounds.snapToPixelBoundaries();
733
734 // When the layer is not an FBO, we may use glCopyTexImage so we
735 // need to make sure the layer does not extend outside the bounds
736 // of the framebuffer
737 if (!bounds.intersect(mSnapshot->previous->viewport)) {
738 bounds.setEmpty();
739 } else if (fboLayer) {
740 clip.set(bounds);
741 mat4 inverse;
742 inverse.loadInverse(currentTransform());
743 inverse.mapRect(clip);
744 clip.snapToPixelBoundaries();
745 if (clip.intersect(untransformedBounds)) {
746 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
747 bounds.set(untransformedBounds);
748 } else {
749 clip.setEmpty();
750 }
751 }
752 } else {
753 bounds.setEmpty();
754 }
755}
756
Chris Craik408eb122013-03-26 18:55:15 -0700757void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
758 bool fboLayer, int alpha) {
759 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
760 bounds.getHeight() > mCaches.maxTextureSize ||
761 (fboLayer && clip.isEmpty())) {
762 mSnapshot->empty = fboLayer;
763 } else {
764 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
765 }
766}
767
Chris Craikd90144d2013-03-19 15:03:48 -0700768int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
769 int alpha, SkXfermode::Mode mode, int flags) {
770 const GLuint previousFbo = mSnapshot->fbo;
771 const int count = saveSnapshot(flags);
772
773 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
774 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
775 // operations will be able to store and restore the current clip and transform info, and
776 // quick rejection will be correct (for display lists)
777
778 Rect bounds(left, top, right, bottom);
779 Rect clip;
780 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700781 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700782
Chris Craik408eb122013-03-26 18:55:15 -0700783 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700784 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
785 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
786 }
787 }
788
789 return count;
790}
791
792
Romain Guy1c740bc2010-09-13 18:00:09 -0700793/**
794 * Layers are viewed by Skia are slightly different than layers in image editing
795 * programs (for instance.) When a layer is created, previously created layers
796 * and the frame buffer still receive every drawing command. For instance, if a
797 * layer is created and a shape intersecting the bounds of the layers and the
798 * framebuffer is draw, the shape will be drawn on both (unless the layer was
799 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
800 *
801 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
802 * texture. Unfortunately, this is inefficient as it requires every primitive to
803 * be drawn n + 1 times, where n is the number of active layers. In practice this
804 * means, for every primitive:
805 * - Switch active frame buffer
806 * - Change viewport, clip and projection matrix
807 * - Issue the drawing
808 *
809 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700810 * To avoid this, layers are implemented in a different way here, at least in the
811 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
812 * is set. When this flag is set we can redirect all drawing operations into a
813 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700814 *
815 * This implementation relies on the frame buffer being at least RGBA 8888. When
816 * a layer is created, only a texture is created, not an FBO. The content of the
817 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700818 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700819 * buffer and drawing continues as normal. This technique therefore treats the
820 * frame buffer as a scratch buffer for the layers.
821 *
822 * To compose the layers back onto the frame buffer, each layer texture
823 * (containing the original frame buffer data) is drawn as a simple quad over
824 * the frame buffer. The trick is that the quad is set as the composition
825 * destination in the blending equation, and the frame buffer becomes the source
826 * of the composition.
827 *
828 * Drawing layers with an alpha value requires an extra step before composition.
829 * An empty quad is drawn over the layer's region in the frame buffer. This quad
830 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
831 * quad is used to multiply the colors in the frame buffer. This is achieved by
832 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
833 * GL_ZERO, GL_SRC_ALPHA.
834 *
835 * Because glCopyTexImage2D() can be slow, an alternative implementation might
836 * be use to draw a single clipped layer. The implementation described above
837 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700838 *
839 * (1) The frame buffer is actually not cleared right away. To allow the GPU
840 * to potentially optimize series of calls to glCopyTexImage2D, the frame
841 * buffer is left untouched until the first drawing operation. Only when
842 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700843 */
Chet Haased48885a2012-08-28 17:43:28 -0700844bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
845 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700846 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700847 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700848
Romain Guyeb993562010-10-05 18:14:38 -0700849 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
850
Romain Guyf607bdc2010-09-10 19:20:06 -0700851 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700852 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700853 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700854 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700855 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700856
857 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700858 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700859 return false;
860 }
Romain Guyf18fd992010-07-08 11:45:51 -0700861
Romain Guya1d3c912011-12-13 14:55:06 -0800862 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700863 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700864 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700865 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700866 }
867
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700869 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700870 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
871 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800872 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700873 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700874 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700875
Romain Guy8fb95422010-08-17 18:38:51 -0700876 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700877 mSnapshot->flags |= Snapshot::kFlagIsLayer;
878 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700879
Chris Craik7273daa2013-03-28 11:25:24 -0700880 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700881 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700882 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700883 } else {
884 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800886 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700887 if (layer->isEmpty()) {
888 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700889 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700890 layer->getWidth(), layer->getHeight(), 0);
891 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800892 } else {
893 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700894 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800895 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700896
Romain Guy54be1cd2011-06-13 19:04:27 -0700897 // Enqueue the buffer coordinates to clear the corresponding region later
898 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700899 }
Romain Guyeb993562010-10-05 18:14:38 -0700900 }
Romain Guyf86ef572010-07-01 11:05:42 -0700901
Romain Guyd55a8612010-06-28 17:42:46 -0700902 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700903}
904
Chet Haased48885a2012-08-28 17:43:28 -0700905bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800906 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700908
Chet Haased48885a2012-08-28 17:43:28 -0700909 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800910 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
911 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700912 mSnapshot->fbo = layer->getFbo();
913 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
914 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
915 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
916 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700917 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700918
Romain Guy2b7028e2012-09-19 17:25:38 -0700919 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700920 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700921 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
923 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700924
925 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 if (layer->isEmpty()) {
927 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
928 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700929 }
930
931 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700933
Romain Guyf735c8e2013-01-31 17:45:55 -0800934 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700935
936 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700937 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800938 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700939 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700940 glClear(GL_COLOR_BUFFER_BIT);
941
942 dirtyClip();
943
944 // Change the ortho projection
945 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
946 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
947
948 return true;
949}
950
Romain Guy1c740bc2010-09-13 18:00:09 -0700951/**
952 * Read the documentation of createLayer() before doing anything in this method.
953 */
Romain Guy1d83e192010-08-17 11:37:00 -0700954void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
955 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000956 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700957 return;
958 }
959
Romain Guy8ce00302013-01-15 18:51:42 -0800960 Layer* layer = current->layer;
961 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700962 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700963
964 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700965 endTiling();
966
Romain Guye0aa84b2012-04-03 19:30:26 -0700967 // Detach the texture from the FBO
968 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800969
970 layer->removeFbo(false);
971
Romain Guyeb993562010-10-05 18:14:38 -0700972 // Unbind current FBO and restore previous one
973 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700974 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700975
976 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700977 }
978
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700980 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700982 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700983 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700984 }
985
Romain Guy03750a02010-10-18 14:06:08 -0700986 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700987
Romain Guya1d3c912011-12-13 14:55:06 -0800988 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700989
Romain Guy5b3b3522010-10-27 18:57:51 -0700990 // When the layer is stored in an FBO, we can save a bit of fillrate by
991 // drawing only the dirty region
992 if (fboLayer) {
993 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 if (layer->getColorFilter()) {
995 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800996 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700997 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800999 resetColorFilter();
1000 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001001 } else if (!rect.isEmpty()) {
1002 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1003 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001004 }
Romain Guy8b55f372010-08-18 17:10:07 -07001005
Romain Guy746b7402010-10-26 16:27:31 -07001006 dirtyClip();
1007
Romain Guyeb993562010-10-05 18:14:38 -07001008 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001009 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001010 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001011 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001012 }
1013}
1014
Romain Guyaa6c24c2011-04-28 18:40:04 -07001015void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001016 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001017
1018 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001020 setupDrawWithTexture();
1021 } else {
1022 setupDrawWithExternalTexture();
1023 }
1024 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001025 setupDrawColor(alpha, alpha, alpha, alpha);
1026 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001027 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001028 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001029 setupDrawPureColorUniforms();
1030 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1032 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001033 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001034 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001035 }
Romain Guy3b753822013-03-05 10:27:35 -08001036 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001037 layer->getWidth() == (uint32_t) rect.getWidth() &&
1038 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001039 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1040 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001041
Romain Guyd21b6e12011-11-30 20:21:23 -08001042 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001043 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1044 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001045 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001046 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1047 }
1048 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001049 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1050
1051 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1052
1053 finishDrawTexture();
1054}
1055
Romain Guy5b3b3522010-10-27 18:57:51 -07001056void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001058 const Rect& texCoords = layer->texCoords;
1059 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1060 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001061
Romain Guy9ace8f52011-07-07 20:50:11 -07001062 float x = rect.left;
1063 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001064 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001065 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001066 layer->getHeight() == (uint32_t) rect.getHeight();
1067
1068 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001069 // When we're swapping, the layer is already in screen coordinates
1070 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001071 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1072 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001073 }
1074
Romain Guyd21b6e12011-11-30 20:21:23 -08001075 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001076 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001077 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001078 }
1079
Chris Craike83569c2013-03-20 16:57:09 -07001080 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
1081 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001083 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001084 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1085 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001086
Romain Guyaa6c24c2011-04-28 18:40:04 -07001087 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1088 } else {
1089 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1090 drawTextureLayer(layer, rect);
1091 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1092 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001093}
1094
1095void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001096 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001097 layer->setRegionAsRect();
1098
Romain Guy40667672011-03-18 14:34:03 -07001099 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001100
Romain Guy5b3b3522010-10-27 18:57:51 -07001101 layer->region.clear();
1102 return;
1103 }
1104
Romain Guy8a3957d2011-09-07 17:55:15 -07001105 // TODO: See LayerRenderer.cpp::generateMesh() for important
1106 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001107 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001108 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001109 const android::Rect* rects;
1110 Region safeRegion;
1111 if (CC_LIKELY(hasRectToRectTransform())) {
1112 rects = layer->region.getArray(&count);
1113 } else {
1114 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1115 rects = safeRegion.getArray(&count);
1116 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001117
Chris Craike83569c2013-03-20 16:57:09 -07001118 const float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guy9ace8f52011-07-07 20:50:11 -07001119 const float texX = 1.0f / float(layer->getWidth());
1120 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001121 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001122
Romain Guy8ce00302013-01-15 18:51:42 -08001123 setupDraw();
1124
1125 // We must get (and therefore bind) the region mesh buffer
1126 // after we setup drawing in case we need to mess with the
1127 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001128 TextureVertex* mesh = mCaches.getRegionMesh();
1129 GLsizei numQuads = 0;
1130
Romain Guy7230a742011-01-10 22:26:16 -08001131 setupDrawWithTexture();
1132 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001133 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001134 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001135 setupDrawProgram();
1136 setupDrawDirtyRegionsDisabled();
1137 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001138 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001139 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001140 if (currentTransform().isPureTranslate()) {
1141 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1142 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001143
Romain Guyd21b6e12011-11-30 20:21:23 -08001144 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001145 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1146 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001147 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001148 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1149 }
Romain Guy15bc6432011-12-13 13:11:32 -08001150 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001151
1152 for (size_t i = 0; i < count; i++) {
1153 const android::Rect* r = &rects[i];
1154
1155 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001156 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001157 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001158 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001159
1160 // TODO: Reject quads outside of the clip
1161 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1162 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1163 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1164 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1165
1166 numQuads++;
1167
1168 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1169 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1170 numQuads = 0;
1171 mesh = mCaches.getRegionMesh();
1172 }
1173 }
1174
1175 if (numQuads > 0) {
1176 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1177 }
1178
Romain Guy7230a742011-01-10 22:26:16 -08001179 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001180
1181#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001182 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001183#endif
1184
1185 layer->region.clear();
1186 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001187}
1188
Romain Guy3a3133d2011-02-01 22:59:58 -08001189void OpenGLRenderer::drawRegionRects(const Region& region) {
1190#if DEBUG_LAYERS_AS_REGIONS
1191 size_t count;
1192 const android::Rect* rects = region.getArray(&count);
1193
1194 uint32_t colors[] = {
1195 0x7fff0000, 0x7f00ff00,
1196 0x7f0000ff, 0x7fff00ff,
1197 };
1198
1199 int offset = 0;
1200 int32_t top = rects[0].top;
1201
1202 for (size_t i = 0; i < count; i++) {
1203 if (top != rects[i].top) {
1204 offset ^= 0x2;
1205 top = rects[i].top;
1206 }
1207
1208 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1209 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1210 SkXfermode::kSrcOver_Mode);
1211 }
1212#endif
1213}
1214
Romain Guy8ce00302013-01-15 18:51:42 -08001215void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1216 SkXfermode::Mode mode, bool dirty) {
1217 int count = 0;
1218 Vector<float> rects;
1219
1220 SkRegion::Iterator it(region);
1221 while (!it.done()) {
1222 const SkIRect& r = it.rect();
1223 rects.push(r.fLeft);
1224 rects.push(r.fTop);
1225 rects.push(r.fRight);
1226 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001227 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001228 it.next();
1229 }
1230
Romain Guy3bbacf22013-02-06 16:51:04 -08001231 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001232}
1233
Romain Guy5b3b3522010-10-27 18:57:51 -07001234void OpenGLRenderer::dirtyLayer(const float left, const float top,
1235 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001236 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001237 Rect bounds(left, top, right, bottom);
1238 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001239 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001241}
1242
1243void OpenGLRenderer::dirtyLayer(const float left, const float top,
1244 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001245 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001247 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001249}
1250
1251void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001252 if (bounds.intersect(*mSnapshot->clipRect)) {
1253 bounds.snapToPixelBoundaries();
1254 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1255 if (!dirty.isEmpty()) {
1256 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 }
1258 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001259}
1260
Romain Guy54be1cd2011-06-13 19:04:27 -07001261void OpenGLRenderer::clearLayerRegions() {
1262 const size_t count = mLayers.size();
1263 if (count == 0) return;
1264
1265 if (!mSnapshot->isIgnored()) {
1266 // Doing several glScissor/glClear here can negatively impact
1267 // GPUs with a tiler architecture, instead we draw quads with
1268 // the Clear blending mode
1269
1270 // The list contains bounds that have already been clipped
1271 // against their initial clip rect, and the current clip
1272 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001273 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001274
1275 Vertex mesh[count * 6];
1276 Vertex* vertex = mesh;
1277
1278 for (uint32_t i = 0; i < count; i++) {
1279 Rect* bounds = mLayers.itemAt(i);
1280
1281 Vertex::set(vertex++, bounds->left, bounds->bottom);
1282 Vertex::set(vertex++, bounds->left, bounds->top);
1283 Vertex::set(vertex++, bounds->right, bounds->top);
1284 Vertex::set(vertex++, bounds->left, bounds->bottom);
1285 Vertex::set(vertex++, bounds->right, bounds->top);
1286 Vertex::set(vertex++, bounds->right, bounds->bottom);
1287
1288 delete bounds;
1289 }
Romain Guye67307c2013-02-11 18:01:20 -08001290 // We must clear the list of dirty rects before we
1291 // call setupDraw() to prevent stencil setup to do
1292 // the same thing again
1293 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001294
1295 setupDraw(false);
1296 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1297 setupDrawBlending(true, SkXfermode::kClear_Mode);
1298 setupDrawProgram();
1299 setupDrawPureColorUniforms();
1300 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001301 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001302
Romain Guy54be1cd2011-06-13 19:04:27 -07001303 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001304
1305 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001306 } else {
1307 for (uint32_t i = 0; i < count; i++) {
1308 delete mLayers.itemAt(i);
1309 }
Romain Guye67307c2013-02-11 18:01:20 -08001310 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001311 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001312}
1313
Romain Guybd6b79b2010-06-26 00:13:53 -07001314///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001315// State Deferral
1316///////////////////////////////////////////////////////////////////////////////
1317
Chris Craikff785832013-03-08 13:12:16 -08001318bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001319 const Rect& currentClip = *(mSnapshot->clipRect);
1320 const mat4& currentMatrix = *(mSnapshot->transform);
1321
Chris Craikff785832013-03-08 13:12:16 -08001322 if (stateDeferFlags & kStateDeferFlag_Draw) {
1323 // state has bounds initialized in local coordinates
1324 if (!state.mBounds.isEmpty()) {
1325 currentMatrix.mapRect(state.mBounds);
1326 if (!state.mBounds.intersect(currentClip)) {
1327 // quick rejected
1328 return true;
1329 }
1330 } else {
1331 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001332 }
1333 }
1334
Chris Craikff785832013-03-08 13:12:16 -08001335 if (stateDeferFlags & kStateDeferFlag_Clip) {
1336 state.mClip.set(currentClip);
1337 } else {
1338 state.mClip.setEmpty();
1339 }
1340
Chris Craik7273daa2013-03-28 11:25:24 -07001341 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1342 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001343 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001344 state.mDrawModifiers = mDrawModifiers;
1345 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001346 return false;
1347}
1348
Chris Craik7273daa2013-03-28 11:25:24 -07001349void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state) {
Romain Guy3b753822013-03-05 10:27:35 -08001350 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001351 mDrawModifiers = state.mDrawModifiers;
1352 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001353
Chris Craikd90144d2013-03-19 15:03:48 -07001354 if (!state.mClip.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001355 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1356 dirtyClip();
1357 }
Chris Craikc3566d02013-02-04 16:16:33 -08001358}
1359
1360///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001361// Transforms
1362///////////////////////////////////////////////////////////////////////////////
1363
1364void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001365 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001366}
1367
1368void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001369 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001370}
1371
1372void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001373 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001374}
1375
Romain Guy807daf72011-01-18 11:19:19 -08001376void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001377 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001378}
1379
Romain Guyf6a11b82010-06-23 17:47:49 -07001380void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001381 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001382 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001383 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001384 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001385 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001386}
1387
Chris Craikb98a0162013-02-21 11:30:22 -08001388bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001389 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001390}
1391
Romain Guyf6a11b82010-06-23 17:47:49 -07001392void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001393 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001394}
1395
1396void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001397 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001398 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001399 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001400 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001401}
1402
1403///////////////////////////////////////////////////////////////////////////////
1404// Clipping
1405///////////////////////////////////////////////////////////////////////////////
1406
Romain Guybb9524b2010-06-22 18:56:38 -07001407void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001408 Rect clip(*mSnapshot->clipRect);
1409 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001410
Romain Guy8a4ac612012-07-17 17:32:48 -07001411 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1412 clip.getWidth(), clip.getHeight())) {
1413 mDirtyClip = false;
1414 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001415}
1416
Romain Guy8ce00302013-01-15 18:51:42 -08001417void OpenGLRenderer::ensureStencilBuffer() {
1418 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1419 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1420 // just hope we have one when hasLayer() returns false.
1421 if (hasLayer()) {
1422 attachStencilBufferToLayer(mSnapshot->layer);
1423 }
1424}
1425
1426void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1427 // The layer's FBO is already bound when we reach this stage
1428 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001429 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1430 // is attached after we initiated tiling. We must turn it off,
1431 // attach the new render buffer then turn tiling back on
1432 endTiling();
1433
Romain Guy8d4aeb72013-02-12 16:08:55 -08001434 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001435 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001436 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001437
Romain Guyf735c8e2013-01-31 17:45:55 -08001438 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001439 }
1440}
1441
1442void OpenGLRenderer::setStencilFromClip() {
1443 if (!mCaches.debugOverdraw) {
1444 if (!mSnapshot->clipRegion->isEmpty()) {
1445 // NOTE: The order here is important, we must set dirtyClip to false
1446 // before any draw call to avoid calling back into this method
1447 mDirtyClip = false;
1448
1449 ensureStencilBuffer();
1450
1451 mCaches.stencil.enableWrite();
1452
1453 // Clear the stencil but first make sure we restrict drawing
1454 // to the region's bounds
1455 bool resetScissor = mCaches.enableScissor();
1456 if (resetScissor) {
1457 // The scissor was not set so we now need to update it
1458 setScissorFromClip();
1459 }
1460 mCaches.stencil.clear();
1461 if (resetScissor) mCaches.disableScissor();
1462
1463 // NOTE: We could use the region contour path to generate a smaller mesh
1464 // Since we are using the stencil we could use the red book path
1465 // drawing technique. It might increase bandwidth usage though.
1466
1467 // The last parameter is important: we are not drawing in the color buffer
1468 // so we don't want to dirty the current layer, if any
1469 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1470
1471 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001472
1473 // Draw the region used to generate the stencil if the appropriate debug
1474 // mode is enabled
1475 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1476 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1477 }
Romain Guy8ce00302013-01-15 18:51:42 -08001478 } else {
1479 mCaches.stencil.disable();
1480 }
1481 }
1482}
1483
Romain Guy9d5316e2010-06-24 19:30:36 -07001484const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001485 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001486}
1487
Romain Guy8a4ac612012-07-17 17:32:48 -07001488bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1489 if (mSnapshot->isIgnored()) {
1490 return true;
1491 }
1492
1493 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001494 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001495 r.snapToPixelBoundaries();
1496
1497 Rect clipRect(*mSnapshot->clipRect);
1498 clipRect.snapToPixelBoundaries();
1499
1500 return !clipRect.intersects(r);
1501}
1502
Romain Guy35643dd2012-09-18 15:40:58 -07001503bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1504 Rect& transformed, Rect& clip) {
1505 if (mSnapshot->isIgnored()) {
1506 return true;
1507 }
1508
1509 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001510 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001511 transformed.snapToPixelBoundaries();
1512
1513 clip.set(*mSnapshot->clipRect);
1514 clip.snapToPixelBoundaries();
1515
1516 return !clip.intersects(transformed);
1517}
1518
Romain Guy672433d2013-01-04 19:05:13 -08001519bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1520 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001521 if (paint->getStyle() != SkPaint::kFill_Style) {
1522 float outset = paint->getStrokeWidth() * 0.5f;
1523 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1524 } else {
1525 return quickReject(left, top, right, bottom);
1526 }
1527}
1528
Romain Guyc7d53492010-06-25 13:41:57 -07001529bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001530 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001531 return true;
1532 }
1533
Romain Guy1d83e192010-08-17 11:37:00 -07001534 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001535 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001536 r.snapToPixelBoundaries();
1537
1538 Rect clipRect(*mSnapshot->clipRect);
1539 clipRect.snapToPixelBoundaries();
1540
Romain Guy586cae32012-07-13 15:28:31 -07001541 bool rejected = !clipRect.intersects(r);
1542 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001543 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001544 }
1545
1546 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001547}
1548
Romain Guy8ce00302013-01-15 18:51:42 -08001549void OpenGLRenderer::debugClip() {
1550#if DEBUG_CLIP_REGIONS
1551 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1552 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1553 }
1554#endif
1555}
1556
Romain Guy079ba2c2010-07-16 14:12:24 -07001557bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001558 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001559 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1560 if (clipped) {
1561 dirtyClip();
1562 }
1563 return !mSnapshot->clipRect->isEmpty();
1564 }
1565
1566 SkPath path;
1567 path.addRect(left, top, right, bottom);
1568
1569 return clipPath(&path, op);
1570}
1571
1572bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1573 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001574 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001575
1576 SkPath transformed;
1577 path->transform(transform, &transformed);
1578
1579 SkRegion clip;
1580 if (!mSnapshot->clipRegion->isEmpty()) {
1581 clip.setRegion(*mSnapshot->clipRegion);
1582 } else {
1583 Rect* bounds = mSnapshot->clipRect;
1584 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1585 }
1586
1587 SkRegion region;
1588 region.setPath(transformed, clip);
1589
1590 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001591 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001592 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001593 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001594 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001595}
1596
Romain Guy735738c2012-12-03 12:34:51 -08001597bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001598 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1599 if (clipped) {
1600 dirtyClip();
1601 }
1602 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001603}
1604
Chet Haasea23eed82012-04-12 15:19:04 -07001605Rect* OpenGLRenderer::getClipRect() {
1606 return mSnapshot->clipRect;
1607}
1608
Romain Guyf6a11b82010-06-23 17:47:49 -07001609///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001610// Drawing commands
1611///////////////////////////////////////////////////////////////////////////////
1612
Romain Guy54be1cd2011-06-13 19:04:27 -07001613void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001614 // TODO: It would be best if we could do this before quickReject()
1615 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001616 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001617 // Make sure setScissor & setStencil happen at the beginning of
1618 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001619 if (mDirtyClip) {
1620 if (mCaches.scissorEnabled) {
1621 setScissorFromClip();
1622 }
Romain Guy8ce00302013-01-15 18:51:42 -08001623 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001624 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001625
Romain Guy70ca14e2010-12-13 18:24:33 -08001626 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001627
Romain Guy70ca14e2010-12-13 18:24:33 -08001628 mSetShaderColor = false;
1629 mColorSet = false;
1630 mColorA = mColorR = mColorG = mColorB = 0.0f;
1631 mTextureUnit = 0;
1632 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001633
1634 // Enable debug highlight when what we're about to draw is tested against
1635 // the stencil buffer and if stencil highlight debugging is on
1636 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1637 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1638 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001639}
1640
1641void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1642 mDescription.hasTexture = true;
1643 mDescription.hasAlpha8Texture = isAlpha8;
1644}
1645
Romain Guyff316ec2013-02-13 18:39:43 -08001646void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1647 mDescription.hasTexture = true;
1648 mDescription.hasColors = true;
1649 mDescription.hasAlpha8Texture = isAlpha8;
1650}
1651
Romain Guyaa6c24c2011-04-28 18:40:04 -07001652void OpenGLRenderer::setupDrawWithExternalTexture() {
1653 mDescription.hasExternalTexture = true;
1654}
1655
Romain Guy15bc6432011-12-13 13:11:32 -08001656void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001657 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001658}
1659
Chris Craik710f46d2012-09-17 17:25:49 -07001660void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001661 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001662}
1663
Romain Guyed6fcb02011-03-21 13:11:28 -07001664void OpenGLRenderer::setupDrawPoint(float pointSize) {
1665 mDescription.isPoint = true;
1666 mDescription.pointSize = pointSize;
1667}
1668
Romain Guy8d0d4782010-12-14 20:13:35 -08001669void OpenGLRenderer::setupDrawColor(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 Guy70ca14e2010-12-13 18:24:33 -08001674 mColorSet = true;
1675 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1676}
1677
Romain Guy86568192010-12-14 15:55:39 -08001678void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1679 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001680 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1681 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1682 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001683 mColorSet = true;
1684 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1685}
1686
Romain Guy41210632012-07-16 17:04:24 -07001687void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1688 mCaches.fontRenderer->describe(mDescription, paint);
1689}
1690
Romain Guy70ca14e2010-12-13 18:24:33 -08001691void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1692 mColorA = a;
1693 mColorR = r;
1694 mColorG = g;
1695 mColorB = b;
1696 mColorSet = true;
1697 mSetShaderColor = mDescription.setColor(r, g, b, a);
1698}
1699
1700void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001701 if (mDrawModifiers.mShader) {
1702 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001703 }
1704}
1705
1706void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001707 if (mDrawModifiers.mColorFilter) {
1708 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001709 }
1710}
1711
Romain Guyf09ef512011-05-27 11:43:46 -07001712void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1713 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1714 mColorA = 1.0f;
1715 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001716 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001717 }
1718}
1719
Romain Guy70ca14e2010-12-13 18:24:33 -08001720void OpenGLRenderer::setupDrawBlending(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 bool blend = (mColorSet && mColorA < 1.0f) ||
1725 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1726 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001727}
1728
1729void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001730 // When the blending mode is kClear_Mode, we need to use a modulate color
1731 // argb=1,0,0,0
1732 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001733 blend |= (mColorSet && mColorA < 1.0f) ||
1734 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1735 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1736 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001737}
1738
1739void OpenGLRenderer::setupDrawProgram() {
1740 useProgram(mCaches.programCache.get(mDescription));
1741}
1742
1743void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1744 mTrackDirtyRegions = false;
1745}
1746
1747void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1748 bool ignoreTransform) {
1749 mModelView.loadTranslate(left, top, 0.0f);
1750 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001751 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1752 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001754 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001755 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1756 }
1757}
1758
Chet Haase8a5cc922011-04-26 07:28:09 -07001759void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001760 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001761}
1762
Romain Guy70ca14e2010-12-13 18:24:33 -08001763void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1764 bool ignoreTransform, bool ignoreModelView) {
1765 if (!ignoreModelView) {
1766 mModelView.loadTranslate(left, top, 0.0f);
1767 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001768 } else {
1769 mModelView.loadIdentity();
1770 }
Romain Guy86568192010-12-14 15:55:39 -08001771 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1772 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001773 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001774 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001775 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001776 }
1777 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001778 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001779 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1780 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001781}
1782
Romain Guyed6fcb02011-03-21 13:11:28 -07001783void OpenGLRenderer::setupDrawPointUniforms() {
1784 int slot = mCaches.currentProgram->getUniform("pointSize");
1785 glUniform1f(slot, mDescription.pointSize);
1786}
1787
Romain Guy70ca14e2010-12-13 18:24:33 -08001788void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001789 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001790 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1791 }
1792}
1793
Romain Guy86568192010-12-14 15:55:39 -08001794void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001795 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001796 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001797 }
1798}
1799
Romain Guy70ca14e2010-12-13 18:24:33 -08001800void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001801 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001802 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001803 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001804 }
Chris Craikc3566d02013-02-04 16:16:33 -08001805 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1806 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001807 }
1808}
1809
Romain Guy8d0d4782010-12-14 20:13:35 -08001810void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001811 if (mDrawModifiers.mShader) {
1812 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001813 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001814 }
1815}
1816
Romain Guy70ca14e2010-12-13 18:24:33 -08001817void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001818 if (mDrawModifiers.mColorFilter) {
1819 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001820 }
1821}
1822
Romain Guy41210632012-07-16 17:04:24 -07001823void OpenGLRenderer::setupDrawTextGammaUniforms() {
1824 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1825}
1826
Romain Guy70ca14e2010-12-13 18:24:33 -08001827void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001828 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001829 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001830 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
1833void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001834 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001835 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001836 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001837}
1838
Romain Guyaa6c24c2011-04-28 18:40:04 -07001839void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1840 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001841 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001842 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001843}
1844
Romain Guy8f0095c2011-05-02 17:24:22 -07001845void OpenGLRenderer::setupDrawTextureTransform() {
1846 mDescription.hasTextureTransform = true;
1847}
1848
1849void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001850 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1851 GL_FALSE, &transform.data[0]);
1852}
1853
Romain Guy70ca14e2010-12-13 18:24:33 -08001854void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001855 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001856 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001857 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001858 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001859 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001860 }
Romain Guyd71dd362011-12-12 19:03:35 -08001861
Chris Craikcb4d6002012-09-25 12:00:29 -07001862 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001863 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001864 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001865 }
1866
1867 mCaches.unbindIndicesBuffer();
1868}
1869
Romain Guyff316ec2013-02-13 18:39:43 -08001870void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1871 bool force = mCaches.unbindMeshBuffer();
1872 GLsizei stride = sizeof(ColorTextureVertex);
1873
1874 mCaches.bindPositionVertexPointer(force, vertices, stride);
1875 if (mCaches.currentProgram->texCoords >= 0) {
1876 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1877 }
1878 int slot = mCaches.currentProgram->getAttrib("colors");
1879 if (slot >= 0) {
1880 glEnableVertexAttribArray(slot);
1881 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1882 }
1883
1884 mCaches.unbindIndicesBuffer();
1885}
1886
Romain Guy15bc6432011-12-13 13:11:32 -08001887void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1888 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001889 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001890 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001891 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001892 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001893}
1894
Chet Haase5b0200b2011-04-13 17:58:08 -07001895void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001896 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001897 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001898 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001899}
1900
Romain Guy70ca14e2010-12-13 18:24:33 -08001901void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001902}
1903
1904///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001905// Drawing
1906///////////////////////////////////////////////////////////////////////////////
1907
Chris Craikff785832013-03-08 13:12:16 -08001908status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1909 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001910 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1911 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001912 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001913 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -07001914 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001915 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1916 displayList->replay(replayStruct, 0);
1917 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001918 }
1919
1920 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001921 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1922 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001923
1924 flushLayers();
1925 startFrame();
1926
Chris Craikff785832013-03-08 13:12:16 -08001927 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001928 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001929
Romain Guy65549432012-03-26 16:45:05 -07001930 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001931}
1932
Chris Craikc3566d02013-02-04 16:16:33 -08001933void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001934 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001935 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001936 }
1937}
1938
Romain Guya168d732011-03-18 16:50:13 -07001939void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1940 int alpha;
1941 SkXfermode::Mode mode;
1942 getAlphaAndMode(paint, &alpha, &mode);
1943
Romain Guy886b2752013-01-04 12:26:18 -08001944 int color = paint != NULL ? paint->getColor() : 0;
1945
Romain Guya168d732011-03-18 16:50:13 -07001946 float x = left;
1947 float y = top;
1948
Romain Guy886b2752013-01-04 12:26:18 -08001949 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1950
Romain Guya168d732011-03-18 16:50:13 -07001951 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001952 if (currentTransform().isPureTranslate()) {
1953 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1954 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001955 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001956
1957 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001958 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001959 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001960 }
1961
Romain Guy886b2752013-01-04 12:26:18 -08001962 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1963 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1964 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001965}
1966
Chet Haase48659092012-05-31 15:21:51 -07001967status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001968 const float right = left + bitmap->width();
1969 const float bottom = top + bitmap->height();
1970
1971 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001972 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001973 }
1974
Romain Guya1d3c912011-12-13 14:55:06 -08001975 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001976 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001977 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001978 const AutoTexture autoCleanup(texture);
1979
Romain Guy211370f2012-02-01 16:10:55 -08001980 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001981 drawAlphaBitmap(texture, left, top, paint);
1982 } else {
1983 drawTextureRect(left, top, right, bottom, texture, paint);
1984 }
Chet Haase48659092012-05-31 15:21:51 -07001985
1986 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001987}
1988
Chet Haase48659092012-05-31 15:21:51 -07001989status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001990 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1991 const mat4 transform(*matrix);
1992 transform.mapRect(r);
1993
Romain Guy6926c722010-07-12 20:20:03 -07001994 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001995 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001996 }
1997
Romain Guya1d3c912011-12-13 14:55:06 -08001998 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001999 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002000 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002001 const AutoTexture autoCleanup(texture);
2002
Romain Guy5b3b3522010-10-27 18:57:51 -07002003 // This could be done in a cheaper way, all we need is pass the matrix
2004 // to the vertex shader. The save/restore is a bit overkill.
2005 save(SkCanvas::kMatrix_SaveFlag);
2006 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002007 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2008 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2009 } else {
2010 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2011 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002012 restore();
Chet Haase48659092012-05-31 15:21:51 -07002013
2014 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002015}
2016
Chet Haase48659092012-05-31 15:21:51 -07002017status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002018 const float right = left + bitmap->width();
2019 const float bottom = top + bitmap->height();
2020
2021 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002022 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002023 }
2024
2025 mCaches.activeTexture(0);
2026 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2027 const AutoTexture autoCleanup(texture);
2028
Romain Guy886b2752013-01-04 12:26:18 -08002029 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2030 drawAlphaBitmap(texture, left, top, paint);
2031 } else {
2032 drawTextureRect(left, top, right, bottom, texture, paint);
2033 }
Chet Haase48659092012-05-31 15:21:51 -07002034
2035 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002036}
2037
Chet Haase48659092012-05-31 15:21:51 -07002038status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002039 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002040 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002041 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002042 }
2043
Romain Guyb18d2d02011-02-10 15:52:54 -08002044 float left = FLT_MAX;
2045 float top = FLT_MAX;
2046 float right = FLT_MIN;
2047 float bottom = FLT_MIN;
2048
Romain Guya92bb4d2012-10-16 11:08:44 -07002049 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002050
Romain Guyff316ec2013-02-13 18:39:43 -08002051 ColorTextureVertex mesh[count];
2052 ColorTextureVertex* vertex = mesh;
2053
2054 bool cleanupColors = false;
2055 if (!colors) {
2056 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2057 colors = new int[colorsCount];
2058 memset(colors, 0xff, colorsCount * sizeof(int));
2059 cleanupColors = true;
2060 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002061
Romain Guy5a7b4662011-01-20 19:09:30 -08002062 for (int32_t y = 0; y < meshHeight; y++) {
2063 for (int32_t x = 0; x < meshWidth; x++) {
2064 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2065
2066 float u1 = float(x) / meshWidth;
2067 float u2 = float(x + 1) / meshWidth;
2068 float v1 = float(y) / meshHeight;
2069 float v2 = float(y + 1) / meshHeight;
2070
2071 int ax = i + (meshWidth + 1) * 2;
2072 int ay = ax + 1;
2073 int bx = i;
2074 int by = bx + 1;
2075 int cx = i + 2;
2076 int cy = cx + 1;
2077 int dx = i + (meshWidth + 1) * 2 + 2;
2078 int dy = dx + 1;
2079
Romain Guyff316ec2013-02-13 18:39:43 -08002080 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2081 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2082 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002083
Romain Guyff316ec2013-02-13 18:39:43 -08002084 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2085 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2086 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002087
Romain Guya92bb4d2012-10-16 11:08:44 -07002088 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2089 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2090 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2091 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002092 }
2093 }
2094
Romain Guya92bb4d2012-10-16 11:08:44 -07002095 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002096 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002097 return DrawGlInfo::kStatusDone;
2098 }
2099
2100 mCaches.activeTexture(0);
2101 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002102 if (!texture) {
2103 if (cleanupColors) delete[] colors;
2104 return DrawGlInfo::kStatusDone;
2105 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002106 const AutoTexture autoCleanup(texture);
2107
2108 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2109 texture->setFilter(FILTER(paint), true);
2110
2111 int alpha;
2112 SkXfermode::Mode mode;
2113 getAlphaAndMode(paint, &alpha, &mode);
2114
Romain Guyff316ec2013-02-13 18:39:43 -08002115 float a = alpha / 255.0f;
2116
Romain Guya92bb4d2012-10-16 11:08:44 -07002117 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002118 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002119 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002120
Romain Guyff316ec2013-02-13 18:39:43 -08002121 setupDraw();
2122 setupDrawWithTextureAndColor();
2123 setupDrawColor(a, a, a, a);
2124 setupDrawColorFilter();
2125 setupDrawBlending(true, mode, false);
2126 setupDrawProgram();
2127 setupDrawDirtyRegionsDisabled();
2128 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2129 setupDrawTexture(texture->id);
2130 setupDrawPureColorUniforms();
2131 setupDrawColorFilterUniforms();
2132 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2133
2134 glDrawArrays(GL_TRIANGLES, 0, count);
2135
2136 finishDrawTexture();
2137
2138 int slot = mCaches.currentProgram->getAttrib("colors");
2139 if (slot >= 0) {
2140 glDisableVertexAttribArray(slot);
2141 }
2142
2143 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002144
2145 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002146}
2147
Chet Haase48659092012-05-31 15:21:51 -07002148status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002149 float srcLeft, float srcTop, float srcRight, float srcBottom,
2150 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002151 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002152 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002153 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002154 }
2155
Romain Guya1d3c912011-12-13 14:55:06 -08002156 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002157 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002158 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002159 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002160
Romain Guy8ba548f2010-06-30 19:21:21 -07002161 const float width = texture->width;
2162 const float height = texture->height;
2163
Romain Guy68169722011-08-22 17:33:33 -07002164 const float u1 = fmax(0.0f, srcLeft / width);
2165 const float v1 = fmax(0.0f, srcTop / height);
2166 const float u2 = fmin(1.0f, srcRight / width);
2167 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002168
Romain Guy03750a02010-10-18 14:06:08 -07002169 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002170 resetDrawTextureTexCoords(u1, v1, u2, v2);
2171
Romain Guy03750a02010-10-18 14:06:08 -07002172 int alpha;
2173 SkXfermode::Mode mode;
2174 getAlphaAndMode(paint, &alpha, &mode);
2175
Romain Guyd21b6e12011-11-30 20:21:23 -08002176 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2177
Romain Guy886b2752013-01-04 12:26:18 -08002178 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2179 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002180
Romain Guy886b2752013-01-04 12:26:18 -08002181 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2182 // Apply a scale transform on the canvas only when a shader is in use
2183 // Skia handles the ratio between the dst and src rects as a scale factor
2184 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002185 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002186 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002187
Romain Guy3b753822013-03-05 10:27:35 -08002188 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2189 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2190 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002191
2192 dstRight = x + (dstRight - dstLeft);
2193 dstBottom = y + (dstBottom - dstTop);
2194
2195 dstLeft = x;
2196 dstTop = y;
2197
2198 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2199 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002200 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002201 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002202 }
2203
2204 if (CC_UNLIKELY(useScaleTransform)) {
2205 save(SkCanvas::kMatrix_SaveFlag);
2206 translate(dstLeft, dstTop);
2207 scale(scaleX, scaleY);
2208
2209 dstLeft = 0.0f;
2210 dstTop = 0.0f;
2211
2212 dstRight = srcRight - srcLeft;
2213 dstBottom = srcBottom - srcTop;
2214 }
2215
2216 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2217 int color = paint ? paint->getColor() : 0;
2218 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2219 texture->id, paint != NULL, color, alpha, mode,
2220 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2221 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2222 } else {
2223 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2224 texture->id, alpha / 255.0f, mode, texture->blend,
2225 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2226 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2227 }
2228
2229 if (CC_UNLIKELY(useScaleTransform)) {
2230 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002231 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002232
2233 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002234
2235 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002236}
2237
Chet Haase48659092012-05-31 15:21:51 -07002238status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002239 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002240 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002241 int alpha;
2242 SkXfermode::Mode mode;
2243 getAlphaAndModeDirect(paint, &alpha, &mode);
2244
2245 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2246 left, top, right, bottom, alpha, mode);
2247}
2248
2249status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2250 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2251 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002252 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002253 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002254 }
2255
Romain Guybe6f9dc2012-07-16 12:41:17 -07002256 alpha *= mSnapshot->alpha;
2257
Romain Guy2728f962010-10-08 18:36:15 -07002258 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002259 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002260
Romain Guy211370f2012-02-01 16:10:55 -08002261 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002262 mCaches.activeTexture(0);
2263 Texture* texture = mCaches.textureCache.get(bitmap);
2264 if (!texture) return DrawGlInfo::kStatusDone;
2265 const AutoTexture autoCleanup(texture);
2266 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2267 texture->setFilter(GL_LINEAR, true);
2268
Romain Guy3b753822013-03-05 10:27:35 -08002269 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002270 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002271 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002272 const float offsetX = left + currentTransform().getTranslateX();
2273 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002274 const size_t count = mesh->quads.size();
2275 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002276 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002277 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002278 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2279 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2280 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002281 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002282 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002283 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 }
2286 }
2287
Romain Guy211370f2012-02-01 16:10:55 -08002288 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002289 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2290 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002291
2292 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2293 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2294 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2295 true, !mesh->hasEmptyQuads);
2296 } else {
2297 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2298 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2299 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2300 true, !mesh->hasEmptyQuads);
2301 }
Romain Guy054dc182010-10-15 17:55:25 -07002302 }
Chet Haase48659092012-05-31 15:21:51 -07002303
2304 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002305}
2306
Chris Craik65cd6122012-12-10 17:56:27 -08002307status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2308 bool useOffset) {
2309 if (!vertexBuffer.getSize()) {
2310 // no vertices to draw
2311 return DrawGlInfo::kStatusDone;
2312 }
2313
Chris Craikcb4d6002012-09-25 12:00:29 -07002314 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002315 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2316 bool isAA = paint->isAntiAlias();
2317
Chris Craik710f46d2012-09-17 17:25:49 -07002318 setupDraw();
2319 setupDrawNoTexture();
2320 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002321 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2322 setupDrawColorFilter();
2323 setupDrawShader();
2324 setupDrawBlending(isAA, mode);
2325 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002326 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002327 setupDrawColorUniforms();
2328 setupDrawColorFilterUniforms();
2329 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002330
Chris Craik710f46d2012-09-17 17:25:49 -07002331 void* vertices = vertexBuffer.getBuffer();
2332 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002333 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002334 mCaches.resetTexCoordsVertexPointer();
2335 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002336
Chris Craik710f46d2012-09-17 17:25:49 -07002337 int alphaSlot = -1;
2338 if (isAA) {
2339 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2340 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002341
Chris Craik710f46d2012-09-17 17:25:49 -07002342 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002343 glEnableVertexAttribArray(alphaSlot);
2344 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002345 }
Romain Guy04299382012-07-18 17:15:41 -07002346
Chris Craik710f46d2012-09-17 17:25:49 -07002347 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002348
Chris Craik710f46d2012-09-17 17:25:49 -07002349 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002350 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002351 }
Chris Craik65cd6122012-12-10 17:56:27 -08002352
2353 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002354}
2355
2356/**
Chris Craik65cd6122012-12-10 17:56:27 -08002357 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2358 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2359 * screen space in all directions. However, instead of using a fragment shader to compute the
2360 * translucency of the color from its position, we simply use a varying parameter to define how far
2361 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2362 *
2363 * Doesn't yet support joins, caps, or path effects.
2364 */
2365status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2366 VertexBuffer vertexBuffer;
2367 // TODO: try clipping large paths to viewport
2368 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2369
Romain Guyc46d07a2013-03-15 19:06:39 -07002370 if (hasLayer()) {
2371 SkRect bounds = path.getBounds();
2372 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2373 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2374 }
Chris Craik65cd6122012-12-10 17:56:27 -08002375
2376 return drawVertexBuffer(vertexBuffer, paint);
2377}
2378
2379/**
2380 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2381 * and additional geometry for defining an alpha slope perimeter.
2382 *
2383 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2384 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2385 * in-shader alpha region, but found it to be taxing on some GPUs.
2386 *
2387 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2388 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002389 */
Chet Haase48659092012-05-31 15:21:51 -07002390status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002391 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002392
Chris Craik65cd6122012-12-10 17:56:27 -08002393 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002394
Chris Craik65cd6122012-12-10 17:56:27 -08002395 VertexBuffer buffer;
2396 SkRect bounds;
2397 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002398
2399 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2400 return DrawGlInfo::kStatusDone;
2401 }
2402
Romain Guy3b753822013-03-05 10:27:35 -08002403 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002404
Chris Craik65cd6122012-12-10 17:56:27 -08002405 bool useOffset = !paint->isAntiAlias();
2406 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002407}
2408
Chet Haase48659092012-05-31 15:21:51 -07002409status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2410 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002411
2412 // TODO: The paint's cap style defines whether the points are square or circular
2413 // TODO: Handle AA for round points
2414
Chet Haase5b0200b2011-04-13 17:58:08 -07002415 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002416 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002417 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002418 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002419 if (isHairLine) {
2420 // Now that we know it's hairline, we can set the effective width, to be used later
2421 strokeWidth = 1.0f;
2422 }
2423 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002424
Romain Guyed6fcb02011-03-21 13:11:28 -07002425 int alpha;
2426 SkXfermode::Mode mode;
2427 getAlphaAndMode(paint, &alpha, &mode);
2428
2429 int verticesCount = count >> 1;
2430 int generatedVerticesCount = 0;
2431
2432 TextureVertex pointsData[verticesCount];
2433 TextureVertex* vertex = &pointsData[0];
2434
Romain Guy04299382012-07-18 17:15:41 -07002435 // TODO: We should optimize this method to not generate vertices for points
2436 // that lie outside of the clip.
2437 mCaches.enableScissor();
2438
Romain Guyed6fcb02011-03-21 13:11:28 -07002439 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002440 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002441 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002442 setupDrawColor(paint->getColor(), alpha);
2443 setupDrawColorFilter();
2444 setupDrawShader();
2445 setupDrawBlending(mode);
2446 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002447 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002448 setupDrawColorUniforms();
2449 setupDrawColorFilterUniforms();
2450 setupDrawPointUniforms();
2451 setupDrawShaderIdentityUniforms();
2452 setupDrawMesh(vertex);
2453
2454 for (int i = 0; i < count; i += 2) {
2455 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2456 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002457
Chet Haase8a5cc922011-04-26 07:28:09 -07002458 float left = points[i] - halfWidth;
2459 float right = points[i] + halfWidth;
2460 float top = points[i + 1] - halfWidth;
2461 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002462
Romain Guy3b753822013-03-05 10:27:35 -08002463 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002464 }
2465
2466 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002467
2468 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002469}
2470
Chet Haase48659092012-05-31 15:21:51 -07002471status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002472 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002473 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002474
Romain Guyae88e5e2010-10-22 17:49:18 -07002475 Rect& clip(*mSnapshot->clipRect);
2476 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002477
Romain Guy3d58c032010-07-14 16:34:53 -07002478 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002479
2480 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002481}
Romain Guy9d5316e2010-06-24 19:30:36 -07002482
Chet Haase48659092012-05-31 15:21:51 -07002483status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2484 SkPaint* paint) {
2485 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002486 const AutoTexture autoCleanup(texture);
2487
2488 const float x = left + texture->left - texture->offset;
2489 const float y = top + texture->top - texture->offset;
2490
2491 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002492
2493 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002494}
2495
Chet Haase48659092012-05-31 15:21:51 -07002496status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002497 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002498 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2499 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002500 return DrawGlInfo::kStatusDone;
2501 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002502
Chris Craikcb4d6002012-09-25 12:00:29 -07002503 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002504 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002505 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002506 right - left, bottom - top, rx, ry, p);
2507 return drawShape(left, top, texture, p);
2508 }
2509
2510 SkPath path;
2511 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002512 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2513 float outset = p->getStrokeWidth() / 2;
2514 rect.outset(outset, outset);
2515 rx += outset;
2516 ry += outset;
2517 }
Chris Craik710f46d2012-09-17 17:25:49 -07002518 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002519 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002520}
2521
Chris Craik710f46d2012-09-17 17:25:49 -07002522status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002524 x + radius, y + radius, p) ||
2525 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002526 return DrawGlInfo::kStatusDone;
2527 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002528 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002529 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002530 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002531 return drawShape(x - radius, y - radius, texture, p);
2532 }
2533
2534 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002535 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2536 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2537 } else {
2538 path.addCircle(x, y, radius);
2539 }
Chris Craik65cd6122012-12-10 17:56:27 -08002540 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002541}
Romain Guy01d58e42011-01-19 21:54:02 -08002542
Chet Haase48659092012-05-31 15:21:51 -07002543status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002544 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002545 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2546 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002547 return DrawGlInfo::kStatusDone;
2548 }
Romain Guy01d58e42011-01-19 21:54:02 -08002549
Chris Craikcb4d6002012-09-25 12:00:29 -07002550 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002551 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002552 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002553 return drawShape(left, top, texture, p);
2554 }
2555
2556 SkPath path;
2557 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002558 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2559 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2560 }
Chris Craik710f46d2012-09-17 17:25:49 -07002561 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002562 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002563}
2564
Chet Haase48659092012-05-31 15:21:51 -07002565status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002566 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002567 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2568 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002569 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002570 }
2571
Chris Craik780c1282012-10-04 14:10:49 -07002572 if (fabs(sweepAngle) >= 360.0f) {
2573 return drawOval(left, top, right, bottom, p);
2574 }
2575
2576 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002577 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002578 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002579 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002580 startAngle, sweepAngle, useCenter, p);
2581 return drawShape(left, top, texture, p);
2582 }
2583
2584 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2585 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2586 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2587 }
2588
2589 SkPath path;
2590 if (useCenter) {
2591 path.moveTo(rect.centerX(), rect.centerY());
2592 }
2593 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2594 if (useCenter) {
2595 path.close();
2596 }
Chris Craik65cd6122012-12-10 17:56:27 -08002597 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002598}
2599
Romain Guycf8675e2012-10-02 12:32:25 -07002600// See SkPaintDefaults.h
2601#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2602
Chet Haase48659092012-05-31 15:21:51 -07002603status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002604 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2605 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002606 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002607 }
2608
Chris Craik710f46d2012-09-17 17:25:49 -07002609 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002610 // only fill style is supported by drawConvexPath, since others have to handle joins
2611 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2612 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2613 mCaches.activeTexture(0);
2614 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002615 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002616 return drawShape(left, top, texture, p);
2617 }
2618
2619 SkPath path;
2620 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2621 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2622 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2623 }
2624 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002625 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002626 }
2627
Romain Guy3b753822013-03-05 10:27:35 -08002628 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002629 SkPath path;
2630 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002631 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002632 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002633 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002634 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002635 }
Romain Guyc7d53492010-06-25 13:41:57 -07002636}
Romain Guy9d5316e2010-06-24 19:30:36 -07002637
Raph Levien416a8472012-07-19 22:48:17 -07002638void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2639 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2640 float x, float y) {
2641 mCaches.activeTexture(0);
2642
2643 // NOTE: The drop shadow will not perform gamma correction
2644 // if shader-based correction is enabled
2645 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2646 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002647 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002648 const AutoTexture autoCleanup(shadow);
2649
Chris Craikc3566d02013-02-04 16:16:33 -08002650 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2651 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002652
Chris Craikc3566d02013-02-04 16:16:33 -08002653 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2654 int shadowColor = mDrawModifiers.mShadowColor;
2655 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002656 shadowColor = 0xffffffff;
2657 }
2658
2659 setupDraw();
2660 setupDrawWithTexture(true);
2661 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2662 setupDrawColorFilter();
2663 setupDrawShader();
2664 setupDrawBlending(true, mode);
2665 setupDrawProgram();
2666 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2667 setupDrawTexture(shadow->id);
2668 setupDrawPureColorUniforms();
2669 setupDrawColorFilterUniforms();
2670 setupDrawShaderUniforms();
2671 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2672
2673 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2674}
2675
Romain Guy768bffc2013-02-27 13:50:45 -08002676bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2677 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2678 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2679}
2680
Romain Guy257ae352013-03-20 16:31:12 -07002681class TextSetupFunctor: public Functor {
2682public:
2683 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2684 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2685 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2686 alpha(alpha), mode(mode), paint(paint) {
2687 }
2688 ~TextSetupFunctor() { }
2689
2690 status_t operator ()(int what, void* data) {
2691 renderer.setupDraw();
2692 renderer.setupDrawTextGamma(paint);
2693 renderer.setupDrawDirtyRegionsDisabled();
2694 renderer.setupDrawWithTexture(true);
2695 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2696 renderer.setupDrawColorFilter();
2697 renderer.setupDrawShader();
2698 renderer.setupDrawBlending(true, mode);
2699 renderer.setupDrawProgram();
2700 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2701 // Calling setupDrawTexture with the name 0 will enable the
2702 // uv attributes and increase the texture unit count
2703 // texture binding will be performed by the font renderer as
2704 // needed
2705 renderer.setupDrawTexture(0);
2706 renderer.setupDrawPureColorUniforms();
2707 renderer.setupDrawColorFilterUniforms();
2708 renderer.setupDrawShaderUniforms(pureTranslate);
2709 renderer.setupDrawTextGammaUniforms();
2710
2711 return NO_ERROR;
2712 }
2713
2714 OpenGLRenderer& renderer;
2715 float x;
2716 float y;
2717 bool pureTranslate;
2718 int alpha;
2719 SkXfermode::Mode mode;
2720 SkPaint* paint;
2721};
2722
Chet Haase48659092012-05-31 15:21:51 -07002723status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002724 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002725 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002726 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002727 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002728
Romain Guy671d6cf2012-01-18 12:39:17 -08002729 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002730 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002731 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002732 }
2733
2734 float x = 0.0f;
2735 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002736 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002737 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002738 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2739 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002740 }
2741
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002742 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002743 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002744
2745 int alpha;
2746 SkXfermode::Mode mode;
2747 getAlphaAndMode(paint, &alpha, &mode);
2748
Chris Craikc3566d02013-02-04 16:16:33 -08002749 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002750 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2751 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002752 }
2753
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002755 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002756 if (pureTranslate && !linearFilter) {
2757 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2758 }
Romain Guy257ae352013-03-20 16:31:12 -07002759 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002760
2761 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2762 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2763
Romain Guy211370f2012-02-01 16:10:55 -08002764 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002765
Romain Guy257ae352013-03-20 16:31:12 -07002766 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002767 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002768 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002769 if (hasActiveLayer) {
2770 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002771 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002772 }
2773 dirtyLayerUnchecked(bounds, getRegion());
2774 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002775 }
Chet Haase48659092012-05-31 15:21:51 -07002776
2777 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002778}
2779
Romain Guy624234f2013-03-05 16:43:31 -08002780mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2781 mat4 fontTransform;
2782 if (CC_LIKELY(transform.isPureTranslate())) {
2783 fontTransform = mat4::identity();
2784 } else {
2785 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002786 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002787 } else {
2788 float sx, sy;
2789 currentTransform().decomposeScale(sx, sy);
2790 fontTransform.loadScale(sx, sy, 1.0f);
2791 }
2792 }
2793 return fontTransform;
2794}
2795
Romain Guyc2525952012-07-27 16:41:22 -07002796status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002797 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002798 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002799 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002800 }
2801
Chet Haasea1cff502012-02-21 13:43:44 -08002802 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002803 switch (paint->getTextAlign()) {
2804 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002805 x -= length / 2.0f;
2806 break;
2807 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002808 x -= length;
2809 break;
2810 default:
2811 break;
2812 }
2813
Romain Guycac5fd32011-12-01 20:08:50 -08002814 SkPaint::FontMetrics metrics;
2815 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002816 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002817 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002818 }
2819
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002820 const float oldX = x;
2821 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002822
2823 const mat4& transform = currentTransform();
2824 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002825
Romain Guy211370f2012-02-01 16:10:55 -08002826 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002827 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2828 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002829 }
2830
Romain Guy86568192010-12-14 15:55:39 -08002831 int alpha;
2832 SkXfermode::Mode mode;
2833 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002834
Romain Guyc74f45a2013-02-26 19:10:14 -08002835 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2836
Chris Craikc3566d02013-02-04 16:16:33 -08002837 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002838 fontRenderer.setFont(paint, mat4::identity());
2839 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2840 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002841 }
2842
Romain Guy19d4dd82013-03-04 11:14:26 -08002843 const bool hasActiveLayer = hasLayer();
2844
Romain Guy624234f2013-03-05 16:43:31 -08002845 // We only pass a partial transform to the font renderer. That partial
2846 // matrix defines how glyphs are rasterized. Typically we want glyphs
2847 // to be rasterized at their final size on screen, which means the partial
2848 // matrix needs to take the scale factor into account.
2849 // When a partial matrix is used to transform glyphs during rasterization,
2850 // the mesh is generated with the inverse transform (in the case of scale,
2851 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2852 // apply the full transform matrix at draw time in the vertex shader.
2853 // Applying the full matrix in the shader is the easiest way to handle
2854 // rotation and perspective and allows us to always generated quads in the
2855 // font renderer which greatly simplifies the code, clipping in particular.
2856 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002857 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002858
Romain Guy6620c6d2010-12-06 18:07:02 -08002859 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002860 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002861 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002862
Romain Guy624234f2013-03-05 16:43:31 -08002863 // TODO: Implement better clipping for scaled/rotated text
2864 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002865 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2866
Raph Levien996e57c2012-07-23 15:22:52 -07002867 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002868 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guya3dc55f2012-09-28 13:55:44 -07002869 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002870 SkPaint paintCopy(*paint);
2871 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2872 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002873 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002874 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002875 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002876 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002877 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002878
2879 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002880 if (!pureTranslate) {
2881 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002882 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002883 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002884 }
Romain Guy694b5192010-07-21 21:33:20 -07002885
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002886 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002887
2888 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002889}
2890
Chet Haase48659092012-05-31 15:21:51 -07002891status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002892 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002893 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002894 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002895 }
2896
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002897 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002898 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002899 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002900
2901 int alpha;
2902 SkXfermode::Mode mode;
2903 getAlphaAndMode(paint, &alpha, &mode);
2904
Romain Guy03d58522012-02-24 17:54:07 -08002905 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002906 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002907 setupDrawDirtyRegionsDisabled();
2908 setupDrawWithTexture(true);
2909 setupDrawAlpha8Color(paint->getColor(), alpha);
2910 setupDrawColorFilter();
2911 setupDrawShader();
2912 setupDrawBlending(true, mode);
2913 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002914 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002915 // Calling setupDrawTexture with the name 0 will enable the
2916 // uv attributes and increase the texture unit count
2917 // texture binding will be performed by the font renderer as
2918 // needed
2919 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002920 setupDrawPureColorUniforms();
2921 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002922 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002923 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002924
Romain Guy97771732012-02-28 18:17:02 -08002925 const Rect* clip = &mSnapshot->getLocalClip();
2926 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 -08002927
Romain Guy97771732012-02-28 18:17:02 -08002928 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002929
2930 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2931 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002932 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002933 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002934 dirtyLayerUnchecked(bounds, getRegion());
2935 }
Romain Guy97771732012-02-28 18:17:02 -08002936 }
Chet Haase48659092012-05-31 15:21:51 -07002937
2938 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002939}
2940
Chet Haase48659092012-05-31 15:21:51 -07002941status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2942 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002943
Romain Guya1d3c912011-12-13 14:55:06 -08002944 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002945
Romain Guyfb8b7632010-08-23 21:05:08 -07002946 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002947 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002948 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002949
Romain Guy8b55f372010-08-18 17:10:07 -07002950 const float x = texture->left - texture->offset;
2951 const float y = texture->top - texture->offset;
2952
Romain Guy01d58e42011-01-19 21:54:02 -08002953 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002954
2955 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002956}
2957
Chris Craika08f95c2013-03-15 17:24:33 -07002958status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002959 if (!layer) {
2960 return DrawGlInfo::kStatusDone;
2961 }
2962
Romain Guyb2e2f242012-10-17 18:18:35 -07002963 mat4* transform = NULL;
2964 if (layer->isTextureLayer()) {
2965 transform = &layer->getTransform();
2966 if (!transform->isIdentity()) {
2967 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002968 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002969 }
2970 }
2971
Romain Guy35643dd2012-09-18 15:40:58 -07002972 Rect transformed;
2973 Rect clip;
2974 const bool rejected = quickRejectNoScissor(x, y,
2975 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2976
2977 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002978 if (transform && !transform->isIdentity()) {
2979 restore();
2980 }
Chet Haase48659092012-05-31 15:21:51 -07002981 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002982 }
2983
Romain Guy5bb3c732012-11-29 17:52:58 -08002984 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002985
Romain Guy87e2f7572012-09-24 11:37:12 -07002986 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002987 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002988
Romain Guy211370f2012-02-01 16:10:55 -08002989 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002990 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2991 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002992
Romain Guyc88e3572011-01-22 00:32:12 -08002993 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002994 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002995 } else if (layer->mesh) {
Chris Craika08f95c2013-03-15 17:24:33 -07002996 const float a = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyc88e3572011-01-22 00:32:12 -08002997 setupDraw();
2998 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002999 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003000 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003001 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003002 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003003 setupDrawPureColorUniforms();
3004 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003005 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003006 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3007 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3008 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003009
Romain Guyd21b6e12011-11-30 20:21:23 -08003010 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003011 setupDrawModelViewTranslate(tx, ty,
3012 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003013 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003014 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003015 setupDrawModelViewTranslate(x, y,
3016 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3017 }
Romain Guyc88e3572011-01-22 00:32:12 -08003018 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003019
Romain Guyc88e3572011-01-22 00:32:12 -08003020 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3021 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003022
Romain Guyc88e3572011-01-22 00:32:12 -08003023 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003024
3025#if DEBUG_LAYERS_AS_REGIONS
3026 drawRegionRects(layer->region);
3027#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003028 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003029
Chris Craikc3566d02013-02-04 16:16:33 -08003030 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003031
Romain Guy5bb3c732012-11-29 17:52:58 -08003032 if (layer->debugDrawUpdate) {
3033 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003034 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3035 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3036 }
Romain Guyf219da52011-01-16 12:54:25 -08003037 }
Chet Haase48659092012-05-31 15:21:51 -07003038
Romain Guyb2e2f242012-10-17 18:18:35 -07003039 if (transform && !transform->isIdentity()) {
3040 restore();
3041 }
3042
Chet Haase48659092012-05-31 15:21:51 -07003043 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003044}
3045
Romain Guy6926c722010-07-12 20:20:03 -07003046///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003047// Shaders
3048///////////////////////////////////////////////////////////////////////////////
3049
3050void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003051 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003052}
3053
Romain Guy06f96e22010-07-30 19:18:16 -07003054void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003055 mDrawModifiers.mShader = shader;
3056 if (mDrawModifiers.mShader) {
3057 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003058 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003059}
3060
Romain Guyd27977d2010-07-14 19:18:51 -07003061///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003062// Color filters
3063///////////////////////////////////////////////////////////////////////////////
3064
3065void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003066 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003067}
3068
3069void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003070 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003071}
3072
3073///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003074// Drop shadow
3075///////////////////////////////////////////////////////////////////////////////
3076
3077void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003078 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003079}
3080
3081void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003082 mDrawModifiers.mHasShadow = true;
3083 mDrawModifiers.mShadowRadius = radius;
3084 mDrawModifiers.mShadowDx = dx;
3085 mDrawModifiers.mShadowDy = dy;
3086 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003087}
3088
3089///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003090// Draw filters
3091///////////////////////////////////////////////////////////////////////////////
3092
3093void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003094 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08003095}
3096
3097void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003098 mDrawModifiers.mHasDrawFilter = true;
3099 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3100 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003101}
3102
Chris Craika08f95c2013-03-15 17:24:33 -07003103SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003104 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003105 return paint;
3106 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003107
3108 uint32_t flags = paint->getFlags();
3109
3110 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003111 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3112 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003113
3114 return &mFilteredPaint;
3115}
3116
3117///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003118// Drawing implementation
3119///////////////////////////////////////////////////////////////////////////////
3120
Romain Guy01d58e42011-01-19 21:54:02 -08003121void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3122 float x, float y, SkPaint* paint) {
3123 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3124 return;
3125 }
3126
3127 int alpha;
3128 SkXfermode::Mode mode;
3129 getAlphaAndMode(paint, &alpha, &mode);
3130
3131 setupDraw();
3132 setupDrawWithTexture(true);
3133 setupDrawAlpha8Color(paint->getColor(), alpha);
3134 setupDrawColorFilter();
3135 setupDrawShader();
3136 setupDrawBlending(true, mode);
3137 setupDrawProgram();
3138 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3139 setupDrawTexture(texture->id);
3140 setupDrawPureColorUniforms();
3141 setupDrawColorFilterUniforms();
3142 setupDrawShaderUniforms();
3143 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3144
3145 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3146
3147 finishDrawTexture();
3148}
3149
Romain Guyf607bdc2010-09-10 19:20:06 -07003150// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003151#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3152#define kStdUnderline_Offset (1.0f / 9.0f)
3153#define kStdUnderline_Thickness (1.0f / 18.0f)
3154
3155void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3156 float x, float y, SkPaint* paint) {
3157 // Handle underline and strike-through
3158 uint32_t flags = paint->getFlags();
3159 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003160 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003161 float underlineWidth = length;
3162 // If length is > 0.0f, we already measured the text for the text alignment
3163 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003164 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003165 }
3166
Romain Guy211370f2012-02-01 16:10:55 -08003167 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003168 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003169 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003170
Raph Levien8b4072d2012-07-30 15:50:00 -07003171 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003172 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003173
Romain Guyf6834472011-01-23 13:32:12 -08003174 int linesCount = 0;
3175 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3176 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3177
3178 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003179 float points[pointsCount];
3180 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003181
3182 if (flags & SkPaint::kUnderlineText_Flag) {
3183 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003184 points[currentPoint++] = left;
3185 points[currentPoint++] = top;
3186 points[currentPoint++] = left + underlineWidth;
3187 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003188 }
3189
3190 if (flags & SkPaint::kStrikeThruText_Flag) {
3191 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003192 points[currentPoint++] = left;
3193 points[currentPoint++] = top;
3194 points[currentPoint++] = left + underlineWidth;
3195 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003196 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003197
Romain Guy726aeba2011-06-01 14:52:00 -07003198 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003199
Romain Guy726aeba2011-06-01 14:52:00 -07003200 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003201 }
3202 }
3203}
3204
Romain Guy672433d2013-01-04 19:05:13 -08003205status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3206 if (mSnapshot->isIgnored()) {
3207 return DrawGlInfo::kStatusDone;
3208 }
3209
Romain Guy735738c2012-12-03 12:34:51 -08003210 int color = paint->getColor();
3211 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003212 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003213 color |= 0x00ffffff;
3214 }
3215 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3216
3217 return drawColorRects(rects, count, color, mode);
3218}
3219
3220status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003221 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003222 if (count == 0) {
3223 return DrawGlInfo::kStatusDone;
3224 }
Romain Guy735738c2012-12-03 12:34:51 -08003225
Romain Guy672433d2013-01-04 19:05:13 -08003226 float left = FLT_MAX;
3227 float top = FLT_MAX;
3228 float right = FLT_MIN;
3229 float bottom = FLT_MIN;
3230
3231 int vertexCount = 0;
3232 Vertex mesh[count * 6];
3233 Vertex* vertex = mesh;
3234
Chris Craik2af46352012-11-26 18:30:17 -08003235 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003236 float l = rects[index + 0];
3237 float t = rects[index + 1];
3238 float r = rects[index + 2];
3239 float b = rects[index + 3];
3240
Romain Guy3b753822013-03-05 10:27:35 -08003241 Vertex::set(vertex++, l, b);
3242 Vertex::set(vertex++, l, t);
3243 Vertex::set(vertex++, r, t);
3244 Vertex::set(vertex++, l, b);
3245 Vertex::set(vertex++, r, t);
3246 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003247
Romain Guy3b753822013-03-05 10:27:35 -08003248 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003249
Romain Guy3b753822013-03-05 10:27:35 -08003250 left = fminf(left, l);
3251 top = fminf(top, t);
3252 right = fmaxf(right, r);
3253 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003254 }
3255
Romain Guy3b753822013-03-05 10:27:35 -08003256 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003257 return DrawGlInfo::kStatusDone;
3258 }
Romain Guy672433d2013-01-04 19:05:13 -08003259
Romain Guy672433d2013-01-04 19:05:13 -08003260 setupDraw();
3261 setupDrawNoTexture();
3262 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3263 setupDrawShader();
3264 setupDrawColorFilter();
3265 setupDrawBlending(mode);
3266 setupDrawProgram();
3267 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003268 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003269 setupDrawColorUniforms();
3270 setupDrawShaderUniforms();
3271 setupDrawColorFilterUniforms();
3272 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3273
Romain Guy8ce00302013-01-15 18:51:42 -08003274 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003275 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003276 }
3277
3278 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3279
3280 return DrawGlInfo::kStatusDrew;
3281}
3282
Romain Guy026c5e162010-06-28 17:12:22 -07003283void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003284 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003285 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003286 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003287 color |= 0x00ffffff;
3288 }
3289
Romain Guy70ca14e2010-12-13 18:24:33 -08003290 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003291 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003292 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003293 setupDrawShader();
3294 setupDrawColorFilter();
3295 setupDrawBlending(mode);
3296 setupDrawProgram();
3297 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3298 setupDrawColorUniforms();
3299 setupDrawShaderUniforms(ignoreTransform);
3300 setupDrawColorFilterUniforms();
3301 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003302
Romain Guyc95c8d62010-09-17 15:31:32 -07003303 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3304}
3305
Romain Guy82ba8142010-07-09 13:25:56 -07003306void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003307 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003308 int alpha;
3309 SkXfermode::Mode mode;
3310 getAlphaAndMode(paint, &alpha, &mode);
3311
Romain Guyd21b6e12011-11-30 20:21:23 -08003312 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003313
Romain Guy3b753822013-03-05 10:27:35 -08003314 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3315 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3316 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003317
Romain Guyd21b6e12011-11-30 20:21:23 -08003318 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003319 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3320 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3321 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3322 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003323 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003324 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3325 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3326 GL_TRIANGLE_STRIP, gMeshCount);
3327 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003328}
3329
Romain Guybd6b79b2010-06-26 00:13:53 -07003330void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003331 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3332 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003333 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003334}
3335
3336void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003337 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003338 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003339 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003340
Romain Guy746b7402010-10-26 16:27:31 -07003341 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003342 setupDrawWithTexture();
3343 setupDrawColor(alpha, alpha, alpha, alpha);
3344 setupDrawColorFilter();
3345 setupDrawBlending(blend, mode, swapSrcDst);
3346 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003347 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003348 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003349 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003350 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003351 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003352 }
Romain Guy886b2752013-01-04 12:26:18 -08003353 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003354 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003355 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003356 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003357
Romain Guy6820ac82010-09-15 18:11:50 -07003358 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003359
3360 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003361}
3362
Romain Guy886b2752013-01-04 12:26:18 -08003363void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3364 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3365 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3366 bool ignoreTransform, bool dirty) {
3367
3368 setupDraw();
3369 setupDrawWithTexture(true);
3370 if (hasColor) {
3371 setupDrawAlpha8Color(color, alpha);
3372 }
3373 setupDrawColorFilter();
3374 setupDrawShader();
3375 setupDrawBlending(true, mode);
3376 setupDrawProgram();
3377 if (!dirty) setupDrawDirtyRegionsDisabled();
3378 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3379 setupDrawTexture(texture);
3380 setupDrawPureColorUniforms();
3381 setupDrawColorFilterUniforms();
3382 setupDrawShaderUniforms();
3383 setupDrawMesh(vertices, texCoords);
3384
3385 glDrawArrays(drawMode, 0, elementsCount);
3386
3387 finishDrawTexture();
3388}
3389
Romain Guya5aed0d2010-09-09 14:42:43 -07003390void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003391 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003392 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003393
Romain Guy82ba8142010-07-09 13:25:56 -07003394 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003395 // These blend modes are not supported by OpenGL directly and have
3396 // to be implemented using shaders. Since the shader will perform
3397 // the blending, turn blending off here
3398 // If the blend mode cannot be implemented using shaders, fall
3399 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003400 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003401 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003402 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003403 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003404
Romain Guy82bc7a72012-01-03 14:13:39 -08003405 if (mCaches.blend) {
3406 glDisable(GL_BLEND);
3407 mCaches.blend = false;
3408 }
3409
3410 return;
3411 } else {
3412 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003413 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003414 }
3415
3416 if (!mCaches.blend) {
3417 glEnable(GL_BLEND);
3418 }
3419
3420 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3421 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3422
3423 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3424 glBlendFunc(sourceMode, destMode);
3425 mCaches.lastSrcMode = sourceMode;
3426 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003427 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003428 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003429 glDisable(GL_BLEND);
3430 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003431 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003432}
3433
Romain Guy889f8d12010-07-29 14:37:42 -07003434bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003435 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003436 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003437 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003438 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003439 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003440 }
Romain Guy6926c722010-07-12 20:20:03 -07003441 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003442}
3443
Romain Guy026c5e162010-06-28 17:12:22 -07003444void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003445 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003446 TextureVertex::setUV(v++, u1, v1);
3447 TextureVertex::setUV(v++, u2, v1);
3448 TextureVertex::setUV(v++, u1, v2);
3449 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003450}
3451
Chet Haase5c13d892010-10-08 08:37:55 -07003452void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003453 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003454 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003455}
3456
Romain Guy9d5316e2010-06-24 19:30:36 -07003457}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003458}; // namespace android