blob: 7c0f3ad85c4a4e5cf105d6e12f2216553df231f4 [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();
448 }
Romain Guyd643bb52011-03-01 14:55:21 -0800449
Romain Guy80911b82011-03-16 15:30:12 -0700450 Rect clip(*mSnapshot->clipRect);
451 clip.snapToPixelBoundaries();
452
Romain Guyd643bb52011-03-01 14:55:21 -0800453 // Since we don't know what the functor will draw, let's dirty
454 // tne entire clip region
455 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800456 dirtyLayerUnchecked(clip, getRegion());
457 }
Romain Guyd643bb52011-03-01 14:55:21 -0800458
Romain Guy08aa2cb2011-03-17 11:06:57 -0700459 DrawGlInfo info;
460 info.clipLeft = clip.left;
461 info.clipTop = clip.top;
462 info.clipRight = clip.right;
463 info.clipBottom = clip.bottom;
464 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700465 info.width = getSnapshot()->viewport.getWidth();
466 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700467 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700468
Chris Craik4a2bff72013-04-16 13:50:16 -0700469 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800470
Romain Guy8f3b8e32012-03-27 16:33:45 -0700471 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800473 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700474
Chris Craik65924a32012-04-05 17:52:11 -0700475 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700476 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700477 }
Romain Guycabfcc12011-03-07 18:06:46 -0800478 }
479
Chet Haasedaf98e92011-01-10 14:10:36 -0800480 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700481 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800482}
483
Romain Guyf6a11b82010-06-23 17:47:49 -0700484///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700485// Debug
486///////////////////////////////////////////////////////////////////////////////
487
Romain Guy0f6675332013-03-01 14:31:04 -0800488void OpenGLRenderer::eventMark(const char* name) const {
489 mCaches.eventMark(0, name);
490}
491
Romain Guy87e2f7572012-09-24 11:37:12 -0700492void OpenGLRenderer::startMark(const char* name) const {
493 mCaches.startMark(0, name);
494}
495
496void OpenGLRenderer::endMark() const {
497 mCaches.endMark();
498}
499
500void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
501 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
502 if (clear) {
503 mCaches.disableScissor();
504 mCaches.stencil.clear();
505 }
506 if (enable) {
507 mCaches.stencil.enableDebugWrite();
508 } else {
509 mCaches.stencil.disable();
510 }
511 }
512}
513
514void OpenGLRenderer::renderOverdraw() {
515 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700516 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700517
518 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700519 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700520 clip->right - clip->left, clip->bottom - clip->top);
521
522 mCaches.stencil.enableDebugTest(2);
523 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
524 mCaches.stencil.enableDebugTest(3);
525 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
526 mCaches.stencil.enableDebugTest(4);
527 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
528 mCaches.stencil.enableDebugTest(4, true);
529 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
530 mCaches.stencil.disable();
531 }
532}
533
Romain Guy78dd96d2013-05-03 14:24:16 -0700534void OpenGLRenderer::countOverdraw() {
535 size_t count = mWidth * mHeight;
536 uint32_t* buffer = new uint32_t[count];
537 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
538
539 size_t total = 0;
540 for (size_t i = 0; i < count; i++) {
541 total += buffer[i] & 0xff;
542 }
543
544 mOverdraw = total / float(count);
545
546 delete[] buffer;
547}
548
Romain Guy87e2f7572012-09-24 11:37:12 -0700549///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700550// Layers
551///////////////////////////////////////////////////////////////////////////////
552
553bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700554 if (layer->deferredUpdateScheduled && layer->renderer &&
555 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700556 ATRACE_CALL();
557
Romain Guy11cb6422012-09-21 00:39:43 -0700558 Rect& dirty = layer->dirtyRect;
559
Romain Guy7c450aa2012-09-21 19:15:00 -0700560 if (inFrame) {
561 endTiling();
562 debugOverdraw(false, false);
563 }
Romain Guy11cb6422012-09-21 00:39:43 -0700564
Romain Guy96885eb2013-03-26 15:05:58 -0700565 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700566 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700567 } else {
568 layer->defer();
569 }
Romain Guy11cb6422012-09-21 00:39:43 -0700570
571 if (inFrame) {
572 resumeAfterLayer();
573 startTiling(mSnapshot);
574 }
575
Romain Guy5bb3c732012-11-29 17:52:58 -0800576 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700577 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700578
579 return true;
580 }
581
582 return false;
583}
584
585void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700586 // If draw deferring is enabled this method will simply defer
587 // the display list of each individual layer. The layers remain
588 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700589 int count = mLayerUpdates.size();
590 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700591 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
592 startMark("Layer Updates");
593 } else {
594 startMark("Defer Layer Updates");
595 }
Romain Guy11cb6422012-09-21 00:39:43 -0700596
Chris Craik1206b9b2013-04-04 14:46:24 -0700597 // Note: it is very important to update the layers in order
598 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700599 Layer* layer = mLayerUpdates.itemAt(i);
600 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700601 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
602 mCaches.resourceCache.decrementRefcount(layer);
603 }
Romain Guy11cb6422012-09-21 00:39:43 -0700604 }
Romain Guy11cb6422012-09-21 00:39:43 -0700605
Romain Guy96885eb2013-03-26 15:05:58 -0700606 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
607 mLayerUpdates.clear();
608 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
609 }
610 endMark();
611 }
612}
613
614void OpenGLRenderer::flushLayers() {
615 int count = mLayerUpdates.size();
616 if (count > 0) {
617 startMark("Apply Layer Updates");
618 char layerName[12];
619
Chris Craik1206b9b2013-04-04 14:46:24 -0700620 // Note: it is very important to update the layers in order
621 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700622 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700623 startMark(layerName);
624
Romain Guy40543602013-06-12 15:31:28 -0700625 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700626 Layer* layer = mLayerUpdates.itemAt(i);
627 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700628 ATRACE_END();
629
Romain Guy02b49b72013-03-29 12:37:16 -0700630 mCaches.resourceCache.decrementRefcount(layer);
631
Romain Guy96885eb2013-03-26 15:05:58 -0700632 endMark();
633 }
634
635 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700636 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700637
Romain Guy11cb6422012-09-21 00:39:43 -0700638 endMark();
639 }
640}
641
642void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
643 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700644 // Make sure we don't introduce duplicates.
645 // SortedVector would do this automatically but we need to respect
646 // the insertion order. The linear search is not an issue since
647 // this list is usually very short (typically one item, at most a few)
648 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
649 if (mLayerUpdates.itemAt(i) == layer) {
650 return;
651 }
652 }
Romain Guy11cb6422012-09-21 00:39:43 -0700653 mLayerUpdates.push_back(layer);
654 mCaches.resourceCache.incrementRefcount(layer);
655 }
656}
657
Romain Guye93482f2013-06-17 13:14:51 -0700658void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
659 if (layer) {
660 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
661 if (mLayerUpdates.itemAt(i) == layer) {
662 mLayerUpdates.removeAt(i);
663 mCaches.resourceCache.decrementRefcount(layer);
664 break;
665 }
666 }
667 }
668}
669
Romain Guy11cb6422012-09-21 00:39:43 -0700670void OpenGLRenderer::clearLayerUpdates() {
671 size_t count = mLayerUpdates.size();
672 if (count > 0) {
673 mCaches.resourceCache.lock();
674 for (size_t i = 0; i < count; i++) {
675 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
676 }
677 mCaches.resourceCache.unlock();
678 mLayerUpdates.clear();
679 }
680}
681
Romain Guy40543602013-06-12 15:31:28 -0700682void OpenGLRenderer::flushLayerUpdates() {
683 syncState();
684 updateLayers();
685 flushLayers();
686 // Wait for all the layer updates to be executed
687 AutoFence fence;
688}
689
Romain Guy11cb6422012-09-21 00:39:43 -0700690///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700691// State management
692///////////////////////////////////////////////////////////////////////////////
693
Romain Guybb9524b2010-06-22 18:56:38 -0700694int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700695 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700696}
697
698int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700699 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700700}
701
702void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700703 if (mSaveCount > 1) {
704 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700705 }
Romain Guybb9524b2010-06-22 18:56:38 -0700706}
707
708void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700709 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700710
Romain Guy8fb95422010-08-17 18:38:51 -0700711 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700712 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700713 }
Romain Guybb9524b2010-06-22 18:56:38 -0700714}
715
Romain Guy8aef54f2010-09-01 15:13:49 -0700716int OpenGLRenderer::saveSnapshot(int flags) {
717 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700718 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700719}
720
721bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700722 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700723 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700724 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700725
Romain Guybd6b79b2010-06-26 00:13:53 -0700726 sp<Snapshot> current = mSnapshot;
727 sp<Snapshot> previous = mSnapshot->previous;
728
Romain Guyeb993562010-10-05 18:14:38 -0700729 if (restoreOrtho) {
730 Rect& r = previous->viewport;
731 glViewport(r.left, r.top, r.right, r.bottom);
732 mOrthoMatrix.load(current->orthoMatrix);
733 }
734
Romain Guy8b55f372010-08-18 17:10:07 -0700735 mSaveCount--;
736 mSnapshot = previous;
737
Romain Guy2542d192010-08-18 11:47:12 -0700738 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700739 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700740 }
Romain Guy2542d192010-08-18 11:47:12 -0700741
Romain Guy5ec99242010-11-03 16:19:08 -0700742 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700743 endMark(); // Savelayer
744 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700745 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700746 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700747 }
748
Romain Guy2542d192010-08-18 11:47:12 -0700749 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700750}
751
Romain Guyf6a11b82010-06-23 17:47:49 -0700752///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700753// Layers
754///////////////////////////////////////////////////////////////////////////////
755
756int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800757 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700758 const GLuint previousFbo = mSnapshot->fbo;
759 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700760
Romain Guyaf636eb2010-12-09 17:47:21 -0800761 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700762 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700763 }
Romain Guyd55a8612010-06-28 17:42:46 -0700764
765 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700766}
767
Chris Craikd90144d2013-03-19 15:03:48 -0700768void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
769 const Rect untransformedBounds(bounds);
770
771 currentTransform().mapRect(bounds);
772
773 // Layers only make sense if they are in the framebuffer's bounds
774 if (bounds.intersect(*mSnapshot->clipRect)) {
775 // We cannot work with sub-pixels in this case
776 bounds.snapToPixelBoundaries();
777
778 // When the layer is not an FBO, we may use glCopyTexImage so we
779 // need to make sure the layer does not extend outside the bounds
780 // of the framebuffer
781 if (!bounds.intersect(mSnapshot->previous->viewport)) {
782 bounds.setEmpty();
783 } else if (fboLayer) {
784 clip.set(bounds);
785 mat4 inverse;
786 inverse.loadInverse(currentTransform());
787 inverse.mapRect(clip);
788 clip.snapToPixelBoundaries();
789 if (clip.intersect(untransformedBounds)) {
790 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
791 bounds.set(untransformedBounds);
792 } else {
793 clip.setEmpty();
794 }
795 }
796 } else {
797 bounds.setEmpty();
798 }
799}
800
Chris Craik408eb122013-03-26 18:55:15 -0700801void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
802 bool fboLayer, int alpha) {
803 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
804 bounds.getHeight() > mCaches.maxTextureSize ||
805 (fboLayer && clip.isEmpty())) {
806 mSnapshot->empty = fboLayer;
807 } else {
808 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
809 }
810}
811
Chris Craikd90144d2013-03-19 15:03:48 -0700812int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
813 int alpha, SkXfermode::Mode mode, int flags) {
814 const GLuint previousFbo = mSnapshot->fbo;
815 const int count = saveSnapshot(flags);
816
817 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
818 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
819 // operations will be able to store and restore the current clip and transform info, and
820 // quick rejection will be correct (for display lists)
821
822 Rect bounds(left, top, right, bottom);
823 Rect clip;
824 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700825 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700826
Chris Craik408eb122013-03-26 18:55:15 -0700827 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700828 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
829 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700830 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700831 }
832 }
833
834 return count;
835}
836
837
Romain Guy1c740bc2010-09-13 18:00:09 -0700838/**
839 * Layers are viewed by Skia are slightly different than layers in image editing
840 * programs (for instance.) When a layer is created, previously created layers
841 * and the frame buffer still receive every drawing command. For instance, if a
842 * layer is created and a shape intersecting the bounds of the layers and the
843 * framebuffer is draw, the shape will be drawn on both (unless the layer was
844 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
845 *
846 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
847 * texture. Unfortunately, this is inefficient as it requires every primitive to
848 * be drawn n + 1 times, where n is the number of active layers. In practice this
849 * means, for every primitive:
850 * - Switch active frame buffer
851 * - Change viewport, clip and projection matrix
852 * - Issue the drawing
853 *
854 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700855 * To avoid this, layers are implemented in a different way here, at least in the
856 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
857 * is set. When this flag is set we can redirect all drawing operations into a
858 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700859 *
860 * This implementation relies on the frame buffer being at least RGBA 8888. When
861 * a layer is created, only a texture is created, not an FBO. The content of the
862 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700863 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700864 * buffer and drawing continues as normal. This technique therefore treats the
865 * frame buffer as a scratch buffer for the layers.
866 *
867 * To compose the layers back onto the frame buffer, each layer texture
868 * (containing the original frame buffer data) is drawn as a simple quad over
869 * the frame buffer. The trick is that the quad is set as the composition
870 * destination in the blending equation, and the frame buffer becomes the source
871 * of the composition.
872 *
873 * Drawing layers with an alpha value requires an extra step before composition.
874 * An empty quad is drawn over the layer's region in the frame buffer. This quad
875 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
876 * quad is used to multiply the colors in the frame buffer. This is achieved by
877 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
878 * GL_ZERO, GL_SRC_ALPHA.
879 *
880 * Because glCopyTexImage2D() can be slow, an alternative implementation might
881 * be use to draw a single clipped layer. The implementation described above
882 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700883 *
884 * (1) The frame buffer is actually not cleared right away. To allow the GPU
885 * to potentially optimize series of calls to glCopyTexImage2D, the frame
886 * buffer is left untouched until the first drawing operation. Only when
887 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700888 */
Chet Haased48885a2012-08-28 17:43:28 -0700889bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
890 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700891 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700892 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700893
Romain Guyeb993562010-10-05 18:14:38 -0700894 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
895
Romain Guyf607bdc2010-09-10 19:20:06 -0700896 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700897 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700898 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700899 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700900 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700901
902 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700903 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700904 return false;
905 }
Romain Guyf18fd992010-07-08 11:45:51 -0700906
Romain Guya1d3c912011-12-13 14:55:06 -0800907 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700908 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700909 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700910 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700911 }
912
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700914 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
916 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800917 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700918 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700919 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700920
Romain Guy8fb95422010-08-17 18:38:51 -0700921 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700922 mSnapshot->flags |= Snapshot::kFlagIsLayer;
923 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700924
Chris Craik7273daa2013-03-28 11:25:24 -0700925 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700926 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700927 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700928 } else {
929 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800931 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700933 // Workaround for some GL drivers. When reading pixels lying outside
934 // of the window we should get undefined values for those pixels.
935 // Unfortunately some drivers will turn the entire target texture black
936 // when reading outside of the window.
937 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
938 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800940 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700941
Romain Guyb254c242013-06-27 17:15:24 -0700942 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
943 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
944
Romain Guy54be1cd2011-06-13 19:04:27 -0700945 // Enqueue the buffer coordinates to clear the corresponding region later
946 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700947 }
Romain Guyeb993562010-10-05 18:14:38 -0700948 }
Romain Guyf86ef572010-07-01 11:05:42 -0700949
Romain Guyd55a8612010-06-28 17:42:46 -0700950 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700951}
952
Chet Haased48885a2012-08-28 17:43:28 -0700953bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800954 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700955 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700956
Chet Haased48885a2012-08-28 17:43:28 -0700957 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800958 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
959 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700960 mSnapshot->fbo = layer->getFbo();
961 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
962 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
963 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
964 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700965 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700966
Romain Guy2b7028e2012-09-19 17:25:38 -0700967 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700968 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700969 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700970 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
971 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700972
973 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700974 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700975 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700976 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700977 }
978
979 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700981
Romain Guyf735c8e2013-01-31 17:45:55 -0800982 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700983
984 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700985 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800986 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700987 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 glClear(GL_COLOR_BUFFER_BIT);
989
990 dirtyClip();
991
992 // Change the ortho projection
993 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
994 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
995
996 return true;
997}
998
Romain Guy1c740bc2010-09-13 18:00:09 -0700999/**
1000 * Read the documentation of createLayer() before doing anything in this method.
1001 */
Romain Guy1d83e192010-08-17 11:37:00 -07001002void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1003 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001004 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001005 return;
1006 }
1007
Romain Guy8ce00302013-01-15 18:51:42 -08001008 Layer* layer = current->layer;
1009 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001011
Chris Craik39a908c2013-06-13 14:39:01 -07001012 bool clipRequired = false;
1013 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1014 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1015
Romain Guyeb993562010-10-05 18:14:38 -07001016 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001017 endTiling();
1018
Romain Guye0aa84b2012-04-03 19:30:26 -07001019 // Detach the texture from the FBO
1020 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001021
1022 layer->removeFbo(false);
1023
Romain Guyeb993562010-10-05 18:14:38 -07001024 // Unbind current FBO and restore previous one
1025 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001026 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001027
1028 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001029 }
1030
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001032 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001033 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001035 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001036 }
1037
Romain Guy03750a02010-10-18 14:06:08 -07001038 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001039
Romain Guya1d3c912011-12-13 14:55:06 -08001040 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001041
Romain Guy5b3b3522010-10-27 18:57:51 -07001042 // When the layer is stored in an FBO, we can save a bit of fillrate by
1043 // drawing only the dirty region
1044 if (fboLayer) {
1045 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001046 if (layer->getColorFilter()) {
1047 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001048 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001049 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001050 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001051 resetColorFilter();
1052 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 } else if (!rect.isEmpty()) {
1054 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1055 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001056 }
Romain Guy8b55f372010-08-18 17:10:07 -07001057
Romain Guy746b7402010-10-26 16:27:31 -07001058 dirtyClip();
1059
Romain Guyeb993562010-10-05 18:14:38 -07001060 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001061 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001062 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001063 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001064 }
1065}
1066
Romain Guyaa6c24c2011-04-28 18:40:04 -07001067void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001068 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001069
1070 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001072 setupDrawWithTexture();
1073 } else {
1074 setupDrawWithExternalTexture();
1075 }
1076 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001077 setupDrawColor(alpha, alpha, alpha, alpha);
1078 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001079 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001080 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001081 setupDrawPureColorUniforms();
1082 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001083 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1084 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001085 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001086 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001087 }
Romain Guy3b753822013-03-05 10:27:35 -08001088 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001089 layer->getWidth() == (uint32_t) rect.getWidth() &&
1090 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001091 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1092 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001093
Romain Guyd21b6e12011-11-30 20:21:23 -08001094 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001095 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1096 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001097 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001098 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1099 }
1100 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001101 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1102
1103 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1104
1105 finishDrawTexture();
1106}
1107
Romain Guy5b3b3522010-10-27 18:57:51 -07001108void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001109 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001110 const Rect& texCoords = layer->texCoords;
1111 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1112 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001113
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 float x = rect.left;
1115 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001116 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001117 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001118 layer->getHeight() == (uint32_t) rect.getHeight();
1119
1120 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001121 // When we're swapping, the layer is already in screen coordinates
1122 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001123 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1124 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001125 }
1126
Romain Guyd21b6e12011-11-30 20:21:23 -08001127 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001128 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001129 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001130 }
1131
Chris Craik16ecda52013-03-29 10:59:59 -07001132 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001133 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001134 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001135 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001136 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1137 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001138
Romain Guyaa6c24c2011-04-28 18:40:04 -07001139 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1140 } else {
1141 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1142 drawTextureLayer(layer, rect);
1143 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1144 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001145}
1146
Chris Craik34416ea2013-04-15 16:08:28 -07001147/**
1148 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1149 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1150 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1151 * by saveLayer's restore
1152 */
1153#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1154 DRAW_COMMAND; \
1155 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1156 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1157 DRAW_COMMAND; \
1158 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1159 } \
1160 }
1161
1162#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1163
Romain Guy5b3b3522010-10-27 18:57:51 -07001164void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001165 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001166 layer->setRegionAsRect();
1167
Chris Craik34416ea2013-04-15 16:08:28 -07001168 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001169
Romain Guy5b3b3522010-10-27 18:57:51 -07001170 layer->region.clear();
1171 return;
1172 }
1173
Romain Guy211370f2012-02-01 16:10:55 -08001174 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001175 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001176 const android::Rect* rects;
1177 Region safeRegion;
1178 if (CC_LIKELY(hasRectToRectTransform())) {
1179 rects = layer->region.getArray(&count);
1180 } else {
1181 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1182 rects = safeRegion.getArray(&count);
1183 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001184
Chris Craik16ecda52013-03-29 10:59:59 -07001185 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001186 const float texX = 1.0f / float(layer->getWidth());
1187 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001188 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001189
Romain Guy8ce00302013-01-15 18:51:42 -08001190 setupDraw();
1191
1192 // We must get (and therefore bind) the region mesh buffer
1193 // after we setup drawing in case we need to mess with the
1194 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001195 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001196 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001197
Romain Guy7230a742011-01-10 22:26:16 -08001198 setupDrawWithTexture();
1199 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001200 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001201 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001202 setupDrawProgram();
1203 setupDrawDirtyRegionsDisabled();
1204 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001205 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001206 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001207 if (currentTransform().isPureTranslate()) {
1208 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1209 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001210
Romain Guyd21b6e12011-11-30 20:21:23 -08001211 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001212 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1213 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001214 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001215 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1216 }
Romain Guy15bc6432011-12-13 13:11:32 -08001217 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001218
1219 for (size_t i = 0; i < count; i++) {
1220 const android::Rect* r = &rects[i];
1221
1222 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001223 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001224 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001225 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001226
1227 // TODO: Reject quads outside of the clip
1228 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1229 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1230 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1231 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1232
1233 numQuads++;
1234
Romain Guy31e08e92013-06-18 15:53:53 -07001235 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001236 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1237 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001238 numQuads = 0;
1239 mesh = mCaches.getRegionMesh();
1240 }
1241 }
1242
1243 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001244 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1245 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 }
1247
Romain Guy7230a742011-01-10 22:26:16 -08001248 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001249
1250#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001251 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001252#endif
1253
1254 layer->region.clear();
1255 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001256}
1257
Romain Guy3a3133d2011-02-01 22:59:58 -08001258void OpenGLRenderer::drawRegionRects(const Region& region) {
1259#if DEBUG_LAYERS_AS_REGIONS
1260 size_t count;
1261 const android::Rect* rects = region.getArray(&count);
1262
1263 uint32_t colors[] = {
1264 0x7fff0000, 0x7f00ff00,
1265 0x7f0000ff, 0x7fff00ff,
1266 };
1267
1268 int offset = 0;
1269 int32_t top = rects[0].top;
1270
1271 for (size_t i = 0; i < count; i++) {
1272 if (top != rects[i].top) {
1273 offset ^= 0x2;
1274 top = rects[i].top;
1275 }
1276
1277 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1278 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1279 SkXfermode::kSrcOver_Mode);
1280 }
1281#endif
1282}
1283
Romain Guy8ce00302013-01-15 18:51:42 -08001284void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1285 SkXfermode::Mode mode, bool dirty) {
1286 int count = 0;
1287 Vector<float> rects;
1288
1289 SkRegion::Iterator it(region);
1290 while (!it.done()) {
1291 const SkIRect& r = it.rect();
1292 rects.push(r.fLeft);
1293 rects.push(r.fTop);
1294 rects.push(r.fRight);
1295 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001296 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001297 it.next();
1298 }
1299
Romain Guy3bbacf22013-02-06 16:51:04 -08001300 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001301}
1302
Romain Guy5b3b3522010-10-27 18:57:51 -07001303void OpenGLRenderer::dirtyLayer(const float left, const float top,
1304 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001305 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001306 Rect bounds(left, top, right, bottom);
1307 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001308 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001309 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001310}
1311
1312void OpenGLRenderer::dirtyLayer(const float left, const float top,
1313 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001314 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001315 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001316 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001317 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001318}
1319
1320void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001321 if (bounds.intersect(*mSnapshot->clipRect)) {
1322 bounds.snapToPixelBoundaries();
1323 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1324 if (!dirty.isEmpty()) {
1325 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001326 }
1327 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001328}
1329
Romain Guy54be1cd2011-06-13 19:04:27 -07001330void OpenGLRenderer::clearLayerRegions() {
1331 const size_t count = mLayers.size();
1332 if (count == 0) return;
1333
1334 if (!mSnapshot->isIgnored()) {
1335 // Doing several glScissor/glClear here can negatively impact
1336 // GPUs with a tiler architecture, instead we draw quads with
1337 // the Clear blending mode
1338
1339 // The list contains bounds that have already been clipped
1340 // against their initial clip rect, and the current clip
1341 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001342 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001343
1344 Vertex mesh[count * 6];
1345 Vertex* vertex = mesh;
1346
1347 for (uint32_t i = 0; i < count; i++) {
1348 Rect* bounds = mLayers.itemAt(i);
1349
1350 Vertex::set(vertex++, bounds->left, bounds->bottom);
1351 Vertex::set(vertex++, bounds->left, bounds->top);
1352 Vertex::set(vertex++, bounds->right, bounds->top);
1353 Vertex::set(vertex++, bounds->left, bounds->bottom);
1354 Vertex::set(vertex++, bounds->right, bounds->top);
1355 Vertex::set(vertex++, bounds->right, bounds->bottom);
1356
1357 delete bounds;
1358 }
Romain Guye67307c2013-02-11 18:01:20 -08001359 // We must clear the list of dirty rects before we
1360 // call setupDraw() to prevent stencil setup to do
1361 // the same thing again
1362 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001363
1364 setupDraw(false);
1365 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1366 setupDrawBlending(true, SkXfermode::kClear_Mode);
1367 setupDrawProgram();
1368 setupDrawPureColorUniforms();
1369 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001370 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001371
Romain Guy54be1cd2011-06-13 19:04:27 -07001372 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001373
1374 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001375 } else {
1376 for (uint32_t i = 0; i < count; i++) {
1377 delete mLayers.itemAt(i);
1378 }
Romain Guye67307c2013-02-11 18:01:20 -08001379 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001380 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001381}
1382
Romain Guybd6b79b2010-06-26 00:13:53 -07001383///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001384// State Deferral
1385///////////////////////////////////////////////////////////////////////////////
1386
Chris Craikff785832013-03-08 13:12:16 -08001387bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001388 const Rect& currentClip = *(mSnapshot->clipRect);
1389 const mat4& currentMatrix = *(mSnapshot->transform);
1390
Chris Craikff785832013-03-08 13:12:16 -08001391 if (stateDeferFlags & kStateDeferFlag_Draw) {
1392 // state has bounds initialized in local coordinates
1393 if (!state.mBounds.isEmpty()) {
1394 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001395 Rect clippedBounds(state.mBounds);
1396 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001397 // quick rejected
1398 return true;
1399 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001400
Chris Craika02c4ed2013-06-14 13:43:58 -07001401 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001402 if (!currentClip.contains(state.mBounds)) {
1403 int& flags = state.mClipSideFlags;
1404 // op partially clipped, so record which sides are clipped for clip-aware merging
1405 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1406 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1407 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1408 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1409 }
1410 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001411 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001412 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1413 // overdraw avoidance (since we don't know what it overlaps)
1414 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001415 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001416 }
1417 }
1418
Chris Craik527a3aa2013-03-04 10:19:31 -08001419 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1420 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001421 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001422 }
1423
Chris Craik7273daa2013-03-28 11:25:24 -07001424 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1425 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001426 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001427 state.mDrawModifiers = mDrawModifiers;
1428 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001429 return false;
1430}
1431
Chris Craik527a3aa2013-03-04 10:19:31 -08001432void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001433 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001434 mDrawModifiers = state.mDrawModifiers;
1435 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001436
Chris Craik527a3aa2013-03-04 10:19:31 -08001437 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001438 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1439 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001440 dirtyClip();
1441 }
Chris Craikc3566d02013-02-04 16:16:33 -08001442}
1443
Chris Craik28ce94a2013-05-31 11:38:03 -07001444/**
1445 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1446 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1447 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1448 *
1449 * This method should be called when restoreDisplayState() won't be restoring the clip
1450 */
1451void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1452 if (clipRect != NULL) {
1453 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1454 } else {
1455 mSnapshot->setClip(0, 0, mWidth, mHeight);
1456 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001457 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001458 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001459}
1460
Chris Craikc3566d02013-02-04 16:16:33 -08001461///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001462// Transforms
1463///////////////////////////////////////////////////////////////////////////////
1464
1465void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001466 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001467}
1468
1469void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001470 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001471}
1472
1473void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001474 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001475}
1476
Romain Guy807daf72011-01-18 11:19:19 -08001477void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001478 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001479}
1480
Romain Guyf6a11b82010-06-23 17:47:49 -07001481void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001482 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001483 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001484 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001485 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001486 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001487}
1488
Chris Craikb98a0162013-02-21 11:30:22 -08001489bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001490 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001491}
1492
Romain Guyf6a11b82010-06-23 17:47:49 -07001493void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001494 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001495}
1496
1497void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001498 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001499 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001500 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001501 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001502}
1503
1504///////////////////////////////////////////////////////////////////////////////
1505// Clipping
1506///////////////////////////////////////////////////////////////////////////////
1507
Romain Guybb9524b2010-06-22 18:56:38 -07001508void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001509 Rect clip(*mSnapshot->clipRect);
1510 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001511
Romain Guy8a4ac612012-07-17 17:32:48 -07001512 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1513 clip.getWidth(), clip.getHeight())) {
1514 mDirtyClip = false;
1515 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001516}
1517
Romain Guy8ce00302013-01-15 18:51:42 -08001518void OpenGLRenderer::ensureStencilBuffer() {
1519 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1520 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1521 // just hope we have one when hasLayer() returns false.
1522 if (hasLayer()) {
1523 attachStencilBufferToLayer(mSnapshot->layer);
1524 }
1525}
1526
1527void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1528 // The layer's FBO is already bound when we reach this stage
1529 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001530 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1531 // is attached after we initiated tiling. We must turn it off,
1532 // attach the new render buffer then turn tiling back on
1533 endTiling();
1534
Romain Guy8d4aeb72013-02-12 16:08:55 -08001535 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001536 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001537 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001538
Romain Guyf735c8e2013-01-31 17:45:55 -08001539 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001540 }
1541}
1542
1543void OpenGLRenderer::setStencilFromClip() {
1544 if (!mCaches.debugOverdraw) {
1545 if (!mSnapshot->clipRegion->isEmpty()) {
1546 // NOTE: The order here is important, we must set dirtyClip to false
1547 // before any draw call to avoid calling back into this method
1548 mDirtyClip = false;
1549
1550 ensureStencilBuffer();
1551
1552 mCaches.stencil.enableWrite();
1553
1554 // Clear the stencil but first make sure we restrict drawing
1555 // to the region's bounds
1556 bool resetScissor = mCaches.enableScissor();
1557 if (resetScissor) {
1558 // The scissor was not set so we now need to update it
1559 setScissorFromClip();
1560 }
1561 mCaches.stencil.clear();
1562 if (resetScissor) mCaches.disableScissor();
1563
1564 // NOTE: We could use the region contour path to generate a smaller mesh
1565 // Since we are using the stencil we could use the red book path
1566 // drawing technique. It might increase bandwidth usage though.
1567
1568 // The last parameter is important: we are not drawing in the color buffer
1569 // so we don't want to dirty the current layer, if any
1570 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1571
1572 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001573
1574 // Draw the region used to generate the stencil if the appropriate debug
1575 // mode is enabled
1576 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1577 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1578 }
Romain Guy8ce00302013-01-15 18:51:42 -08001579 } else {
1580 mCaches.stencil.disable();
1581 }
1582 }
1583}
1584
Romain Guy9d5316e2010-06-24 19:30:36 -07001585const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001586 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001587}
1588
Chris Craik39a908c2013-06-13 14:39:01 -07001589bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1590 bool* clipRequired) {
1591 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001592 return true;
1593 }
1594
1595 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001596 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001597 r.snapToPixelBoundaries();
1598
1599 Rect clipRect(*mSnapshot->clipRect);
1600 clipRect.snapToPixelBoundaries();
1601
Chris Craik39a908c2013-06-13 14:39:01 -07001602 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001603
Chris Craik39a908c2013-06-13 14:39:01 -07001604 if (clipRequired) *clipRequired = !clipRect.contains(r);
1605 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001606}
1607
Romain Guy672433d2013-01-04 19:05:13 -08001608bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1609 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001610 if (paint->getStyle() != SkPaint::kFill_Style) {
1611 float outset = paint->getStrokeWidth() * 0.5f;
1612 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1613 } else {
1614 return quickReject(left, top, right, bottom);
1615 }
1616}
1617
Romain Guyc7d53492010-06-25 13:41:57 -07001618bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craik39a908c2013-06-13 14:39:01 -07001619 bool clipRequired = false;
1620 if (quickRejectNoScissor(left, top, right, bottom, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001621 return true;
1622 }
1623
Chris Craik39a908c2013-06-13 14:39:01 -07001624 if (!isDeferred()) {
1625 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001626 }
Chris Craik39a908c2013-06-13 14:39:01 -07001627 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001628}
1629
Romain Guy8ce00302013-01-15 18:51:42 -08001630void OpenGLRenderer::debugClip() {
1631#if DEBUG_CLIP_REGIONS
1632 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1633 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1634 }
1635#endif
1636}
1637
Romain Guy079ba2c2010-07-16 14:12:24 -07001638bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001639 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001640 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1641 if (clipped) {
1642 dirtyClip();
1643 }
1644 return !mSnapshot->clipRect->isEmpty();
1645 }
1646
1647 SkPath path;
1648 path.addRect(left, top, right, bottom);
1649
1650 return clipPath(&path, op);
1651}
1652
1653bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1654 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001655 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001656
1657 SkPath transformed;
1658 path->transform(transform, &transformed);
1659
1660 SkRegion clip;
1661 if (!mSnapshot->clipRegion->isEmpty()) {
1662 clip.setRegion(*mSnapshot->clipRegion);
1663 } else {
1664 Rect* bounds = mSnapshot->clipRect;
1665 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1666 }
1667
1668 SkRegion region;
1669 region.setPath(transformed, clip);
1670
1671 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001672 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001673 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001674 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001675 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001676}
1677
Romain Guy735738c2012-12-03 12:34:51 -08001678bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001679 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1680 if (clipped) {
1681 dirtyClip();
1682 }
1683 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001684}
1685
Chet Haasea23eed82012-04-12 15:19:04 -07001686Rect* OpenGLRenderer::getClipRect() {
1687 return mSnapshot->clipRect;
1688}
1689
Romain Guyf6a11b82010-06-23 17:47:49 -07001690///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001691// Drawing commands
1692///////////////////////////////////////////////////////////////////////////////
1693
Romain Guy54be1cd2011-06-13 19:04:27 -07001694void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001695 // TODO: It would be best if we could do this before quickReject()
1696 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001697 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001698 // Make sure setScissor & setStencil happen at the beginning of
1699 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001700 if (mDirtyClip) {
1701 if (mCaches.scissorEnabled) {
1702 setScissorFromClip();
1703 }
Romain Guy8ce00302013-01-15 18:51:42 -08001704 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001705 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001706
Romain Guy70ca14e2010-12-13 18:24:33 -08001707 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001708
Romain Guy70ca14e2010-12-13 18:24:33 -08001709 mSetShaderColor = false;
1710 mColorSet = false;
1711 mColorA = mColorR = mColorG = mColorB = 0.0f;
1712 mTextureUnit = 0;
1713 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001714
1715 // Enable debug highlight when what we're about to draw is tested against
1716 // the stencil buffer and if stencil highlight debugging is on
1717 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1718 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1719 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001720
1721 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001722}
1723
1724void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1725 mDescription.hasTexture = true;
1726 mDescription.hasAlpha8Texture = isAlpha8;
1727}
1728
Romain Guyff316ec2013-02-13 18:39:43 -08001729void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1730 mDescription.hasTexture = true;
1731 mDescription.hasColors = true;
1732 mDescription.hasAlpha8Texture = isAlpha8;
1733}
1734
Romain Guyaa6c24c2011-04-28 18:40:04 -07001735void OpenGLRenderer::setupDrawWithExternalTexture() {
1736 mDescription.hasExternalTexture = true;
1737}
1738
Romain Guy15bc6432011-12-13 13:11:32 -08001739void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001740 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001741}
1742
Chris Craik710f46d2012-09-17 17:25:49 -07001743void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001744 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001745}
1746
Romain Guy8d0d4782010-12-14 20:13:35 -08001747void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1748 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001749 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1750 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1751 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 mColorSet = true;
1753 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1754}
1755
Romain Guy86568192010-12-14 15:55:39 -08001756void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1757 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001758 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1759 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1760 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001761 mColorSet = true;
1762 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1763}
1764
Romain Guy41210632012-07-16 17:04:24 -07001765void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1766 mCaches.fontRenderer->describe(mDescription, paint);
1767}
1768
Romain Guy70ca14e2010-12-13 18:24:33 -08001769void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1770 mColorA = a;
1771 mColorR = r;
1772 mColorG = g;
1773 mColorB = b;
1774 mColorSet = true;
1775 mSetShaderColor = mDescription.setColor(r, g, b, a);
1776}
1777
1778void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001779 if (mDrawModifiers.mShader) {
1780 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001781 }
1782}
1783
1784void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001785 if (mDrawModifiers.mColorFilter) {
1786 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001787 }
1788}
1789
Romain Guyf09ef512011-05-27 11:43:46 -07001790void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1791 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1792 mColorA = 1.0f;
1793 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001794 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001795 }
1796}
1797
Romain Guy70ca14e2010-12-13 18:24:33 -08001798void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001799 // When the blending mode is kClear_Mode, we need to use a modulate color
1800 // argb=1,0,0,0
1801 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001802 bool blend = (mColorSet && mColorA < 1.0f) ||
1803 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1804 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805}
1806
1807void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001808 // When the blending mode is kClear_Mode, we need to use a modulate color
1809 // argb=1,0,0,0
1810 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001811 blend |= (mColorSet && mColorA < 1.0f) ||
1812 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1813 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1814 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001815}
1816
1817void OpenGLRenderer::setupDrawProgram() {
1818 useProgram(mCaches.programCache.get(mDescription));
1819}
1820
1821void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1822 mTrackDirtyRegions = false;
1823}
1824
1825void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1826 bool ignoreTransform) {
1827 mModelView.loadTranslate(left, top, 0.0f);
1828 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001829 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1830 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001831 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001832 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001833 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1834 }
1835}
1836
Chet Haase8a5cc922011-04-26 07:28:09 -07001837void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001838 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001839}
1840
Romain Guy70ca14e2010-12-13 18:24:33 -08001841void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1842 bool ignoreTransform, bool ignoreModelView) {
1843 if (!ignoreModelView) {
1844 mModelView.loadTranslate(left, top, 0.0f);
1845 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001846 } else {
1847 mModelView.loadIdentity();
1848 }
Romain Guy86568192010-12-14 15:55:39 -08001849 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1850 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001851 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001852 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001853 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001854 }
1855 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001856 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001857 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1858 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001859}
1860
1861void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001862 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001863 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1864 }
1865}
1866
Romain Guy86568192010-12-14 15:55:39 -08001867void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001868 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001869 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001870 }
1871}
1872
Romain Guy70ca14e2010-12-13 18:24:33 -08001873void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001874 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001875 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001876 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001877 }
Chris Craikc3566d02013-02-04 16:16:33 -08001878 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1879 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001880 }
1881}
1882
Romain Guy8d0d4782010-12-14 20:13:35 -08001883void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001884 if (mDrawModifiers.mShader) {
1885 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001886 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001887 }
1888}
1889
Romain Guy70ca14e2010-12-13 18:24:33 -08001890void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001891 if (mDrawModifiers.mColorFilter) {
1892 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001893 }
1894}
1895
Romain Guy41210632012-07-16 17:04:24 -07001896void OpenGLRenderer::setupDrawTextGammaUniforms() {
1897 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1898}
1899
Romain Guy70ca14e2010-12-13 18:24:33 -08001900void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001901 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001902 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001903 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001904}
1905
1906void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001907 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001908 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001909 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001910}
1911
Romain Guyaa6c24c2011-04-28 18:40:04 -07001912void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1913 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001914 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001915 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001916}
1917
Romain Guy8f0095c2011-05-02 17:24:22 -07001918void OpenGLRenderer::setupDrawTextureTransform() {
1919 mDescription.hasTextureTransform = true;
1920}
1921
1922void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001923 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1924 GL_FALSE, &transform.data[0]);
1925}
1926
Romain Guy70ca14e2010-12-13 18:24:33 -08001927void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001928 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001929 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001930 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001931 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001932 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001933 }
Romain Guyd71dd362011-12-12 19:03:35 -08001934
Chris Craikcb4d6002012-09-25 12:00:29 -07001935 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001936 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001937 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001938 }
1939
1940 mCaches.unbindIndicesBuffer();
1941}
1942
Romain Guyff316ec2013-02-13 18:39:43 -08001943void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1944 bool force = mCaches.unbindMeshBuffer();
1945 GLsizei stride = sizeof(ColorTextureVertex);
1946
1947 mCaches.bindPositionVertexPointer(force, vertices, stride);
1948 if (mCaches.currentProgram->texCoords >= 0) {
1949 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1950 }
1951 int slot = mCaches.currentProgram->getAttrib("colors");
1952 if (slot >= 0) {
1953 glEnableVertexAttribArray(slot);
1954 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1955 }
1956
1957 mCaches.unbindIndicesBuffer();
1958}
1959
Romain Guy3b748a42013-04-17 18:54:38 -07001960void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1961 bool force = false;
1962 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1963 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1964 // use the default VBO found in Caches
1965 if (!vertices || vbo) {
1966 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1967 } else {
1968 force = mCaches.unbindMeshBuffer();
1969 }
1970 mCaches.bindIndicesBuffer();
1971
Chris Craikcb4d6002012-09-25 12:00:29 -07001972 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001973 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001974 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001975 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001976}
1977
Chet Haase5b0200b2011-04-13 17:58:08 -07001978void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001979 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001980 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001981 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001982}
1983
Romain Guy70ca14e2010-12-13 18:24:33 -08001984void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001985}
1986
1987///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001988// Drawing
1989///////////////////////////////////////////////////////////////////////////////
1990
Chris Craikff785832013-03-08 13:12:16 -08001991status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1992 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001993 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001994 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1995 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001996 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001997 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001998 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001999 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2000 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002001 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002002 }
2003
Chris Craik28ce94a2013-05-31 11:38:03 -07002004 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2005 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002006 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2007 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002008
2009 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002010 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002011
Chet Haase58d110a2013-04-10 07:43:29 -07002012 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002013 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002014
Romain Guy65549432012-03-26 16:45:05 -07002015 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002016}
2017
Chris Craikc3566d02013-02-04 16:16:33 -08002018void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002019 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002020 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002021 }
2022}
2023
Romain Guya168d732011-03-18 16:50:13 -07002024void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2025 int alpha;
2026 SkXfermode::Mode mode;
2027 getAlphaAndMode(paint, &alpha, &mode);
2028
Romain Guy886b2752013-01-04 12:26:18 -08002029 int color = paint != NULL ? paint->getColor() : 0;
2030
Romain Guya168d732011-03-18 16:50:13 -07002031 float x = left;
2032 float y = top;
2033
Romain Guy886b2752013-01-04 12:26:18 -08002034 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2035
Romain Guya168d732011-03-18 16:50:13 -07002036 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002037 if (currentTransform().isPureTranslate()) {
2038 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2039 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002040 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002041
2042 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002043 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002044 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002045 }
2046
Romain Guy3b748a42013-04-17 18:54:38 -07002047 // No need to check for a UV mapper on the texture object, only ARGB_8888
2048 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002049 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002050 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2051 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002052}
2053
Romain Guy03c00b52013-06-20 18:30:28 -07002054/**
2055 * Important note: this method is intended to draw batches of bitmaps and
2056 * will not set the scissor enable or dirty the current layer, if any.
2057 * The caller is responsible for properly dirtying the current layer.
2058 */
Romain Guy55b6f952013-06-27 15:27:09 -07002059status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2060 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002061 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002062 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002063 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002064
Chris Craik527a3aa2013-03-04 10:19:31 -08002065 const AutoTexture autoCleanup(texture);
2066
2067 int alpha;
2068 SkXfermode::Mode mode;
2069 getAlphaAndMode(paint, &alpha, &mode);
2070
2071 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002072 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002073
2074 const float x = (int) floorf(bounds.left + 0.5f);
2075 const float y = (int) floorf(bounds.top + 0.5f);
2076 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2077 int color = paint != NULL ? paint->getColor() : 0;
2078 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2079 texture->id, paint != NULL, color, alpha, mode,
2080 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002081 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002082 } else {
2083 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2084 texture->id, alpha / 255.0f, mode, texture->blend,
2085 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002086 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002087 }
2088
2089 return DrawGlInfo::kStatusDrew;
2090}
2091
Chet Haase48659092012-05-31 15:21:51 -07002092status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002093 const float right = left + bitmap->width();
2094 const float bottom = top + bitmap->height();
2095
2096 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002097 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002098 }
2099
Romain Guya1d3c912011-12-13 14:55:06 -08002100 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002101 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002102 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002103 const AutoTexture autoCleanup(texture);
2104
Romain Guy211370f2012-02-01 16:10:55 -08002105 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002106 drawAlphaBitmap(texture, left, top, paint);
2107 } else {
2108 drawTextureRect(left, top, right, bottom, texture, paint);
2109 }
Chet Haase48659092012-05-31 15:21:51 -07002110
2111 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002112}
2113
Chet Haase48659092012-05-31 15:21:51 -07002114status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002115 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2116 const mat4 transform(*matrix);
2117 transform.mapRect(r);
2118
Romain Guy6926c722010-07-12 20:20:03 -07002119 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002120 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002121 }
2122
Romain Guya1d3c912011-12-13 14:55:06 -08002123 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002124 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002125 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002126 const AutoTexture autoCleanup(texture);
2127
Romain Guy5b3b3522010-10-27 18:57:51 -07002128 // This could be done in a cheaper way, all we need is pass the matrix
2129 // to the vertex shader. The save/restore is a bit overkill.
2130 save(SkCanvas::kMatrix_SaveFlag);
2131 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002132 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2133 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2134 } else {
2135 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2136 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002137 restore();
Chet Haase48659092012-05-31 15:21:51 -07002138
2139 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002140}
2141
Chet Haase48659092012-05-31 15:21:51 -07002142status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002143 const float right = left + bitmap->width();
2144 const float bottom = top + bitmap->height();
2145
2146 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002147 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002148 }
2149
2150 mCaches.activeTexture(0);
2151 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2152 const AutoTexture autoCleanup(texture);
2153
Romain Guy886b2752013-01-04 12:26:18 -08002154 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2155 drawAlphaBitmap(texture, left, top, paint);
2156 } else {
2157 drawTextureRect(left, top, right, bottom, texture, paint);
2158 }
Chet Haase48659092012-05-31 15:21:51 -07002159
2160 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002161}
2162
Chet Haase48659092012-05-31 15:21:51 -07002163status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002164 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002165 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002166 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002167 }
2168
Chris Craik39a908c2013-06-13 14:39:01 -07002169 // TODO: use quickReject on bounds from vertices
2170 mCaches.enableScissor();
2171
Romain Guyb18d2d02011-02-10 15:52:54 -08002172 float left = FLT_MAX;
2173 float top = FLT_MAX;
2174 float right = FLT_MIN;
2175 float bottom = FLT_MIN;
2176
Romain Guya92bb4d2012-10-16 11:08:44 -07002177 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002178
Romain Guyff316ec2013-02-13 18:39:43 -08002179 ColorTextureVertex mesh[count];
2180 ColorTextureVertex* vertex = mesh;
2181
2182 bool cleanupColors = false;
2183 if (!colors) {
2184 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2185 colors = new int[colorsCount];
2186 memset(colors, 0xff, colorsCount * sizeof(int));
2187 cleanupColors = true;
2188 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002189
Romain Guy3b748a42013-04-17 18:54:38 -07002190 mCaches.activeTexture(0);
2191 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2192 const UvMapper& mapper(getMapper(texture));
2193
Romain Guy5a7b4662011-01-20 19:09:30 -08002194 for (int32_t y = 0; y < meshHeight; y++) {
2195 for (int32_t x = 0; x < meshWidth; x++) {
2196 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2197
2198 float u1 = float(x) / meshWidth;
2199 float u2 = float(x + 1) / meshWidth;
2200 float v1 = float(y) / meshHeight;
2201 float v2 = float(y + 1) / meshHeight;
2202
Romain Guy3b748a42013-04-17 18:54:38 -07002203 mapper.map(u1, v1, u2, v2);
2204
Romain Guy5a7b4662011-01-20 19:09:30 -08002205 int ax = i + (meshWidth + 1) * 2;
2206 int ay = ax + 1;
2207 int bx = i;
2208 int by = bx + 1;
2209 int cx = i + 2;
2210 int cy = cx + 1;
2211 int dx = i + (meshWidth + 1) * 2 + 2;
2212 int dy = dx + 1;
2213
Romain Guyff316ec2013-02-13 18:39:43 -08002214 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2215 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2216 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002217
Romain Guyff316ec2013-02-13 18:39:43 -08002218 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2219 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2220 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002221
Romain Guya92bb4d2012-10-16 11:08:44 -07002222 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2223 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2224 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2225 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002226 }
2227 }
2228
Romain Guya92bb4d2012-10-16 11:08:44 -07002229 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002230 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002231 return DrawGlInfo::kStatusDone;
2232 }
2233
Romain Guyff316ec2013-02-13 18:39:43 -08002234 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002235 texture = mCaches.textureCache.get(bitmap);
2236 if (!texture) {
2237 if (cleanupColors) delete[] colors;
2238 return DrawGlInfo::kStatusDone;
2239 }
Romain Guyff316ec2013-02-13 18:39:43 -08002240 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002241 const AutoTexture autoCleanup(texture);
2242
2243 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2244 texture->setFilter(FILTER(paint), true);
2245
2246 int alpha;
2247 SkXfermode::Mode mode;
2248 getAlphaAndMode(paint, &alpha, &mode);
2249
Romain Guyff316ec2013-02-13 18:39:43 -08002250 float a = alpha / 255.0f;
2251
Romain Guya92bb4d2012-10-16 11:08:44 -07002252 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002253 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002254 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002255
Romain Guyff316ec2013-02-13 18:39:43 -08002256 setupDraw();
2257 setupDrawWithTextureAndColor();
2258 setupDrawColor(a, a, a, a);
2259 setupDrawColorFilter();
2260 setupDrawBlending(true, mode, false);
2261 setupDrawProgram();
2262 setupDrawDirtyRegionsDisabled();
2263 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2264 setupDrawTexture(texture->id);
2265 setupDrawPureColorUniforms();
2266 setupDrawColorFilterUniforms();
2267 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2268
2269 glDrawArrays(GL_TRIANGLES, 0, count);
2270
2271 finishDrawTexture();
2272
2273 int slot = mCaches.currentProgram->getAttrib("colors");
2274 if (slot >= 0) {
2275 glDisableVertexAttribArray(slot);
2276 }
2277
2278 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002279
2280 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002281}
2282
Chet Haase48659092012-05-31 15:21:51 -07002283status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002284 float srcLeft, float srcTop, float srcRight, float srcBottom,
2285 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002286 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002287 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002288 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002289 }
2290
Romain Guya1d3c912011-12-13 14:55:06 -08002291 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002292 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002293 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002294 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002295
Romain Guy8ba548f2010-06-30 19:21:21 -07002296 const float width = texture->width;
2297 const float height = texture->height;
2298
Romain Guy3b748a42013-04-17 18:54:38 -07002299 float u1 = fmax(0.0f, srcLeft / width);
2300 float v1 = fmax(0.0f, srcTop / height);
2301 float u2 = fmin(1.0f, srcRight / width);
2302 float v2 = fmin(1.0f, srcBottom / height);
2303
2304 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002305
Romain Guy03750a02010-10-18 14:06:08 -07002306 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002307 resetDrawTextureTexCoords(u1, v1, u2, v2);
2308
Romain Guy03750a02010-10-18 14:06:08 -07002309 int alpha;
2310 SkXfermode::Mode mode;
2311 getAlphaAndMode(paint, &alpha, &mode);
2312
Romain Guyd21b6e12011-11-30 20:21:23 -08002313 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2314
Romain Guy886b2752013-01-04 12:26:18 -08002315 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2316 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002317
Romain Guy886b2752013-01-04 12:26:18 -08002318 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2319 // Apply a scale transform on the canvas only when a shader is in use
2320 // Skia handles the ratio between the dst and src rects as a scale factor
2321 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002322 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002323 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002324
Romain Guy3b753822013-03-05 10:27:35 -08002325 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2326 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2327 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002328
2329 dstRight = x + (dstRight - dstLeft);
2330 dstBottom = y + (dstBottom - dstTop);
2331
2332 dstLeft = x;
2333 dstTop = y;
2334
2335 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2336 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002337 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002338 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002339 }
2340
2341 if (CC_UNLIKELY(useScaleTransform)) {
2342 save(SkCanvas::kMatrix_SaveFlag);
2343 translate(dstLeft, dstTop);
2344 scale(scaleX, scaleY);
2345
2346 dstLeft = 0.0f;
2347 dstTop = 0.0f;
2348
2349 dstRight = srcRight - srcLeft;
2350 dstBottom = srcBottom - srcTop;
2351 }
2352
2353 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2354 int color = paint ? paint->getColor() : 0;
2355 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2356 texture->id, paint != NULL, color, alpha, mode,
2357 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2358 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2359 } else {
2360 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2361 texture->id, alpha / 255.0f, mode, texture->blend,
2362 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2363 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2364 }
2365
2366 if (CC_UNLIKELY(useScaleTransform)) {
2367 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002368 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002369
2370 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002371
2372 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002373}
2374
Romain Guy3b748a42013-04-17 18:54:38 -07002375status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002376 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002377 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002378 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002379 }
2380
Romain Guy4c2547f2013-06-11 16:19:24 -07002381 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002382 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2383 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002384
Romain Guy03c00b52013-06-20 18:30:28 -07002385 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002386}
2387
Romain Guy03c00b52013-06-20 18:30:28 -07002388status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2389 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002390 if (quickReject(left, top, right, bottom)) {
2391 return DrawGlInfo::kStatusDone;
2392 }
2393
Romain Guy211370f2012-02-01 16:10:55 -08002394 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002395 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002396 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002397 if (!texture) return DrawGlInfo::kStatusDone;
2398 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002399
Romain Guya4adcf02013-02-28 12:15:35 -08002400 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2401 texture->setFilter(GL_LINEAR, true);
2402
Romain Guy03c00b52013-06-20 18:30:28 -07002403 int alpha;
2404 SkXfermode::Mode mode;
2405 getAlphaAndMode(paint, &alpha, &mode);
2406
Romain Guy3b753822013-03-05 10:27:35 -08002407 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002408 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002409 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002410 const float offsetX = left + currentTransform().getTranslateX();
2411 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002412 const size_t count = mesh->quads.size();
2413 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002414 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002415 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002416 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2417 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2418 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002419 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002420 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002421 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002422 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002423 }
2424 }
2425
Romain Guy211370f2012-02-01 16:10:55 -08002426 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002427 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2428 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002429
Romain Guy3b748a42013-04-17 18:54:38 -07002430 right = x + right - left;
2431 bottom = y + bottom - top;
2432 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2433 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2434 GL_TRIANGLES, mesh->indexCount, false, true,
2435 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002436 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002437 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2438 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2439 GL_TRIANGLES, mesh->indexCount, false, false,
2440 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002441 }
Romain Guy054dc182010-10-15 17:55:25 -07002442 }
Chet Haase48659092012-05-31 15:21:51 -07002443
2444 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002445}
2446
Romain Guy03c00b52013-06-20 18:30:28 -07002447/**
2448 * Important note: this method is intended to draw batches of 9-patch objects and
2449 * will not set the scissor enable or dirty the current layer, if any.
2450 * The caller is responsible for properly dirtying the current layer.
2451 */
2452status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2453 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2454 mCaches.activeTexture(0);
2455 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2456 if (!texture) return DrawGlInfo::kStatusDone;
2457 const AutoTexture autoCleanup(texture);
2458
2459 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2460 texture->setFilter(GL_LINEAR, true);
2461
2462 int alpha;
2463 SkXfermode::Mode mode;
2464 getAlphaAndMode(paint, &alpha, &mode);
2465
2466 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2467 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2468 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2469
2470 return DrawGlInfo::kStatusDrew;
2471}
2472
Chris Craik65cd6122012-12-10 17:56:27 -08002473status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2474 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002475 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002476 // no vertices to draw
2477 return DrawGlInfo::kStatusDone;
2478 }
2479
Chris Craikcb4d6002012-09-25 12:00:29 -07002480 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002481 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2482 bool isAA = paint->isAntiAlias();
2483
Chris Craik710f46d2012-09-17 17:25:49 -07002484 setupDraw();
2485 setupDrawNoTexture();
2486 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002487 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2488 setupDrawColorFilter();
2489 setupDrawShader();
2490 setupDrawBlending(isAA, mode);
2491 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002492 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002493 setupDrawColorUniforms();
2494 setupDrawColorFilterUniforms();
2495 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002496
Chris Craik710f46d2012-09-17 17:25:49 -07002497 void* vertices = vertexBuffer.getBuffer();
2498 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002499 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002500 mCaches.resetTexCoordsVertexPointer();
2501 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002502
Chris Craik710f46d2012-09-17 17:25:49 -07002503 int alphaSlot = -1;
2504 if (isAA) {
2505 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2506 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002507
Chris Craik710f46d2012-09-17 17:25:49 -07002508 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002509 glEnableVertexAttribArray(alphaSlot);
2510 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002511 }
Romain Guy04299382012-07-18 17:15:41 -07002512
Chris Craik6d29c8d2013-05-08 18:35:44 -07002513 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002514
Chris Craik710f46d2012-09-17 17:25:49 -07002515 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002516 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002517 }
Chris Craik65cd6122012-12-10 17:56:27 -08002518
2519 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002520}
2521
2522/**
Chris Craik65cd6122012-12-10 17:56:27 -08002523 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2524 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2525 * screen space in all directions. However, instead of using a fragment shader to compute the
2526 * translucency of the color from its position, we simply use a varying parameter to define how far
2527 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2528 *
2529 * Doesn't yet support joins, caps, or path effects.
2530 */
2531status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2532 VertexBuffer vertexBuffer;
2533 // TODO: try clipping large paths to viewport
2534 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2535
Romain Guyc46d07a2013-03-15 19:06:39 -07002536 if (hasLayer()) {
2537 SkRect bounds = path.getBounds();
2538 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2539 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2540 }
Chris Craik65cd6122012-12-10 17:56:27 -08002541
2542 return drawVertexBuffer(vertexBuffer, paint);
2543}
2544
2545/**
2546 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2547 * and additional geometry for defining an alpha slope perimeter.
2548 *
2549 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2550 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2551 * in-shader alpha region, but found it to be taxing on some GPUs.
2552 *
2553 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2554 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002555 */
Chet Haase48659092012-05-31 15:21:51 -07002556status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002557 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002558
Chris Craik65cd6122012-12-10 17:56:27 -08002559 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002560
Chris Craik65cd6122012-12-10 17:56:27 -08002561 VertexBuffer buffer;
2562 SkRect bounds;
2563 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002564
2565 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2566 return DrawGlInfo::kStatusDone;
2567 }
2568
Romain Guy3b753822013-03-05 10:27:35 -08002569 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002570
Chris Craik65cd6122012-12-10 17:56:27 -08002571 bool useOffset = !paint->isAntiAlias();
2572 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002573}
2574
Chet Haase48659092012-05-31 15:21:51 -07002575status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002576 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002577
Chris Craik6d29c8d2013-05-08 18:35:44 -07002578 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002579
Chris Craik6d29c8d2013-05-08 18:35:44 -07002580 VertexBuffer buffer;
2581 SkRect bounds;
2582 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002583
Chris Craik6d29c8d2013-05-08 18:35:44 -07002584 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2585 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002586 }
2587
Chris Craik6d29c8d2013-05-08 18:35:44 -07002588 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002589
Chris Craik6d29c8d2013-05-08 18:35:44 -07002590 bool useOffset = !paint->isAntiAlias();
2591 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002592}
2593
Chet Haase48659092012-05-31 15:21:51 -07002594status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002595 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002596 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002597
Romain Guyae88e5e2010-10-22 17:49:18 -07002598 Rect& clip(*mSnapshot->clipRect);
2599 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002600
Romain Guy3d58c032010-07-14 16:34:53 -07002601 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002602
2603 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002604}
Romain Guy9d5316e2010-06-24 19:30:36 -07002605
Chet Haase48659092012-05-31 15:21:51 -07002606status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2607 SkPaint* paint) {
2608 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002609 const AutoTexture autoCleanup(texture);
2610
2611 const float x = left + texture->left - texture->offset;
2612 const float y = top + texture->top - texture->offset;
2613
2614 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002615
2616 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002617}
2618
Chet Haase48659092012-05-31 15:21:51 -07002619status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002620 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002621 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2622 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002623 return DrawGlInfo::kStatusDone;
2624 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002625
Chris Craikcb4d6002012-09-25 12:00:29 -07002626 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002627 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002628 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002629 right - left, bottom - top, rx, ry, p);
2630 return drawShape(left, top, texture, p);
2631 }
2632
2633 SkPath path;
2634 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002635 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2636 float outset = p->getStrokeWidth() / 2;
2637 rect.outset(outset, outset);
2638 rx += outset;
2639 ry += outset;
2640 }
Chris Craik710f46d2012-09-17 17:25:49 -07002641 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002642 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002643}
2644
Chris Craik710f46d2012-09-17 17:25:49 -07002645status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002646 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002647 x + radius, y + radius, p) ||
2648 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002649 return DrawGlInfo::kStatusDone;
2650 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002651 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002652 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002653 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002654 return drawShape(x - radius, y - radius, texture, p);
2655 }
2656
2657 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002658 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2659 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2660 } else {
2661 path.addCircle(x, y, radius);
2662 }
Chris Craik65cd6122012-12-10 17:56:27 -08002663 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002664}
Romain Guy01d58e42011-01-19 21:54:02 -08002665
Chet Haase48659092012-05-31 15:21:51 -07002666status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002667 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002668 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2669 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002670 return DrawGlInfo::kStatusDone;
2671 }
Romain Guy01d58e42011-01-19 21:54:02 -08002672
Chris Craikcb4d6002012-09-25 12:00:29 -07002673 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002674 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002675 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002676 return drawShape(left, top, texture, p);
2677 }
2678
2679 SkPath path;
2680 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002681 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2682 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2683 }
Chris Craik710f46d2012-09-17 17:25:49 -07002684 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002685 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002686}
2687
Chet Haase48659092012-05-31 15:21:51 -07002688status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002689 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002690 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2691 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002692 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002693 }
2694
Chris Craik780c1282012-10-04 14:10:49 -07002695 if (fabs(sweepAngle) >= 360.0f) {
2696 return drawOval(left, top, right, bottom, p);
2697 }
2698
2699 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002700 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002701 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002702 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002703 startAngle, sweepAngle, useCenter, p);
2704 return drawShape(left, top, texture, p);
2705 }
2706
2707 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2708 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2709 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2710 }
2711
2712 SkPath path;
2713 if (useCenter) {
2714 path.moveTo(rect.centerX(), rect.centerY());
2715 }
2716 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2717 if (useCenter) {
2718 path.close();
2719 }
Chris Craik65cd6122012-12-10 17:56:27 -08002720 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002721}
2722
Romain Guycf8675e2012-10-02 12:32:25 -07002723// See SkPaintDefaults.h
2724#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2725
Chet Haase48659092012-05-31 15:21:51 -07002726status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002727 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2728 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002729 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002730 }
2731
Chris Craik710f46d2012-09-17 17:25:49 -07002732 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002733 // only fill style is supported by drawConvexPath, since others have to handle joins
2734 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2735 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2736 mCaches.activeTexture(0);
2737 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002738 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002739 return drawShape(left, top, texture, p);
2740 }
2741
2742 SkPath path;
2743 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2744 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2745 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2746 }
2747 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002748 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002749 }
2750
Romain Guy3b753822013-03-05 10:27:35 -08002751 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002752 SkPath path;
2753 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002754 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002755 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002756 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002757 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002758 }
Romain Guyc7d53492010-06-25 13:41:57 -07002759}
Romain Guy9d5316e2010-06-24 19:30:36 -07002760
Raph Levien416a8472012-07-19 22:48:17 -07002761void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2762 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2763 float x, float y) {
2764 mCaches.activeTexture(0);
2765
2766 // NOTE: The drop shadow will not perform gamma correction
2767 // if shader-based correction is enabled
2768 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2769 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002770 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002771 // If the drop shadow exceeds the max texture size or couldn't be
2772 // allocated, skip drawing
2773 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002774 const AutoTexture autoCleanup(shadow);
2775
Chris Craikc3566d02013-02-04 16:16:33 -08002776 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2777 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002778
Chris Craikc3566d02013-02-04 16:16:33 -08002779 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2780 int shadowColor = mDrawModifiers.mShadowColor;
2781 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002782 shadowColor = 0xffffffff;
2783 }
2784
2785 setupDraw();
2786 setupDrawWithTexture(true);
2787 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2788 setupDrawColorFilter();
2789 setupDrawShader();
2790 setupDrawBlending(true, mode);
2791 setupDrawProgram();
2792 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2793 setupDrawTexture(shadow->id);
2794 setupDrawPureColorUniforms();
2795 setupDrawColorFilterUniforms();
2796 setupDrawShaderUniforms();
2797 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2798
2799 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2800}
2801
Romain Guy768bffc2013-02-27 13:50:45 -08002802bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2803 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2804 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2805}
2806
Romain Guy257ae352013-03-20 16:31:12 -07002807class TextSetupFunctor: public Functor {
2808public:
2809 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2810 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2811 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2812 alpha(alpha), mode(mode), paint(paint) {
2813 }
2814 ~TextSetupFunctor() { }
2815
2816 status_t operator ()(int what, void* data) {
2817 renderer.setupDraw();
2818 renderer.setupDrawTextGamma(paint);
2819 renderer.setupDrawDirtyRegionsDisabled();
2820 renderer.setupDrawWithTexture(true);
2821 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2822 renderer.setupDrawColorFilter();
2823 renderer.setupDrawShader();
2824 renderer.setupDrawBlending(true, mode);
2825 renderer.setupDrawProgram();
2826 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2827 // Calling setupDrawTexture with the name 0 will enable the
2828 // uv attributes and increase the texture unit count
2829 // texture binding will be performed by the font renderer as
2830 // needed
2831 renderer.setupDrawTexture(0);
2832 renderer.setupDrawPureColorUniforms();
2833 renderer.setupDrawColorFilterUniforms();
2834 renderer.setupDrawShaderUniforms(pureTranslate);
2835 renderer.setupDrawTextGammaUniforms();
2836
2837 return NO_ERROR;
2838 }
2839
2840 OpenGLRenderer& renderer;
2841 float x;
2842 float y;
2843 bool pureTranslate;
2844 int alpha;
2845 SkXfermode::Mode mode;
2846 SkPaint* paint;
2847};
2848
Chet Haase48659092012-05-31 15:21:51 -07002849status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002850 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002851 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002852 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002853 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002854
Romain Guy671d6cf2012-01-18 12:39:17 -08002855 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002856 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002857 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002858 }
2859
Chris Craik39a908c2013-06-13 14:39:01 -07002860 mCaches.enableScissor();
2861
Romain Guy671d6cf2012-01-18 12:39:17 -08002862 float x = 0.0f;
2863 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002864 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002865 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002866 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2867 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002868 }
2869
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002870 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002871 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002872
2873 int alpha;
2874 SkXfermode::Mode mode;
2875 getAlphaAndMode(paint, &alpha, &mode);
2876
Chris Craikc3566d02013-02-04 16:16:33 -08002877 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002878 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2879 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002880 }
2881
Romain Guy671d6cf2012-01-18 12:39:17 -08002882 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002883 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002884 if (pureTranslate && !linearFilter) {
2885 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2886 }
Romain Guy257ae352013-03-20 16:31:12 -07002887 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002888
2889 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2890 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2891
Romain Guy211370f2012-02-01 16:10:55 -08002892 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002893
Romain Guy257ae352013-03-20 16:31:12 -07002894 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002895 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002896 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002897 if (hasActiveLayer) {
2898 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002899 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002900 }
2901 dirtyLayerUnchecked(bounds, getRegion());
2902 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 }
Chet Haase48659092012-05-31 15:21:51 -07002904
2905 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002906}
2907
Romain Guy624234f2013-03-05 16:43:31 -08002908mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2909 mat4 fontTransform;
2910 if (CC_LIKELY(transform.isPureTranslate())) {
2911 fontTransform = mat4::identity();
2912 } else {
2913 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002914 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002915 } else {
2916 float sx, sy;
2917 currentTransform().decomposeScale(sx, sy);
2918 fontTransform.loadScale(sx, sy, 1.0f);
2919 }
2920 }
2921 return fontTransform;
2922}
2923
Chris Craik41541822013-05-03 16:35:54 -07002924status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2925 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002926 DrawOpMode drawOpMode) {
2927
Chris Craik527a3aa2013-03-04 10:19:31 -08002928 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002929 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2930 // drawing as ops from DeferredDisplayList are already filtered for these
2931 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2932 quickReject(bounds)) {
2933 return DrawGlInfo::kStatusDone;
2934 }
Romain Guycac5fd32011-12-01 20:08:50 -08002935 }
2936
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002937 const float oldX = x;
2938 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002939
2940 const mat4& transform = currentTransform();
2941 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002942
Romain Guy211370f2012-02-01 16:10:55 -08002943 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002944 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2945 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002946 }
2947
Romain Guy86568192010-12-14 15:55:39 -08002948 int alpha;
2949 SkXfermode::Mode mode;
2950 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002951
Romain Guyc74f45a2013-02-26 19:10:14 -08002952 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2953
Chris Craikc3566d02013-02-04 16:16:33 -08002954 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002955 fontRenderer.setFont(paint, mat4::identity());
2956 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2957 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002958 }
2959
Romain Guy19d4dd82013-03-04 11:14:26 -08002960 const bool hasActiveLayer = hasLayer();
2961
Romain Guy624234f2013-03-05 16:43:31 -08002962 // We only pass a partial transform to the font renderer. That partial
2963 // matrix defines how glyphs are rasterized. Typically we want glyphs
2964 // to be rasterized at their final size on screen, which means the partial
2965 // matrix needs to take the scale factor into account.
2966 // When a partial matrix is used to transform glyphs during rasterization,
2967 // the mesh is generated with the inverse transform (in the case of scale,
2968 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2969 // apply the full transform matrix at draw time in the vertex shader.
2970 // Applying the full matrix in the shader is the easiest way to handle
2971 // rotation and perspective and allows us to always generated quads in the
2972 // font renderer which greatly simplifies the code, clipping in particular.
2973 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002974 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002975
Romain Guy6620c6d2010-12-06 18:07:02 -08002976 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002977 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002978 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002979
Romain Guy624234f2013-03-05 16:43:31 -08002980 // TODO: Implement better clipping for scaled/rotated text
2981 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002982 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 -07002983
Raph Levien996e57c2012-07-23 15:22:52 -07002984 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002985 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002986
2987 // don't call issuedrawcommand, do it at end of batch
2988 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002989 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002990 SkPaint paintCopy(*paint);
2991 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2992 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002993 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002994 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002995 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002996 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002997 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002998
Chris Craik527a3aa2013-03-04 10:19:31 -08002999 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003000 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003001 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003002 }
Chris Craik41541822013-05-03 16:35:54 -07003003 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003004 }
Romain Guy694b5192010-07-21 21:33:20 -07003005
Chris Craik41541822013-05-03 16:35:54 -07003006 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003007
3008 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003009}
3010
Chet Haase48659092012-05-31 15:21:51 -07003011status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003012 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003013 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003014 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003015 }
3016
Chris Craik39a908c2013-06-13 14:39:01 -07003017 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3018 mCaches.enableScissor();
3019
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003020 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003021 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003022 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003023
3024 int alpha;
3025 SkXfermode::Mode mode;
3026 getAlphaAndMode(paint, &alpha, &mode);
3027
Romain Guy03d58522012-02-24 17:54:07 -08003028 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07003029 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08003030 setupDrawDirtyRegionsDisabled();
3031 setupDrawWithTexture(true);
3032 setupDrawAlpha8Color(paint->getColor(), alpha);
3033 setupDrawColorFilter();
3034 setupDrawShader();
3035 setupDrawBlending(true, mode);
3036 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08003037 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07003038 // Calling setupDrawTexture with the name 0 will enable the
3039 // uv attributes and increase the texture unit count
3040 // texture binding will be performed by the font renderer as
3041 // needed
3042 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003043 setupDrawPureColorUniforms();
3044 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003045 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003046 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003047
Romain Guy97771732012-02-28 18:17:02 -08003048 const Rect* clip = &mSnapshot->getLocalClip();
3049 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 -08003050
Romain Guy97771732012-02-28 18:17:02 -08003051 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003052
3053 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3054 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003055 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003056 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003057 dirtyLayerUnchecked(bounds, getRegion());
3058 }
Romain Guy97771732012-02-28 18:17:02 -08003059 }
Chet Haase48659092012-05-31 15:21:51 -07003060
3061 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003062}
3063
Chet Haase48659092012-05-31 15:21:51 -07003064status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3065 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003066
Romain Guya1d3c912011-12-13 14:55:06 -08003067 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003068
Romain Guyfb8b7632010-08-23 21:05:08 -07003069 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003070 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003071 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003072
Romain Guy8b55f372010-08-18 17:10:07 -07003073 const float x = texture->left - texture->offset;
3074 const float y = texture->top - texture->offset;
3075
Romain Guy01d58e42011-01-19 21:54:02 -08003076 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003077
3078 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003079}
3080
Chris Craika08f95c2013-03-15 17:24:33 -07003081status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003082 if (!layer) {
3083 return DrawGlInfo::kStatusDone;
3084 }
3085
Romain Guyb2e2f242012-10-17 18:18:35 -07003086 mat4* transform = NULL;
3087 if (layer->isTextureLayer()) {
3088 transform = &layer->getTransform();
3089 if (!transform->isIdentity()) {
3090 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003091 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003092 }
3093 }
3094
Chris Craik39a908c2013-06-13 14:39:01 -07003095 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003096 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik39a908c2013-06-13 14:39:01 -07003097 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003098
3099 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003100 if (transform && !transform->isIdentity()) {
3101 restore();
3102 }
Chet Haase48659092012-05-31 15:21:51 -07003103 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003104 }
3105
Romain Guy5bb3c732012-11-29 17:52:58 -08003106 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003107
Chris Craik39a908c2013-06-13 14:39:01 -07003108 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003109 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003110
Romain Guy211370f2012-02-01 16:10:55 -08003111 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003112 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3113 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003114
Romain Guyc88e3572011-01-22 00:32:12 -08003115 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003116 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3117 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003118 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003119 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003120 setupDraw();
3121 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003122 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003123 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003124 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003125 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003126 setupDrawPureColorUniforms();
3127 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003128 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003129 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3130 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3131 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003132
Romain Guyd21b6e12011-11-30 20:21:23 -08003133 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003134 setupDrawModelViewTranslate(tx, ty,
3135 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003136 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003137 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003138 setupDrawModelViewTranslate(x, y,
3139 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3140 }
Romain Guyc88e3572011-01-22 00:32:12 -08003141 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003142
Chris Craik34416ea2013-04-15 16:08:28 -07003143 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3144 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3145 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003146
Romain Guyc88e3572011-01-22 00:32:12 -08003147 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003148
3149#if DEBUG_LAYERS_AS_REGIONS
3150 drawRegionRects(layer->region);
3151#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003152 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003153
Chris Craikc3566d02013-02-04 16:16:33 -08003154 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003155
Romain Guy5bb3c732012-11-29 17:52:58 -08003156 if (layer->debugDrawUpdate) {
3157 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003158 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3159 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3160 }
Romain Guyf219da52011-01-16 12:54:25 -08003161 }
Chris Craik34416ea2013-04-15 16:08:28 -07003162 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003163
Romain Guyb2e2f242012-10-17 18:18:35 -07003164 if (transform && !transform->isIdentity()) {
3165 restore();
3166 }
3167
Chet Haase48659092012-05-31 15:21:51 -07003168 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003169}
3170
Romain Guy6926c722010-07-12 20:20:03 -07003171///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003172// Shaders
3173///////////////////////////////////////////////////////////////////////////////
3174
3175void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003176 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003177}
3178
Romain Guy06f96e22010-07-30 19:18:16 -07003179void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003180 mDrawModifiers.mShader = shader;
3181 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003182 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003183 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003184}
3185
Romain Guyd27977d2010-07-14 19:18:51 -07003186///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003187// Color filters
3188///////////////////////////////////////////////////////////////////////////////
3189
3190void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003191 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003192}
3193
3194void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003195 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003196}
3197
3198///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003199// Drop shadow
3200///////////////////////////////////////////////////////////////////////////////
3201
3202void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003203 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003204}
3205
3206void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003207 mDrawModifiers.mHasShadow = true;
3208 mDrawModifiers.mShadowRadius = radius;
3209 mDrawModifiers.mShadowDx = dx;
3210 mDrawModifiers.mShadowDy = dy;
3211 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003212}
3213
3214///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003215// Draw filters
3216///////////////////////////////////////////////////////////////////////////////
3217
3218void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003219 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3220 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003221 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003222 mDrawModifiers.mPaintFilterClearBits = 0;
3223 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003224}
3225
3226void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003227 mDrawModifiers.mHasDrawFilter = true;
3228 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3229 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003230}
3231
Chris Craika08f95c2013-03-15 17:24:33 -07003232SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003233 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003234 return paint;
3235 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003236
3237 uint32_t flags = paint->getFlags();
3238
3239 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003240 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3241 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003242
3243 return &mFilteredPaint;
3244}
3245
3246///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003247// Drawing implementation
3248///////////////////////////////////////////////////////////////////////////////
3249
Romain Guy3b748a42013-04-17 18:54:38 -07003250Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3251 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3252 if (!texture) {
3253 return mCaches.textureCache.get(bitmap);
3254 }
3255 return texture;
3256}
3257
Romain Guy01d58e42011-01-19 21:54:02 -08003258void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3259 float x, float y, SkPaint* paint) {
3260 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3261 return;
3262 }
3263
3264 int alpha;
3265 SkXfermode::Mode mode;
3266 getAlphaAndMode(paint, &alpha, &mode);
3267
3268 setupDraw();
3269 setupDrawWithTexture(true);
3270 setupDrawAlpha8Color(paint->getColor(), alpha);
3271 setupDrawColorFilter();
3272 setupDrawShader();
3273 setupDrawBlending(true, mode);
3274 setupDrawProgram();
3275 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3276 setupDrawTexture(texture->id);
3277 setupDrawPureColorUniforms();
3278 setupDrawColorFilterUniforms();
3279 setupDrawShaderUniforms();
3280 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3281
3282 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3283
3284 finishDrawTexture();
3285}
3286
Romain Guyf607bdc2010-09-10 19:20:06 -07003287// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003288#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3289#define kStdUnderline_Offset (1.0f / 9.0f)
3290#define kStdUnderline_Thickness (1.0f / 18.0f)
3291
Chris Craik41541822013-05-03 16:35:54 -07003292void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003293 float x, float y, SkPaint* paint) {
3294 // Handle underline and strike-through
3295 uint32_t flags = paint->getFlags();
3296 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003297 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003298
Romain Guy211370f2012-02-01 16:10:55 -08003299 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003300 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003301 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003302
Raph Levien8b4072d2012-07-30 15:50:00 -07003303 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003304 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003305
Romain Guyf6834472011-01-23 13:32:12 -08003306 int linesCount = 0;
3307 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3308 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3309
3310 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003311 float points[pointsCount];
3312 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003313
3314 if (flags & SkPaint::kUnderlineText_Flag) {
3315 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003316 points[currentPoint++] = left;
3317 points[currentPoint++] = top;
3318 points[currentPoint++] = left + underlineWidth;
3319 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003320 }
3321
3322 if (flags & SkPaint::kStrikeThruText_Flag) {
3323 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003324 points[currentPoint++] = left;
3325 points[currentPoint++] = top;
3326 points[currentPoint++] = left + underlineWidth;
3327 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003328 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003329
Romain Guy726aeba2011-06-01 14:52:00 -07003330 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003331
Romain Guy726aeba2011-06-01 14:52:00 -07003332 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003333 }
3334 }
3335}
3336
Romain Guy672433d2013-01-04 19:05:13 -08003337status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3338 if (mSnapshot->isIgnored()) {
3339 return DrawGlInfo::kStatusDone;
3340 }
3341
Romain Guy735738c2012-12-03 12:34:51 -08003342 int color = paint->getColor();
3343 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003344 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003345 color |= 0x00ffffff;
3346 }
3347 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3348
3349 return drawColorRects(rects, count, color, mode);
3350}
3351
3352status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003353 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003354 if (count == 0) {
3355 return DrawGlInfo::kStatusDone;
3356 }
Romain Guy735738c2012-12-03 12:34:51 -08003357
Romain Guy672433d2013-01-04 19:05:13 -08003358 float left = FLT_MAX;
3359 float top = FLT_MAX;
3360 float right = FLT_MIN;
3361 float bottom = FLT_MIN;
3362
3363 int vertexCount = 0;
3364 Vertex mesh[count * 6];
3365 Vertex* vertex = mesh;
3366
Chris Craik2af46352012-11-26 18:30:17 -08003367 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003368 float l = rects[index + 0];
3369 float t = rects[index + 1];
3370 float r = rects[index + 2];
3371 float b = rects[index + 3];
3372
Romain Guy3b753822013-03-05 10:27:35 -08003373 Vertex::set(vertex++, l, b);
3374 Vertex::set(vertex++, l, t);
3375 Vertex::set(vertex++, r, t);
3376 Vertex::set(vertex++, l, b);
3377 Vertex::set(vertex++, r, t);
3378 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003379
Romain Guy3b753822013-03-05 10:27:35 -08003380 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003381
Romain Guy3b753822013-03-05 10:27:35 -08003382 left = fminf(left, l);
3383 top = fminf(top, t);
3384 right = fmaxf(right, r);
3385 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003386 }
3387
Romain Guy3b753822013-03-05 10:27:35 -08003388 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003389 return DrawGlInfo::kStatusDone;
3390 }
Romain Guy672433d2013-01-04 19:05:13 -08003391
Romain Guy672433d2013-01-04 19:05:13 -08003392 setupDraw();
3393 setupDrawNoTexture();
3394 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3395 setupDrawShader();
3396 setupDrawColorFilter();
3397 setupDrawBlending(mode);
3398 setupDrawProgram();
3399 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003400 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003401 setupDrawColorUniforms();
3402 setupDrawShaderUniforms();
3403 setupDrawColorFilterUniforms();
3404 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3405
Romain Guy8ce00302013-01-15 18:51:42 -08003406 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003407 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003408 }
3409
3410 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3411
3412 return DrawGlInfo::kStatusDrew;
3413}
3414
Romain Guy026c5e162010-06-28 17:12:22 -07003415void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003416 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003417 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003418 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003419 color |= 0x00ffffff;
3420 }
3421
Romain Guy70ca14e2010-12-13 18:24:33 -08003422 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003423 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003424 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003425 setupDrawShader();
3426 setupDrawColorFilter();
3427 setupDrawBlending(mode);
3428 setupDrawProgram();
3429 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3430 setupDrawColorUniforms();
3431 setupDrawShaderUniforms(ignoreTransform);
3432 setupDrawColorFilterUniforms();
3433 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003434
Romain Guyc95c8d62010-09-17 15:31:32 -07003435 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3436}
3437
Romain Guy82ba8142010-07-09 13:25:56 -07003438void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003439 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003440 int alpha;
3441 SkXfermode::Mode mode;
3442 getAlphaAndMode(paint, &alpha, &mode);
3443
Romain Guyd21b6e12011-11-30 20:21:23 -08003444 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003445
Romain Guy3b748a42013-04-17 18:54:38 -07003446 GLvoid* vertices = (GLvoid*) NULL;
3447 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3448
3449 if (texture->uvMapper) {
3450 vertices = &mMeshVertices[0].position[0];
3451 texCoords = &mMeshVertices[0].texture[0];
3452
3453 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3454 texture->uvMapper->map(uvs);
3455
3456 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3457 }
3458
Romain Guy3b753822013-03-05 10:27:35 -08003459 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3460 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3461 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003462
Romain Guyd21b6e12011-11-30 20:21:23 -08003463 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003464 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003465 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3466 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003467 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003468 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003469 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003470 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3471 }
3472
3473 if (texture->uvMapper) {
3474 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003475 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003476}
3477
Romain Guybd6b79b2010-06-26 00:13:53 -07003478void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003479 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3480 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003481 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003482}
3483
3484void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003485 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003486 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003487 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003488
Romain Guy746b7402010-10-26 16:27:31 -07003489 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003490 setupDrawWithTexture();
3491 setupDrawColor(alpha, alpha, alpha, alpha);
3492 setupDrawColorFilter();
3493 setupDrawBlending(blend, mode, swapSrcDst);
3494 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003495 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003496 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003497 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003498 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003499 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003500 }
Romain Guy886b2752013-01-04 12:26:18 -08003501 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003502 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003503 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003504 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003505
Romain Guy6820ac82010-09-15 18:11:50 -07003506 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003507
3508 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003509}
3510
Romain Guy3b748a42013-04-17 18:54:38 -07003511void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3512 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3513 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3514 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3515
3516 setupDraw();
3517 setupDrawWithTexture();
3518 setupDrawColor(alpha, alpha, alpha, alpha);
3519 setupDrawColorFilter();
3520 setupDrawBlending(blend, mode, swapSrcDst);
3521 setupDrawProgram();
3522 if (!dirty) setupDrawDirtyRegionsDisabled();
3523 if (!ignoreScale) {
3524 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3525 } else {
3526 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3527 }
3528 setupDrawTexture(texture);
3529 setupDrawPureColorUniforms();
3530 setupDrawColorFilterUniforms();
3531 setupDrawMeshIndices(vertices, texCoords, vbo);
3532
3533 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3534
3535 finishDrawTexture();
3536}
3537
Romain Guy886b2752013-01-04 12:26:18 -08003538void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3539 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3540 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003541 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003542
3543 setupDraw();
3544 setupDrawWithTexture(true);
3545 if (hasColor) {
3546 setupDrawAlpha8Color(color, alpha);
3547 }
3548 setupDrawColorFilter();
3549 setupDrawShader();
3550 setupDrawBlending(true, mode);
3551 setupDrawProgram();
3552 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003553 if (!ignoreScale) {
3554 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3555 } else {
3556 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3557 }
Romain Guy886b2752013-01-04 12:26:18 -08003558 setupDrawTexture(texture);
3559 setupDrawPureColorUniforms();
3560 setupDrawColorFilterUniforms();
3561 setupDrawShaderUniforms();
3562 setupDrawMesh(vertices, texCoords);
3563
3564 glDrawArrays(drawMode, 0, elementsCount);
3565
3566 finishDrawTexture();
3567}
3568
Romain Guya5aed0d2010-09-09 14:42:43 -07003569void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003570 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003571 if (mCountOverdraw) {
3572 if (!mCaches.blend) glEnable(GL_BLEND);
3573 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3574 glBlendFunc(GL_ONE, GL_ONE);
3575 }
3576
3577 mCaches.blend = true;
3578 mCaches.lastSrcMode = GL_ONE;
3579 mCaches.lastDstMode = GL_ONE;
3580
3581 return;
3582 }
3583
Romain Guy82ba8142010-07-09 13:25:56 -07003584 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003585
Romain Guy82ba8142010-07-09 13:25:56 -07003586 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003587 // These blend modes are not supported by OpenGL directly and have
3588 // to be implemented using shaders. Since the shader will perform
3589 // the blending, turn blending off here
3590 // If the blend mode cannot be implemented using shaders, fall
3591 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003592 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003593 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003594 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003595 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003596
Romain Guy82bc7a72012-01-03 14:13:39 -08003597 if (mCaches.blend) {
3598 glDisable(GL_BLEND);
3599 mCaches.blend = false;
3600 }
3601
3602 return;
3603 } else {
3604 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003605 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003606 }
3607
3608 if (!mCaches.blend) {
3609 glEnable(GL_BLEND);
3610 }
3611
3612 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3613 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3614
3615 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3616 glBlendFunc(sourceMode, destMode);
3617 mCaches.lastSrcMode = sourceMode;
3618 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003619 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003620 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003621 glDisable(GL_BLEND);
3622 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003623 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003624}
3625
Romain Guy889f8d12010-07-29 14:37:42 -07003626bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003627 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003628 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003629 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003630 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003631 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003632 }
Romain Guy6926c722010-07-12 20:20:03 -07003633 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003634}
3635
Romain Guy026c5e162010-06-28 17:12:22 -07003636void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003637 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003638 TextureVertex::setUV(v++, u1, v1);
3639 TextureVertex::setUV(v++, u2, v1);
3640 TextureVertex::setUV(v++, u1, v2);
3641 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003642}
3643
Chris Craik16ecda52013-03-29 10:59:59 -07003644void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003645 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003646 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3647 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003648 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003649 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003650 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003651}
3652
Chris Craik16ecda52013-03-29 10:59:59 -07003653float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3654 float alpha;
3655 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3656 alpha = mDrawModifiers.mOverrideLayerAlpha;
3657 } else {
3658 alpha = layer->getAlpha() / 255.0f;
3659 }
3660 return alpha * mSnapshot->alpha;
3661}
3662
Romain Guy9d5316e2010-06-24 19:30:36 -07003663}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003664}; // namespace android