blob: 3e842734418bed5eb22fd9878bb4c41ff2d9123a [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.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 Craik527a3aa2013-03-04 10:19:31 -0800115 // *set* draw modifiers to be 0
116 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700117 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700122 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700123 mCountOverdraw = 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) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700226
Romain Guy96885eb2013-03-26 15:05:58 -0700227 setupFrameState(left, top, right, bottom, opaque);
228
229 // Layer renderers will start the frame immediately
230 // The framebuffer renderer will first defer the display list
231 // for each layer and wait until the first drawing command
232 // to start the frame
233 if (mSnapshot->fbo == 0) {
234 syncState();
235 updateLayers();
236 } else {
237 return startFrame();
238 }
239
240 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800248 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800249 const bool isFbo = getTargetFbo() == 0;
250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Romain Guy7c25aab2012-10-18 15:05:02 -0700257status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700258 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700260 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700262 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700263 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700264
Romain Guy7c25aab2012-10-18 15:05:02 -0700265 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700266 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700267}
268
269void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700270 if (mCaches.blend) {
271 glEnable(GL_BLEND);
272 } else {
273 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
Romain Guy57b52682012-09-20 17:38:46 -0700277void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700278 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700279 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800280 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700281 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700282 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700283
Romain Guyc3fedaf2013-01-29 17:26:25 -0800284 startTiling(*clip, s->height, opaque);
285 }
286}
287
288void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
289 if (!mSuppressTiling) {
290 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800291 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700292 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700293}
294
295void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700296 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700297}
298
Romain Guyb025b9c2010-09-16 14:16:48 -0700299void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700300 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700301 endTiling();
302
Romain Guyca89e2a2013-03-08 17:44:20 -0800303 // When finish() is invoked on FBO 0 we've reached the end
304 // of the current frame
305 if (getTargetFbo() == 0) {
306 mCaches.pathCache.trim();
307 }
308
Romain Guy11cb6422012-09-21 00:39:43 -0700309 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700310#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700311 GLenum status = GL_NO_ERROR;
312 while ((status = glGetError()) != GL_NO_ERROR) {
313 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
314 switch (status) {
315 case GL_INVALID_ENUM:
316 ALOGE(" GL_INVALID_ENUM");
317 break;
318 case GL_INVALID_VALUE:
319 ALOGE(" GL_INVALID_VALUE");
320 break;
321 case GL_INVALID_OPERATION:
322 ALOGE(" GL_INVALID_OPERATION");
323 break;
324 case GL_OUT_OF_MEMORY:
325 ALOGE(" Out of memory!");
326 break;
327 }
Romain Guya07105b2011-01-10 21:14:18 -0800328 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700329#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700330
Romain Guyc15008e2010-11-10 11:59:15 -0800331#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800332 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700333#else
334 if (mCaches.getDebugLevel() & kDebugMemory) {
335 mCaches.dumpMemoryUsage();
336 }
Romain Guyc15008e2010-11-10 11:59:15 -0800337#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700338 }
Romain Guy96885eb2013-03-26 15:05:58 -0700339
Romain Guy78dd96d2013-05-03 14:24:16 -0700340 if (mCountOverdraw) {
341 countOverdraw();
342 }
343
Romain Guy96885eb2013-03-26 15:05:58 -0700344 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700345}
346
Romain Guy6c319ca2011-01-11 14:29:25 -0800347void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700348 if (mCaches.currentProgram) {
349 if (mCaches.currentProgram->isInUse()) {
350 mCaches.currentProgram->remove();
351 mCaches.currentProgram = NULL;
352 }
353 }
Romain Guy50c0f092010-10-19 11:42:22 -0700354 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800355 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800356 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800357 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700358 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700359}
360
Romain Guy6c319ca2011-01-11 14:29:25 -0800361void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800362 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800363 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700364 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700365 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700366
Romain Guy3e263fa2011-12-12 16:47:48 -0800367 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
368
Chet Haase80250612012-08-15 13:46:54 -0700369 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700370 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800371 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700372 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700373
Romain Guya1d3c912011-12-13 14:55:06 -0800374 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700375 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700376
Romain Guy50c0f092010-10-19 11:42:22 -0700377 mCaches.blend = true;
378 glEnable(GL_BLEND);
379 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
380 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700381}
382
Romain Guy35643dd2012-09-18 15:40:58 -0700383void OpenGLRenderer::resumeAfterLayer() {
384 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
385 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
386 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700387 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700388
389 mCaches.resetScissor();
390 dirtyClip();
391}
392
Romain Guyba6be8a2012-04-23 18:22:09 -0700393void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700394 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700395}
396
397void OpenGLRenderer::attachFunctor(Functor* functor) {
398 mFunctors.add(functor);
399}
400
Romain Guy8f3b8e32012-03-27 16:33:45 -0700401status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
402 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700403 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700404
Romain Guyba6be8a2012-04-23 18:22:09 -0700405 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800406 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700407 SortedVector<Functor*> functors(mFunctors);
408 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700409
Romain Guyba6be8a2012-04-23 18:22:09 -0700410 DrawGlInfo info;
411 info.clipLeft = 0;
412 info.clipTop = 0;
413 info.clipRight = 0;
414 info.clipBottom = 0;
415 info.isLayer = false;
416 info.width = 0;
417 info.height = 0;
418 memset(info.transform, 0, sizeof(float) * 16);
419
420 for (size_t i = 0; i < count; i++) {
421 Functor* f = functors.itemAt(i);
422 result |= (*f)(DrawGlInfo::kModeProcess, &info);
423
Chris Craikc2c95432012-04-25 15:13:52 -0700424 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700425 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
426 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700427 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700428
Chris Craikc2c95432012-04-25 15:13:52 -0700429 if (result & DrawGlInfo::kStatusInvoke) {
430 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700431 }
432 }
Chris Craikd15321b2012-11-28 14:45:04 -0800433 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434 }
435
436 return result;
437}
438
439status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700440 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
441
Chet Haasedaf98e92011-01-10 14:10:36 -0800442 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700443 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700444
Romain Guy8a4ac612012-07-17 17:32:48 -0700445 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800446 if (mDirtyClip) {
447 setScissorFromClip();
Chris Craikecca6da2013-07-16 13:27:18 -0700448 setStencilFromClip();
Romain Guyf90f8172011-01-25 22:53:24 -0800449 }
Romain Guyd643bb52011-03-01 14:55:21 -0800450
Romain Guy80911b82011-03-16 15:30:12 -0700451 Rect clip(*mSnapshot->clipRect);
452 clip.snapToPixelBoundaries();
453
Romain Guyd643bb52011-03-01 14:55:21 -0800454 // Since we don't know what the functor will draw, let's dirty
455 // tne entire clip region
456 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800457 dirtyLayerUnchecked(clip, getRegion());
458 }
Romain Guyd643bb52011-03-01 14:55:21 -0800459
Romain Guy08aa2cb2011-03-17 11:06:57 -0700460 DrawGlInfo info;
461 info.clipLeft = clip.left;
462 info.clipTop = clip.top;
463 info.clipRight = clip.right;
464 info.clipBottom = clip.bottom;
465 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700466 info.width = getSnapshot()->viewport.getWidth();
467 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700468 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700469
Chris Craik4a2bff72013-04-16 13:50:16 -0700470 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800471
Romain Guy8f3b8e32012-03-27 16:33:45 -0700472 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700473 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800474 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700475
Chris Craik65924a32012-04-05 17:52:11 -0700476 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700477 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700478 }
Romain Guycabfcc12011-03-07 18:06:46 -0800479 }
480
Chet Haasedaf98e92011-01-10 14:10:36 -0800481 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700482 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800483}
484
Romain Guyf6a11b82010-06-23 17:47:49 -0700485///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700486// Debug
487///////////////////////////////////////////////////////////////////////////////
488
Romain Guy0f667532013-03-01 14:31:04 -0800489void OpenGLRenderer::eventMark(const char* name) const {
490 mCaches.eventMark(0, name);
491}
492
Romain Guy87e2f7572012-09-24 11:37:12 -0700493void OpenGLRenderer::startMark(const char* name) const {
494 mCaches.startMark(0, name);
495}
496
497void OpenGLRenderer::endMark() const {
498 mCaches.endMark();
499}
500
501void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
502 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
503 if (clear) {
504 mCaches.disableScissor();
505 mCaches.stencil.clear();
506 }
507 if (enable) {
508 mCaches.stencil.enableDebugWrite();
509 } else {
510 mCaches.stencil.disable();
511 }
512 }
513}
514
515void OpenGLRenderer::renderOverdraw() {
516 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700517 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700518
519 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700520 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700521 clip->right - clip->left, clip->bottom - clip->top);
522
523 mCaches.stencil.enableDebugTest(2);
524 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
525 mCaches.stencil.enableDebugTest(3);
526 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
527 mCaches.stencil.enableDebugTest(4);
528 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
529 mCaches.stencil.enableDebugTest(4, true);
530 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
531 mCaches.stencil.disable();
532 }
533}
534
Romain Guy78dd96d2013-05-03 14:24:16 -0700535void OpenGLRenderer::countOverdraw() {
536 size_t count = mWidth * mHeight;
537 uint32_t* buffer = new uint32_t[count];
538 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
539
540 size_t total = 0;
541 for (size_t i = 0; i < count; i++) {
542 total += buffer[i] & 0xff;
543 }
544
545 mOverdraw = total / float(count);
546
547 delete[] buffer;
548}
549
Romain Guy87e2f7572012-09-24 11:37:12 -0700550///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700551// Layers
552///////////////////////////////////////////////////////////////////////////////
553
554bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700555 if (layer->deferredUpdateScheduled && layer->renderer &&
556 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700557 ATRACE_CALL();
558
Romain Guy11cb6422012-09-21 00:39:43 -0700559 Rect& dirty = layer->dirtyRect;
560
Romain Guy7c450aa2012-09-21 19:15:00 -0700561 if (inFrame) {
562 endTiling();
563 debugOverdraw(false, false);
564 }
Romain Guy11cb6422012-09-21 00:39:43 -0700565
Romain Guy96885eb2013-03-26 15:05:58 -0700566 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700567 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700568 } else {
569 layer->defer();
570 }
Romain Guy11cb6422012-09-21 00:39:43 -0700571
572 if (inFrame) {
573 resumeAfterLayer();
574 startTiling(mSnapshot);
575 }
576
Romain Guy5bb3c732012-11-29 17:52:58 -0800577 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700578 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700579
580 return true;
581 }
582
583 return false;
584}
585
586void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700587 // If draw deferring is enabled this method will simply defer
588 // the display list of each individual layer. The layers remain
589 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700590 int count = mLayerUpdates.size();
591 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700592 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
593 startMark("Layer Updates");
594 } else {
595 startMark("Defer Layer Updates");
596 }
Romain Guy11cb6422012-09-21 00:39:43 -0700597
Chris Craik1206b9b2013-04-04 14:46:24 -0700598 // Note: it is very important to update the layers in order
599 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700600 Layer* layer = mLayerUpdates.itemAt(i);
601 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700602 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
603 mCaches.resourceCache.decrementRefcount(layer);
604 }
Romain Guy11cb6422012-09-21 00:39:43 -0700605 }
Romain Guy11cb6422012-09-21 00:39:43 -0700606
Romain Guy96885eb2013-03-26 15:05:58 -0700607 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
608 mLayerUpdates.clear();
609 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
610 }
611 endMark();
612 }
613}
614
615void OpenGLRenderer::flushLayers() {
616 int count = mLayerUpdates.size();
617 if (count > 0) {
618 startMark("Apply Layer Updates");
619 char layerName[12];
620
Chris Craik1206b9b2013-04-04 14:46:24 -0700621 // Note: it is very important to update the layers in order
622 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700623 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700624 startMark(layerName);
625
Romain Guy40543602013-06-12 15:31:28 -0700626 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700627 Layer* layer = mLayerUpdates.itemAt(i);
628 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700629 ATRACE_END();
630
Romain Guy02b49b72013-03-29 12:37:16 -0700631 mCaches.resourceCache.decrementRefcount(layer);
632
Romain Guy96885eb2013-03-26 15:05:58 -0700633 endMark();
634 }
635
636 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700637 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700638
Romain Guy11cb6422012-09-21 00:39:43 -0700639 endMark();
640 }
641}
642
643void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
644 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700645 // Make sure we don't introduce duplicates.
646 // SortedVector would do this automatically but we need to respect
647 // the insertion order. The linear search is not an issue since
648 // this list is usually very short (typically one item, at most a few)
649 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
650 if (mLayerUpdates.itemAt(i) == layer) {
651 return;
652 }
653 }
Romain Guy11cb6422012-09-21 00:39:43 -0700654 mLayerUpdates.push_back(layer);
655 mCaches.resourceCache.incrementRefcount(layer);
656 }
657}
658
Romain Guye93482f2013-06-17 13:14:51 -0700659void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
660 if (layer) {
661 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
662 if (mLayerUpdates.itemAt(i) == layer) {
663 mLayerUpdates.removeAt(i);
664 mCaches.resourceCache.decrementRefcount(layer);
665 break;
666 }
667 }
668 }
669}
670
Romain Guy11cb6422012-09-21 00:39:43 -0700671void OpenGLRenderer::clearLayerUpdates() {
672 size_t count = mLayerUpdates.size();
673 if (count > 0) {
674 mCaches.resourceCache.lock();
675 for (size_t i = 0; i < count; i++) {
676 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
677 }
678 mCaches.resourceCache.unlock();
679 mLayerUpdates.clear();
680 }
681}
682
Romain Guy40543602013-06-12 15:31:28 -0700683void OpenGLRenderer::flushLayerUpdates() {
684 syncState();
685 updateLayers();
686 flushLayers();
687 // Wait for all the layer updates to be executed
688 AutoFence fence;
689}
690
Romain Guy11cb6422012-09-21 00:39:43 -0700691///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700692// State management
693///////////////////////////////////////////////////////////////////////////////
694
Romain Guybb9524b2010-06-22 18:56:38 -0700695int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700696 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700697}
698
699int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700700 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700701}
702
703void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700704 if (mSaveCount > 1) {
705 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700706 }
Romain Guybb9524b2010-06-22 18:56:38 -0700707}
708
709void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700710 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700711
Romain Guy8fb95422010-08-17 18:38:51 -0700712 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700713 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700714 }
Romain Guybb9524b2010-06-22 18:56:38 -0700715}
716
Romain Guy8aef54f2010-09-01 15:13:49 -0700717int OpenGLRenderer::saveSnapshot(int flags) {
718 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700719 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700720}
721
722bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700723 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700724 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700725 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700726
Romain Guybd6b79b2010-06-26 00:13:53 -0700727 sp<Snapshot> current = mSnapshot;
728 sp<Snapshot> previous = mSnapshot->previous;
729
Romain Guyeb993562010-10-05 18:14:38 -0700730 if (restoreOrtho) {
731 Rect& r = previous->viewport;
732 glViewport(r.left, r.top, r.right, r.bottom);
733 mOrthoMatrix.load(current->orthoMatrix);
734 }
735
Romain Guy8b55f372010-08-18 17:10:07 -0700736 mSaveCount--;
737 mSnapshot = previous;
738
Romain Guy2542d192010-08-18 11:47:12 -0700739 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700740 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700741 }
Romain Guy2542d192010-08-18 11:47:12 -0700742
Romain Guy5ec99242010-11-03 16:19:08 -0700743 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700744 endMark(); // Savelayer
745 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700746 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700747 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700748 }
749
Romain Guy2542d192010-08-18 11:47:12 -0700750 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700751}
752
Romain Guyf6a11b82010-06-23 17:47:49 -0700753///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700754// Layers
755///////////////////////////////////////////////////////////////////////////////
756
757int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800758 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700759 const GLuint previousFbo = mSnapshot->fbo;
760 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700761
Romain Guyaf636eb2010-12-09 17:47:21 -0800762 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700763 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700764 }
Romain Guyd55a8612010-06-28 17:42:46 -0700765
766 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700767}
768
Chris Craikd90144d2013-03-19 15:03:48 -0700769void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
770 const Rect untransformedBounds(bounds);
771
772 currentTransform().mapRect(bounds);
773
774 // Layers only make sense if they are in the framebuffer's bounds
775 if (bounds.intersect(*mSnapshot->clipRect)) {
776 // We cannot work with sub-pixels in this case
777 bounds.snapToPixelBoundaries();
778
779 // When the layer is not an FBO, we may use glCopyTexImage so we
780 // need to make sure the layer does not extend outside the bounds
781 // of the framebuffer
782 if (!bounds.intersect(mSnapshot->previous->viewport)) {
783 bounds.setEmpty();
784 } else if (fboLayer) {
785 clip.set(bounds);
786 mat4 inverse;
787 inverse.loadInverse(currentTransform());
788 inverse.mapRect(clip);
789 clip.snapToPixelBoundaries();
790 if (clip.intersect(untransformedBounds)) {
791 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
792 bounds.set(untransformedBounds);
793 } else {
794 clip.setEmpty();
795 }
796 }
797 } else {
798 bounds.setEmpty();
799 }
800}
801
Chris Craik408eb122013-03-26 18:55:15 -0700802void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
803 bool fboLayer, int alpha) {
804 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
805 bounds.getHeight() > mCaches.maxTextureSize ||
806 (fboLayer && clip.isEmpty())) {
807 mSnapshot->empty = fboLayer;
808 } else {
809 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
810 }
811}
812
Chris Craikd90144d2013-03-19 15:03:48 -0700813int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
814 int alpha, SkXfermode::Mode mode, int flags) {
815 const GLuint previousFbo = mSnapshot->fbo;
816 const int count = saveSnapshot(flags);
817
818 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
819 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
820 // operations will be able to store and restore the current clip and transform info, and
821 // quick rejection will be correct (for display lists)
822
823 Rect bounds(left, top, right, bottom);
824 Rect clip;
825 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700826 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700827
Chris Craik408eb122013-03-26 18:55:15 -0700828 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700829 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
830 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700831 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700832 }
833 }
834
835 return count;
836}
837
838
Romain Guy1c740bc2010-09-13 18:00:09 -0700839/**
840 * Layers are viewed by Skia are slightly different than layers in image editing
841 * programs (for instance.) When a layer is created, previously created layers
842 * and the frame buffer still receive every drawing command. For instance, if a
843 * layer is created and a shape intersecting the bounds of the layers and the
844 * framebuffer is draw, the shape will be drawn on both (unless the layer was
845 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
846 *
847 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
848 * texture. Unfortunately, this is inefficient as it requires every primitive to
849 * be drawn n + 1 times, where n is the number of active layers. In practice this
850 * means, for every primitive:
851 * - Switch active frame buffer
852 * - Change viewport, clip and projection matrix
853 * - Issue the drawing
854 *
855 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700856 * To avoid this, layers are implemented in a different way here, at least in the
857 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
858 * is set. When this flag is set we can redirect all drawing operations into a
859 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700860 *
861 * This implementation relies on the frame buffer being at least RGBA 8888. When
862 * a layer is created, only a texture is created, not an FBO. The content of the
863 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700864 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700865 * buffer and drawing continues as normal. This technique therefore treats the
866 * frame buffer as a scratch buffer for the layers.
867 *
868 * To compose the layers back onto the frame buffer, each layer texture
869 * (containing the original frame buffer data) is drawn as a simple quad over
870 * the frame buffer. The trick is that the quad is set as the composition
871 * destination in the blending equation, and the frame buffer becomes the source
872 * of the composition.
873 *
874 * Drawing layers with an alpha value requires an extra step before composition.
875 * An empty quad is drawn over the layer's region in the frame buffer. This quad
876 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
877 * quad is used to multiply the colors in the frame buffer. This is achieved by
878 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
879 * GL_ZERO, GL_SRC_ALPHA.
880 *
881 * Because glCopyTexImage2D() can be slow, an alternative implementation might
882 * be use to draw a single clipped layer. The implementation described above
883 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700884 *
885 * (1) The frame buffer is actually not cleared right away. To allow the GPU
886 * to potentially optimize series of calls to glCopyTexImage2D, the frame
887 * buffer is left untouched until the first drawing operation. Only when
888 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700889 */
Chet Haased48885a2012-08-28 17:43:28 -0700890bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
891 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700892 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700893 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700894
Romain Guyeb993562010-10-05 18:14:38 -0700895 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
896
Romain Guyf607bdc2010-09-10 19:20:06 -0700897 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700898 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700899 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700900 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700901 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700902
903 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700904 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700905 return false;
906 }
Romain Guyf18fd992010-07-08 11:45:51 -0700907
Romain Guya1d3c912011-12-13 14:55:06 -0800908 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700909 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700910 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700911 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700912 }
913
Romain Guy9ace8f52011-07-07 20:50:11 -0700914 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700915 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
917 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800918 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700919 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700920 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700921
Romain Guy8fb95422010-08-17 18:38:51 -0700922 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700923 mSnapshot->flags |= Snapshot::kFlagIsLayer;
924 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700925
Chris Craik7273daa2013-03-28 11:25:24 -0700926 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700927 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700928 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700929 } else {
930 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800932 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700933 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700934 // Workaround for some GL drivers. When reading pixels lying outside
935 // of the window we should get undefined values for those pixels.
936 // Unfortunately some drivers will turn the entire target texture black
937 // when reading outside of the window.
938 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
939 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800941 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700942
Romain Guyb254c242013-06-27 17:15:24 -0700943 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
944 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
945
Romain Guy54be1cd2011-06-13 19:04:27 -0700946 // Enqueue the buffer coordinates to clear the corresponding region later
947 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700948 }
Romain Guyeb993562010-10-05 18:14:38 -0700949 }
Romain Guyf86ef572010-07-01 11:05:42 -0700950
Romain Guyd55a8612010-06-28 17:42:46 -0700951 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700952}
953
Chet Haased48885a2012-08-28 17:43:28 -0700954bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800955 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700957
Chet Haased48885a2012-08-28 17:43:28 -0700958 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800959 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
960 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700961 mSnapshot->fbo = layer->getFbo();
962 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
963 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
964 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
965 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700966 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700967
Romain Guy2b7028e2012-09-19 17:25:38 -0700968 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700969 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700970 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700971 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
972 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700973
974 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700976 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700978 }
979
980 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700982
Romain Guyf735c8e2013-01-31 17:45:55 -0800983 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700984
985 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700986 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800987 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700989 glClear(GL_COLOR_BUFFER_BIT);
990
991 dirtyClip();
992
993 // Change the ortho projection
994 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
995 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
996
997 return true;
998}
999
Romain Guy1c740bc2010-09-13 18:00:09 -07001000/**
1001 * Read the documentation of createLayer() before doing anything in this method.
1002 */
Romain Guy1d83e192010-08-17 11:37:00 -07001003void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1004 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001005 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001006 return;
1007 }
1008
Romain Guy8ce00302013-01-15 18:51:42 -08001009 Layer* layer = current->layer;
1010 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001011 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001012
Chris Craik39a908c2013-06-13 14:39:01 -07001013 bool clipRequired = false;
1014 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1015 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1016
Romain Guyeb993562010-10-05 18:14:38 -07001017 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001018 endTiling();
1019
Romain Guye0aa84b2012-04-03 19:30:26 -07001020 // Detach the texture from the FBO
1021 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001022
1023 layer->removeFbo(false);
1024
Romain Guyeb993562010-10-05 18:14:38 -07001025 // Unbind current FBO and restore previous one
1026 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001027 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001028
1029 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001030 }
1031
Romain Guy9ace8f52011-07-07 20:50:11 -07001032 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001033 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001034 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001035 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001036 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001037 }
1038
Romain Guy03750a02010-10-18 14:06:08 -07001039 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001040
Romain Guya1d3c912011-12-13 14:55:06 -08001041 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001042
Romain Guy5b3b3522010-10-27 18:57:51 -07001043 // When the layer is stored in an FBO, we can save a bit of fillrate by
1044 // drawing only the dirty region
1045 if (fboLayer) {
1046 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001047 if (layer->getColorFilter()) {
1048 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001049 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001050 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001051 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001052 resetColorFilter();
1053 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001054 } else if (!rect.isEmpty()) {
1055 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1056 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001057 }
Romain Guy8b55f372010-08-18 17:10:07 -07001058
Romain Guy746b7402010-10-26 16:27:31 -07001059 dirtyClip();
1060
Romain Guyeb993562010-10-05 18:14:38 -07001061 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001062 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001063 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001064 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001065 }
1066}
1067
Romain Guyaa6c24c2011-04-28 18:40:04 -07001068void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001069 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001070
1071 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001073 setupDrawWithTexture();
1074 } else {
1075 setupDrawWithExternalTexture();
1076 }
1077 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001078 setupDrawColor(alpha, alpha, alpha, alpha);
1079 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001080 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001081 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001082 setupDrawPureColorUniforms();
1083 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001084 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1085 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001086 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001087 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001088 }
Romain Guy3b753822013-03-05 10:27:35 -08001089 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001090 layer->getWidth() == (uint32_t) rect.getWidth() &&
1091 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001092 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1093 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001094
Romain Guyd21b6e12011-11-30 20:21:23 -08001095 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001096 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1097 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001098 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001099 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1100 }
1101 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001102 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1103
1104 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1105
1106 finishDrawTexture();
1107}
1108
Romain Guy5b3b3522010-10-27 18:57:51 -07001109void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001110 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001111 const Rect& texCoords = layer->texCoords;
1112 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1113 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001114
Romain Guy9ace8f52011-07-07 20:50:11 -07001115 float x = rect.left;
1116 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001117 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001118 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001119 layer->getHeight() == (uint32_t) rect.getHeight();
1120
1121 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001122 // When we're swapping, the layer is already in screen coordinates
1123 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001124 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1125 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001126 }
1127
Romain Guyd21b6e12011-11-30 20:21:23 -08001128 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001129 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001130 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001131 }
1132
Chris Craik16ecda52013-03-29 10:59:59 -07001133 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001134 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001135 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001136 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001137 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1138 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001139
Romain Guyaa6c24c2011-04-28 18:40:04 -07001140 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1141 } else {
1142 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1143 drawTextureLayer(layer, rect);
1144 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1145 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001146}
1147
Chris Craik34416ea2013-04-15 16:08:28 -07001148/**
1149 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1150 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1151 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1152 * by saveLayer's restore
1153 */
1154#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1155 DRAW_COMMAND; \
1156 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1157 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1158 DRAW_COMMAND; \
1159 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1160 } \
1161 }
1162
1163#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1164
Romain Guy5b3b3522010-10-27 18:57:51 -07001165void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001166 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001167 layer->setRegionAsRect();
1168
Chris Craik34416ea2013-04-15 16:08:28 -07001169 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001170
Romain Guy5b3b3522010-10-27 18:57:51 -07001171 layer->region.clear();
1172 return;
1173 }
1174
Romain Guy211370f2012-02-01 16:10:55 -08001175 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001177 const android::Rect* rects;
1178 Region safeRegion;
1179 if (CC_LIKELY(hasRectToRectTransform())) {
1180 rects = layer->region.getArray(&count);
1181 } else {
1182 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1183 rects = safeRegion.getArray(&count);
1184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185
Chris Craik16ecda52013-03-29 10:59:59 -07001186 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001187 const float texX = 1.0f / float(layer->getWidth());
1188 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001189 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001190
Romain Guy8ce00302013-01-15 18:51:42 -08001191 setupDraw();
1192
1193 // We must get (and therefore bind) the region mesh buffer
1194 // after we setup drawing in case we need to mess with the
1195 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001196 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001197 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001198
Romain Guy7230a742011-01-10 22:26:16 -08001199 setupDrawWithTexture();
1200 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001201 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001202 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001203 setupDrawProgram();
1204 setupDrawDirtyRegionsDisabled();
1205 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001206 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001207 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001208 if (currentTransform().isPureTranslate()) {
1209 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1210 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001211
Romain Guyd21b6e12011-11-30 20:21:23 -08001212 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001213 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1214 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001215 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001216 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1217 }
Romain Guy15bc6432011-12-13 13:11:32 -08001218 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001219
1220 for (size_t i = 0; i < count; i++) {
1221 const android::Rect* r = &rects[i];
1222
1223 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001224 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001225 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001226 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001227
1228 // TODO: Reject quads outside of the clip
1229 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1230 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1231 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1232 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1233
1234 numQuads++;
1235
Romain Guy31e08e92013-06-18 15:53:53 -07001236 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001237 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1238 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001239 numQuads = 0;
1240 mesh = mCaches.getRegionMesh();
1241 }
1242 }
1243
1244 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001245 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1246 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001247 }
1248
Romain Guy7230a742011-01-10 22:26:16 -08001249 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001250
1251#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001252 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001253#endif
1254
1255 layer->region.clear();
1256 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001257}
1258
Romain Guy3a3133d2011-02-01 22:59:58 -08001259void OpenGLRenderer::drawRegionRects(const Region& region) {
1260#if DEBUG_LAYERS_AS_REGIONS
1261 size_t count;
1262 const android::Rect* rects = region.getArray(&count);
1263
1264 uint32_t colors[] = {
1265 0x7fff0000, 0x7f00ff00,
1266 0x7f0000ff, 0x7fff00ff,
1267 };
1268
1269 int offset = 0;
1270 int32_t top = rects[0].top;
1271
1272 for (size_t i = 0; i < count; i++) {
1273 if (top != rects[i].top) {
1274 offset ^= 0x2;
1275 top = rects[i].top;
1276 }
1277
1278 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1279 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1280 SkXfermode::kSrcOver_Mode);
1281 }
1282#endif
1283}
1284
Romain Guy8ce00302013-01-15 18:51:42 -08001285void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1286 SkXfermode::Mode mode, bool dirty) {
1287 int count = 0;
1288 Vector<float> rects;
1289
1290 SkRegion::Iterator it(region);
1291 while (!it.done()) {
1292 const SkIRect& r = it.rect();
1293 rects.push(r.fLeft);
1294 rects.push(r.fTop);
1295 rects.push(r.fRight);
1296 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001297 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001298 it.next();
1299 }
1300
Romain Guy3bbacf22013-02-06 16:51:04 -08001301 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001302}
1303
Romain Guy5b3b3522010-10-27 18:57:51 -07001304void OpenGLRenderer::dirtyLayer(const float left, const float top,
1305 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001306 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001307 Rect bounds(left, top, right, bottom);
1308 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001309 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001310 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001311}
1312
1313void OpenGLRenderer::dirtyLayer(const float left, const float top,
1314 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001315 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001316 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001317 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001318 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001319}
1320
1321void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001322 if (bounds.intersect(*mSnapshot->clipRect)) {
1323 bounds.snapToPixelBoundaries();
1324 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1325 if (!dirty.isEmpty()) {
1326 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001327 }
1328 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001329}
1330
Romain Guy54be1cd2011-06-13 19:04:27 -07001331void OpenGLRenderer::clearLayerRegions() {
1332 const size_t count = mLayers.size();
1333 if (count == 0) return;
1334
1335 if (!mSnapshot->isIgnored()) {
1336 // Doing several glScissor/glClear here can negatively impact
1337 // GPUs with a tiler architecture, instead we draw quads with
1338 // the Clear blending mode
1339
1340 // The list contains bounds that have already been clipped
1341 // against their initial clip rect, and the current clip
1342 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001343 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001344
1345 Vertex mesh[count * 6];
1346 Vertex* vertex = mesh;
1347
1348 for (uint32_t i = 0; i < count; i++) {
1349 Rect* bounds = mLayers.itemAt(i);
1350
1351 Vertex::set(vertex++, bounds->left, bounds->bottom);
1352 Vertex::set(vertex++, bounds->left, bounds->top);
1353 Vertex::set(vertex++, bounds->right, bounds->top);
1354 Vertex::set(vertex++, bounds->left, bounds->bottom);
1355 Vertex::set(vertex++, bounds->right, bounds->top);
1356 Vertex::set(vertex++, bounds->right, bounds->bottom);
1357
1358 delete bounds;
1359 }
Romain Guye67307c2013-02-11 18:01:20 -08001360 // We must clear the list of dirty rects before we
1361 // call setupDraw() to prevent stencil setup to do
1362 // the same thing again
1363 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001364
1365 setupDraw(false);
1366 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1367 setupDrawBlending(true, SkXfermode::kClear_Mode);
1368 setupDrawProgram();
1369 setupDrawPureColorUniforms();
1370 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001371 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001372
Romain Guy54be1cd2011-06-13 19:04:27 -07001373 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001374
1375 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001376 } else {
1377 for (uint32_t i = 0; i < count; i++) {
1378 delete mLayers.itemAt(i);
1379 }
Romain Guye67307c2013-02-11 18:01:20 -08001380 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001381 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001382}
1383
Romain Guybd6b79b2010-06-26 00:13:53 -07001384///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001385// State Deferral
1386///////////////////////////////////////////////////////////////////////////////
1387
Chris Craikff785832013-03-08 13:12:16 -08001388bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001389 const Rect& currentClip = *(mSnapshot->clipRect);
1390 const mat4& currentMatrix = *(mSnapshot->transform);
1391
Chris Craikff785832013-03-08 13:12:16 -08001392 if (stateDeferFlags & kStateDeferFlag_Draw) {
1393 // state has bounds initialized in local coordinates
1394 if (!state.mBounds.isEmpty()) {
1395 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001396 Rect clippedBounds(state.mBounds);
1397 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001398 // quick rejected
1399 return true;
1400 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001401
Chris Craika02c4ed2013-06-14 13:43:58 -07001402 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001403 if (!currentClip.contains(state.mBounds)) {
1404 int& flags = state.mClipSideFlags;
1405 // op partially clipped, so record which sides are clipped for clip-aware merging
1406 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1407 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1408 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1409 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1410 }
1411 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001412 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001413 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1414 // overdraw avoidance (since we don't know what it overlaps)
1415 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001416 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001417 }
1418 }
1419
Chris Craik527a3aa2013-03-04 10:19:31 -08001420 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1421 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001422 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001423 }
1424
Chris Craik7273daa2013-03-28 11:25:24 -07001425 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1426 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001427 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001428 state.mDrawModifiers = mDrawModifiers;
1429 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001430 return false;
1431}
1432
Chris Craik527a3aa2013-03-04 10:19:31 -08001433void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001434 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001435 mDrawModifiers = state.mDrawModifiers;
1436 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001437
Chris Craik527a3aa2013-03-04 10:19:31 -08001438 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001439 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1440 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001441 dirtyClip();
1442 }
Chris Craikc3566d02013-02-04 16:16:33 -08001443}
1444
Chris Craik28ce94a2013-05-31 11:38:03 -07001445/**
1446 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1447 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1448 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1449 *
1450 * This method should be called when restoreDisplayState() won't be restoring the clip
1451 */
1452void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1453 if (clipRect != NULL) {
1454 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1455 } else {
1456 mSnapshot->setClip(0, 0, mWidth, mHeight);
1457 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001458 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001459 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001460}
1461
Chris Craikc3566d02013-02-04 16:16:33 -08001462///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001463// Transforms
1464///////////////////////////////////////////////////////////////////////////////
1465
1466void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001467 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001468}
1469
1470void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001471 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001472}
1473
1474void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001475 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001476}
1477
Romain Guy807daf72011-01-18 11:19:19 -08001478void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001479 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001480}
1481
Romain Guyf6a11b82010-06-23 17:47:49 -07001482void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001483 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001484 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001485 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001486 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001487 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001488}
1489
Chris Craikb98a0162013-02-21 11:30:22 -08001490bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001491 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001492}
1493
Romain Guyf6a11b82010-06-23 17:47:49 -07001494void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001495 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001496}
1497
1498void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001499 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001500 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001501 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001502 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001503}
1504
1505///////////////////////////////////////////////////////////////////////////////
1506// Clipping
1507///////////////////////////////////////////////////////////////////////////////
1508
Romain Guybb9524b2010-06-22 18:56:38 -07001509void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001510 Rect clip(*mSnapshot->clipRect);
1511 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001512
Romain Guy8a4ac612012-07-17 17:32:48 -07001513 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1514 clip.getWidth(), clip.getHeight())) {
1515 mDirtyClip = false;
1516 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001517}
1518
Romain Guy8ce00302013-01-15 18:51:42 -08001519void OpenGLRenderer::ensureStencilBuffer() {
1520 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1521 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1522 // just hope we have one when hasLayer() returns false.
1523 if (hasLayer()) {
1524 attachStencilBufferToLayer(mSnapshot->layer);
1525 }
1526}
1527
1528void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1529 // The layer's FBO is already bound when we reach this stage
1530 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001531 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1532 // is attached after we initiated tiling. We must turn it off,
1533 // attach the new render buffer then turn tiling back on
1534 endTiling();
1535
Romain Guy8d4aeb72013-02-12 16:08:55 -08001536 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001537 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001538 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001539
Romain Guyf735c8e2013-01-31 17:45:55 -08001540 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001541 }
1542}
1543
1544void OpenGLRenderer::setStencilFromClip() {
1545 if (!mCaches.debugOverdraw) {
1546 if (!mSnapshot->clipRegion->isEmpty()) {
1547 // NOTE: The order here is important, we must set dirtyClip to false
1548 // before any draw call to avoid calling back into this method
1549 mDirtyClip = false;
1550
1551 ensureStencilBuffer();
1552
1553 mCaches.stencil.enableWrite();
1554
1555 // Clear the stencil but first make sure we restrict drawing
1556 // to the region's bounds
1557 bool resetScissor = mCaches.enableScissor();
1558 if (resetScissor) {
1559 // The scissor was not set so we now need to update it
1560 setScissorFromClip();
1561 }
1562 mCaches.stencil.clear();
1563 if (resetScissor) mCaches.disableScissor();
1564
1565 // NOTE: We could use the region contour path to generate a smaller mesh
1566 // Since we are using the stencil we could use the red book path
1567 // drawing technique. It might increase bandwidth usage though.
1568
1569 // The last parameter is important: we are not drawing in the color buffer
1570 // so we don't want to dirty the current layer, if any
1571 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1572
1573 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001574
1575 // Draw the region used to generate the stencil if the appropriate debug
1576 // mode is enabled
1577 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1578 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1579 }
Romain Guy8ce00302013-01-15 18:51:42 -08001580 } else {
1581 mCaches.stencil.disable();
1582 }
1583 }
1584}
1585
Romain Guy9d5316e2010-06-24 19:30:36 -07001586const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001587 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001588}
1589
Chris Craik39a908c2013-06-13 14:39:01 -07001590bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1591 bool* clipRequired) {
1592 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001593 return true;
1594 }
1595
1596 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001597 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001598 r.snapToPixelBoundaries();
1599
1600 Rect clipRect(*mSnapshot->clipRect);
1601 clipRect.snapToPixelBoundaries();
1602
Chris Craik39a908c2013-06-13 14:39:01 -07001603 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001604
Chris Craik39a908c2013-06-13 14:39:01 -07001605 if (clipRequired) *clipRequired = !clipRect.contains(r);
1606 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001607}
1608
Romain Guy672433d2013-01-04 19:05:13 -08001609bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1610 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001611 if (paint->getStyle() != SkPaint::kFill_Style) {
1612 float outset = paint->getStrokeWidth() * 0.5f;
1613 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1614 } else {
1615 return quickReject(left, top, right, bottom);
1616 }
1617}
1618
Romain Guyc7d53492010-06-25 13:41:57 -07001619bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craik39a908c2013-06-13 14:39:01 -07001620 bool clipRequired = false;
1621 if (quickRejectNoScissor(left, top, right, bottom, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001622 return true;
1623 }
1624
Chris Craik39a908c2013-06-13 14:39:01 -07001625 if (!isDeferred()) {
1626 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001627 }
Chris Craik39a908c2013-06-13 14:39:01 -07001628 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001629}
1630
Romain Guy8ce00302013-01-15 18:51:42 -08001631void OpenGLRenderer::debugClip() {
1632#if DEBUG_CLIP_REGIONS
1633 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1634 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1635 }
1636#endif
1637}
1638
Romain Guy079ba2c2010-07-16 14:12:24 -07001639bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001640 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001641 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1642 if (clipped) {
1643 dirtyClip();
1644 }
1645 return !mSnapshot->clipRect->isEmpty();
1646 }
1647
1648 SkPath path;
1649 path.addRect(left, top, right, bottom);
1650
1651 return clipPath(&path, op);
1652}
1653
1654bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1655 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001656 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001657
1658 SkPath transformed;
1659 path->transform(transform, &transformed);
1660
1661 SkRegion clip;
1662 if (!mSnapshot->clipRegion->isEmpty()) {
1663 clip.setRegion(*mSnapshot->clipRegion);
1664 } else {
1665 Rect* bounds = mSnapshot->clipRect;
1666 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1667 }
1668
1669 SkRegion region;
1670 region.setPath(transformed, clip);
1671
1672 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001673 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001674 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001675 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001676 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001677}
1678
Romain Guy735738c2012-12-03 12:34:51 -08001679bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001680 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1681 if (clipped) {
1682 dirtyClip();
1683 }
1684 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001685}
1686
Chet Haasea23eed82012-04-12 15:19:04 -07001687Rect* OpenGLRenderer::getClipRect() {
1688 return mSnapshot->clipRect;
1689}
1690
Romain Guyf6a11b82010-06-23 17:47:49 -07001691///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001692// Drawing commands
1693///////////////////////////////////////////////////////////////////////////////
1694
Romain Guy54be1cd2011-06-13 19:04:27 -07001695void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001696 // TODO: It would be best if we could do this before quickReject()
1697 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001698 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001699 // Make sure setScissor & setStencil happen at the beginning of
1700 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001701 if (mDirtyClip) {
1702 if (mCaches.scissorEnabled) {
1703 setScissorFromClip();
1704 }
Romain Guy8ce00302013-01-15 18:51:42 -08001705 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001706 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001707
Romain Guy70ca14e2010-12-13 18:24:33 -08001708 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001709
Romain Guy70ca14e2010-12-13 18:24:33 -08001710 mSetShaderColor = false;
1711 mColorSet = false;
1712 mColorA = mColorR = mColorG = mColorB = 0.0f;
1713 mTextureUnit = 0;
1714 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001715
1716 // Enable debug highlight when what we're about to draw is tested against
1717 // the stencil buffer and if stencil highlight debugging is on
1718 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1719 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1720 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001721
1722 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001723}
1724
1725void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1726 mDescription.hasTexture = true;
1727 mDescription.hasAlpha8Texture = isAlpha8;
1728}
1729
Romain Guyff316ec2013-02-13 18:39:43 -08001730void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1731 mDescription.hasTexture = true;
1732 mDescription.hasColors = true;
1733 mDescription.hasAlpha8Texture = isAlpha8;
1734}
1735
Romain Guyaa6c24c2011-04-28 18:40:04 -07001736void OpenGLRenderer::setupDrawWithExternalTexture() {
1737 mDescription.hasExternalTexture = true;
1738}
1739
Romain Guy15bc6432011-12-13 13:11:32 -08001740void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001741 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001742}
1743
Chris Craik710f46d2012-09-17 17:25:49 -07001744void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001745 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001746}
1747
Romain Guy8d0d4782010-12-14 20:13:35 -08001748void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1749 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001750 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1751 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1752 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 mColorSet = true;
1754 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1755}
1756
Romain Guy86568192010-12-14 15:55:39 -08001757void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1758 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001759 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1760 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1761 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001762 mColorSet = true;
1763 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1764}
1765
Romain Guy41210632012-07-16 17:04:24 -07001766void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1767 mCaches.fontRenderer->describe(mDescription, paint);
1768}
1769
Romain Guy70ca14e2010-12-13 18:24:33 -08001770void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1771 mColorA = a;
1772 mColorR = r;
1773 mColorG = g;
1774 mColorB = b;
1775 mColorSet = true;
1776 mSetShaderColor = mDescription.setColor(r, g, b, a);
1777}
1778
1779void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001780 if (mDrawModifiers.mShader) {
1781 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001782 }
1783}
1784
1785void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001786 if (mDrawModifiers.mColorFilter) {
1787 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001788 }
1789}
1790
Romain Guyf09ef512011-05-27 11:43:46 -07001791void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1792 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1793 mColorA = 1.0f;
1794 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001795 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001796 }
1797}
1798
Romain Guy70ca14e2010-12-13 18:24:33 -08001799void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001800 // When the blending mode is kClear_Mode, we need to use a modulate color
1801 // argb=1,0,0,0
1802 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001803 bool blend = (mColorSet && mColorA < 1.0f) ||
1804 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1805 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001806}
1807
1808void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001809 // When the blending mode is kClear_Mode, we need to use a modulate color
1810 // argb=1,0,0,0
1811 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001812 blend |= (mColorSet && mColorA < 1.0f) ||
1813 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1814 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1815 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001816}
1817
1818void OpenGLRenderer::setupDrawProgram() {
1819 useProgram(mCaches.programCache.get(mDescription));
1820}
1821
1822void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1823 mTrackDirtyRegions = false;
1824}
1825
1826void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1827 bool ignoreTransform) {
1828 mModelView.loadTranslate(left, top, 0.0f);
1829 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001830 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1831 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001832 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001833 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001834 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1835 }
1836}
1837
Chet Haase8a5cc922011-04-26 07:28:09 -07001838void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001839 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001840}
1841
Romain Guy70ca14e2010-12-13 18:24:33 -08001842void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1843 bool ignoreTransform, bool ignoreModelView) {
1844 if (!ignoreModelView) {
1845 mModelView.loadTranslate(left, top, 0.0f);
1846 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001847 } else {
1848 mModelView.loadIdentity();
1849 }
Romain Guy86568192010-12-14 15:55:39 -08001850 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1851 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001852 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001853 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001854 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001855 }
1856 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001857 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001858 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1859 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001860}
1861
1862void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001863 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001864 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1865 }
1866}
1867
Romain Guy86568192010-12-14 15:55:39 -08001868void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001869 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001870 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001871 }
1872}
1873
Romain Guy70ca14e2010-12-13 18:24:33 -08001874void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001875 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001876 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001877 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001878 }
Chris Craikc3566d02013-02-04 16:16:33 -08001879 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1880 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001881 }
1882}
1883
Romain Guy8d0d4782010-12-14 20:13:35 -08001884void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001885 if (mDrawModifiers.mShader) {
1886 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001887 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001888 }
1889}
1890
Romain Guy70ca14e2010-12-13 18:24:33 -08001891void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001892 if (mDrawModifiers.mColorFilter) {
1893 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001894 }
1895}
1896
Romain Guy41210632012-07-16 17:04:24 -07001897void OpenGLRenderer::setupDrawTextGammaUniforms() {
1898 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1899}
1900
Romain Guy70ca14e2010-12-13 18:24:33 -08001901void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001902 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001903 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001904 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001905}
1906
1907void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001908 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001909 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001910 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001911}
1912
Romain Guyaa6c24c2011-04-28 18:40:04 -07001913void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1914 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001915 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001916 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001917}
1918
Romain Guy8f0095c2011-05-02 17:24:22 -07001919void OpenGLRenderer::setupDrawTextureTransform() {
1920 mDescription.hasTextureTransform = true;
1921}
1922
1923void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001924 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1925 GL_FALSE, &transform.data[0]);
1926}
1927
Romain Guy70ca14e2010-12-13 18:24:33 -08001928void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001929 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001930 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001931 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001932 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001933 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001934 }
Romain Guyd71dd362011-12-12 19:03:35 -08001935
Chris Craikcb4d6002012-09-25 12:00:29 -07001936 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001937 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001938 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001939 }
1940
1941 mCaches.unbindIndicesBuffer();
1942}
1943
Romain Guyff316ec2013-02-13 18:39:43 -08001944void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1945 bool force = mCaches.unbindMeshBuffer();
1946 GLsizei stride = sizeof(ColorTextureVertex);
1947
1948 mCaches.bindPositionVertexPointer(force, vertices, stride);
1949 if (mCaches.currentProgram->texCoords >= 0) {
1950 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1951 }
1952 int slot = mCaches.currentProgram->getAttrib("colors");
1953 if (slot >= 0) {
1954 glEnableVertexAttribArray(slot);
1955 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1956 }
1957
1958 mCaches.unbindIndicesBuffer();
1959}
1960
Romain Guy3b748a42013-04-17 18:54:38 -07001961void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1962 bool force = false;
1963 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1964 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1965 // use the default VBO found in Caches
1966 if (!vertices || vbo) {
1967 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1968 } else {
1969 force = mCaches.unbindMeshBuffer();
1970 }
1971 mCaches.bindIndicesBuffer();
1972
Chris Craikcb4d6002012-09-25 12:00:29 -07001973 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001974 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001975 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001976 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001977}
1978
Chet Haase5b0200b2011-04-13 17:58:08 -07001979void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001980 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001981 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001982 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001983}
1984
Romain Guy70ca14e2010-12-13 18:24:33 -08001985void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001986}
1987
1988///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001989// Drawing
1990///////////////////////////////////////////////////////////////////////////////
1991
Chris Craikff785832013-03-08 13:12:16 -08001992status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1993 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001994 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001995 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1996 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001997 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001998 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001999 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002000 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2001 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002002 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002003 }
2004
Chris Craik28ce94a2013-05-31 11:38:03 -07002005 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2006 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002007 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2008 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002009
2010 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002011 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002012
Chet Haase58d110a2013-04-10 07:43:29 -07002013 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002014 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002015
Romain Guy65549432012-03-26 16:45:05 -07002016 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002017}
2018
Chris Craikc3566d02013-02-04 16:16:33 -08002019void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002020 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002021 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002022 }
2023}
2024
Romain Guya168d732011-03-18 16:50:13 -07002025void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2026 int alpha;
2027 SkXfermode::Mode mode;
2028 getAlphaAndMode(paint, &alpha, &mode);
2029
Romain Guy886b2752013-01-04 12:26:18 -08002030 int color = paint != NULL ? paint->getColor() : 0;
2031
Romain Guya168d732011-03-18 16:50:13 -07002032 float x = left;
2033 float y = top;
2034
Romain Guy886b2752013-01-04 12:26:18 -08002035 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2036
Romain Guya168d732011-03-18 16:50:13 -07002037 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002038 if (currentTransform().isPureTranslate()) {
2039 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2040 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002041 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002042
2043 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002044 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002045 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002046 }
2047
Romain Guy3b748a42013-04-17 18:54:38 -07002048 // No need to check for a UV mapper on the texture object, only ARGB_8888
2049 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002050 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002051 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2052 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002053}
2054
Romain Guy03c00b52013-06-20 18:30:28 -07002055/**
2056 * Important note: this method is intended to draw batches of bitmaps and
2057 * will not set the scissor enable or dirty the current layer, if any.
2058 * The caller is responsible for properly dirtying the current layer.
2059 */
Romain Guy55b6f952013-06-27 15:27:09 -07002060status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2061 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002062 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002063 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002064 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002065
Chris Craik527a3aa2013-03-04 10:19:31 -08002066 const AutoTexture autoCleanup(texture);
2067
2068 int alpha;
2069 SkXfermode::Mode mode;
2070 getAlphaAndMode(paint, &alpha, &mode);
2071
2072 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002073 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002074
2075 const float x = (int) floorf(bounds.left + 0.5f);
2076 const float y = (int) floorf(bounds.top + 0.5f);
2077 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2078 int color = paint != NULL ? paint->getColor() : 0;
2079 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2080 texture->id, paint != NULL, color, alpha, mode,
2081 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002082 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002083 } else {
2084 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2085 texture->id, alpha / 255.0f, mode, texture->blend,
2086 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002087 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002088 }
2089
2090 return DrawGlInfo::kStatusDrew;
2091}
2092
Chet Haase48659092012-05-31 15:21:51 -07002093status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002094 const float right = left + bitmap->width();
2095 const float bottom = top + bitmap->height();
2096
2097 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002098 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002099 }
2100
Romain Guya1d3c912011-12-13 14:55:06 -08002101 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002102 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002103 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002104 const AutoTexture autoCleanup(texture);
2105
Romain Guy211370f2012-02-01 16:10:55 -08002106 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002107 drawAlphaBitmap(texture, left, top, paint);
2108 } else {
2109 drawTextureRect(left, top, right, bottom, texture, paint);
2110 }
Chet Haase48659092012-05-31 15:21:51 -07002111
2112 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002113}
2114
Chet Haase48659092012-05-31 15:21:51 -07002115status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002116 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2117 const mat4 transform(*matrix);
2118 transform.mapRect(r);
2119
Romain Guy6926c722010-07-12 20:20:03 -07002120 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002121 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002122 }
2123
Romain Guya1d3c912011-12-13 14:55:06 -08002124 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002125 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002126 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002127 const AutoTexture autoCleanup(texture);
2128
Romain Guy5b3b3522010-10-27 18:57:51 -07002129 // This could be done in a cheaper way, all we need is pass the matrix
2130 // to the vertex shader. The save/restore is a bit overkill.
2131 save(SkCanvas::kMatrix_SaveFlag);
2132 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002133 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2134 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2135 } else {
2136 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2137 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002138 restore();
Chet Haase48659092012-05-31 15:21:51 -07002139
2140 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002141}
2142
Chet Haase48659092012-05-31 15:21:51 -07002143status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002144 const float right = left + bitmap->width();
2145 const float bottom = top + bitmap->height();
2146
2147 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002148 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002149 }
2150
2151 mCaches.activeTexture(0);
2152 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2153 const AutoTexture autoCleanup(texture);
2154
Romain Guy886b2752013-01-04 12:26:18 -08002155 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2156 drawAlphaBitmap(texture, left, top, paint);
2157 } else {
2158 drawTextureRect(left, top, right, bottom, texture, paint);
2159 }
Chet Haase48659092012-05-31 15:21:51 -07002160
2161 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002162}
2163
Chet Haase48659092012-05-31 15:21:51 -07002164status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002165 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002166 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002167 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002168 }
2169
Chris Craik39a908c2013-06-13 14:39:01 -07002170 // TODO: use quickReject on bounds from vertices
2171 mCaches.enableScissor();
2172
Romain Guyb18d2d02011-02-10 15:52:54 -08002173 float left = FLT_MAX;
2174 float top = FLT_MAX;
2175 float right = FLT_MIN;
2176 float bottom = FLT_MIN;
2177
Romain Guya92bb4d2012-10-16 11:08:44 -07002178 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002179
Romain Guyff316ec2013-02-13 18:39:43 -08002180 ColorTextureVertex mesh[count];
2181 ColorTextureVertex* vertex = mesh;
2182
2183 bool cleanupColors = false;
2184 if (!colors) {
2185 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2186 colors = new int[colorsCount];
2187 memset(colors, 0xff, colorsCount * sizeof(int));
2188 cleanupColors = true;
2189 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002190
Romain Guy3b748a42013-04-17 18:54:38 -07002191 mCaches.activeTexture(0);
2192 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2193 const UvMapper& mapper(getMapper(texture));
2194
Romain Guy5a7b4662011-01-20 19:09:30 -08002195 for (int32_t y = 0; y < meshHeight; y++) {
2196 for (int32_t x = 0; x < meshWidth; x++) {
2197 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2198
2199 float u1 = float(x) / meshWidth;
2200 float u2 = float(x + 1) / meshWidth;
2201 float v1 = float(y) / meshHeight;
2202 float v2 = float(y + 1) / meshHeight;
2203
Romain Guy3b748a42013-04-17 18:54:38 -07002204 mapper.map(u1, v1, u2, v2);
2205
Romain Guy5a7b4662011-01-20 19:09:30 -08002206 int ax = i + (meshWidth + 1) * 2;
2207 int ay = ax + 1;
2208 int bx = i;
2209 int by = bx + 1;
2210 int cx = i + 2;
2211 int cy = cx + 1;
2212 int dx = i + (meshWidth + 1) * 2 + 2;
2213 int dy = dx + 1;
2214
Romain Guyff316ec2013-02-13 18:39:43 -08002215 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2216 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2217 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002218
Romain Guyff316ec2013-02-13 18:39:43 -08002219 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2220 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2221 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002222
Romain Guya92bb4d2012-10-16 11:08:44 -07002223 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2224 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2225 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2226 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002227 }
2228 }
2229
Romain Guya92bb4d2012-10-16 11:08:44 -07002230 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002231 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002232 return DrawGlInfo::kStatusDone;
2233 }
2234
Romain Guyff316ec2013-02-13 18:39:43 -08002235 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002236 texture = mCaches.textureCache.get(bitmap);
2237 if (!texture) {
2238 if (cleanupColors) delete[] colors;
2239 return DrawGlInfo::kStatusDone;
2240 }
Romain Guyff316ec2013-02-13 18:39:43 -08002241 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002242 const AutoTexture autoCleanup(texture);
2243
2244 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2245 texture->setFilter(FILTER(paint), true);
2246
2247 int alpha;
2248 SkXfermode::Mode mode;
2249 getAlphaAndMode(paint, &alpha, &mode);
2250
Romain Guyff316ec2013-02-13 18:39:43 -08002251 float a = alpha / 255.0f;
2252
Romain Guya92bb4d2012-10-16 11:08:44 -07002253 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002254 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002255 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002256
Romain Guyff316ec2013-02-13 18:39:43 -08002257 setupDraw();
2258 setupDrawWithTextureAndColor();
2259 setupDrawColor(a, a, a, a);
2260 setupDrawColorFilter();
2261 setupDrawBlending(true, mode, false);
2262 setupDrawProgram();
2263 setupDrawDirtyRegionsDisabled();
2264 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2265 setupDrawTexture(texture->id);
2266 setupDrawPureColorUniforms();
2267 setupDrawColorFilterUniforms();
2268 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2269
2270 glDrawArrays(GL_TRIANGLES, 0, count);
2271
2272 finishDrawTexture();
2273
2274 int slot = mCaches.currentProgram->getAttrib("colors");
2275 if (slot >= 0) {
2276 glDisableVertexAttribArray(slot);
2277 }
2278
2279 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002280
2281 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002282}
2283
Chet Haase48659092012-05-31 15:21:51 -07002284status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002285 float srcLeft, float srcTop, float srcRight, float srcBottom,
2286 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002287 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002288 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002289 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002290 }
2291
Romain Guya1d3c912011-12-13 14:55:06 -08002292 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002293 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002294 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002295 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002296
Romain Guy8ba548f2010-06-30 19:21:21 -07002297 const float width = texture->width;
2298 const float height = texture->height;
2299
Romain Guy3b748a42013-04-17 18:54:38 -07002300 float u1 = fmax(0.0f, srcLeft / width);
2301 float v1 = fmax(0.0f, srcTop / height);
2302 float u2 = fmin(1.0f, srcRight / width);
2303 float v2 = fmin(1.0f, srcBottom / height);
2304
2305 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002306
Romain Guy03750a02010-10-18 14:06:08 -07002307 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002308 resetDrawTextureTexCoords(u1, v1, u2, v2);
2309
Romain Guy03750a02010-10-18 14:06:08 -07002310 int alpha;
2311 SkXfermode::Mode mode;
2312 getAlphaAndMode(paint, &alpha, &mode);
2313
Romain Guyd21b6e12011-11-30 20:21:23 -08002314 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2315
Romain Guy886b2752013-01-04 12:26:18 -08002316 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2317 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002318
Romain Guy886b2752013-01-04 12:26:18 -08002319 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2320 // Apply a scale transform on the canvas only when a shader is in use
2321 // Skia handles the ratio between the dst and src rects as a scale factor
2322 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002323 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002324 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002325
Romain Guy3b753822013-03-05 10:27:35 -08002326 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2327 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2328 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002329
2330 dstRight = x + (dstRight - dstLeft);
2331 dstBottom = y + (dstBottom - dstTop);
2332
2333 dstLeft = x;
2334 dstTop = y;
2335
2336 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2337 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002338 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002339 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002340 }
2341
2342 if (CC_UNLIKELY(useScaleTransform)) {
2343 save(SkCanvas::kMatrix_SaveFlag);
2344 translate(dstLeft, dstTop);
2345 scale(scaleX, scaleY);
2346
2347 dstLeft = 0.0f;
2348 dstTop = 0.0f;
2349
2350 dstRight = srcRight - srcLeft;
2351 dstBottom = srcBottom - srcTop;
2352 }
2353
2354 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2355 int color = paint ? paint->getColor() : 0;
2356 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2357 texture->id, paint != NULL, color, alpha, mode,
2358 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2359 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2360 } else {
2361 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2362 texture->id, alpha / 255.0f, mode, texture->blend,
2363 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2364 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2365 }
2366
2367 if (CC_UNLIKELY(useScaleTransform)) {
2368 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002369 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002370
2371 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002372
2373 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002374}
2375
Romain Guy3b748a42013-04-17 18:54:38 -07002376status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002377 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002378 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002379 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002380 }
2381
Romain Guy4c2547f2013-06-11 16:19:24 -07002382 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002383 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2384 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002385
Romain Guy03c00b52013-06-20 18:30:28 -07002386 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002387}
2388
Romain Guy03c00b52013-06-20 18:30:28 -07002389status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2390 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002391 if (quickReject(left, top, right, bottom)) {
2392 return DrawGlInfo::kStatusDone;
2393 }
2394
Romain Guy211370f2012-02-01 16:10:55 -08002395 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002396 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002397 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002398 if (!texture) return DrawGlInfo::kStatusDone;
2399 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002400
Romain Guya4adcf02013-02-28 12:15:35 -08002401 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2402 texture->setFilter(GL_LINEAR, true);
2403
Romain Guy03c00b52013-06-20 18:30:28 -07002404 int alpha;
2405 SkXfermode::Mode mode;
2406 getAlphaAndMode(paint, &alpha, &mode);
2407
Romain Guy3b753822013-03-05 10:27:35 -08002408 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002409 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002410 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002411 const float offsetX = left + currentTransform().getTranslateX();
2412 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002413 const size_t count = mesh->quads.size();
2414 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002415 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002416 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002417 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2418 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2419 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002420 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002421 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002422 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002423 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002424 }
2425 }
2426
Romain Guy211370f2012-02-01 16:10:55 -08002427 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002428 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2429 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002430
Romain Guy3b748a42013-04-17 18:54:38 -07002431 right = x + right - left;
2432 bottom = y + bottom - top;
2433 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2434 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2435 GL_TRIANGLES, mesh->indexCount, false, true,
2436 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002437 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002438 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2439 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2440 GL_TRIANGLES, mesh->indexCount, false, false,
2441 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002442 }
Romain Guy054dc182010-10-15 17:55:25 -07002443 }
Chet Haase48659092012-05-31 15:21:51 -07002444
2445 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002446}
2447
Romain Guy03c00b52013-06-20 18:30:28 -07002448/**
2449 * Important note: this method is intended to draw batches of 9-patch objects and
2450 * will not set the scissor enable or dirty the current layer, if any.
2451 * The caller is responsible for properly dirtying the current layer.
2452 */
2453status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2454 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2455 mCaches.activeTexture(0);
2456 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2457 if (!texture) return DrawGlInfo::kStatusDone;
2458 const AutoTexture autoCleanup(texture);
2459
2460 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2461 texture->setFilter(GL_LINEAR, true);
2462
2463 int alpha;
2464 SkXfermode::Mode mode;
2465 getAlphaAndMode(paint, &alpha, &mode);
2466
2467 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2468 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2469 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2470
2471 return DrawGlInfo::kStatusDrew;
2472}
2473
Chris Craik65cd6122012-12-10 17:56:27 -08002474status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2475 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002476 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002477 // no vertices to draw
2478 return DrawGlInfo::kStatusDone;
2479 }
2480
Chris Craikcb4d6002012-09-25 12:00:29 -07002481 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002482 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2483 bool isAA = paint->isAntiAlias();
2484
Chris Craik710f46d2012-09-17 17:25:49 -07002485 setupDraw();
2486 setupDrawNoTexture();
2487 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002488 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2489 setupDrawColorFilter();
2490 setupDrawShader();
2491 setupDrawBlending(isAA, mode);
2492 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002493 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002494 setupDrawColorUniforms();
2495 setupDrawColorFilterUniforms();
2496 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002497
Chris Craik710f46d2012-09-17 17:25:49 -07002498 void* vertices = vertexBuffer.getBuffer();
2499 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002500 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002501 mCaches.resetTexCoordsVertexPointer();
2502 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002503
Chris Craik710f46d2012-09-17 17:25:49 -07002504 int alphaSlot = -1;
2505 if (isAA) {
2506 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2507 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002508
Chris Craik710f46d2012-09-17 17:25:49 -07002509 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002510 glEnableVertexAttribArray(alphaSlot);
2511 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002512 }
Romain Guy04299382012-07-18 17:15:41 -07002513
Chris Craik6d29c8d2013-05-08 18:35:44 -07002514 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002515
Chris Craik710f46d2012-09-17 17:25:49 -07002516 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002517 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002518 }
Chris Craik65cd6122012-12-10 17:56:27 -08002519
2520 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002521}
2522
2523/**
Chris Craik65cd6122012-12-10 17:56:27 -08002524 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2525 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2526 * screen space in all directions. However, instead of using a fragment shader to compute the
2527 * translucency of the color from its position, we simply use a varying parameter to define how far
2528 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2529 *
2530 * Doesn't yet support joins, caps, or path effects.
2531 */
2532status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2533 VertexBuffer vertexBuffer;
2534 // TODO: try clipping large paths to viewport
2535 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2536
Romain Guyc46d07a2013-03-15 19:06:39 -07002537 if (hasLayer()) {
2538 SkRect bounds = path.getBounds();
2539 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2540 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2541 }
Chris Craik65cd6122012-12-10 17:56:27 -08002542
2543 return drawVertexBuffer(vertexBuffer, paint);
2544}
2545
2546/**
2547 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2548 * and additional geometry for defining an alpha slope perimeter.
2549 *
2550 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2551 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2552 * in-shader alpha region, but found it to be taxing on some GPUs.
2553 *
2554 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2555 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002556 */
Chet Haase48659092012-05-31 15:21:51 -07002557status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002558 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002559
Chris Craik65cd6122012-12-10 17:56:27 -08002560 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002561
Chris Craik65cd6122012-12-10 17:56:27 -08002562 VertexBuffer buffer;
2563 SkRect bounds;
2564 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002565
2566 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2567 return DrawGlInfo::kStatusDone;
2568 }
2569
Romain Guy3b753822013-03-05 10:27:35 -08002570 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002571
Chris Craik65cd6122012-12-10 17:56:27 -08002572 bool useOffset = !paint->isAntiAlias();
2573 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002574}
2575
Chet Haase48659092012-05-31 15:21:51 -07002576status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002577 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002578
Chris Craik6d29c8d2013-05-08 18:35:44 -07002579 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002580
Chris Craik6d29c8d2013-05-08 18:35:44 -07002581 VertexBuffer buffer;
2582 SkRect bounds;
2583 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002584
Chris Craik6d29c8d2013-05-08 18:35:44 -07002585 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2586 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002587 }
2588
Chris Craik6d29c8d2013-05-08 18:35:44 -07002589 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002590
Chris Craik6d29c8d2013-05-08 18:35:44 -07002591 bool useOffset = !paint->isAntiAlias();
2592 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002593}
2594
Chet Haase48659092012-05-31 15:21:51 -07002595status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002596 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002597 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002598
Romain Guyae88e5e2010-10-22 17:49:18 -07002599 Rect& clip(*mSnapshot->clipRect);
2600 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002601
Romain Guy3d58c032010-07-14 16:34:53 -07002602 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002603
2604 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002605}
Romain Guy9d5316e2010-06-24 19:30:36 -07002606
Chet Haase48659092012-05-31 15:21:51 -07002607status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2608 SkPaint* paint) {
2609 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002610 const AutoTexture autoCleanup(texture);
2611
2612 const float x = left + texture->left - texture->offset;
2613 const float y = top + texture->top - texture->offset;
2614
2615 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002616
2617 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002618}
2619
Chet Haase48659092012-05-31 15:21:51 -07002620status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002621 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002622 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2623 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002624 return DrawGlInfo::kStatusDone;
2625 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002626
Chris Craikcb4d6002012-09-25 12:00:29 -07002627 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002628 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002629 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002630 right - left, bottom - top, rx, ry, p);
2631 return drawShape(left, top, texture, p);
2632 }
2633
2634 SkPath path;
2635 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002636 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2637 float outset = p->getStrokeWidth() / 2;
2638 rect.outset(outset, outset);
2639 rx += outset;
2640 ry += outset;
2641 }
Chris Craik710f46d2012-09-17 17:25:49 -07002642 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002643 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002644}
2645
Chris Craik710f46d2012-09-17 17:25:49 -07002646status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002647 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002648 x + radius, y + radius, p) ||
2649 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002650 return DrawGlInfo::kStatusDone;
2651 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002652 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002653 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002654 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002655 return drawShape(x - radius, y - radius, texture, p);
2656 }
2657
2658 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002659 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2660 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2661 } else {
2662 path.addCircle(x, y, radius);
2663 }
Chris Craik65cd6122012-12-10 17:56:27 -08002664 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002665}
Romain Guy01d58e42011-01-19 21:54:02 -08002666
Chet Haase48659092012-05-31 15:21:51 -07002667status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002668 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002669 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2670 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002671 return DrawGlInfo::kStatusDone;
2672 }
Romain Guy01d58e42011-01-19 21:54:02 -08002673
Chris Craikcb4d6002012-09-25 12:00:29 -07002674 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002675 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002676 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002677 return drawShape(left, top, texture, p);
2678 }
2679
2680 SkPath path;
2681 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002682 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2683 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2684 }
Chris Craik710f46d2012-09-17 17:25:49 -07002685 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002686 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002687}
2688
Chet Haase48659092012-05-31 15:21:51 -07002689status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002690 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002691 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2692 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002693 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002694 }
2695
Chris Craik780c1282012-10-04 14:10:49 -07002696 if (fabs(sweepAngle) >= 360.0f) {
2697 return drawOval(left, top, right, bottom, p);
2698 }
2699
2700 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002701 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002702 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002703 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002704 startAngle, sweepAngle, useCenter, p);
2705 return drawShape(left, top, texture, p);
2706 }
2707
2708 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2709 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2710 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2711 }
2712
2713 SkPath path;
2714 if (useCenter) {
2715 path.moveTo(rect.centerX(), rect.centerY());
2716 }
2717 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2718 if (useCenter) {
2719 path.close();
2720 }
Chris Craik65cd6122012-12-10 17:56:27 -08002721 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002722}
2723
Romain Guycf8675e2012-10-02 12:32:25 -07002724// See SkPaintDefaults.h
2725#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2726
Chet Haase48659092012-05-31 15:21:51 -07002727status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002728 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2729 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002730 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002731 }
2732
Chris Craik710f46d2012-09-17 17:25:49 -07002733 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002734 // only fill style is supported by drawConvexPath, since others have to handle joins
2735 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2736 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2737 mCaches.activeTexture(0);
2738 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002739 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002740 return drawShape(left, top, texture, p);
2741 }
2742
2743 SkPath path;
2744 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2745 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2746 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2747 }
2748 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002749 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002750 }
2751
Romain Guy3b753822013-03-05 10:27:35 -08002752 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002753 SkPath path;
2754 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002755 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002756 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002757 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002758 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002759 }
Romain Guyc7d53492010-06-25 13:41:57 -07002760}
Romain Guy9d5316e2010-06-24 19:30:36 -07002761
Raph Levien416a8472012-07-19 22:48:17 -07002762void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2763 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2764 float x, float y) {
2765 mCaches.activeTexture(0);
2766
2767 // NOTE: The drop shadow will not perform gamma correction
2768 // if shader-based correction is enabled
2769 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2770 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002771 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002772 // If the drop shadow exceeds the max texture size or couldn't be
2773 // allocated, skip drawing
2774 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002775 const AutoTexture autoCleanup(shadow);
2776
Chris Craikc3566d02013-02-04 16:16:33 -08002777 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2778 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002779
Chris Craikc3566d02013-02-04 16:16:33 -08002780 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2781 int shadowColor = mDrawModifiers.mShadowColor;
2782 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002783 shadowColor = 0xffffffff;
2784 }
2785
2786 setupDraw();
2787 setupDrawWithTexture(true);
2788 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2789 setupDrawColorFilter();
2790 setupDrawShader();
2791 setupDrawBlending(true, mode);
2792 setupDrawProgram();
2793 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2794 setupDrawTexture(shadow->id);
2795 setupDrawPureColorUniforms();
2796 setupDrawColorFilterUniforms();
2797 setupDrawShaderUniforms();
2798 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2799
2800 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2801}
2802
Romain Guy768bffc2013-02-27 13:50:45 -08002803bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2804 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2805 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2806}
2807
Romain Guy257ae352013-03-20 16:31:12 -07002808class TextSetupFunctor: public Functor {
2809public:
2810 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2811 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2812 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2813 alpha(alpha), mode(mode), paint(paint) {
2814 }
2815 ~TextSetupFunctor() { }
2816
2817 status_t operator ()(int what, void* data) {
2818 renderer.setupDraw();
2819 renderer.setupDrawTextGamma(paint);
2820 renderer.setupDrawDirtyRegionsDisabled();
2821 renderer.setupDrawWithTexture(true);
2822 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2823 renderer.setupDrawColorFilter();
2824 renderer.setupDrawShader();
2825 renderer.setupDrawBlending(true, mode);
2826 renderer.setupDrawProgram();
2827 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2828 // Calling setupDrawTexture with the name 0 will enable the
2829 // uv attributes and increase the texture unit count
2830 // texture binding will be performed by the font renderer as
2831 // needed
2832 renderer.setupDrawTexture(0);
2833 renderer.setupDrawPureColorUniforms();
2834 renderer.setupDrawColorFilterUniforms();
2835 renderer.setupDrawShaderUniforms(pureTranslate);
2836 renderer.setupDrawTextGammaUniforms();
2837
2838 return NO_ERROR;
2839 }
2840
2841 OpenGLRenderer& renderer;
2842 float x;
2843 float y;
2844 bool pureTranslate;
2845 int alpha;
2846 SkXfermode::Mode mode;
2847 SkPaint* paint;
2848};
2849
Chet Haase48659092012-05-31 15:21:51 -07002850status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002851 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002852 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002853 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002854 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002855
Romain Guy671d6cf2012-01-18 12:39:17 -08002856 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002857 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002858 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002859 }
2860
Chris Craik39a908c2013-06-13 14:39:01 -07002861 mCaches.enableScissor();
2862
Romain Guy671d6cf2012-01-18 12:39:17 -08002863 float x = 0.0f;
2864 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002865 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002866 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002867 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2868 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002869 }
2870
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002871 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002872 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002873
2874 int alpha;
2875 SkXfermode::Mode mode;
2876 getAlphaAndMode(paint, &alpha, &mode);
2877
Chris Craikc3566d02013-02-04 16:16:33 -08002878 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002879 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2880 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002881 }
2882
Romain Guy671d6cf2012-01-18 12:39:17 -08002883 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002884 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002885 if (pureTranslate && !linearFilter) {
2886 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2887 }
Romain Guy257ae352013-03-20 16:31:12 -07002888 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002889
2890 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2891 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2892
Romain Guy211370f2012-02-01 16:10:55 -08002893 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002894
Romain Guy257ae352013-03-20 16:31:12 -07002895 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002896 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002897 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002898 if (hasActiveLayer) {
2899 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002900 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002901 }
2902 dirtyLayerUnchecked(bounds, getRegion());
2903 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002904 }
Chet Haase48659092012-05-31 15:21:51 -07002905
2906 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002907}
2908
Romain Guy624234f2013-03-05 16:43:31 -08002909mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2910 mat4 fontTransform;
2911 if (CC_LIKELY(transform.isPureTranslate())) {
2912 fontTransform = mat4::identity();
2913 } else {
2914 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002915 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002916 } else {
2917 float sx, sy;
2918 currentTransform().decomposeScale(sx, sy);
2919 fontTransform.loadScale(sx, sy, 1.0f);
2920 }
2921 }
2922 return fontTransform;
2923}
2924
Chris Craik41541822013-05-03 16:35:54 -07002925status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2926 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002927 DrawOpMode drawOpMode) {
2928
Chris Craik527a3aa2013-03-04 10:19:31 -08002929 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002930 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2931 // drawing as ops from DeferredDisplayList are already filtered for these
2932 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2933 quickReject(bounds)) {
2934 return DrawGlInfo::kStatusDone;
2935 }
Romain Guycac5fd32011-12-01 20:08:50 -08002936 }
2937
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002938 const float oldX = x;
2939 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002940
2941 const mat4& transform = currentTransform();
2942 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002943
Romain Guy211370f2012-02-01 16:10:55 -08002944 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002945 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2946 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002947 }
2948
Romain Guy86568192010-12-14 15:55:39 -08002949 int alpha;
2950 SkXfermode::Mode mode;
2951 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002952
Romain Guyc74f45a2013-02-26 19:10:14 -08002953 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2954
Chris Craikc3566d02013-02-04 16:16:33 -08002955 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002956 fontRenderer.setFont(paint, mat4::identity());
2957 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2958 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002959 }
2960
Romain Guy19d4dd82013-03-04 11:14:26 -08002961 const bool hasActiveLayer = hasLayer();
2962
Romain Guy624234f2013-03-05 16:43:31 -08002963 // We only pass a partial transform to the font renderer. That partial
2964 // matrix defines how glyphs are rasterized. Typically we want glyphs
2965 // to be rasterized at their final size on screen, which means the partial
2966 // matrix needs to take the scale factor into account.
2967 // When a partial matrix is used to transform glyphs during rasterization,
2968 // the mesh is generated with the inverse transform (in the case of scale,
2969 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2970 // apply the full transform matrix at draw time in the vertex shader.
2971 // Applying the full matrix in the shader is the easiest way to handle
2972 // rotation and perspective and allows us to always generated quads in the
2973 // font renderer which greatly simplifies the code, clipping in particular.
2974 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002975 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002976
Romain Guy6620c6d2010-12-06 18:07:02 -08002977 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002978 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002979 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002980
Romain Guy624234f2013-03-05 16:43:31 -08002981 // TODO: Implement better clipping for scaled/rotated text
2982 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002983 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07002984
Raph Levien996e57c2012-07-23 15:22:52 -07002985 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002986 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002987
2988 // don't call issuedrawcommand, do it at end of batch
2989 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002990 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002991 SkPaint paintCopy(*paint);
2992 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2993 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002994 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002995 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002996 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002997 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002998 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002999
Chris Craik527a3aa2013-03-04 10:19:31 -08003000 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003001 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003002 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003003 }
Chris Craik41541822013-05-03 16:35:54 -07003004 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003005 }
Romain Guy694b5192010-07-21 21:33:20 -07003006
Chris Craik41541822013-05-03 16:35:54 -07003007 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003008
3009 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003010}
3011
Chet Haase48659092012-05-31 15:21:51 -07003012status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003013 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003014 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003015 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003016 }
3017
Chris Craik39a908c2013-06-13 14:39:01 -07003018 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3019 mCaches.enableScissor();
3020
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003021 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003022 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003023 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003024
3025 int alpha;
3026 SkXfermode::Mode mode;
3027 getAlphaAndMode(paint, &alpha, &mode);
3028
Romain Guy03d58522012-02-24 17:54:07 -08003029 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07003030 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08003031 setupDrawDirtyRegionsDisabled();
3032 setupDrawWithTexture(true);
3033 setupDrawAlpha8Color(paint->getColor(), alpha);
3034 setupDrawColorFilter();
3035 setupDrawShader();
3036 setupDrawBlending(true, mode);
3037 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08003038 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07003039 // Calling setupDrawTexture with the name 0 will enable the
3040 // uv attributes and increase the texture unit count
3041 // texture binding will be performed by the font renderer as
3042 // needed
3043 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003044 setupDrawPureColorUniforms();
3045 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003046 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003047 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003048
Romain Guy97771732012-02-28 18:17:02 -08003049 const Rect* clip = &mSnapshot->getLocalClip();
3050 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 -08003051
Romain Guy97771732012-02-28 18:17:02 -08003052 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003053
3054 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3055 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003056 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003057 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003058 dirtyLayerUnchecked(bounds, getRegion());
3059 }
Romain Guy97771732012-02-28 18:17:02 -08003060 }
Chet Haase48659092012-05-31 15:21:51 -07003061
3062 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003063}
3064
Chet Haase48659092012-05-31 15:21:51 -07003065status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3066 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003067
Romain Guya1d3c912011-12-13 14:55:06 -08003068 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003069
Romain Guyfb8b7632010-08-23 21:05:08 -07003070 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003071 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003072 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003073
Romain Guy8b55f372010-08-18 17:10:07 -07003074 const float x = texture->left - texture->offset;
3075 const float y = texture->top - texture->offset;
3076
Romain Guy01d58e42011-01-19 21:54:02 -08003077 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003078
3079 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003080}
3081
Chris Craika08f95c2013-03-15 17:24:33 -07003082status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003083 if (!layer) {
3084 return DrawGlInfo::kStatusDone;
3085 }
3086
Romain Guyb2e2f242012-10-17 18:18:35 -07003087 mat4* transform = NULL;
3088 if (layer->isTextureLayer()) {
3089 transform = &layer->getTransform();
3090 if (!transform->isIdentity()) {
3091 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003092 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003093 }
3094 }
3095
Chris Craik39a908c2013-06-13 14:39:01 -07003096 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003097 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik39a908c2013-06-13 14:39:01 -07003098 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003099
3100 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003101 if (transform && !transform->isIdentity()) {
3102 restore();
3103 }
Chet Haase48659092012-05-31 15:21:51 -07003104 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003105 }
3106
Romain Guy5bb3c732012-11-29 17:52:58 -08003107 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003108
Chris Craik39a908c2013-06-13 14:39:01 -07003109 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003110 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003111
Romain Guy211370f2012-02-01 16:10:55 -08003112 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003113 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3114 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003115
Romain Guyc88e3572011-01-22 00:32:12 -08003116 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003117 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3118 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003119 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003120 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003121 setupDraw();
3122 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003123 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003124 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003125 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003126 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003127 setupDrawPureColorUniforms();
3128 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003129 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003130 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3131 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3132 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003133
Romain Guyd21b6e12011-11-30 20:21:23 -08003134 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003135 setupDrawModelViewTranslate(tx, ty,
3136 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003137 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003138 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003139 setupDrawModelViewTranslate(x, y,
3140 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3141 }
Romain Guyc88e3572011-01-22 00:32:12 -08003142 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003143
Chris Craik34416ea2013-04-15 16:08:28 -07003144 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3145 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3146 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003147
Romain Guyc88e3572011-01-22 00:32:12 -08003148 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003149
3150#if DEBUG_LAYERS_AS_REGIONS
3151 drawRegionRects(layer->region);
3152#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003153 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003154
Chris Craikc3566d02013-02-04 16:16:33 -08003155 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003156
Romain Guy5bb3c732012-11-29 17:52:58 -08003157 if (layer->debugDrawUpdate) {
3158 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003159 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3160 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3161 }
Romain Guyf219da52011-01-16 12:54:25 -08003162 }
Chris Craik34416ea2013-04-15 16:08:28 -07003163 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003164
Romain Guyb2e2f242012-10-17 18:18:35 -07003165 if (transform && !transform->isIdentity()) {
3166 restore();
3167 }
3168
Chet Haase48659092012-05-31 15:21:51 -07003169 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003170}
3171
Romain Guy6926c722010-07-12 20:20:03 -07003172///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003173// Shaders
3174///////////////////////////////////////////////////////////////////////////////
3175
3176void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003177 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003178}
3179
Romain Guy06f96e22010-07-30 19:18:16 -07003180void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003181 mDrawModifiers.mShader = shader;
3182 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003183 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003184 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003185}
3186
Romain Guyd27977d2010-07-14 19:18:51 -07003187///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003188// Color filters
3189///////////////////////////////////////////////////////////////////////////////
3190
3191void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003192 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003193}
3194
3195void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003196 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003197}
3198
3199///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003200// Drop shadow
3201///////////////////////////////////////////////////////////////////////////////
3202
3203void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003204 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003205}
3206
3207void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003208 mDrawModifiers.mHasShadow = true;
3209 mDrawModifiers.mShadowRadius = radius;
3210 mDrawModifiers.mShadowDx = dx;
3211 mDrawModifiers.mShadowDy = dy;
3212 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003213}
3214
3215///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003216// Draw filters
3217///////////////////////////////////////////////////////////////////////////////
3218
3219void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003220 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3221 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003222 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003223 mDrawModifiers.mPaintFilterClearBits = 0;
3224 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003225}
3226
3227void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003228 mDrawModifiers.mHasDrawFilter = true;
3229 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3230 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003231}
3232
Chris Craika08f95c2013-03-15 17:24:33 -07003233SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003234 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003235 return paint;
3236 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003237
3238 uint32_t flags = paint->getFlags();
3239
3240 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003241 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3242 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003243
3244 return &mFilteredPaint;
3245}
3246
3247///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003248// Drawing implementation
3249///////////////////////////////////////////////////////////////////////////////
3250
Romain Guy3b748a42013-04-17 18:54:38 -07003251Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3252 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3253 if (!texture) {
3254 return mCaches.textureCache.get(bitmap);
3255 }
3256 return texture;
3257}
3258
Romain Guy01d58e42011-01-19 21:54:02 -08003259void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3260 float x, float y, SkPaint* paint) {
3261 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3262 return;
3263 }
3264
3265 int alpha;
3266 SkXfermode::Mode mode;
3267 getAlphaAndMode(paint, &alpha, &mode);
3268
3269 setupDraw();
3270 setupDrawWithTexture(true);
3271 setupDrawAlpha8Color(paint->getColor(), alpha);
3272 setupDrawColorFilter();
3273 setupDrawShader();
3274 setupDrawBlending(true, mode);
3275 setupDrawProgram();
3276 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3277 setupDrawTexture(texture->id);
3278 setupDrawPureColorUniforms();
3279 setupDrawColorFilterUniforms();
3280 setupDrawShaderUniforms();
3281 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3282
3283 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3284
3285 finishDrawTexture();
3286}
3287
Romain Guyf607bdc2010-09-10 19:20:06 -07003288// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003289#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3290#define kStdUnderline_Offset (1.0f / 9.0f)
3291#define kStdUnderline_Thickness (1.0f / 18.0f)
3292
Chris Craik41541822013-05-03 16:35:54 -07003293void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003294 float x, float y, SkPaint* paint) {
3295 // Handle underline and strike-through
3296 uint32_t flags = paint->getFlags();
3297 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003298 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003299
Romain Guy211370f2012-02-01 16:10:55 -08003300 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003301 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003302 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003303
Raph Levien8b4072d2012-07-30 15:50:00 -07003304 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003305 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003306
Romain Guyf6834472011-01-23 13:32:12 -08003307 int linesCount = 0;
3308 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3309 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3310
3311 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003312 float points[pointsCount];
3313 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003314
3315 if (flags & SkPaint::kUnderlineText_Flag) {
3316 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003317 points[currentPoint++] = left;
3318 points[currentPoint++] = top;
3319 points[currentPoint++] = left + underlineWidth;
3320 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003321 }
3322
3323 if (flags & SkPaint::kStrikeThruText_Flag) {
3324 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003325 points[currentPoint++] = left;
3326 points[currentPoint++] = top;
3327 points[currentPoint++] = left + underlineWidth;
3328 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003329 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003330
Romain Guy726aeba2011-06-01 14:52:00 -07003331 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003332
Romain Guy726aeba2011-06-01 14:52:00 -07003333 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003334 }
3335 }
3336}
3337
Romain Guy672433d2013-01-04 19:05:13 -08003338status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3339 if (mSnapshot->isIgnored()) {
3340 return DrawGlInfo::kStatusDone;
3341 }
3342
Romain Guy735738c2012-12-03 12:34:51 -08003343 int color = paint->getColor();
3344 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003345 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003346 color |= 0x00ffffff;
3347 }
3348 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3349
3350 return drawColorRects(rects, count, color, mode);
3351}
3352
3353status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003354 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003355 if (count == 0) {
3356 return DrawGlInfo::kStatusDone;
3357 }
Romain Guy735738c2012-12-03 12:34:51 -08003358
Romain Guy672433d2013-01-04 19:05:13 -08003359 float left = FLT_MAX;
3360 float top = FLT_MAX;
3361 float right = FLT_MIN;
3362 float bottom = FLT_MIN;
3363
3364 int vertexCount = 0;
3365 Vertex mesh[count * 6];
3366 Vertex* vertex = mesh;
3367
Chris Craik2af46352012-11-26 18:30:17 -08003368 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003369 float l = rects[index + 0];
3370 float t = rects[index + 1];
3371 float r = rects[index + 2];
3372 float b = rects[index + 3];
3373
Romain Guy3b753822013-03-05 10:27:35 -08003374 Vertex::set(vertex++, l, b);
3375 Vertex::set(vertex++, l, t);
3376 Vertex::set(vertex++, r, t);
3377 Vertex::set(vertex++, l, b);
3378 Vertex::set(vertex++, r, t);
3379 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003380
Romain Guy3b753822013-03-05 10:27:35 -08003381 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003382
Romain Guy3b753822013-03-05 10:27:35 -08003383 left = fminf(left, l);
3384 top = fminf(top, t);
3385 right = fmaxf(right, r);
3386 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003387 }
3388
Romain Guy3b753822013-03-05 10:27:35 -08003389 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003390 return DrawGlInfo::kStatusDone;
3391 }
Romain Guy672433d2013-01-04 19:05:13 -08003392
Romain Guy672433d2013-01-04 19:05:13 -08003393 setupDraw();
3394 setupDrawNoTexture();
3395 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3396 setupDrawShader();
3397 setupDrawColorFilter();
3398 setupDrawBlending(mode);
3399 setupDrawProgram();
3400 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003401 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003402 setupDrawColorUniforms();
3403 setupDrawShaderUniforms();
3404 setupDrawColorFilterUniforms();
3405 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3406
Romain Guy8ce00302013-01-15 18:51:42 -08003407 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003408 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003409 }
3410
3411 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3412
3413 return DrawGlInfo::kStatusDrew;
3414}
3415
Romain Guy026c5e162010-06-28 17:12:22 -07003416void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003417 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003418 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003419 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003420 color |= 0x00ffffff;
3421 }
3422
Romain Guy70ca14e2010-12-13 18:24:33 -08003423 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003424 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003425 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003426 setupDrawShader();
3427 setupDrawColorFilter();
3428 setupDrawBlending(mode);
3429 setupDrawProgram();
3430 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3431 setupDrawColorUniforms();
3432 setupDrawShaderUniforms(ignoreTransform);
3433 setupDrawColorFilterUniforms();
3434 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003435
Romain Guyc95c8d62010-09-17 15:31:32 -07003436 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3437}
3438
Romain Guy82ba8142010-07-09 13:25:56 -07003439void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003440 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003441 int alpha;
3442 SkXfermode::Mode mode;
3443 getAlphaAndMode(paint, &alpha, &mode);
3444
Romain Guyd21b6e12011-11-30 20:21:23 -08003445 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003446
Romain Guy3b748a42013-04-17 18:54:38 -07003447 GLvoid* vertices = (GLvoid*) NULL;
3448 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3449
3450 if (texture->uvMapper) {
3451 vertices = &mMeshVertices[0].position[0];
3452 texCoords = &mMeshVertices[0].texture[0];
3453
3454 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3455 texture->uvMapper->map(uvs);
3456
3457 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3458 }
3459
Romain Guy3b753822013-03-05 10:27:35 -08003460 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3461 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3462 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003463
Romain Guyd21b6e12011-11-30 20:21:23 -08003464 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003465 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003466 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3467 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003468 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003469 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003470 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003471 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3472 }
3473
3474 if (texture->uvMapper) {
3475 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003476 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003477}
3478
Romain Guybd6b79b2010-06-26 00:13:53 -07003479void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003480 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3481 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003482 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003483}
3484
3485void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003486 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003487 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003488 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003489
Romain Guy746b7402010-10-26 16:27:31 -07003490 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003491 setupDrawWithTexture();
3492 setupDrawColor(alpha, alpha, alpha, alpha);
3493 setupDrawColorFilter();
3494 setupDrawBlending(blend, mode, swapSrcDst);
3495 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003496 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003497 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003498 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003499 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003500 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003501 }
Romain Guy886b2752013-01-04 12:26:18 -08003502 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003503 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003504 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003505 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003506
Romain Guy6820ac82010-09-15 18:11:50 -07003507 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003508
3509 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003510}
3511
Romain Guy3b748a42013-04-17 18:54:38 -07003512void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3513 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3514 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3515 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3516
3517 setupDraw();
3518 setupDrawWithTexture();
3519 setupDrawColor(alpha, alpha, alpha, alpha);
3520 setupDrawColorFilter();
3521 setupDrawBlending(blend, mode, swapSrcDst);
3522 setupDrawProgram();
3523 if (!dirty) setupDrawDirtyRegionsDisabled();
3524 if (!ignoreScale) {
3525 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3526 } else {
3527 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3528 }
3529 setupDrawTexture(texture);
3530 setupDrawPureColorUniforms();
3531 setupDrawColorFilterUniforms();
3532 setupDrawMeshIndices(vertices, texCoords, vbo);
3533
3534 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3535
3536 finishDrawTexture();
3537}
3538
Romain Guy886b2752013-01-04 12:26:18 -08003539void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3540 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3541 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003542 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003543
3544 setupDraw();
3545 setupDrawWithTexture(true);
3546 if (hasColor) {
3547 setupDrawAlpha8Color(color, alpha);
3548 }
3549 setupDrawColorFilter();
3550 setupDrawShader();
3551 setupDrawBlending(true, mode);
3552 setupDrawProgram();
3553 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003554 if (!ignoreScale) {
3555 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3556 } else {
3557 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3558 }
Romain Guy886b2752013-01-04 12:26:18 -08003559 setupDrawTexture(texture);
3560 setupDrawPureColorUniforms();
3561 setupDrawColorFilterUniforms();
3562 setupDrawShaderUniforms();
3563 setupDrawMesh(vertices, texCoords);
3564
3565 glDrawArrays(drawMode, 0, elementsCount);
3566
3567 finishDrawTexture();
3568}
3569
Romain Guya5aed0d2010-09-09 14:42:43 -07003570void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003571 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003572 if (mCountOverdraw) {
3573 if (!mCaches.blend) glEnable(GL_BLEND);
3574 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3575 glBlendFunc(GL_ONE, GL_ONE);
3576 }
3577
3578 mCaches.blend = true;
3579 mCaches.lastSrcMode = GL_ONE;
3580 mCaches.lastDstMode = GL_ONE;
3581
3582 return;
3583 }
3584
Romain Guy82ba8142010-07-09 13:25:56 -07003585 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003586
Romain Guy82ba8142010-07-09 13:25:56 -07003587 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003588 // These blend modes are not supported by OpenGL directly and have
3589 // to be implemented using shaders. Since the shader will perform
3590 // the blending, turn blending off here
3591 // If the blend mode cannot be implemented using shaders, fall
3592 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003593 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003594 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003595 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003596 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003597
Romain Guy82bc7a72012-01-03 14:13:39 -08003598 if (mCaches.blend) {
3599 glDisable(GL_BLEND);
3600 mCaches.blend = false;
3601 }
3602
3603 return;
3604 } else {
3605 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003606 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003607 }
3608
3609 if (!mCaches.blend) {
3610 glEnable(GL_BLEND);
3611 }
3612
3613 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3614 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3615
3616 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3617 glBlendFunc(sourceMode, destMode);
3618 mCaches.lastSrcMode = sourceMode;
3619 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003620 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003621 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003622 glDisable(GL_BLEND);
3623 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003624 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003625}
3626
Romain Guy889f8d12010-07-29 14:37:42 -07003627bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003628 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003629 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003630 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003631 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003632 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003633 }
Romain Guy6926c722010-07-12 20:20:03 -07003634 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003635}
3636
Romain Guy026c5e162010-06-28 17:12:22 -07003637void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003638 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003639 TextureVertex::setUV(v++, u1, v1);
3640 TextureVertex::setUV(v++, u2, v1);
3641 TextureVertex::setUV(v++, u1, v2);
3642 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003643}
3644
Chris Craik16ecda52013-03-29 10:59:59 -07003645void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003646 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003647 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3648 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003649 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003650 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003651 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003652}
3653
Chris Craik16ecda52013-03-29 10:59:59 -07003654float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3655 float alpha;
3656 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3657 alpha = mDrawModifiers.mOverrideLayerAlpha;
3658 } else {
3659 alpha = layer->getAlpha() / 255.0f;
3660 }
3661 return alpha * mSnapshot->alpha;
3662}
3663
Romain Guy9d5316e2010-06-24 19:30:36 -07003664}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003665}; // namespace android