blob: bc28d655aec4a76b76fcc26ed42f699c934f09f4 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700115 mDrawModifiers.mShader = NULL;
116 mDrawModifiers.mColorFilter = NULL;
117 mDrawModifiers.mHasShadow = false;
118 mDrawModifiers.mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700119
Romain Guyac670c02010-07-27 17:39:27 -0700120 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
121
Romain Guyae5575b2010-07-29 18:48:04 -0700122 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700123
124 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700128 // The context has already been destroyed at this point, do not call
129 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700130}
131
Romain Guy87e2f7572012-09-24 11:37:12 -0700132void OpenGLRenderer::initProperties() {
133 char property[PROPERTY_VALUE_MAX];
134 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
135 mScissorOptimizationDisabled = !strcasecmp(property, "true");
136 INIT_LOGD(" Scissor optimization %s",
137 mScissorOptimizationDisabled ? "disabled" : "enabled");
138 } else {
139 INIT_LOGD(" Scissor optimization enabled");
140 }
Romain Guy13631f32012-01-30 17:41:55 -0800141}
142
143///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700144// Setup
145///////////////////////////////////////////////////////////////////////////////
146
Romain Guyef359272013-01-31 19:07:29 -0800147void OpenGLRenderer::setName(const char* name) {
148 if (name) {
149 mName.setTo(name);
150 } else {
151 mName.clear();
152 }
153}
154
155const char* OpenGLRenderer::getName() const {
156 return mName.string();
157}
158
Romain Guy49c5fc02012-05-15 11:10:01 -0700159bool OpenGLRenderer::isDeferred() {
160 return false;
161}
162
Romain Guy85bf02f2010-06-22 13:11:24 -0700163void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700164 initViewport(width, height);
165
166 glDisable(GL_DITHER);
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168
169 glEnableVertexAttribArray(Program::kBindingPosition);
170}
171
172void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700174
175 mWidth = width;
176 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700177
178 mFirstSnapshot->height = height;
179 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700180}
181
Romain Guy7c25aab2012-10-18 15:05:02 -0700182status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700183 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800184}
185
Romain Guyc3fedaf2013-01-29 17:26:25 -0800186status_t OpenGLRenderer::prepareDirty(float left, float top,
187 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800188 mCaches.clearGarbage();
189
Romain Guy8aef54f2010-09-01 15:13:49 -0700190 mSnapshot = new Snapshot(mFirstSnapshot,
191 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800192 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700193 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700194
Romain Guy7d7b5492011-01-24 16:33:45 -0800195 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700196 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700197
Romain Guy11cb6422012-09-21 00:39:43 -0700198 updateLayers();
199
Romain Guydcfc8362013-01-03 13:08:57 -0800200 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700201
Romain Guyddf74372012-05-22 14:07:07 -0700202 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800203
Romain Guy54c1a642012-09-27 17:55:46 -0700204 // Functors break the tiling extension in pretty spectacular ways
205 // This ensures we don't use tiling when a functor is going to be
206 // invoked during the frame
207 mSuppressTiling = mCaches.hasRegisteredFunctors();
208
Romain Guy2b7028e2012-09-19 17:25:38 -0700209 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700210 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700211
Romain Guy7c450aa2012-09-21 19:15:00 -0700212 debugOverdraw(true, true);
213
Romain Guy7c25aab2012-10-18 15:05:02 -0700214 return clear(left, top, right, bottom, opaque);
215}
216
Romain Guydcfc8362013-01-03 13:08:57 -0800217void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
218 // If we know that we are going to redraw the entire framebuffer,
219 // perform a discard to let the driver know we don't need to preserve
220 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800221 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800222 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800223 const bool isFbo = getTargetFbo() == 0;
224 const GLenum attachments[] = {
225 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
226 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800227 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
228 }
229}
230
Romain Guy7c25aab2012-10-18 15:05:02 -0700231status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700232 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700233 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700234 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700235 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700236 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700237 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700238
Romain Guy7c25aab2012-10-18 15:05:02 -0700239 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700240 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700241}
242
243void OpenGLRenderer::syncState() {
244 glViewport(0, 0, mWidth, mHeight);
245
246 if (mCaches.blend) {
247 glEnable(GL_BLEND);
248 } else {
249 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700250 }
Romain Guybb9524b2010-06-22 18:56:38 -0700251}
252
Romain Guy57b52682012-09-20 17:38:46 -0700253void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700254 if (!mSuppressTiling) {
255 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800256 if (s->flags & Snapshot::kFlagFboTarget) {
257 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700258 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700259
Romain Guyc3fedaf2013-01-29 17:26:25 -0800260 startTiling(*clip, s->height, opaque);
261 }
262}
263
264void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
265 if (!mSuppressTiling) {
266 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800267 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700268 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700269}
270
271void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700272 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700273}
274
Romain Guyb025b9c2010-09-16 14:16:48 -0700275void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700276 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700277 endTiling();
278
Romain Guyca89e2a2013-03-08 17:44:20 -0800279 // When finish() is invoked on FBO 0 we've reached the end
280 // of the current frame
281 if (getTargetFbo() == 0) {
282 mCaches.pathCache.trim();
283 }
284
Romain Guy11cb6422012-09-21 00:39:43 -0700285 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700286#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700287 GLenum status = GL_NO_ERROR;
288 while ((status = glGetError()) != GL_NO_ERROR) {
289 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
290 switch (status) {
291 case GL_INVALID_ENUM:
292 ALOGE(" GL_INVALID_ENUM");
293 break;
294 case GL_INVALID_VALUE:
295 ALOGE(" GL_INVALID_VALUE");
296 break;
297 case GL_INVALID_OPERATION:
298 ALOGE(" GL_INVALID_OPERATION");
299 break;
300 case GL_OUT_OF_MEMORY:
301 ALOGE(" Out of memory!");
302 break;
303 }
Romain Guya07105b2011-01-10 21:14:18 -0800304 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700305#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700306
Romain Guyc15008e2010-11-10 11:59:15 -0800307#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800308 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700309#else
310 if (mCaches.getDebugLevel() & kDebugMemory) {
311 mCaches.dumpMemoryUsage();
312 }
Romain Guyc15008e2010-11-10 11:59:15 -0800313#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700314 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700315}
316
Romain Guy6c319ca2011-01-11 14:29:25 -0800317void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700318 if (mCaches.currentProgram) {
319 if (mCaches.currentProgram->isInUse()) {
320 mCaches.currentProgram->remove();
321 mCaches.currentProgram = NULL;
322 }
323 }
Romain Guy50c0f092010-10-19 11:42:22 -0700324 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800325 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800326 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800327 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700329}
330
Romain Guy6c319ca2011-01-11 14:29:25 -0800331void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800332 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800333 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700334 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700335 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700336
Romain Guy3e263fa2011-12-12 16:47:48 -0800337 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
338
Chet Haase80250612012-08-15 13:46:54 -0700339 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700340 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800341 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700342 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700343
Romain Guya1d3c912011-12-13 14:55:06 -0800344 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700345
Romain Guy50c0f092010-10-19 11:42:22 -0700346 mCaches.blend = true;
347 glEnable(GL_BLEND);
348 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
349 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700350}
351
Romain Guy35643dd2012-09-18 15:40:58 -0700352void OpenGLRenderer::resumeAfterLayer() {
353 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
354 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
355 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700356 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700357
358 mCaches.resetScissor();
359 dirtyClip();
360}
361
Romain Guyba6be8a2012-04-23 18:22:09 -0700362void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700363 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700364}
365
366void OpenGLRenderer::attachFunctor(Functor* functor) {
367 mFunctors.add(functor);
368}
369
Romain Guy8f3b8e32012-03-27 16:33:45 -0700370status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
371 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700372 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700373
Romain Guyba6be8a2012-04-23 18:22:09 -0700374 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800375 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700376 SortedVector<Functor*> functors(mFunctors);
377 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700378
Romain Guyba6be8a2012-04-23 18:22:09 -0700379 DrawGlInfo info;
380 info.clipLeft = 0;
381 info.clipTop = 0;
382 info.clipRight = 0;
383 info.clipBottom = 0;
384 info.isLayer = false;
385 info.width = 0;
386 info.height = 0;
387 memset(info.transform, 0, sizeof(float) * 16);
388
389 for (size_t i = 0; i < count; i++) {
390 Functor* f = functors.itemAt(i);
391 result |= (*f)(DrawGlInfo::kModeProcess, &info);
392
Chris Craikc2c95432012-04-25 15:13:52 -0700393 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700394 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
395 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700396 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700397
Chris Craikc2c95432012-04-25 15:13:52 -0700398 if (result & DrawGlInfo::kStatusInvoke) {
399 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700400 }
401 }
Chris Craikd15321b2012-11-28 14:45:04 -0800402 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700403 }
404
405 return result;
406}
407
408status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800409 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700410 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700411
Romain Guy8a4ac612012-07-17 17:32:48 -0700412 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800413 if (mDirtyClip) {
414 setScissorFromClip();
415 }
Romain Guyd643bb52011-03-01 14:55:21 -0800416
Romain Guy80911b82011-03-16 15:30:12 -0700417 Rect clip(*mSnapshot->clipRect);
418 clip.snapToPixelBoundaries();
419
Romain Guyd643bb52011-03-01 14:55:21 -0800420 // Since we don't know what the functor will draw, let's dirty
421 // tne entire clip region
422 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800423 dirtyLayerUnchecked(clip, getRegion());
424 }
Romain Guyd643bb52011-03-01 14:55:21 -0800425
Romain Guy08aa2cb2011-03-17 11:06:57 -0700426 DrawGlInfo info;
427 info.clipLeft = clip.left;
428 info.clipTop = clip.top;
429 info.clipRight = clip.right;
430 info.clipBottom = clip.bottom;
431 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700432 info.width = getSnapshot()->viewport.getWidth();
433 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700434 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700435
Chet Haase48659092012-05-31 15:21:51 -0700436 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800437
Romain Guy8f3b8e32012-03-27 16:33:45 -0700438 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700439 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800440 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441
Chris Craik65924a32012-04-05 17:52:11 -0700442 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700443 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
Romain Guycabfcc12011-03-07 18:06:46 -0800445 }
446
Chet Haasedaf98e92011-01-10 14:10:36 -0800447 resume();
Romain Guy65549432012-03-26 16:45:05 -0700448 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800449}
450
Romain Guyf6a11b82010-06-23 17:47:49 -0700451///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700452// Debug
453///////////////////////////////////////////////////////////////////////////////
454
Romain Guy0f6675332013-03-01 14:31:04 -0800455void OpenGLRenderer::eventMark(const char* name) const {
456 mCaches.eventMark(0, name);
457}
458
Romain Guy87e2f7572012-09-24 11:37:12 -0700459void OpenGLRenderer::startMark(const char* name) const {
460 mCaches.startMark(0, name);
461}
462
463void OpenGLRenderer::endMark() const {
464 mCaches.endMark();
465}
466
467void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
468 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
469 if (clear) {
470 mCaches.disableScissor();
471 mCaches.stencil.clear();
472 }
473 if (enable) {
474 mCaches.stencil.enableDebugWrite();
475 } else {
476 mCaches.stencil.disable();
477 }
478 }
479}
480
481void OpenGLRenderer::renderOverdraw() {
482 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
483 const Rect* clip = mTilingSnapshot->clipRect;
484
485 mCaches.enableScissor();
486 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
487 clip->right - clip->left, clip->bottom - clip->top);
488
489 mCaches.stencil.enableDebugTest(2);
490 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
491 mCaches.stencil.enableDebugTest(3);
492 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
493 mCaches.stencil.enableDebugTest(4);
494 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
495 mCaches.stencil.enableDebugTest(4, true);
496 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
497 mCaches.stencil.disable();
498 }
499}
500
501///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700502// Layers
503///////////////////////////////////////////////////////////////////////////////
504
505bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
506 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
507 OpenGLRenderer* renderer = layer->renderer;
508 Rect& dirty = layer->dirtyRect;
509
Romain Guy7c450aa2012-09-21 19:15:00 -0700510 if (inFrame) {
511 endTiling();
512 debugOverdraw(false, false);
513 }
Romain Guy11cb6422012-09-21 00:39:43 -0700514
515 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
516 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
517 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
518 renderer->finish();
519
520 if (inFrame) {
521 resumeAfterLayer();
522 startTiling(mSnapshot);
523 }
524
525 dirty.setEmpty();
526 layer->deferredUpdateScheduled = false;
527 layer->renderer = NULL;
528 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800529 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700530
531 return true;
532 }
533
534 return false;
535}
536
537void OpenGLRenderer::updateLayers() {
538 int count = mLayerUpdates.size();
539 if (count > 0) {
540 startMark("Layer Updates");
541
542 // Note: it is very important to update the layers in reverse order
543 for (int i = count - 1; i >= 0; i--) {
544 Layer* layer = mLayerUpdates.itemAt(i);
545 updateLayer(layer, false);
546 mCaches.resourceCache.decrementRefcount(layer);
547 }
548 mLayerUpdates.clear();
549
550 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
551 endMark();
552 }
553}
554
555void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
556 if (layer) {
557 mLayerUpdates.push_back(layer);
558 mCaches.resourceCache.incrementRefcount(layer);
559 }
560}
561
562void OpenGLRenderer::clearLayerUpdates() {
563 size_t count = mLayerUpdates.size();
564 if (count > 0) {
565 mCaches.resourceCache.lock();
566 for (size_t i = 0; i < count; i++) {
567 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
568 }
569 mCaches.resourceCache.unlock();
570 mLayerUpdates.clear();
571 }
572}
573
574///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700575// State management
576///////////////////////////////////////////////////////////////////////////////
577
Romain Guybb9524b2010-06-22 18:56:38 -0700578int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700579 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700580}
581
582int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700583 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700584}
585
586void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700587 if (mSaveCount > 1) {
588 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700589 }
Romain Guybb9524b2010-06-22 18:56:38 -0700590}
591
592void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700593 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700594
Romain Guy8fb95422010-08-17 18:38:51 -0700595 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700596 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700597 }
Romain Guybb9524b2010-06-22 18:56:38 -0700598}
599
Romain Guy8aef54f2010-09-01 15:13:49 -0700600int OpenGLRenderer::saveSnapshot(int flags) {
601 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700602 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700603}
604
605bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700606 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700607 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700608 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700609
Romain Guybd6b79b2010-06-26 00:13:53 -0700610 sp<Snapshot> current = mSnapshot;
611 sp<Snapshot> previous = mSnapshot->previous;
612
Romain Guyeb993562010-10-05 18:14:38 -0700613 if (restoreOrtho) {
614 Rect& r = previous->viewport;
615 glViewport(r.left, r.top, r.right, r.bottom);
616 mOrthoMatrix.load(current->orthoMatrix);
617 }
618
Romain Guy8b55f372010-08-18 17:10:07 -0700619 mSaveCount--;
620 mSnapshot = previous;
621
Romain Guy2542d192010-08-18 11:47:12 -0700622 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700623 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700624 }
Romain Guy2542d192010-08-18 11:47:12 -0700625
Romain Guy5ec99242010-11-03 16:19:08 -0700626 if (restoreLayer) {
627 composeLayer(current, previous);
628 }
629
Romain Guy2542d192010-08-18 11:47:12 -0700630 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700631}
632
Romain Guyf6a11b82010-06-23 17:47:49 -0700633///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700634// Layers
635///////////////////////////////////////////////////////////////////////////////
636
637int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800638 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700639 const GLuint previousFbo = mSnapshot->fbo;
640 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700641
Romain Guyaf636eb2010-12-09 17:47:21 -0800642 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700643 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700644 }
Romain Guyd55a8612010-06-28 17:42:46 -0700645
646 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700647}
648
Chris Craikd90144d2013-03-19 15:03:48 -0700649void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
650 const Rect untransformedBounds(bounds);
651
652 currentTransform().mapRect(bounds);
653
654 // Layers only make sense if they are in the framebuffer's bounds
655 if (bounds.intersect(*mSnapshot->clipRect)) {
656 // We cannot work with sub-pixels in this case
657 bounds.snapToPixelBoundaries();
658
659 // When the layer is not an FBO, we may use glCopyTexImage so we
660 // need to make sure the layer does not extend outside the bounds
661 // of the framebuffer
662 if (!bounds.intersect(mSnapshot->previous->viewport)) {
663 bounds.setEmpty();
664 } else if (fboLayer) {
665 clip.set(bounds);
666 mat4 inverse;
667 inverse.loadInverse(currentTransform());
668 inverse.mapRect(clip);
669 clip.snapToPixelBoundaries();
670 if (clip.intersect(untransformedBounds)) {
671 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
672 bounds.set(untransformedBounds);
673 } else {
674 clip.setEmpty();
675 }
676 }
677 } else {
678 bounds.setEmpty();
679 }
680}
681
682int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
683 int alpha, SkXfermode::Mode mode, int flags) {
684 const GLuint previousFbo = mSnapshot->fbo;
685 const int count = saveSnapshot(flags);
686
687 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
688 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
689 // operations will be able to store and restore the current clip and transform info, and
690 // quick rejection will be correct (for display lists)
691
692 Rect bounds(left, top, right, bottom);
693 Rect clip;
694 calculateLayerBoundsAndClip(bounds, clip, true);
695
696 if (!bounds.isEmpty() && !clip.isEmpty()) {
697 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
698 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
699 }
700 }
701
702 return count;
703}
704
705
Romain Guy1c740bc2010-09-13 18:00:09 -0700706/**
707 * Layers are viewed by Skia are slightly different than layers in image editing
708 * programs (for instance.) When a layer is created, previously created layers
709 * and the frame buffer still receive every drawing command. For instance, if a
710 * layer is created and a shape intersecting the bounds of the layers and the
711 * framebuffer is draw, the shape will be drawn on both (unless the layer was
712 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
713 *
714 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
715 * texture. Unfortunately, this is inefficient as it requires every primitive to
716 * be drawn n + 1 times, where n is the number of active layers. In practice this
717 * means, for every primitive:
718 * - Switch active frame buffer
719 * - Change viewport, clip and projection matrix
720 * - Issue the drawing
721 *
722 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700723 * To avoid this, layers are implemented in a different way here, at least in the
724 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
725 * is set. When this flag is set we can redirect all drawing operations into a
726 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700727 *
728 * This implementation relies on the frame buffer being at least RGBA 8888. When
729 * a layer is created, only a texture is created, not an FBO. The content of the
730 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700731 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700732 * buffer and drawing continues as normal. This technique therefore treats the
733 * frame buffer as a scratch buffer for the layers.
734 *
735 * To compose the layers back onto the frame buffer, each layer texture
736 * (containing the original frame buffer data) is drawn as a simple quad over
737 * the frame buffer. The trick is that the quad is set as the composition
738 * destination in the blending equation, and the frame buffer becomes the source
739 * of the composition.
740 *
741 * Drawing layers with an alpha value requires an extra step before composition.
742 * An empty quad is drawn over the layer's region in the frame buffer. This quad
743 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
744 * quad is used to multiply the colors in the frame buffer. This is achieved by
745 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
746 * GL_ZERO, GL_SRC_ALPHA.
747 *
748 * Because glCopyTexImage2D() can be slow, an alternative implementation might
749 * be use to draw a single clipped layer. The implementation described above
750 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700751 *
752 * (1) The frame buffer is actually not cleared right away. To allow the GPU
753 * to potentially optimize series of calls to glCopyTexImage2D, the frame
754 * buffer is left untouched until the first drawing operation. Only when
755 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700756 */
Chet Haased48885a2012-08-28 17:43:28 -0700757bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
758 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700759 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700760 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700761
Romain Guyeb993562010-10-05 18:14:38 -0700762 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
763
Romain Guyf607bdc2010-09-10 19:20:06 -0700764 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700765 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700767 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Romain Guybf434112010-09-16 14:40:17 -0700768
Romain Guy746b7402010-10-26 16:27:31 -0700769 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700770 bounds.getHeight() > mCaches.maxTextureSize ||
771 (fboLayer && clip.isEmpty())) {
772 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700773 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700774 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700775 }
776
777 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700778 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700779 return false;
780 }
Romain Guyf18fd992010-07-08 11:45:51 -0700781
Romain Guya1d3c912011-12-13 14:55:06 -0800782 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700783 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700784 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700785 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700786 }
787
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700789 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700790 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
791 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800792 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700793 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700794 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700795
Romain Guy8fb95422010-08-17 18:38:51 -0700796 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700797 mSnapshot->flags |= Snapshot::kFlagIsLayer;
798 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700799
Romain Guyeb993562010-10-05 18:14:38 -0700800 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700801 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700802 } else {
803 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700804 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800805 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 if (layer->isEmpty()) {
807 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700808 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 layer->getWidth(), layer->getHeight(), 0);
810 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800811 } else {
812 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700813 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800814 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700815
Romain Guy54be1cd2011-06-13 19:04:27 -0700816 // Enqueue the buffer coordinates to clear the corresponding region later
817 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700818 }
Romain Guyeb993562010-10-05 18:14:38 -0700819 }
Romain Guyf86ef572010-07-01 11:05:42 -0700820
Romain Guyd55a8612010-06-28 17:42:46 -0700821 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700822}
823
Chet Haased48885a2012-08-28 17:43:28 -0700824bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800825 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700827
Chet Haased48885a2012-08-28 17:43:28 -0700828 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800829 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
830 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700831 mSnapshot->fbo = layer->getFbo();
832 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
833 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
834 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
835 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700836 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700837
Romain Guy2b7028e2012-09-19 17:25:38 -0700838 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700839 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
842 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700843
844 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700845 if (layer->isEmpty()) {
846 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
847 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 }
849
850 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700852
Romain Guyf735c8e2013-01-31 17:45:55 -0800853 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700854
855 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700856 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800857 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700858 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 glClear(GL_COLOR_BUFFER_BIT);
860
861 dirtyClip();
862
863 // Change the ortho projection
864 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
865 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
866
867 return true;
868}
869
Romain Guy1c740bc2010-09-13 18:00:09 -0700870/**
871 * Read the documentation of createLayer() before doing anything in this method.
872 */
Romain Guy1d83e192010-08-17 11:37:00 -0700873void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
874 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000875 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700876 return;
877 }
878
Romain Guy8ce00302013-01-15 18:51:42 -0800879 Layer* layer = current->layer;
880 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700881 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700882
883 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700884 endTiling();
885
Romain Guye0aa84b2012-04-03 19:30:26 -0700886 // Detach the texture from the FBO
887 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800888
889 layer->removeFbo(false);
890
Romain Guyeb993562010-10-05 18:14:38 -0700891 // Unbind current FBO and restore previous one
892 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700893 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700894
895 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700896 }
897
Romain Guy9ace8f52011-07-07 20:50:11 -0700898 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700899 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700900 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700903 }
904
Romain Guy03750a02010-10-18 14:06:08 -0700905 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700906
Romain Guya1d3c912011-12-13 14:55:06 -0800907 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700908
Romain Guy5b3b3522010-10-27 18:57:51 -0700909 // When the layer is stored in an FBO, we can save a bit of fillrate by
910 // drawing only the dirty region
911 if (fboLayer) {
912 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 if (layer->getColorFilter()) {
914 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800915 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700916 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700917 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800918 resetColorFilter();
919 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700920 } else if (!rect.isEmpty()) {
921 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
922 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700923 }
Romain Guy8b55f372010-08-18 17:10:07 -0700924
Romain Guy746b7402010-10-26 16:27:31 -0700925 dirtyClip();
926
Romain Guyeb993562010-10-05 18:14:38 -0700927 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700928 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700929 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700930 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700931 }
932}
933
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -0700935 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936
937 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700938 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700939 setupDrawWithTexture();
940 } else {
941 setupDrawWithExternalTexture();
942 }
943 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700944 setupDrawColor(alpha, alpha, alpha, alpha);
945 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700947 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700948 setupDrawPureColorUniforms();
949 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700950 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
951 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700952 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700954 }
Romain Guy3b753822013-03-05 10:27:35 -0800955 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700956 layer->getWidth() == (uint32_t) rect.getWidth() &&
957 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -0800958 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
959 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700960
Romain Guyd21b6e12011-11-30 20:21:23 -0800961 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
963 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800964 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
966 }
967 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700968 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
969
970 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
971
972 finishDrawTexture();
973}
974
Romain Guy5b3b3522010-10-27 18:57:51 -0700975void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700976 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700977 const Rect& texCoords = layer->texCoords;
978 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
979 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700980
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 float x = rect.left;
982 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -0800983 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700984 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700985 layer->getHeight() == (uint32_t) rect.getHeight();
986
987 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700988 // When we're swapping, the layer is already in screen coordinates
989 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -0800990 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
991 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 }
993
Romain Guyd21b6e12011-11-30 20:21:23 -0800994 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800996 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 }
998
Chris Craike83569c2013-03-20 16:57:09 -0700999 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
1000 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001001 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001002 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001003 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1004 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
Romain Guyaa6c24c2011-04-28 18:40:04 -07001006 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1007 } else {
1008 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1009 drawTextureLayer(layer, rect);
1010 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1011 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001012}
1013
1014void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001015 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001016 layer->setRegionAsRect();
1017
Romain Guy40667672011-03-18 14:34:03 -07001018 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001019
Romain Guy5b3b3522010-10-27 18:57:51 -07001020 layer->region.clear();
1021 return;
1022 }
1023
Romain Guy8a3957d2011-09-07 17:55:15 -07001024 // TODO: See LayerRenderer.cpp::generateMesh() for important
1025 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001026 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001028 const android::Rect* rects;
1029 Region safeRegion;
1030 if (CC_LIKELY(hasRectToRectTransform())) {
1031 rects = layer->region.getArray(&count);
1032 } else {
1033 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1034 rects = safeRegion.getArray(&count);
1035 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001036
Chris Craike83569c2013-03-20 16:57:09 -07001037 const float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guy9ace8f52011-07-07 20:50:11 -07001038 const float texX = 1.0f / float(layer->getWidth());
1039 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001040 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001041
Romain Guy8ce00302013-01-15 18:51:42 -08001042 setupDraw();
1043
1044 // We must get (and therefore bind) the region mesh buffer
1045 // after we setup drawing in case we need to mess with the
1046 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001047 TextureVertex* mesh = mCaches.getRegionMesh();
1048 GLsizei numQuads = 0;
1049
Romain Guy7230a742011-01-10 22:26:16 -08001050 setupDrawWithTexture();
1051 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001052 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001054 setupDrawProgram();
1055 setupDrawDirtyRegionsDisabled();
1056 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001057 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001059 if (currentTransform().isPureTranslate()) {
1060 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1061 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001062
Romain Guyd21b6e12011-11-30 20:21:23 -08001063 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001064 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1065 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001066 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001067 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1068 }
Romain Guy15bc6432011-12-13 13:11:32 -08001069 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001070
1071 for (size_t i = 0; i < count; i++) {
1072 const android::Rect* r = &rects[i];
1073
1074 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001075 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001076 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001077 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001078
1079 // TODO: Reject quads outside of the clip
1080 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1081 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1082 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1083 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1084
1085 numQuads++;
1086
1087 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1088 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1089 numQuads = 0;
1090 mesh = mCaches.getRegionMesh();
1091 }
1092 }
1093
1094 if (numQuads > 0) {
1095 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1096 }
1097
Romain Guy7230a742011-01-10 22:26:16 -08001098 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001099
1100#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001101 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001102#endif
1103
1104 layer->region.clear();
1105 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001106}
1107
Romain Guy3a3133d2011-02-01 22:59:58 -08001108void OpenGLRenderer::drawRegionRects(const Region& region) {
1109#if DEBUG_LAYERS_AS_REGIONS
1110 size_t count;
1111 const android::Rect* rects = region.getArray(&count);
1112
1113 uint32_t colors[] = {
1114 0x7fff0000, 0x7f00ff00,
1115 0x7f0000ff, 0x7fff00ff,
1116 };
1117
1118 int offset = 0;
1119 int32_t top = rects[0].top;
1120
1121 for (size_t i = 0; i < count; i++) {
1122 if (top != rects[i].top) {
1123 offset ^= 0x2;
1124 top = rects[i].top;
1125 }
1126
1127 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1128 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1129 SkXfermode::kSrcOver_Mode);
1130 }
1131#endif
1132}
1133
Romain Guy8ce00302013-01-15 18:51:42 -08001134void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1135 SkXfermode::Mode mode, bool dirty) {
1136 int count = 0;
1137 Vector<float> rects;
1138
1139 SkRegion::Iterator it(region);
1140 while (!it.done()) {
1141 const SkIRect& r = it.rect();
1142 rects.push(r.fLeft);
1143 rects.push(r.fTop);
1144 rects.push(r.fRight);
1145 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001146 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001147 it.next();
1148 }
1149
Romain Guy3bbacf22013-02-06 16:51:04 -08001150 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001151}
1152
Romain Guy5b3b3522010-10-27 18:57:51 -07001153void OpenGLRenderer::dirtyLayer(const float left, const float top,
1154 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001155 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001156 Rect bounds(left, top, right, bottom);
1157 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001158 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001159 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001160}
1161
1162void OpenGLRenderer::dirtyLayer(const float left, const float top,
1163 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001164 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001165 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001166 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001167 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001168}
1169
1170void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001171 if (bounds.intersect(*mSnapshot->clipRect)) {
1172 bounds.snapToPixelBoundaries();
1173 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1174 if (!dirty.isEmpty()) {
1175 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 }
1177 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001178}
1179
Romain Guy54be1cd2011-06-13 19:04:27 -07001180void OpenGLRenderer::clearLayerRegions() {
1181 const size_t count = mLayers.size();
1182 if (count == 0) return;
1183
1184 if (!mSnapshot->isIgnored()) {
1185 // Doing several glScissor/glClear here can negatively impact
1186 // GPUs with a tiler architecture, instead we draw quads with
1187 // the Clear blending mode
1188
1189 // The list contains bounds that have already been clipped
1190 // against their initial clip rect, and the current clip
1191 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001192 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001193
1194 Vertex mesh[count * 6];
1195 Vertex* vertex = mesh;
1196
1197 for (uint32_t i = 0; i < count; i++) {
1198 Rect* bounds = mLayers.itemAt(i);
1199
1200 Vertex::set(vertex++, bounds->left, bounds->bottom);
1201 Vertex::set(vertex++, bounds->left, bounds->top);
1202 Vertex::set(vertex++, bounds->right, bounds->top);
1203 Vertex::set(vertex++, bounds->left, bounds->bottom);
1204 Vertex::set(vertex++, bounds->right, bounds->top);
1205 Vertex::set(vertex++, bounds->right, bounds->bottom);
1206
1207 delete bounds;
1208 }
Romain Guye67307c2013-02-11 18:01:20 -08001209 // We must clear the list of dirty rects before we
1210 // call setupDraw() to prevent stencil setup to do
1211 // the same thing again
1212 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001213
1214 setupDraw(false);
1215 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1216 setupDrawBlending(true, SkXfermode::kClear_Mode);
1217 setupDrawProgram();
1218 setupDrawPureColorUniforms();
1219 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001220 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001221
Romain Guy54be1cd2011-06-13 19:04:27 -07001222 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001223
1224 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001225 } else {
1226 for (uint32_t i = 0; i < count; i++) {
1227 delete mLayers.itemAt(i);
1228 }
Romain Guye67307c2013-02-11 18:01:20 -08001229 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001230 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001231}
1232
Romain Guybd6b79b2010-06-26 00:13:53 -07001233///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001234// State Deferral
1235///////////////////////////////////////////////////////////////////////////////
1236
Chris Craikff785832013-03-08 13:12:16 -08001237bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001238 const Rect& currentClip = *(mSnapshot->clipRect);
1239 const mat4& currentMatrix = *(mSnapshot->transform);
1240
Chris Craikff785832013-03-08 13:12:16 -08001241 if (stateDeferFlags & kStateDeferFlag_Draw) {
1242 // state has bounds initialized in local coordinates
1243 if (!state.mBounds.isEmpty()) {
1244 currentMatrix.mapRect(state.mBounds);
1245 if (!state.mBounds.intersect(currentClip)) {
1246 // quick rejected
1247 return true;
1248 }
1249 } else {
1250 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001251 }
Chris Craikff785832013-03-08 13:12:16 -08001252 state.mDrawModifiers = mDrawModifiers;
1253 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001254 }
1255
Chris Craikff785832013-03-08 13:12:16 -08001256 if (stateDeferFlags & kStateDeferFlag_Clip) {
1257 state.mClip.set(currentClip);
1258 } else {
1259 state.mClip.setEmpty();
1260 }
1261
1262 // transform always deferred
Chris Craikc3566d02013-02-04 16:16:33 -08001263 state.mMatrix.load(currentMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001264 return false;
1265}
1266
Chris Craikff785832013-03-08 13:12:16 -08001267void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, int stateDeferFlags) {
Romain Guy3b753822013-03-05 10:27:35 -08001268 currentTransform().load(state.mMatrix);
Chris Craikc3566d02013-02-04 16:16:33 -08001269
Chris Craikff785832013-03-08 13:12:16 -08001270 if (stateDeferFlags & kStateDeferFlag_Draw) {
1271 mDrawModifiers = state.mDrawModifiers;
1272 mSnapshot->alpha = state.mAlpha;
1273 }
1274
Chris Craikd90144d2013-03-19 15:03:48 -07001275 if (!state.mClip.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001276 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1277 dirtyClip();
1278 }
Chris Craikc3566d02013-02-04 16:16:33 -08001279}
1280
1281///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001282// Transforms
1283///////////////////////////////////////////////////////////////////////////////
1284
1285void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001286 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001287}
1288
1289void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001290 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001291}
1292
1293void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001294 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001295}
1296
Romain Guy807daf72011-01-18 11:19:19 -08001297void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001298 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001299}
1300
Romain Guyf6a11b82010-06-23 17:47:49 -07001301void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001302 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001303 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001304 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001305 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001306 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001307}
1308
Chris Craikb98a0162013-02-21 11:30:22 -08001309bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001310 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001311}
1312
Romain Guyf6a11b82010-06-23 17:47:49 -07001313void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001314 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001315}
1316
1317void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001318 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001319 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001320 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001321 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001322}
1323
1324///////////////////////////////////////////////////////////////////////////////
1325// Clipping
1326///////////////////////////////////////////////////////////////////////////////
1327
Romain Guybb9524b2010-06-22 18:56:38 -07001328void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001329 Rect clip(*mSnapshot->clipRect);
1330 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001331
Romain Guy8a4ac612012-07-17 17:32:48 -07001332 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1333 clip.getWidth(), clip.getHeight())) {
1334 mDirtyClip = false;
1335 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001336}
1337
Romain Guy8ce00302013-01-15 18:51:42 -08001338void OpenGLRenderer::ensureStencilBuffer() {
1339 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1340 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1341 // just hope we have one when hasLayer() returns false.
1342 if (hasLayer()) {
1343 attachStencilBufferToLayer(mSnapshot->layer);
1344 }
1345}
1346
1347void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1348 // The layer's FBO is already bound when we reach this stage
1349 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001350 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1351 // is attached after we initiated tiling. We must turn it off,
1352 // attach the new render buffer then turn tiling back on
1353 endTiling();
1354
Romain Guy8d4aeb72013-02-12 16:08:55 -08001355 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001356 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001357 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001358
Romain Guyf735c8e2013-01-31 17:45:55 -08001359 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001360 }
1361}
1362
1363void OpenGLRenderer::setStencilFromClip() {
1364 if (!mCaches.debugOverdraw) {
1365 if (!mSnapshot->clipRegion->isEmpty()) {
1366 // NOTE: The order here is important, we must set dirtyClip to false
1367 // before any draw call to avoid calling back into this method
1368 mDirtyClip = false;
1369
1370 ensureStencilBuffer();
1371
1372 mCaches.stencil.enableWrite();
1373
1374 // Clear the stencil but first make sure we restrict drawing
1375 // to the region's bounds
1376 bool resetScissor = mCaches.enableScissor();
1377 if (resetScissor) {
1378 // The scissor was not set so we now need to update it
1379 setScissorFromClip();
1380 }
1381 mCaches.stencil.clear();
1382 if (resetScissor) mCaches.disableScissor();
1383
1384 // NOTE: We could use the region contour path to generate a smaller mesh
1385 // Since we are using the stencil we could use the red book path
1386 // drawing technique. It might increase bandwidth usage though.
1387
1388 // The last parameter is important: we are not drawing in the color buffer
1389 // so we don't want to dirty the current layer, if any
1390 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1391
1392 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001393
1394 // Draw the region used to generate the stencil if the appropriate debug
1395 // mode is enabled
1396 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1397 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1398 }
Romain Guy8ce00302013-01-15 18:51:42 -08001399 } else {
1400 mCaches.stencil.disable();
1401 }
1402 }
1403}
1404
Romain Guy9d5316e2010-06-24 19:30:36 -07001405const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001406 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001407}
1408
Romain Guy8a4ac612012-07-17 17:32:48 -07001409bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1410 if (mSnapshot->isIgnored()) {
1411 return true;
1412 }
1413
1414 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001415 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001416 r.snapToPixelBoundaries();
1417
1418 Rect clipRect(*mSnapshot->clipRect);
1419 clipRect.snapToPixelBoundaries();
1420
1421 return !clipRect.intersects(r);
1422}
1423
Romain Guy35643dd2012-09-18 15:40:58 -07001424bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1425 Rect& transformed, Rect& clip) {
1426 if (mSnapshot->isIgnored()) {
1427 return true;
1428 }
1429
1430 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001431 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001432 transformed.snapToPixelBoundaries();
1433
1434 clip.set(*mSnapshot->clipRect);
1435 clip.snapToPixelBoundaries();
1436
1437 return !clip.intersects(transformed);
1438}
1439
Romain Guy672433d2013-01-04 19:05:13 -08001440bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1441 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001442 if (paint->getStyle() != SkPaint::kFill_Style) {
1443 float outset = paint->getStrokeWidth() * 0.5f;
1444 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1445 } else {
1446 return quickReject(left, top, right, bottom);
1447 }
1448}
1449
Romain Guyc7d53492010-06-25 13:41:57 -07001450bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001451 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001452 return true;
1453 }
1454
Romain Guy1d83e192010-08-17 11:37:00 -07001455 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001456 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001457 r.snapToPixelBoundaries();
1458
1459 Rect clipRect(*mSnapshot->clipRect);
1460 clipRect.snapToPixelBoundaries();
1461
Romain Guy586cae32012-07-13 15:28:31 -07001462 bool rejected = !clipRect.intersects(r);
1463 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001464 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001465 }
1466
1467 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001468}
1469
Romain Guy8ce00302013-01-15 18:51:42 -08001470void OpenGLRenderer::debugClip() {
1471#if DEBUG_CLIP_REGIONS
1472 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1473 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1474 }
1475#endif
1476}
1477
Romain Guy079ba2c2010-07-16 14:12:24 -07001478bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001479 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001480 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1481 if (clipped) {
1482 dirtyClip();
1483 }
1484 return !mSnapshot->clipRect->isEmpty();
1485 }
1486
1487 SkPath path;
1488 path.addRect(left, top, right, bottom);
1489
1490 return clipPath(&path, op);
1491}
1492
1493bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1494 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001495 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001496
1497 SkPath transformed;
1498 path->transform(transform, &transformed);
1499
1500 SkRegion clip;
1501 if (!mSnapshot->clipRegion->isEmpty()) {
1502 clip.setRegion(*mSnapshot->clipRegion);
1503 } else {
1504 Rect* bounds = mSnapshot->clipRect;
1505 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1506 }
1507
1508 SkRegion region;
1509 region.setPath(transformed, clip);
1510
1511 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001512 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001513 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001514 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001515 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001516}
1517
Romain Guy735738c2012-12-03 12:34:51 -08001518bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001519 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1520 if (clipped) {
1521 dirtyClip();
1522 }
1523 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001524}
1525
Chet Haasea23eed82012-04-12 15:19:04 -07001526Rect* OpenGLRenderer::getClipRect() {
1527 return mSnapshot->clipRect;
1528}
1529
Romain Guyf6a11b82010-06-23 17:47:49 -07001530///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001531// Drawing commands
1532///////////////////////////////////////////////////////////////////////////////
1533
Romain Guy54be1cd2011-06-13 19:04:27 -07001534void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001535 // TODO: It would be best if we could do this before quickReject()
1536 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001537 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001538 // Make sure setScissor & setStencil happen at the beginning of
1539 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001540 if (mDirtyClip) {
1541 if (mCaches.scissorEnabled) {
1542 setScissorFromClip();
1543 }
Romain Guy8ce00302013-01-15 18:51:42 -08001544 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001545 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001546
Romain Guy70ca14e2010-12-13 18:24:33 -08001547 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001548
Romain Guy70ca14e2010-12-13 18:24:33 -08001549 mSetShaderColor = false;
1550 mColorSet = false;
1551 mColorA = mColorR = mColorG = mColorB = 0.0f;
1552 mTextureUnit = 0;
1553 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001554
1555 // Enable debug highlight when what we're about to draw is tested against
1556 // the stencil buffer and if stencil highlight debugging is on
1557 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1558 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1559 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001560}
1561
1562void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1563 mDescription.hasTexture = true;
1564 mDescription.hasAlpha8Texture = isAlpha8;
1565}
1566
Romain Guyff316ec2013-02-13 18:39:43 -08001567void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1568 mDescription.hasTexture = true;
1569 mDescription.hasColors = true;
1570 mDescription.hasAlpha8Texture = isAlpha8;
1571}
1572
Romain Guyaa6c24c2011-04-28 18:40:04 -07001573void OpenGLRenderer::setupDrawWithExternalTexture() {
1574 mDescription.hasExternalTexture = true;
1575}
1576
Romain Guy15bc6432011-12-13 13:11:32 -08001577void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001578 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001579}
1580
Chris Craik710f46d2012-09-17 17:25:49 -07001581void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001582 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001583}
1584
Romain Guyed6fcb02011-03-21 13:11:28 -07001585void OpenGLRenderer::setupDrawPoint(float pointSize) {
1586 mDescription.isPoint = true;
1587 mDescription.pointSize = pointSize;
1588}
1589
Romain Guy8d0d4782010-12-14 20:13:35 -08001590void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1591 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001592 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1593 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1594 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001595 mColorSet = true;
1596 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1597}
1598
Romain Guy86568192010-12-14 15:55:39 -08001599void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1600 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001601 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1602 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1603 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001604 mColorSet = true;
1605 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1606}
1607
Romain Guy41210632012-07-16 17:04:24 -07001608void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1609 mCaches.fontRenderer->describe(mDescription, paint);
1610}
1611
Romain Guy70ca14e2010-12-13 18:24:33 -08001612void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1613 mColorA = a;
1614 mColorR = r;
1615 mColorG = g;
1616 mColorB = b;
1617 mColorSet = true;
1618 mSetShaderColor = mDescription.setColor(r, g, b, a);
1619}
1620
1621void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001622 if (mDrawModifiers.mShader) {
1623 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001624 }
1625}
1626
1627void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001628 if (mDrawModifiers.mColorFilter) {
1629 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001630 }
1631}
1632
Romain Guyf09ef512011-05-27 11:43:46 -07001633void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1634 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1635 mColorA = 1.0f;
1636 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001637 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001638 }
1639}
1640
Romain Guy70ca14e2010-12-13 18:24:33 -08001641void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001642 // When the blending mode is kClear_Mode, we need to use a modulate color
1643 // argb=1,0,0,0
1644 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001645 bool blend = (mColorSet && mColorA < 1.0f) ||
1646 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1647 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001648}
1649
1650void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001651 // When the blending mode is kClear_Mode, we need to use a modulate color
1652 // argb=1,0,0,0
1653 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001654 blend |= (mColorSet && mColorA < 1.0f) ||
1655 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1656 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1657 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001658}
1659
1660void OpenGLRenderer::setupDrawProgram() {
1661 useProgram(mCaches.programCache.get(mDescription));
1662}
1663
1664void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1665 mTrackDirtyRegions = false;
1666}
1667
1668void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1669 bool ignoreTransform) {
1670 mModelView.loadTranslate(left, top, 0.0f);
1671 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001672 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1673 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001674 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001675 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001676 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1677 }
1678}
1679
Chet Haase8a5cc922011-04-26 07:28:09 -07001680void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001681 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001682}
1683
Romain Guy70ca14e2010-12-13 18:24:33 -08001684void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1685 bool ignoreTransform, bool ignoreModelView) {
1686 if (!ignoreModelView) {
1687 mModelView.loadTranslate(left, top, 0.0f);
1688 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001689 } else {
1690 mModelView.loadIdentity();
1691 }
Romain Guy86568192010-12-14 15:55:39 -08001692 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1693 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001694 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001695 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001696 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001697 }
1698 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001699 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001700 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1701 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001702}
1703
Romain Guyed6fcb02011-03-21 13:11:28 -07001704void OpenGLRenderer::setupDrawPointUniforms() {
1705 int slot = mCaches.currentProgram->getUniform("pointSize");
1706 glUniform1f(slot, mDescription.pointSize);
1707}
1708
Romain Guy70ca14e2010-12-13 18:24:33 -08001709void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001710 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001711 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1712 }
1713}
1714
Romain Guy86568192010-12-14 15:55:39 -08001715void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001716 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001717 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001718 }
1719}
1720
Romain Guy70ca14e2010-12-13 18:24:33 -08001721void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001722 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001723 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001724 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001725 }
Chris Craikc3566d02013-02-04 16:16:33 -08001726 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1727 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001728 }
1729}
1730
Romain Guy8d0d4782010-12-14 20:13:35 -08001731void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001732 if (mDrawModifiers.mShader) {
1733 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001734 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001735 }
1736}
1737
Romain Guy70ca14e2010-12-13 18:24:33 -08001738void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001739 if (mDrawModifiers.mColorFilter) {
1740 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001741 }
1742}
1743
Romain Guy41210632012-07-16 17:04:24 -07001744void OpenGLRenderer::setupDrawTextGammaUniforms() {
1745 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1746}
1747
Romain Guy70ca14e2010-12-13 18:24:33 -08001748void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001749 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001750 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001751 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001752}
1753
1754void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001755 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001756 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001757 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001758}
1759
Romain Guyaa6c24c2011-04-28 18:40:04 -07001760void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1761 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001762 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001763 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001764}
1765
Romain Guy8f0095c2011-05-02 17:24:22 -07001766void OpenGLRenderer::setupDrawTextureTransform() {
1767 mDescription.hasTextureTransform = true;
1768}
1769
1770void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001771 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1772 GL_FALSE, &transform.data[0]);
1773}
1774
Romain Guy70ca14e2010-12-13 18:24:33 -08001775void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001776 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001777 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001778 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001779 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001780 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001781 }
Romain Guyd71dd362011-12-12 19:03:35 -08001782
Chris Craikcb4d6002012-09-25 12:00:29 -07001783 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001784 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001785 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001786 }
1787
1788 mCaches.unbindIndicesBuffer();
1789}
1790
Romain Guyff316ec2013-02-13 18:39:43 -08001791void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1792 bool force = mCaches.unbindMeshBuffer();
1793 GLsizei stride = sizeof(ColorTextureVertex);
1794
1795 mCaches.bindPositionVertexPointer(force, vertices, stride);
1796 if (mCaches.currentProgram->texCoords >= 0) {
1797 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1798 }
1799 int slot = mCaches.currentProgram->getAttrib("colors");
1800 if (slot >= 0) {
1801 glEnableVertexAttribArray(slot);
1802 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1803 }
1804
1805 mCaches.unbindIndicesBuffer();
1806}
1807
Romain Guy15bc6432011-12-13 13:11:32 -08001808void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1809 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001810 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001811 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001812 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001813 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001814}
1815
Chet Haase5b0200b2011-04-13 17:58:08 -07001816void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001817 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001818 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001819 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001820}
1821
Romain Guy70ca14e2010-12-13 18:24:33 -08001822void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001823}
1824
1825///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001826// Drawing
1827///////////////////////////////////////////////////////////////////////////////
1828
Chris Craikff785832013-03-08 13:12:16 -08001829status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1830 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001831 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1832 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001833 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001834 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chris Craikff785832013-03-08 13:12:16 -08001835 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1836 displayList->replay(replayStruct, 0);
1837 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001838 }
1839
1840 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001841 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1842 displayList->defer(deferStruct, 0);
1843 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001844 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001845
Romain Guy65549432012-03-26 16:45:05 -07001846 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001847}
1848
Chris Craikc3566d02013-02-04 16:16:33 -08001849void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001850 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001851 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001852 }
1853}
1854
Romain Guya168d732011-03-18 16:50:13 -07001855void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1856 int alpha;
1857 SkXfermode::Mode mode;
1858 getAlphaAndMode(paint, &alpha, &mode);
1859
Romain Guy886b2752013-01-04 12:26:18 -08001860 int color = paint != NULL ? paint->getColor() : 0;
1861
Romain Guya168d732011-03-18 16:50:13 -07001862 float x = left;
1863 float y = top;
1864
Romain Guy886b2752013-01-04 12:26:18 -08001865 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1866
Romain Guya168d732011-03-18 16:50:13 -07001867 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001868 if (currentTransform().isPureTranslate()) {
1869 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1870 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001871 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001872
1873 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001874 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001875 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001876 }
1877
Romain Guy886b2752013-01-04 12:26:18 -08001878 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1879 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1880 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001881}
1882
Chet Haase48659092012-05-31 15:21:51 -07001883status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001884 const float right = left + bitmap->width();
1885 const float bottom = top + bitmap->height();
1886
1887 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001888 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001889 }
1890
Romain Guya1d3c912011-12-13 14:55:06 -08001891 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001892 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001893 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001894 const AutoTexture autoCleanup(texture);
1895
Romain Guy211370f2012-02-01 16:10:55 -08001896 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001897 drawAlphaBitmap(texture, left, top, paint);
1898 } else {
1899 drawTextureRect(left, top, right, bottom, texture, paint);
1900 }
Chet Haase48659092012-05-31 15:21:51 -07001901
1902 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001903}
1904
Chet Haase48659092012-05-31 15:21:51 -07001905status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001906 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1907 const mat4 transform(*matrix);
1908 transform.mapRect(r);
1909
Romain Guy6926c722010-07-12 20:20:03 -07001910 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001911 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001912 }
1913
Romain Guya1d3c912011-12-13 14:55:06 -08001914 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001915 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001916 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001917 const AutoTexture autoCleanup(texture);
1918
Romain Guy5b3b3522010-10-27 18:57:51 -07001919 // This could be done in a cheaper way, all we need is pass the matrix
1920 // to the vertex shader. The save/restore is a bit overkill.
1921 save(SkCanvas::kMatrix_SaveFlag);
1922 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001923 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1924 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1925 } else {
1926 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1927 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001928 restore();
Chet Haase48659092012-05-31 15:21:51 -07001929
1930 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001931}
1932
Chet Haase48659092012-05-31 15:21:51 -07001933status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001934 const float right = left + bitmap->width();
1935 const float bottom = top + bitmap->height();
1936
1937 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001938 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001939 }
1940
1941 mCaches.activeTexture(0);
1942 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1943 const AutoTexture autoCleanup(texture);
1944
Romain Guy886b2752013-01-04 12:26:18 -08001945 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1946 drawAlphaBitmap(texture, left, top, paint);
1947 } else {
1948 drawTextureRect(left, top, right, bottom, texture, paint);
1949 }
Chet Haase48659092012-05-31 15:21:51 -07001950
1951 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001952}
1953
Chet Haase48659092012-05-31 15:21:51 -07001954status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001955 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001956 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001957 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001958 }
1959
Romain Guyb18d2d02011-02-10 15:52:54 -08001960 float left = FLT_MAX;
1961 float top = FLT_MAX;
1962 float right = FLT_MIN;
1963 float bottom = FLT_MIN;
1964
Romain Guya92bb4d2012-10-16 11:08:44 -07001965 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001966
Romain Guyff316ec2013-02-13 18:39:43 -08001967 ColorTextureVertex mesh[count];
1968 ColorTextureVertex* vertex = mesh;
1969
1970 bool cleanupColors = false;
1971 if (!colors) {
1972 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
1973 colors = new int[colorsCount];
1974 memset(colors, 0xff, colorsCount * sizeof(int));
1975 cleanupColors = true;
1976 }
Romain Guya92bb4d2012-10-16 11:08:44 -07001977
Romain Guy5a7b4662011-01-20 19:09:30 -08001978 for (int32_t y = 0; y < meshHeight; y++) {
1979 for (int32_t x = 0; x < meshWidth; x++) {
1980 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1981
1982 float u1 = float(x) / meshWidth;
1983 float u2 = float(x + 1) / meshWidth;
1984 float v1 = float(y) / meshHeight;
1985 float v2 = float(y + 1) / meshHeight;
1986
1987 int ax = i + (meshWidth + 1) * 2;
1988 int ay = ax + 1;
1989 int bx = i;
1990 int by = bx + 1;
1991 int cx = i + 2;
1992 int cy = cx + 1;
1993 int dx = i + (meshWidth + 1) * 2 + 2;
1994 int dy = dx + 1;
1995
Romain Guyff316ec2013-02-13 18:39:43 -08001996 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
1997 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
1998 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08001999
Romain Guyff316ec2013-02-13 18:39:43 -08002000 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2001 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2002 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002003
Romain Guya92bb4d2012-10-16 11:08:44 -07002004 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2005 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2006 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2007 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002008 }
2009 }
2010
Romain Guya92bb4d2012-10-16 11:08:44 -07002011 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002012 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002013 return DrawGlInfo::kStatusDone;
2014 }
2015
2016 mCaches.activeTexture(0);
2017 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002018 if (!texture) {
2019 if (cleanupColors) delete[] colors;
2020 return DrawGlInfo::kStatusDone;
2021 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002022 const AutoTexture autoCleanup(texture);
2023
2024 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2025 texture->setFilter(FILTER(paint), true);
2026
2027 int alpha;
2028 SkXfermode::Mode mode;
2029 getAlphaAndMode(paint, &alpha, &mode);
2030
Romain Guyff316ec2013-02-13 18:39:43 -08002031 float a = alpha / 255.0f;
2032
Romain Guya92bb4d2012-10-16 11:08:44 -07002033 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002034 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002035 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002036
Romain Guyff316ec2013-02-13 18:39:43 -08002037 setupDraw();
2038 setupDrawWithTextureAndColor();
2039 setupDrawColor(a, a, a, a);
2040 setupDrawColorFilter();
2041 setupDrawBlending(true, mode, false);
2042 setupDrawProgram();
2043 setupDrawDirtyRegionsDisabled();
2044 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2045 setupDrawTexture(texture->id);
2046 setupDrawPureColorUniforms();
2047 setupDrawColorFilterUniforms();
2048 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2049
2050 glDrawArrays(GL_TRIANGLES, 0, count);
2051
2052 finishDrawTexture();
2053
2054 int slot = mCaches.currentProgram->getAttrib("colors");
2055 if (slot >= 0) {
2056 glDisableVertexAttribArray(slot);
2057 }
2058
2059 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002060
2061 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002062}
2063
Chet Haase48659092012-05-31 15:21:51 -07002064status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002065 float srcLeft, float srcTop, float srcRight, float srcBottom,
2066 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002067 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002068 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002069 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002070 }
2071
Romain Guya1d3c912011-12-13 14:55:06 -08002072 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002073 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002074 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002075 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002076
Romain Guy8ba548f2010-06-30 19:21:21 -07002077 const float width = texture->width;
2078 const float height = texture->height;
2079
Romain Guy68169722011-08-22 17:33:33 -07002080 const float u1 = fmax(0.0f, srcLeft / width);
2081 const float v1 = fmax(0.0f, srcTop / height);
2082 const float u2 = fmin(1.0f, srcRight / width);
2083 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002084
Romain Guy03750a02010-10-18 14:06:08 -07002085 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002086 resetDrawTextureTexCoords(u1, v1, u2, v2);
2087
Romain Guy03750a02010-10-18 14:06:08 -07002088 int alpha;
2089 SkXfermode::Mode mode;
2090 getAlphaAndMode(paint, &alpha, &mode);
2091
Romain Guyd21b6e12011-11-30 20:21:23 -08002092 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2093
Romain Guy886b2752013-01-04 12:26:18 -08002094 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2095 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002096
Romain Guy886b2752013-01-04 12:26:18 -08002097 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2098 // Apply a scale transform on the canvas only when a shader is in use
2099 // Skia handles the ratio between the dst and src rects as a scale factor
2100 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002101 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002102 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002103
Romain Guy3b753822013-03-05 10:27:35 -08002104 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2105 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2106 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002107
2108 dstRight = x + (dstRight - dstLeft);
2109 dstBottom = y + (dstBottom - dstTop);
2110
2111 dstLeft = x;
2112 dstTop = y;
2113
2114 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2115 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002116 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002117 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002118 }
2119
2120 if (CC_UNLIKELY(useScaleTransform)) {
2121 save(SkCanvas::kMatrix_SaveFlag);
2122 translate(dstLeft, dstTop);
2123 scale(scaleX, scaleY);
2124
2125 dstLeft = 0.0f;
2126 dstTop = 0.0f;
2127
2128 dstRight = srcRight - srcLeft;
2129 dstBottom = srcBottom - srcTop;
2130 }
2131
2132 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2133 int color = paint ? paint->getColor() : 0;
2134 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2135 texture->id, paint != NULL, color, alpha, mode,
2136 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2137 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2138 } else {
2139 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2140 texture->id, alpha / 255.0f, mode, texture->blend,
2141 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2142 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2143 }
2144
2145 if (CC_UNLIKELY(useScaleTransform)) {
2146 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002147 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002148
2149 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002150
2151 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002152}
2153
Chet Haase48659092012-05-31 15:21:51 -07002154status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002155 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002156 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002157 int alpha;
2158 SkXfermode::Mode mode;
2159 getAlphaAndModeDirect(paint, &alpha, &mode);
2160
2161 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2162 left, top, right, bottom, alpha, mode);
2163}
2164
2165status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2166 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2167 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002168 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002169 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002170 }
2171
Romain Guybe6f9dc2012-07-16 12:41:17 -07002172 alpha *= mSnapshot->alpha;
2173
Romain Guy2728f962010-10-08 18:36:15 -07002174 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002175 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002176
Romain Guy211370f2012-02-01 16:10:55 -08002177 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002178 mCaches.activeTexture(0);
2179 Texture* texture = mCaches.textureCache.get(bitmap);
2180 if (!texture) return DrawGlInfo::kStatusDone;
2181 const AutoTexture autoCleanup(texture);
2182 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2183 texture->setFilter(GL_LINEAR, true);
2184
Romain Guy3b753822013-03-05 10:27:35 -08002185 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002186 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002187 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002188 const float offsetX = left + currentTransform().getTranslateX();
2189 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002190 const size_t count = mesh->quads.size();
2191 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002192 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002193 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002194 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2195 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2196 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002197 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002198 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002199 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002200 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002201 }
2202 }
2203
Romain Guy211370f2012-02-01 16:10:55 -08002204 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002205 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2206 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002207
2208 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2209 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2210 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2211 true, !mesh->hasEmptyQuads);
2212 } else {
2213 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2214 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2215 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2216 true, !mesh->hasEmptyQuads);
2217 }
Romain Guy054dc182010-10-15 17:55:25 -07002218 }
Chet Haase48659092012-05-31 15:21:51 -07002219
2220 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002221}
2222
Chris Craik65cd6122012-12-10 17:56:27 -08002223status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2224 bool useOffset) {
2225 if (!vertexBuffer.getSize()) {
2226 // no vertices to draw
2227 return DrawGlInfo::kStatusDone;
2228 }
2229
Chris Craikcb4d6002012-09-25 12:00:29 -07002230 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002231 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2232 bool isAA = paint->isAntiAlias();
2233
Chris Craik710f46d2012-09-17 17:25:49 -07002234 setupDraw();
2235 setupDrawNoTexture();
2236 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002237 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2238 setupDrawColorFilter();
2239 setupDrawShader();
2240 setupDrawBlending(isAA, mode);
2241 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002242 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002243 setupDrawColorUniforms();
2244 setupDrawColorFilterUniforms();
2245 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002246
Chris Craik710f46d2012-09-17 17:25:49 -07002247 void* vertices = vertexBuffer.getBuffer();
2248 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002249 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002250 mCaches.resetTexCoordsVertexPointer();
2251 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002252
Chris Craik710f46d2012-09-17 17:25:49 -07002253 int alphaSlot = -1;
2254 if (isAA) {
2255 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2256 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002257
Chris Craik710f46d2012-09-17 17:25:49 -07002258 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002259 glEnableVertexAttribArray(alphaSlot);
2260 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002261 }
Romain Guy04299382012-07-18 17:15:41 -07002262
Chris Craik710f46d2012-09-17 17:25:49 -07002263 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002264
Chris Craik710f46d2012-09-17 17:25:49 -07002265 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002266 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002267 }
Chris Craik65cd6122012-12-10 17:56:27 -08002268
2269 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002270}
2271
2272/**
Chris Craik65cd6122012-12-10 17:56:27 -08002273 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2274 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2275 * screen space in all directions. However, instead of using a fragment shader to compute the
2276 * translucency of the color from its position, we simply use a varying parameter to define how far
2277 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2278 *
2279 * Doesn't yet support joins, caps, or path effects.
2280 */
2281status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2282 VertexBuffer vertexBuffer;
2283 // TODO: try clipping large paths to viewport
2284 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2285
Romain Guyc46d07a2013-03-15 19:06:39 -07002286 if (hasLayer()) {
2287 SkRect bounds = path.getBounds();
2288 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2289 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2290 }
Chris Craik65cd6122012-12-10 17:56:27 -08002291
2292 return drawVertexBuffer(vertexBuffer, paint);
2293}
2294
2295/**
2296 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2297 * and additional geometry for defining an alpha slope perimeter.
2298 *
2299 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2300 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2301 * in-shader alpha region, but found it to be taxing on some GPUs.
2302 *
2303 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2304 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002305 */
Chet Haase48659092012-05-31 15:21:51 -07002306status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002307 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002308
Chris Craik65cd6122012-12-10 17:56:27 -08002309 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002310
Chris Craik65cd6122012-12-10 17:56:27 -08002311 VertexBuffer buffer;
2312 SkRect bounds;
2313 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002314
2315 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2316 return DrawGlInfo::kStatusDone;
2317 }
2318
Romain Guy3b753822013-03-05 10:27:35 -08002319 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002320
Chris Craik65cd6122012-12-10 17:56:27 -08002321 bool useOffset = !paint->isAntiAlias();
2322 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002323}
2324
Chet Haase48659092012-05-31 15:21:51 -07002325status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2326 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002327
2328 // TODO: The paint's cap style defines whether the points are square or circular
2329 // TODO: Handle AA for round points
2330
Chet Haase5b0200b2011-04-13 17:58:08 -07002331 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002332 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002333 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002334 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002335 if (isHairLine) {
2336 // Now that we know it's hairline, we can set the effective width, to be used later
2337 strokeWidth = 1.0f;
2338 }
2339 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002340
Romain Guyed6fcb02011-03-21 13:11:28 -07002341 int alpha;
2342 SkXfermode::Mode mode;
2343 getAlphaAndMode(paint, &alpha, &mode);
2344
2345 int verticesCount = count >> 1;
2346 int generatedVerticesCount = 0;
2347
2348 TextureVertex pointsData[verticesCount];
2349 TextureVertex* vertex = &pointsData[0];
2350
Romain Guy04299382012-07-18 17:15:41 -07002351 // TODO: We should optimize this method to not generate vertices for points
2352 // that lie outside of the clip.
2353 mCaches.enableScissor();
2354
Romain Guyed6fcb02011-03-21 13:11:28 -07002355 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002356 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002357 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002358 setupDrawColor(paint->getColor(), alpha);
2359 setupDrawColorFilter();
2360 setupDrawShader();
2361 setupDrawBlending(mode);
2362 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002363 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002364 setupDrawColorUniforms();
2365 setupDrawColorFilterUniforms();
2366 setupDrawPointUniforms();
2367 setupDrawShaderIdentityUniforms();
2368 setupDrawMesh(vertex);
2369
2370 for (int i = 0; i < count; i += 2) {
2371 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2372 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002373
Chet Haase8a5cc922011-04-26 07:28:09 -07002374 float left = points[i] - halfWidth;
2375 float right = points[i] + halfWidth;
2376 float top = points[i + 1] - halfWidth;
2377 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002378
Romain Guy3b753822013-03-05 10:27:35 -08002379 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002380 }
2381
2382 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002383
2384 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002385}
2386
Chet Haase48659092012-05-31 15:21:51 -07002387status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002388 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002389 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002390
Romain Guyae88e5e2010-10-22 17:49:18 -07002391 Rect& clip(*mSnapshot->clipRect);
2392 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002393
Romain Guy3d58c032010-07-14 16:34:53 -07002394 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002395
2396 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002397}
Romain Guy9d5316e2010-06-24 19:30:36 -07002398
Chet Haase48659092012-05-31 15:21:51 -07002399status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2400 SkPaint* paint) {
2401 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002402 const AutoTexture autoCleanup(texture);
2403
2404 const float x = left + texture->left - texture->offset;
2405 const float y = top + texture->top - texture->offset;
2406
2407 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002408
2409 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002410}
2411
Chet Haase48659092012-05-31 15:21:51 -07002412status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002413 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002414 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2415 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002416 return DrawGlInfo::kStatusDone;
2417 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002418
Chris Craikcb4d6002012-09-25 12:00:29 -07002419 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002420 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002421 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002422 right - left, bottom - top, rx, ry, p);
2423 return drawShape(left, top, texture, p);
2424 }
2425
2426 SkPath path;
2427 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002428 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2429 float outset = p->getStrokeWidth() / 2;
2430 rect.outset(outset, outset);
2431 rx += outset;
2432 ry += outset;
2433 }
Chris Craik710f46d2012-09-17 17:25:49 -07002434 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002435 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002436}
2437
Chris Craik710f46d2012-09-17 17:25:49 -07002438status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002439 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002440 x + radius, y + radius, p) ||
2441 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002442 return DrawGlInfo::kStatusDone;
2443 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002444 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002445 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002446 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002447 return drawShape(x - radius, y - radius, texture, p);
2448 }
2449
2450 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002451 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2452 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2453 } else {
2454 path.addCircle(x, y, radius);
2455 }
Chris Craik65cd6122012-12-10 17:56:27 -08002456 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002457}
Romain Guy01d58e42011-01-19 21:54:02 -08002458
Chet Haase48659092012-05-31 15:21:51 -07002459status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002460 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002461 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2462 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002463 return DrawGlInfo::kStatusDone;
2464 }
Romain Guy01d58e42011-01-19 21:54:02 -08002465
Chris Craikcb4d6002012-09-25 12:00:29 -07002466 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002467 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002468 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002469 return drawShape(left, top, texture, p);
2470 }
2471
2472 SkPath path;
2473 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002474 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2475 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2476 }
Chris Craik710f46d2012-09-17 17:25:49 -07002477 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002478 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002479}
2480
Chet Haase48659092012-05-31 15:21:51 -07002481status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002482 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002483 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2484 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002485 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002486 }
2487
Chris Craik780c1282012-10-04 14:10:49 -07002488 if (fabs(sweepAngle) >= 360.0f) {
2489 return drawOval(left, top, right, bottom, p);
2490 }
2491
2492 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002493 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002494 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002495 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002496 startAngle, sweepAngle, useCenter, p);
2497 return drawShape(left, top, texture, p);
2498 }
2499
2500 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2501 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2502 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2503 }
2504
2505 SkPath path;
2506 if (useCenter) {
2507 path.moveTo(rect.centerX(), rect.centerY());
2508 }
2509 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2510 if (useCenter) {
2511 path.close();
2512 }
Chris Craik65cd6122012-12-10 17:56:27 -08002513 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002514}
2515
Romain Guycf8675e2012-10-02 12:32:25 -07002516// See SkPaintDefaults.h
2517#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2518
Chet Haase48659092012-05-31 15:21:51 -07002519status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002520 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2521 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002522 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002523 }
2524
Chris Craik710f46d2012-09-17 17:25:49 -07002525 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002526 // only fill style is supported by drawConvexPath, since others have to handle joins
2527 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2528 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2529 mCaches.activeTexture(0);
2530 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002531 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002532 return drawShape(left, top, texture, p);
2533 }
2534
2535 SkPath path;
2536 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2537 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2538 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2539 }
2540 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002541 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002542 }
2543
Romain Guy3b753822013-03-05 10:27:35 -08002544 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002545 SkPath path;
2546 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002547 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002548 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002549 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002550 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002551 }
Romain Guyc7d53492010-06-25 13:41:57 -07002552}
Romain Guy9d5316e2010-06-24 19:30:36 -07002553
Raph Levien416a8472012-07-19 22:48:17 -07002554void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2555 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2556 float x, float y) {
2557 mCaches.activeTexture(0);
2558
2559 // NOTE: The drop shadow will not perform gamma correction
2560 // if shader-based correction is enabled
2561 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2562 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002563 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002564 const AutoTexture autoCleanup(shadow);
2565
Chris Craikc3566d02013-02-04 16:16:33 -08002566 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2567 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002568
Chris Craikc3566d02013-02-04 16:16:33 -08002569 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2570 int shadowColor = mDrawModifiers.mShadowColor;
2571 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002572 shadowColor = 0xffffffff;
2573 }
2574
2575 setupDraw();
2576 setupDrawWithTexture(true);
2577 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2578 setupDrawColorFilter();
2579 setupDrawShader();
2580 setupDrawBlending(true, mode);
2581 setupDrawProgram();
2582 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2583 setupDrawTexture(shadow->id);
2584 setupDrawPureColorUniforms();
2585 setupDrawColorFilterUniforms();
2586 setupDrawShaderUniforms();
2587 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2588
2589 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2590}
2591
Romain Guy768bffc2013-02-27 13:50:45 -08002592bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2593 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2594 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2595}
2596
Romain Guy257ae352013-03-20 16:31:12 -07002597class TextSetupFunctor: public Functor {
2598public:
2599 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2600 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2601 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2602 alpha(alpha), mode(mode), paint(paint) {
2603 }
2604 ~TextSetupFunctor() { }
2605
2606 status_t operator ()(int what, void* data) {
2607 renderer.setupDraw();
2608 renderer.setupDrawTextGamma(paint);
2609 renderer.setupDrawDirtyRegionsDisabled();
2610 renderer.setupDrawWithTexture(true);
2611 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2612 renderer.setupDrawColorFilter();
2613 renderer.setupDrawShader();
2614 renderer.setupDrawBlending(true, mode);
2615 renderer.setupDrawProgram();
2616 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2617 // Calling setupDrawTexture with the name 0 will enable the
2618 // uv attributes and increase the texture unit count
2619 // texture binding will be performed by the font renderer as
2620 // needed
2621 renderer.setupDrawTexture(0);
2622 renderer.setupDrawPureColorUniforms();
2623 renderer.setupDrawColorFilterUniforms();
2624 renderer.setupDrawShaderUniforms(pureTranslate);
2625 renderer.setupDrawTextGammaUniforms();
2626
2627 return NO_ERROR;
2628 }
2629
2630 OpenGLRenderer& renderer;
2631 float x;
2632 float y;
2633 bool pureTranslate;
2634 int alpha;
2635 SkXfermode::Mode mode;
2636 SkPaint* paint;
2637};
2638
Chet Haase48659092012-05-31 15:21:51 -07002639status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002640 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002641 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002642 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002643 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002644
Romain Guy671d6cf2012-01-18 12:39:17 -08002645 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002646 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002647 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002648 }
2649
2650 float x = 0.0f;
2651 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002652 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002653 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002654 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2655 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002656 }
2657
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002658 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002659 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002660
2661 int alpha;
2662 SkXfermode::Mode mode;
2663 getAlphaAndMode(paint, &alpha, &mode);
2664
Chris Craikc3566d02013-02-04 16:16:33 -08002665 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002666 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2667 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002668 }
2669
Romain Guy671d6cf2012-01-18 12:39:17 -08002670 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002671 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002672 if (pureTranslate && !linearFilter) {
2673 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2674 }
Romain Guy257ae352013-03-20 16:31:12 -07002675 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002676
2677 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2678 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2679
Romain Guy211370f2012-02-01 16:10:55 -08002680 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002681
Romain Guy257ae352013-03-20 16:31:12 -07002682 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002683 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002684 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002685 if (hasActiveLayer) {
2686 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002687 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002688 }
2689 dirtyLayerUnchecked(bounds, getRegion());
2690 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002691 }
Chet Haase48659092012-05-31 15:21:51 -07002692
2693 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002694}
2695
Romain Guy624234f2013-03-05 16:43:31 -08002696mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2697 mat4 fontTransform;
2698 if (CC_LIKELY(transform.isPureTranslate())) {
2699 fontTransform = mat4::identity();
2700 } else {
2701 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002702 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002703 } else {
2704 float sx, sy;
2705 currentTransform().decomposeScale(sx, sy);
2706 fontTransform.loadScale(sx, sy, 1.0f);
2707 }
2708 }
2709 return fontTransform;
2710}
2711
Romain Guyc2525952012-07-27 16:41:22 -07002712status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002713 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002714 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002715 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002716 }
2717
Chet Haasea1cff502012-02-21 13:43:44 -08002718 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002719 switch (paint->getTextAlign()) {
2720 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002721 x -= length / 2.0f;
2722 break;
2723 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002724 x -= length;
2725 break;
2726 default:
2727 break;
2728 }
2729
Romain Guycac5fd32011-12-01 20:08:50 -08002730 SkPaint::FontMetrics metrics;
2731 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002732 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002733 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002734 }
2735
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002736 const float oldX = x;
2737 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002738
2739 const mat4& transform = currentTransform();
2740 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002741
Romain Guy211370f2012-02-01 16:10:55 -08002742 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002743 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2744 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002745 }
2746
Romain Guy86568192010-12-14 15:55:39 -08002747 int alpha;
2748 SkXfermode::Mode mode;
2749 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002750
Romain Guyc74f45a2013-02-26 19:10:14 -08002751 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2752
Chris Craikc3566d02013-02-04 16:16:33 -08002753 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002754 fontRenderer.setFont(paint, mat4::identity());
2755 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2756 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002757 }
2758
Romain Guy19d4dd82013-03-04 11:14:26 -08002759 const bool hasActiveLayer = hasLayer();
2760
Romain Guy624234f2013-03-05 16:43:31 -08002761 // We only pass a partial transform to the font renderer. That partial
2762 // matrix defines how glyphs are rasterized. Typically we want glyphs
2763 // to be rasterized at their final size on screen, which means the partial
2764 // matrix needs to take the scale factor into account.
2765 // When a partial matrix is used to transform glyphs during rasterization,
2766 // the mesh is generated with the inverse transform (in the case of scale,
2767 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2768 // apply the full transform matrix at draw time in the vertex shader.
2769 // Applying the full matrix in the shader is the easiest way to handle
2770 // rotation and perspective and allows us to always generated quads in the
2771 // font renderer which greatly simplifies the code, clipping in particular.
2772 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002773 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002774
Romain Guy6620c6d2010-12-06 18:07:02 -08002775 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002776 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002777 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002778
Romain Guy624234f2013-03-05 16:43:31 -08002779 // TODO: Implement better clipping for scaled/rotated text
2780 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002781 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2782
Raph Levien996e57c2012-07-23 15:22:52 -07002783 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002784 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guya3dc55f2012-09-28 13:55:44 -07002785 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002786 SkPaint paintCopy(*paint);
2787 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2788 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002789 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002790 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002791 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002792 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002793 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002794
2795 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002796 if (!pureTranslate) {
2797 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002798 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002799 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002800 }
Romain Guy694b5192010-07-21 21:33:20 -07002801
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002802 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002803
2804 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002805}
2806
Chet Haase48659092012-05-31 15:21:51 -07002807status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002808 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002809 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002810 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002811 }
2812
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002813 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002814 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002815 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002816
2817 int alpha;
2818 SkXfermode::Mode mode;
2819 getAlphaAndMode(paint, &alpha, &mode);
2820
Romain Guy03d58522012-02-24 17:54:07 -08002821 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002822 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002823 setupDrawDirtyRegionsDisabled();
2824 setupDrawWithTexture(true);
2825 setupDrawAlpha8Color(paint->getColor(), alpha);
2826 setupDrawColorFilter();
2827 setupDrawShader();
2828 setupDrawBlending(true, mode);
2829 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002830 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002831 // Calling setupDrawTexture with the name 0 will enable the
2832 // uv attributes and increase the texture unit count
2833 // texture binding will be performed by the font renderer as
2834 // needed
2835 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002836 setupDrawPureColorUniforms();
2837 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002838 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002839 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002840
Romain Guy97771732012-02-28 18:17:02 -08002841 const Rect* clip = &mSnapshot->getLocalClip();
2842 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 -08002843
Romain Guy97771732012-02-28 18:17:02 -08002844 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002845
2846 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2847 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002848 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002849 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002850 dirtyLayerUnchecked(bounds, getRegion());
2851 }
Romain Guy97771732012-02-28 18:17:02 -08002852 }
Chet Haase48659092012-05-31 15:21:51 -07002853
2854 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002855}
2856
Chet Haase48659092012-05-31 15:21:51 -07002857status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2858 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002859
Romain Guya1d3c912011-12-13 14:55:06 -08002860 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002861
Romain Guyfb8b7632010-08-23 21:05:08 -07002862 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002863 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002864 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002865
Romain Guy8b55f372010-08-18 17:10:07 -07002866 const float x = texture->left - texture->offset;
2867 const float y = texture->top - texture->offset;
2868
Romain Guy01d58e42011-01-19 21:54:02 -08002869 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002870
2871 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002872}
2873
Chris Craika08f95c2013-03-15 17:24:33 -07002874status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002875 if (!layer) {
2876 return DrawGlInfo::kStatusDone;
2877 }
2878
Romain Guyb2e2f242012-10-17 18:18:35 -07002879 mat4* transform = NULL;
2880 if (layer->isTextureLayer()) {
2881 transform = &layer->getTransform();
2882 if (!transform->isIdentity()) {
2883 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002884 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002885 }
2886 }
2887
Romain Guy35643dd2012-09-18 15:40:58 -07002888 Rect transformed;
2889 Rect clip;
2890 const bool rejected = quickRejectNoScissor(x, y,
2891 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2892
2893 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002894 if (transform && !transform->isIdentity()) {
2895 restore();
2896 }
Chet Haase48659092012-05-31 15:21:51 -07002897 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002898 }
2899
Romain Guy5bb3c732012-11-29 17:52:58 -08002900 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002901
Romain Guy87e2f7572012-09-24 11:37:12 -07002902 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002903 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002904
Romain Guy211370f2012-02-01 16:10:55 -08002905 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002906 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2907 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002908
Romain Guyc88e3572011-01-22 00:32:12 -08002909 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002910 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002911 } else if (layer->mesh) {
Chris Craika08f95c2013-03-15 17:24:33 -07002912 const float a = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyc88e3572011-01-22 00:32:12 -08002913 setupDraw();
2914 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002915 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002916 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002917 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002918 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002919 setupDrawPureColorUniforms();
2920 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002921 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002922 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2923 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2924 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002925
Romain Guyd21b6e12011-11-30 20:21:23 -08002926 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002927 setupDrawModelViewTranslate(tx, ty,
2928 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002929 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002930 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002931 setupDrawModelViewTranslate(x, y,
2932 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2933 }
Romain Guyc88e3572011-01-22 00:32:12 -08002934 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002935
Romain Guyc88e3572011-01-22 00:32:12 -08002936 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2937 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002938
Romain Guyc88e3572011-01-22 00:32:12 -08002939 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002940
2941#if DEBUG_LAYERS_AS_REGIONS
2942 drawRegionRects(layer->region);
2943#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002944 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002945
Chris Craikc3566d02013-02-04 16:16:33 -08002946 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07002947
Romain Guy5bb3c732012-11-29 17:52:58 -08002948 if (layer->debugDrawUpdate) {
2949 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002950 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2951 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2952 }
Romain Guyf219da52011-01-16 12:54:25 -08002953 }
Chet Haase48659092012-05-31 15:21:51 -07002954
Romain Guyb2e2f242012-10-17 18:18:35 -07002955 if (transform && !transform->isIdentity()) {
2956 restore();
2957 }
2958
Chet Haase48659092012-05-31 15:21:51 -07002959 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002960}
2961
Romain Guy6926c722010-07-12 20:20:03 -07002962///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002963// Shaders
2964///////////////////////////////////////////////////////////////////////////////
2965
2966void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002967 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002968}
2969
Romain Guy06f96e22010-07-30 19:18:16 -07002970void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08002971 mDrawModifiers.mShader = shader;
2972 if (mDrawModifiers.mShader) {
2973 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002974 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002975}
2976
Romain Guyd27977d2010-07-14 19:18:51 -07002977///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002978// Color filters
2979///////////////////////////////////////////////////////////////////////////////
2980
2981void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08002982 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07002983}
2984
2985void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08002986 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07002987}
2988
2989///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002990// Drop shadow
2991///////////////////////////////////////////////////////////////////////////////
2992
2993void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08002994 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07002995}
2996
2997void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08002998 mDrawModifiers.mHasShadow = true;
2999 mDrawModifiers.mShadowRadius = radius;
3000 mDrawModifiers.mShadowDx = dx;
3001 mDrawModifiers.mShadowDy = dy;
3002 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003003}
3004
3005///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003006// Draw filters
3007///////////////////////////////////////////////////////////////////////////////
3008
3009void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003010 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08003011}
3012
3013void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003014 mDrawModifiers.mHasDrawFilter = true;
3015 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3016 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003017}
3018
Chris Craika08f95c2013-03-15 17:24:33 -07003019SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003020 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003021 return paint;
3022 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003023
3024 uint32_t flags = paint->getFlags();
3025
3026 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003027 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3028 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003029
3030 return &mFilteredPaint;
3031}
3032
3033///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003034// Drawing implementation
3035///////////////////////////////////////////////////////////////////////////////
3036
Romain Guy01d58e42011-01-19 21:54:02 -08003037void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3038 float x, float y, SkPaint* paint) {
3039 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3040 return;
3041 }
3042
3043 int alpha;
3044 SkXfermode::Mode mode;
3045 getAlphaAndMode(paint, &alpha, &mode);
3046
3047 setupDraw();
3048 setupDrawWithTexture(true);
3049 setupDrawAlpha8Color(paint->getColor(), alpha);
3050 setupDrawColorFilter();
3051 setupDrawShader();
3052 setupDrawBlending(true, mode);
3053 setupDrawProgram();
3054 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3055 setupDrawTexture(texture->id);
3056 setupDrawPureColorUniforms();
3057 setupDrawColorFilterUniforms();
3058 setupDrawShaderUniforms();
3059 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3060
3061 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3062
3063 finishDrawTexture();
3064}
3065
Romain Guyf607bdc2010-09-10 19:20:06 -07003066// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003067#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3068#define kStdUnderline_Offset (1.0f / 9.0f)
3069#define kStdUnderline_Thickness (1.0f / 18.0f)
3070
3071void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3072 float x, float y, SkPaint* paint) {
3073 // Handle underline and strike-through
3074 uint32_t flags = paint->getFlags();
3075 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003076 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003077 float underlineWidth = length;
3078 // If length is > 0.0f, we already measured the text for the text alignment
3079 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003080 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003081 }
3082
Romain Guy211370f2012-02-01 16:10:55 -08003083 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003084 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003085 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003086
Raph Levien8b4072d2012-07-30 15:50:00 -07003087 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003088 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003089
Romain Guyf6834472011-01-23 13:32:12 -08003090 int linesCount = 0;
3091 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3092 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3093
3094 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003095 float points[pointsCount];
3096 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003097
3098 if (flags & SkPaint::kUnderlineText_Flag) {
3099 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003100 points[currentPoint++] = left;
3101 points[currentPoint++] = top;
3102 points[currentPoint++] = left + underlineWidth;
3103 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003104 }
3105
3106 if (flags & SkPaint::kStrikeThruText_Flag) {
3107 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003108 points[currentPoint++] = left;
3109 points[currentPoint++] = top;
3110 points[currentPoint++] = left + underlineWidth;
3111 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003112 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003113
Romain Guy726aeba2011-06-01 14:52:00 -07003114 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003115
Romain Guy726aeba2011-06-01 14:52:00 -07003116 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003117 }
3118 }
3119}
3120
Romain Guy672433d2013-01-04 19:05:13 -08003121status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3122 if (mSnapshot->isIgnored()) {
3123 return DrawGlInfo::kStatusDone;
3124 }
3125
Romain Guy735738c2012-12-03 12:34:51 -08003126 int color = paint->getColor();
3127 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003128 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003129 color |= 0x00ffffff;
3130 }
3131 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3132
3133 return drawColorRects(rects, count, color, mode);
3134}
3135
3136status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003137 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003138 if (count == 0) {
3139 return DrawGlInfo::kStatusDone;
3140 }
Romain Guy735738c2012-12-03 12:34:51 -08003141
Romain Guy672433d2013-01-04 19:05:13 -08003142 float left = FLT_MAX;
3143 float top = FLT_MAX;
3144 float right = FLT_MIN;
3145 float bottom = FLT_MIN;
3146
3147 int vertexCount = 0;
3148 Vertex mesh[count * 6];
3149 Vertex* vertex = mesh;
3150
Chris Craik2af46352012-11-26 18:30:17 -08003151 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003152 float l = rects[index + 0];
3153 float t = rects[index + 1];
3154 float r = rects[index + 2];
3155 float b = rects[index + 3];
3156
Romain Guy3b753822013-03-05 10:27:35 -08003157 Vertex::set(vertex++, l, b);
3158 Vertex::set(vertex++, l, t);
3159 Vertex::set(vertex++, r, t);
3160 Vertex::set(vertex++, l, b);
3161 Vertex::set(vertex++, r, t);
3162 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003163
Romain Guy3b753822013-03-05 10:27:35 -08003164 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003165
Romain Guy3b753822013-03-05 10:27:35 -08003166 left = fminf(left, l);
3167 top = fminf(top, t);
3168 right = fmaxf(right, r);
3169 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003170 }
3171
Romain Guy3b753822013-03-05 10:27:35 -08003172 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003173 return DrawGlInfo::kStatusDone;
3174 }
Romain Guy672433d2013-01-04 19:05:13 -08003175
Romain Guy672433d2013-01-04 19:05:13 -08003176 setupDraw();
3177 setupDrawNoTexture();
3178 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3179 setupDrawShader();
3180 setupDrawColorFilter();
3181 setupDrawBlending(mode);
3182 setupDrawProgram();
3183 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003184 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003185 setupDrawColorUniforms();
3186 setupDrawShaderUniforms();
3187 setupDrawColorFilterUniforms();
3188 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3189
Romain Guy8ce00302013-01-15 18:51:42 -08003190 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003191 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003192 }
3193
3194 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3195
3196 return DrawGlInfo::kStatusDrew;
3197}
3198
Romain Guy026c5e162010-06-28 17:12:22 -07003199void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003200 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003201 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003202 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003203 color |= 0x00ffffff;
3204 }
3205
Romain Guy70ca14e2010-12-13 18:24:33 -08003206 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003207 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003208 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003209 setupDrawShader();
3210 setupDrawColorFilter();
3211 setupDrawBlending(mode);
3212 setupDrawProgram();
3213 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3214 setupDrawColorUniforms();
3215 setupDrawShaderUniforms(ignoreTransform);
3216 setupDrawColorFilterUniforms();
3217 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003218
Romain Guyc95c8d62010-09-17 15:31:32 -07003219 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3220}
3221
Romain Guy82ba8142010-07-09 13:25:56 -07003222void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003223 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003224 int alpha;
3225 SkXfermode::Mode mode;
3226 getAlphaAndMode(paint, &alpha, &mode);
3227
Romain Guyd21b6e12011-11-30 20:21:23 -08003228 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003229
Romain Guy3b753822013-03-05 10:27:35 -08003230 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3231 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3232 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003233
Romain Guyd21b6e12011-11-30 20:21:23 -08003234 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003235 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3236 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3237 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3238 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003239 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003240 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3241 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3242 GL_TRIANGLE_STRIP, gMeshCount);
3243 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003244}
3245
Romain Guybd6b79b2010-06-26 00:13:53 -07003246void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003247 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3248 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003249 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003250}
3251
3252void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003253 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003254 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003255 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003256
Romain Guy746b7402010-10-26 16:27:31 -07003257 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003258 setupDrawWithTexture();
3259 setupDrawColor(alpha, alpha, alpha, alpha);
3260 setupDrawColorFilter();
3261 setupDrawBlending(blend, mode, swapSrcDst);
3262 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003263 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003264 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003265 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003266 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003268 }
Romain Guy886b2752013-01-04 12:26:18 -08003269 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003270 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003271 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003272 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003273
Romain Guy6820ac82010-09-15 18:11:50 -07003274 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003275
3276 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003277}
3278
Romain Guy886b2752013-01-04 12:26:18 -08003279void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3280 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3281 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3282 bool ignoreTransform, bool dirty) {
3283
3284 setupDraw();
3285 setupDrawWithTexture(true);
3286 if (hasColor) {
3287 setupDrawAlpha8Color(color, alpha);
3288 }
3289 setupDrawColorFilter();
3290 setupDrawShader();
3291 setupDrawBlending(true, mode);
3292 setupDrawProgram();
3293 if (!dirty) setupDrawDirtyRegionsDisabled();
3294 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3295 setupDrawTexture(texture);
3296 setupDrawPureColorUniforms();
3297 setupDrawColorFilterUniforms();
3298 setupDrawShaderUniforms();
3299 setupDrawMesh(vertices, texCoords);
3300
3301 glDrawArrays(drawMode, 0, elementsCount);
3302
3303 finishDrawTexture();
3304}
3305
Romain Guya5aed0d2010-09-09 14:42:43 -07003306void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003307 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003308 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003309
Romain Guy82ba8142010-07-09 13:25:56 -07003310 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003311 // These blend modes are not supported by OpenGL directly and have
3312 // to be implemented using shaders. Since the shader will perform
3313 // the blending, turn blending off here
3314 // If the blend mode cannot be implemented using shaders, fall
3315 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003316 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003317 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003318 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003319 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003320
Romain Guy82bc7a72012-01-03 14:13:39 -08003321 if (mCaches.blend) {
3322 glDisable(GL_BLEND);
3323 mCaches.blend = false;
3324 }
3325
3326 return;
3327 } else {
3328 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003329 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003330 }
3331
3332 if (!mCaches.blend) {
3333 glEnable(GL_BLEND);
3334 }
3335
3336 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3337 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3338
3339 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3340 glBlendFunc(sourceMode, destMode);
3341 mCaches.lastSrcMode = sourceMode;
3342 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003343 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003344 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003345 glDisable(GL_BLEND);
3346 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003347 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003348}
3349
Romain Guy889f8d12010-07-29 14:37:42 -07003350bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003351 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003352 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003353 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003354 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003355 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003356 }
Romain Guy6926c722010-07-12 20:20:03 -07003357 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003358}
3359
Romain Guy026c5e162010-06-28 17:12:22 -07003360void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003361 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003362 TextureVertex::setUV(v++, u1, v1);
3363 TextureVertex::setUV(v++, u2, v1);
3364 TextureVertex::setUV(v++, u1, v2);
3365 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003366}
3367
Chet Haase5c13d892010-10-08 08:37:55 -07003368void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003369 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003370 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003371}
3372
Romain Guy9d5316e2010-06-24 19:30:36 -07003373}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003374}; // namespace android