blob: 1fa1b201dc18abd583abc81e6430aaa7da668295 [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"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik710f46d2012-09-17 17:25:49 -070036#include "PathRenderer.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
83 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
84 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
104 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guyfb8b7632010-08-23 21:05:08 -0700112OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700113 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700114 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700115 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800116 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
Romain Guy87e2f7572012-09-24 11:37:12 -0700121
122 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700126 // The context has already been destroyed at this point, do not call
127 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700128}
129
Romain Guy87e2f7572012-09-24 11:37:12 -0700130void OpenGLRenderer::initProperties() {
131 char property[PROPERTY_VALUE_MAX];
132 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
133 mScissorOptimizationDisabled = !strcasecmp(property, "true");
134 INIT_LOGD(" Scissor optimization %s",
135 mScissorOptimizationDisabled ? "disabled" : "enabled");
136 } else {
137 INIT_LOGD(" Scissor optimization enabled");
138 }
Romain Guy13631f32012-01-30 17:41:55 -0800139}
140
141///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700142// Setup
143///////////////////////////////////////////////////////////////////////////////
144
Romain Guy49c5fc02012-05-15 11:10:01 -0700145bool OpenGLRenderer::isDeferred() {
146 return false;
147}
148
Romain Guy85bf02f2010-06-22 13:11:24 -0700149void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700150 initViewport(width, height);
151
152 glDisable(GL_DITHER);
153 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155 glEnableVertexAttribArray(Program::kBindingPosition);
156}
157
158void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700159 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700160
161 mWidth = width;
162 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700163
164 mFirstSnapshot->height = height;
165 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700166}
167
Romain Guy7c25aab2012-10-18 15:05:02 -0700168status_t OpenGLRenderer::prepare(bool opaque) {
Chet Haase44b2fe32012-06-06 19:03:58 -0700169 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800170}
171
Romain Guyc3fedaf2013-01-29 17:26:25 -0800172status_t OpenGLRenderer::prepareDirty(float left, float top,
173 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800174 mCaches.clearGarbage();
175
Romain Guy8aef54f2010-09-01 15:13:49 -0700176 mSnapshot = new Snapshot(mFirstSnapshot,
177 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800178 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700179 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180
Romain Guy7d7b5492011-01-24 16:33:45 -0800181 mSnapshot->setClip(left, top, right, bottom);
Romain Guy41308e22012-10-22 20:02:43 -0700182 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700183
Romain Guy11cb6422012-09-21 00:39:43 -0700184 updateLayers();
185
Romain Guydcfc8362013-01-03 13:08:57 -0800186 discardFramebuffer(left, top, right, bottom);
Romain Guy45e4c3d2012-09-11 17:17:07 -0700187
Romain Guyddf74372012-05-22 14:07:07 -0700188 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800189
Romain Guy54c1a642012-09-27 17:55:46 -0700190 // Functors break the tiling extension in pretty spectacular ways
191 // This ensures we don't use tiling when a functor is going to be
192 // invoked during the frame
193 mSuppressTiling = mCaches.hasRegisteredFunctors();
194
Romain Guy2b7028e2012-09-19 17:25:38 -0700195 mTilingSnapshot = mSnapshot;
Romain Guy57b52682012-09-20 17:38:46 -0700196 startTiling(mTilingSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700197
Romain Guy7c450aa2012-09-21 19:15:00 -0700198 debugOverdraw(true, true);
199
Romain Guy7c25aab2012-10-18 15:05:02 -0700200 return clear(left, top, right, bottom, opaque);
201}
202
Romain Guydcfc8362013-01-03 13:08:57 -0800203void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
204 // If we know that we are going to redraw the entire framebuffer,
205 // perform a discard to let the driver know we don't need to preserve
206 // the back buffer for this frame.
207 if (mCaches.extensions.hasDiscardFramebuffer() &&
208 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
209 const GLenum attachments[] = { getTargetFbo() == 0 ? (const GLenum) GL_COLOR_EXT :
Romain Guyc3fedaf2013-01-29 17:26:25 -0800210 (const GLenum) GL_COLOR_ATTACHMENT0, GL_STENCIL_EXT };
Romain Guydcfc8362013-01-03 13:08:57 -0800211 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
212 }
213}
214
Romain Guy7c25aab2012-10-18 15:05:02 -0700215status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700216 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700217 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700218 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700219 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700220 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700221 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700222
Romain Guy7c25aab2012-10-18 15:05:02 -0700223 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700224 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700225}
226
227void OpenGLRenderer::syncState() {
228 glViewport(0, 0, mWidth, mHeight);
229
230 if (mCaches.blend) {
231 glEnable(GL_BLEND);
232 } else {
233 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700234 }
Romain Guybb9524b2010-06-22 18:56:38 -0700235}
236
Romain Guy57b52682012-09-20 17:38:46 -0700237void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700238 if (!mSuppressTiling) {
239 Rect* clip = mTilingSnapshot->clipRect;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800240 if (s->flags & Snapshot::kFlagFboTarget) {
241 clip = &s->layer->clipRect;
Romain Guy54c1a642012-09-27 17:55:46 -0700242 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700243
Romain Guyc3fedaf2013-01-29 17:26:25 -0800244 startTiling(*clip, s->height, opaque);
245 }
246}
247
248void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
249 if (!mSuppressTiling) {
250 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
251 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700252 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700253}
254
255void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700256 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700257}
258
Romain Guyb025b9c2010-09-16 14:16:48 -0700259void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700260 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700261 endTiling();
262
Romain Guy11cb6422012-09-21 00:39:43 -0700263 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700264#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700265 GLenum status = GL_NO_ERROR;
266 while ((status = glGetError()) != GL_NO_ERROR) {
267 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
268 switch (status) {
269 case GL_INVALID_ENUM:
270 ALOGE(" GL_INVALID_ENUM");
271 break;
272 case GL_INVALID_VALUE:
273 ALOGE(" GL_INVALID_VALUE");
274 break;
275 case GL_INVALID_OPERATION:
276 ALOGE(" GL_INVALID_OPERATION");
277 break;
278 case GL_OUT_OF_MEMORY:
279 ALOGE(" Out of memory!");
280 break;
281 }
Romain Guya07105b2011-01-10 21:14:18 -0800282 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700283#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700284
Romain Guyc15008e2010-11-10 11:59:15 -0800285#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800286 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700287#else
288 if (mCaches.getDebugLevel() & kDebugMemory) {
289 mCaches.dumpMemoryUsage();
290 }
Romain Guyc15008e2010-11-10 11:59:15 -0800291#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700292 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700293}
294
Romain Guy6c319ca2011-01-11 14:29:25 -0800295void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700296 if (mCaches.currentProgram) {
297 if (mCaches.currentProgram->isInUse()) {
298 mCaches.currentProgram->remove();
299 mCaches.currentProgram = NULL;
300 }
301 }
Romain Guy50c0f092010-10-19 11:42:22 -0700302 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800303 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800304 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800305 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700306 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700307}
308
Romain Guy6c319ca2011-01-11 14:29:25 -0800309void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800310 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800311 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700312 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700313 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700314
Romain Guy3e263fa2011-12-12 16:47:48 -0800315 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
316
Chet Haase80250612012-08-15 13:46:54 -0700317 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700318 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800319 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700320 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700321
Romain Guya1d3c912011-12-13 14:55:06 -0800322 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700323
Romain Guy50c0f092010-10-19 11:42:22 -0700324 mCaches.blend = true;
325 glEnable(GL_BLEND);
326 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
327 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700328}
329
Romain Guy35643dd2012-09-18 15:40:58 -0700330void OpenGLRenderer::resumeAfterLayer() {
331 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
332 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
333 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700334 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700335
336 mCaches.resetScissor();
337 dirtyClip();
338}
339
Romain Guyba6be8a2012-04-23 18:22:09 -0700340void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700341 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700342}
343
344void OpenGLRenderer::attachFunctor(Functor* functor) {
345 mFunctors.add(functor);
346}
347
Romain Guy8f3b8e32012-03-27 16:33:45 -0700348status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
349 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700350 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700351
Romain Guyba6be8a2012-04-23 18:22:09 -0700352 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800353 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700354 SortedVector<Functor*> functors(mFunctors);
355 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700356
Romain Guyba6be8a2012-04-23 18:22:09 -0700357 DrawGlInfo info;
358 info.clipLeft = 0;
359 info.clipTop = 0;
360 info.clipRight = 0;
361 info.clipBottom = 0;
362 info.isLayer = false;
363 info.width = 0;
364 info.height = 0;
365 memset(info.transform, 0, sizeof(float) * 16);
366
367 for (size_t i = 0; i < count; i++) {
368 Functor* f = functors.itemAt(i);
369 result |= (*f)(DrawGlInfo::kModeProcess, &info);
370
Chris Craikc2c95432012-04-25 15:13:52 -0700371 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700372 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
373 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700374 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700375
Chris Craikc2c95432012-04-25 15:13:52 -0700376 if (result & DrawGlInfo::kStatusInvoke) {
377 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700378 }
379 }
Chris Craikd15321b2012-11-28 14:45:04 -0800380 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700381 }
382
383 return result;
384}
385
386status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800387 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700388 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700389
Romain Guy8a4ac612012-07-17 17:32:48 -0700390 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800391 if (mDirtyClip) {
392 setScissorFromClip();
393 }
Romain Guyd643bb52011-03-01 14:55:21 -0800394
Romain Guy80911b82011-03-16 15:30:12 -0700395 Rect clip(*mSnapshot->clipRect);
396 clip.snapToPixelBoundaries();
397
Romain Guyd643bb52011-03-01 14:55:21 -0800398 // Since we don't know what the functor will draw, let's dirty
399 // tne entire clip region
400 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800401 dirtyLayerUnchecked(clip, getRegion());
402 }
Romain Guyd643bb52011-03-01 14:55:21 -0800403
Romain Guy08aa2cb2011-03-17 11:06:57 -0700404 DrawGlInfo info;
405 info.clipLeft = clip.left;
406 info.clipTop = clip.top;
407 info.clipRight = clip.right;
408 info.clipBottom = clip.bottom;
409 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700410 info.width = getSnapshot()->viewport.getWidth();
411 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700412 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700413
Chet Haase48659092012-05-31 15:21:51 -0700414 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800415
Romain Guy8f3b8e32012-03-27 16:33:45 -0700416 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700417 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800418 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Chris Craik65924a32012-04-05 17:52:11 -0700420 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700421 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700422 }
Romain Guycabfcc12011-03-07 18:06:46 -0800423 }
424
Chet Haasedaf98e92011-01-10 14:10:36 -0800425 resume();
Romain Guy65549432012-03-26 16:45:05 -0700426 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800427}
428
Romain Guyf6a11b82010-06-23 17:47:49 -0700429///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700430// Debug
431///////////////////////////////////////////////////////////////////////////////
432
433void OpenGLRenderer::startMark(const char* name) const {
434 mCaches.startMark(0, name);
435}
436
437void OpenGLRenderer::endMark() const {
438 mCaches.endMark();
439}
440
441void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
442 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
443 if (clear) {
444 mCaches.disableScissor();
445 mCaches.stencil.clear();
446 }
447 if (enable) {
448 mCaches.stencil.enableDebugWrite();
449 } else {
450 mCaches.stencil.disable();
451 }
452 }
453}
454
455void OpenGLRenderer::renderOverdraw() {
456 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
457 const Rect* clip = mTilingSnapshot->clipRect;
458
459 mCaches.enableScissor();
460 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
461 clip->right - clip->left, clip->bottom - clip->top);
462
463 mCaches.stencil.enableDebugTest(2);
464 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
465 mCaches.stencil.enableDebugTest(3);
466 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
467 mCaches.stencil.enableDebugTest(4);
468 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
469 mCaches.stencil.enableDebugTest(4, true);
470 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
471 mCaches.stencil.disable();
472 }
473}
474
475///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700476// Layers
477///////////////////////////////////////////////////////////////////////////////
478
479bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
480 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
481 OpenGLRenderer* renderer = layer->renderer;
482 Rect& dirty = layer->dirtyRect;
483
Romain Guy7c450aa2012-09-21 19:15:00 -0700484 if (inFrame) {
485 endTiling();
486 debugOverdraw(false, false);
487 }
Romain Guy11cb6422012-09-21 00:39:43 -0700488
489 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
490 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
491 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
492 renderer->finish();
493
494 if (inFrame) {
495 resumeAfterLayer();
496 startTiling(mSnapshot);
497 }
498
499 dirty.setEmpty();
500 layer->deferredUpdateScheduled = false;
501 layer->renderer = NULL;
502 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800503 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700504
505 return true;
506 }
507
508 return false;
509}
510
511void OpenGLRenderer::updateLayers() {
512 int count = mLayerUpdates.size();
513 if (count > 0) {
514 startMark("Layer Updates");
515
516 // Note: it is very important to update the layers in reverse order
517 for (int i = count - 1; i >= 0; i--) {
518 Layer* layer = mLayerUpdates.itemAt(i);
519 updateLayer(layer, false);
520 mCaches.resourceCache.decrementRefcount(layer);
521 }
522 mLayerUpdates.clear();
523
524 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
525 endMark();
526 }
527}
528
529void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
530 if (layer) {
531 mLayerUpdates.push_back(layer);
532 mCaches.resourceCache.incrementRefcount(layer);
533 }
534}
535
536void OpenGLRenderer::clearLayerUpdates() {
537 size_t count = mLayerUpdates.size();
538 if (count > 0) {
539 mCaches.resourceCache.lock();
540 for (size_t i = 0; i < count; i++) {
541 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
542 }
543 mCaches.resourceCache.unlock();
544 mLayerUpdates.clear();
545 }
546}
547
548///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700549// State management
550///////////////////////////////////////////////////////////////////////////////
551
Romain Guybb9524b2010-06-22 18:56:38 -0700552int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700553 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700554}
555
556int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700557 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700558}
559
560void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700561 if (mSaveCount > 1) {
562 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700563 }
Romain Guybb9524b2010-06-22 18:56:38 -0700564}
565
566void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700567 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700568
Romain Guy8fb95422010-08-17 18:38:51 -0700569 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700570 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700571 }
Romain Guybb9524b2010-06-22 18:56:38 -0700572}
573
Romain Guy8aef54f2010-09-01 15:13:49 -0700574int OpenGLRenderer::saveSnapshot(int flags) {
575 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700576 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700577}
578
579bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700580 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700581 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700582 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700583
Romain Guybd6b79b2010-06-26 00:13:53 -0700584 sp<Snapshot> current = mSnapshot;
585 sp<Snapshot> previous = mSnapshot->previous;
586
Romain Guyeb993562010-10-05 18:14:38 -0700587 if (restoreOrtho) {
588 Rect& r = previous->viewport;
589 glViewport(r.left, r.top, r.right, r.bottom);
590 mOrthoMatrix.load(current->orthoMatrix);
591 }
592
Romain Guy8b55f372010-08-18 17:10:07 -0700593 mSaveCount--;
594 mSnapshot = previous;
595
Romain Guy2542d192010-08-18 11:47:12 -0700596 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700597 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700598 }
Romain Guy2542d192010-08-18 11:47:12 -0700599
Romain Guy5ec99242010-11-03 16:19:08 -0700600 if (restoreLayer) {
601 composeLayer(current, previous);
602 }
603
Romain Guy2542d192010-08-18 11:47:12 -0700604 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700605}
606
Romain Guyf6a11b82010-06-23 17:47:49 -0700607///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700608// Layers
609///////////////////////////////////////////////////////////////////////////////
610
611int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700612 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700613 const GLuint previousFbo = mSnapshot->fbo;
614 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700615
Romain Guyaf636eb2010-12-09 17:47:21 -0800616 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700617 int alpha = 255;
618 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700619
Romain Guye45362c2010-11-03 19:58:32 -0700620 if (p) {
621 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700622 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700623 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700624 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700625 }
Romain Guyd55a8612010-06-28 17:42:46 -0700626
Chet Haased48885a2012-08-28 17:43:28 -0700627 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700628 }
Romain Guyd55a8612010-06-28 17:42:46 -0700629
630 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700631}
632
633int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
634 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700635 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700636 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700637 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700638 SkPaint paint;
639 paint.setAlpha(alpha);
640 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700641 }
Romain Guyd55a8612010-06-28 17:42:46 -0700642}
Romain Guybd6b79b2010-06-26 00:13:53 -0700643
Romain Guy1c740bc2010-09-13 18:00:09 -0700644/**
645 * Layers are viewed by Skia are slightly different than layers in image editing
646 * programs (for instance.) When a layer is created, previously created layers
647 * and the frame buffer still receive every drawing command. For instance, if a
648 * layer is created and a shape intersecting the bounds of the layers and the
649 * framebuffer is draw, the shape will be drawn on both (unless the layer was
650 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
651 *
652 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
653 * texture. Unfortunately, this is inefficient as it requires every primitive to
654 * be drawn n + 1 times, where n is the number of active layers. In practice this
655 * means, for every primitive:
656 * - Switch active frame buffer
657 * - Change viewport, clip and projection matrix
658 * - Issue the drawing
659 *
660 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700661 * To avoid this, layers are implemented in a different way here, at least in the
662 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
663 * is set. When this flag is set we can redirect all drawing operations into a
664 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700665 *
666 * This implementation relies on the frame buffer being at least RGBA 8888. When
667 * a layer is created, only a texture is created, not an FBO. The content of the
668 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700669 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700670 * buffer and drawing continues as normal. This technique therefore treats the
671 * frame buffer as a scratch buffer for the layers.
672 *
673 * To compose the layers back onto the frame buffer, each layer texture
674 * (containing the original frame buffer data) is drawn as a simple quad over
675 * the frame buffer. The trick is that the quad is set as the composition
676 * destination in the blending equation, and the frame buffer becomes the source
677 * of the composition.
678 *
679 * Drawing layers with an alpha value requires an extra step before composition.
680 * An empty quad is drawn over the layer's region in the frame buffer. This quad
681 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
682 * quad is used to multiply the colors in the frame buffer. This is achieved by
683 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
684 * GL_ZERO, GL_SRC_ALPHA.
685 *
686 * Because glCopyTexImage2D() can be slow, an alternative implementation might
687 * be use to draw a single clipped layer. The implementation described above
688 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700689 *
690 * (1) The frame buffer is actually not cleared right away. To allow the GPU
691 * to potentially optimize series of calls to glCopyTexImage2D, the frame
692 * buffer is left untouched until the first drawing operation. Only when
693 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700694 */
Chet Haased48885a2012-08-28 17:43:28 -0700695bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
696 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700697 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700698 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700699
Romain Guyeb993562010-10-05 18:14:38 -0700700 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
701
Romain Guyf607bdc2010-09-10 19:20:06 -0700702 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700703 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700704 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700705 Rect untransformedBounds(bounds);
706 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700707
Chet Haased48885a2012-08-28 17:43:28 -0700708 // Layers only make sense if they are in the framebuffer's bounds
709 if (bounds.intersect(*mSnapshot->clipRect)) {
710 // We cannot work with sub-pixels in this case
711 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700712
Chet Haased48885a2012-08-28 17:43:28 -0700713 // When the layer is not an FBO, we may use glCopyTexImage so we
714 // need to make sure the layer does not extend outside the bounds
715 // of the framebuffer
716 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700717 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700718 } else if (fboLayer) {
719 clip.set(bounds);
720 mat4 inverse;
721 inverse.loadInverse(*mSnapshot->transform);
722 inverse.mapRect(clip);
723 clip.snapToPixelBoundaries();
724 if (clip.intersect(untransformedBounds)) {
725 clip.translate(-left, -top);
726 bounds.set(untransformedBounds);
727 } else {
728 clip.setEmpty();
729 }
Romain Guyad37cd32011-03-15 11:12:25 -0700730 }
Chet Haased48885a2012-08-28 17:43:28 -0700731 } else {
732 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700733 }
Romain Guybf434112010-09-16 14:40:17 -0700734
Romain Guy746b7402010-10-26 16:27:31 -0700735 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700736 bounds.getHeight() > mCaches.maxTextureSize ||
737 (fboLayer && clip.isEmpty())) {
738 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700739 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700740 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700741 }
742
743 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700744 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700745 return false;
746 }
Romain Guyf18fd992010-07-08 11:45:51 -0700747
Romain Guya1d3c912011-12-13 14:55:06 -0800748 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700749 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700750 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700751 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700752 }
753
Romain Guy9ace8f52011-07-07 20:50:11 -0700754 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700755 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
757 bounds.getWidth() / float(layer->getWidth()), 0.0f);
758 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700759 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700760 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700761
Romain Guy8fb95422010-08-17 18:38:51 -0700762 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700763 mSnapshot->flags |= Snapshot::kFlagIsLayer;
764 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700765
Romain Guyeb993562010-10-05 18:14:38 -0700766 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700767 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700768 } else {
769 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800771 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 if (layer->isEmpty()) {
773 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700774 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 layer->getWidth(), layer->getHeight(), 0);
776 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800777 } else {
778 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700779 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800780 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700781
Romain Guy54be1cd2011-06-13 19:04:27 -0700782 // Enqueue the buffer coordinates to clear the corresponding region later
783 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700784 }
Romain Guyeb993562010-10-05 18:14:38 -0700785 }
Romain Guyf86ef572010-07-01 11:05:42 -0700786
Romain Guyd55a8612010-06-28 17:42:46 -0700787 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700788}
789
Chet Haased48885a2012-08-28 17:43:28 -0700790bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800791 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700793
Chet Haased48885a2012-08-28 17:43:28 -0700794 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800795 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
796 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700797 mSnapshot->fbo = layer->getFbo();
798 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
799 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
800 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
801 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700802 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700803
Romain Guy2b7028e2012-09-19 17:25:38 -0700804 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700805 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
808 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
810 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 if (layer->isEmpty()) {
812 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
813 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700814 }
815
816 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700817 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guyc3fedaf2013-01-29 17:26:25 -0800819 startTiling(mSnapshot, !layer->isBlend());
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
821 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700822 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800823 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700825 glClear(GL_COLOR_BUFFER_BIT);
826
827 dirtyClip();
828
829 // Change the ortho projection
830 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
831 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
832
833 return true;
834}
835
Romain Guy1c740bc2010-09-13 18:00:09 -0700836/**
837 * Read the documentation of createLayer() before doing anything in this method.
838 */
Romain Guy1d83e192010-08-17 11:37:00 -0700839void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
840 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000841 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700842 return;
843 }
844
Romain Guy8ce00302013-01-15 18:51:42 -0800845 Layer* layer = current->layer;
846 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700848
849 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700850 endTiling();
851
Romain Guye0aa84b2012-04-03 19:30:26 -0700852 // Detach the texture from the FBO
853 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800854
855 layer->removeFbo(false);
856
Romain Guyeb993562010-10-05 18:14:38 -0700857 // Unbind current FBO and restore previous one
858 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700859 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700860
861 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700862 }
863
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700865 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700867 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700869 }
870
Romain Guy03750a02010-10-18 14:06:08 -0700871 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700872
Romain Guya1d3c912011-12-13 14:55:06 -0800873 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700874
Romain Guy5b3b3522010-10-27 18:57:51 -0700875 // When the layer is stored in an FBO, we can save a bit of fillrate by
876 // drawing only the dirty region
877 if (fboLayer) {
878 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 if (layer->getColorFilter()) {
880 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800881 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700882 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800884 resetColorFilter();
885 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 } else if (!rect.isEmpty()) {
887 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
888 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700889 }
Romain Guy8b55f372010-08-18 17:10:07 -0700890
Romain Guy746b7402010-10-26 16:27:31 -0700891 dirtyClip();
892
Romain Guyeb993562010-10-05 18:14:38 -0700893 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700894 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700895 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700896 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700897 }
898}
899
Romain Guyaa6c24c2011-04-28 18:40:04 -0700900void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700901 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700902
903 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700905 setupDrawWithTexture();
906 } else {
907 setupDrawWithExternalTexture();
908 }
909 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700910 setupDrawColor(alpha, alpha, alpha, alpha);
911 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700912 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700913 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914 setupDrawPureColorUniforms();
915 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
917 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700918 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700920 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700921 if (mSnapshot->transform->isPureTranslate() &&
922 layer->getWidth() == (uint32_t) rect.getWidth() &&
923 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
925 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
926
Romain Guyd21b6e12011-11-30 20:21:23 -0800927 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
929 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800930 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
932 }
933 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
935
936 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
937
938 finishDrawTexture();
939}
940
Romain Guy5b3b3522010-10-27 18:57:51 -0700941void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700943 const Rect& texCoords = layer->texCoords;
944 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
945 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700946
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 float x = rect.left;
948 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700949 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700950 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700951 layer->getHeight() == (uint32_t) rect.getHeight();
952
953 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 // When we're swapping, the layer is already in screen coordinates
955 if (!swap) {
956 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
957 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
958 }
959
Romain Guyd21b6e12011-11-30 20:21:23 -0800960 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800962 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 }
964
965 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
966 layer->getTexture(), layer->getAlpha() / 255.0f,
967 layer->getMode(), layer->isBlend(),
968 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
969 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700970
Romain Guyaa6c24c2011-04-28 18:40:04 -0700971 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
972 } else {
973 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
974 drawTextureLayer(layer, rect);
975 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
976 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700977}
978
979void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700981 layer->setRegionAsRect();
982
Romain Guy40667672011-03-18 14:34:03 -0700983 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700984
Romain Guy5b3b3522010-10-27 18:57:51 -0700985 layer->region.clear();
986 return;
987 }
988
Romain Guy8a3957d2011-09-07 17:55:15 -0700989 // TODO: See LayerRenderer.cpp::generateMesh() for important
990 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800991 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700992 size_t count;
993 const android::Rect* rects = layer->region.getArray(&count);
994
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 const float alpha = layer->getAlpha() / 255.0f;
996 const float texX = 1.0f / float(layer->getWidth());
997 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800998 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700999
Romain Guy8ce00302013-01-15 18:51:42 -08001000 setupDraw();
1001
1002 // We must get (and therefore bind) the region mesh buffer
1003 // after we setup drawing in case we need to mess with the
1004 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001005 TextureVertex* mesh = mCaches.getRegionMesh();
1006 GLsizei numQuads = 0;
1007
Romain Guy7230a742011-01-10 22:26:16 -08001008 setupDrawWithTexture();
1009 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001010 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001011 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001012 setupDrawProgram();
1013 setupDrawDirtyRegionsDisabled();
1014 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001015 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 setupDrawTexture(layer->getTexture());
1017 if (mSnapshot->transform->isPureTranslate()) {
1018 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1019 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1020
Romain Guyd21b6e12011-11-30 20:21:23 -08001021 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001022 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1023 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001024 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001025 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1026 }
Romain Guy15bc6432011-12-13 13:11:32 -08001027 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001028
1029 for (size_t i = 0; i < count; i++) {
1030 const android::Rect* r = &rects[i];
1031
1032 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001033 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001035 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001036
1037 // TODO: Reject quads outside of the clip
1038 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1039 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1040 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1041 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1042
1043 numQuads++;
1044
1045 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1046 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1047 numQuads = 0;
1048 mesh = mCaches.getRegionMesh();
1049 }
1050 }
1051
1052 if (numQuads > 0) {
1053 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1054 }
1055
Romain Guy7230a742011-01-10 22:26:16 -08001056 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001057
1058#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001059 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001060#endif
1061
1062 layer->region.clear();
1063 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001064}
1065
Romain Guy3a3133d2011-02-01 22:59:58 -08001066void OpenGLRenderer::drawRegionRects(const Region& region) {
1067#if DEBUG_LAYERS_AS_REGIONS
1068 size_t count;
1069 const android::Rect* rects = region.getArray(&count);
1070
1071 uint32_t colors[] = {
1072 0x7fff0000, 0x7f00ff00,
1073 0x7f0000ff, 0x7fff00ff,
1074 };
1075
1076 int offset = 0;
1077 int32_t top = rects[0].top;
1078
1079 for (size_t i = 0; i < count; i++) {
1080 if (top != rects[i].top) {
1081 offset ^= 0x2;
1082 top = rects[i].top;
1083 }
1084
1085 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1086 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1087 SkXfermode::kSrcOver_Mode);
1088 }
1089#endif
1090}
1091
Romain Guy8ce00302013-01-15 18:51:42 -08001092void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1093 SkXfermode::Mode mode, bool dirty) {
1094 int count = 0;
1095 Vector<float> rects;
1096
1097 SkRegion::Iterator it(region);
1098 while (!it.done()) {
1099 const SkIRect& r = it.rect();
1100 rects.push(r.fLeft);
1101 rects.push(r.fTop);
1102 rects.push(r.fRight);
1103 rects.push(r.fBottom);
1104 count++;
1105 it.next();
1106 }
1107
1108 drawColorRects(rects.array(), count, color, mode, true, dirty);
1109}
1110
Romain Guy5b3b3522010-10-27 18:57:51 -07001111void OpenGLRenderer::dirtyLayer(const float left, const float top,
1112 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001113 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001114 Rect bounds(left, top, right, bottom);
1115 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001116 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001118}
1119
1120void OpenGLRenderer::dirtyLayer(const float left, const float top,
1121 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001122 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001123 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001124 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001125 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001126}
1127
1128void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001129 if (bounds.intersect(*mSnapshot->clipRect)) {
1130 bounds.snapToPixelBoundaries();
1131 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1132 if (!dirty.isEmpty()) {
1133 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001134 }
1135 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001136}
1137
Romain Guy54be1cd2011-06-13 19:04:27 -07001138void OpenGLRenderer::clearLayerRegions() {
1139 const size_t count = mLayers.size();
1140 if (count == 0) return;
1141
1142 if (!mSnapshot->isIgnored()) {
1143 // Doing several glScissor/glClear here can negatively impact
1144 // GPUs with a tiler architecture, instead we draw quads with
1145 // the Clear blending mode
1146
1147 // The list contains bounds that have already been clipped
1148 // against their initial clip rect, and the current clip
1149 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001150 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001151
1152 Vertex mesh[count * 6];
1153 Vertex* vertex = mesh;
1154
1155 for (uint32_t i = 0; i < count; i++) {
1156 Rect* bounds = mLayers.itemAt(i);
1157
1158 Vertex::set(vertex++, bounds->left, bounds->bottom);
1159 Vertex::set(vertex++, bounds->left, bounds->top);
1160 Vertex::set(vertex++, bounds->right, bounds->top);
1161 Vertex::set(vertex++, bounds->left, bounds->bottom);
1162 Vertex::set(vertex++, bounds->right, bounds->top);
1163 Vertex::set(vertex++, bounds->right, bounds->bottom);
1164
1165 delete bounds;
1166 }
1167
1168 setupDraw(false);
1169 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1170 setupDrawBlending(true, SkXfermode::kClear_Mode);
1171 setupDrawProgram();
1172 setupDrawPureColorUniforms();
1173 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001174 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001175
Romain Guy54be1cd2011-06-13 19:04:27 -07001176 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001177
1178 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001179 } else {
1180 for (uint32_t i = 0; i < count; i++) {
1181 delete mLayers.itemAt(i);
1182 }
1183 }
1184
1185 mLayers.clear();
1186}
1187
Romain Guybd6b79b2010-06-26 00:13:53 -07001188///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001189// Transforms
1190///////////////////////////////////////////////////////////////////////////////
1191
1192void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001193 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001194}
1195
1196void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001197 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001198}
1199
1200void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001201 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001202}
1203
Romain Guy807daf72011-01-18 11:19:19 -08001204void OpenGLRenderer::skew(float sx, float sy) {
1205 mSnapshot->transform->skew(sx, sy);
1206}
1207
Romain Guyf6a11b82010-06-23 17:47:49 -07001208void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001209 if (matrix) {
1210 mSnapshot->transform->load(*matrix);
1211 } else {
1212 mSnapshot->transform->loadIdentity();
1213 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001214}
1215
1216void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001217 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001218}
1219
1220void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001221 SkMatrix transform;
1222 mSnapshot->transform->copyTo(transform);
1223 transform.preConcat(*matrix);
1224 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001225}
1226
1227///////////////////////////////////////////////////////////////////////////////
1228// Clipping
1229///////////////////////////////////////////////////////////////////////////////
1230
Romain Guybb9524b2010-06-22 18:56:38 -07001231void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001232 Rect clip(*mSnapshot->clipRect);
1233 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001234
Romain Guy8a4ac612012-07-17 17:32:48 -07001235 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1236 clip.getWidth(), clip.getHeight())) {
1237 mDirtyClip = false;
1238 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001239}
1240
Romain Guy8ce00302013-01-15 18:51:42 -08001241void OpenGLRenderer::ensureStencilBuffer() {
1242 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1243 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1244 // just hope we have one when hasLayer() returns false.
1245 if (hasLayer()) {
1246 attachStencilBufferToLayer(mSnapshot->layer);
1247 }
1248}
1249
1250void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1251 // The layer's FBO is already bound when we reach this stage
1252 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001253 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1254 // is attached after we initiated tiling. We must turn it off,
1255 // attach the new render buffer then turn tiling back on
1256 endTiling();
1257
Romain Guy8ce00302013-01-15 18:51:42 -08001258 // TODO: See Layer::removeFbo(). The stencil renderbuffer should be cached
1259 GLuint buffer;
1260 glGenRenderbuffers(1, &buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001261
Romain Guy8ce00302013-01-15 18:51:42 -08001262 layer->setStencilRenderBuffer(buffer);
Romain Guy2055aba2013-01-18 16:42:51 -08001263 layer->bindStencilRenderBuffer();
1264 layer->allocateStencilRenderBuffer();
1265
1266 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001267
1268 startTiling(layer->clipRect, layer->layer.getHeight(), !layer->isBlend());
Romain Guy8ce00302013-01-15 18:51:42 -08001269 }
1270}
1271
1272void OpenGLRenderer::setStencilFromClip() {
1273 if (!mCaches.debugOverdraw) {
1274 if (!mSnapshot->clipRegion->isEmpty()) {
1275 // NOTE: The order here is important, we must set dirtyClip to false
1276 // before any draw call to avoid calling back into this method
1277 mDirtyClip = false;
1278
1279 ensureStencilBuffer();
1280
1281 mCaches.stencil.enableWrite();
1282
1283 // Clear the stencil but first make sure we restrict drawing
1284 // to the region's bounds
1285 bool resetScissor = mCaches.enableScissor();
1286 if (resetScissor) {
1287 // The scissor was not set so we now need to update it
1288 setScissorFromClip();
1289 }
1290 mCaches.stencil.clear();
1291 if (resetScissor) mCaches.disableScissor();
1292
1293 // NOTE: We could use the region contour path to generate a smaller mesh
1294 // Since we are using the stencil we could use the red book path
1295 // drawing technique. It might increase bandwidth usage though.
1296
1297 // The last parameter is important: we are not drawing in the color buffer
1298 // so we don't want to dirty the current layer, if any
1299 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1300
1301 mCaches.stencil.enableTest();
1302 } else {
1303 mCaches.stencil.disable();
1304 }
1305 }
1306}
1307
Romain Guy9d5316e2010-06-24 19:30:36 -07001308const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001309 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001310}
1311
Romain Guy8a4ac612012-07-17 17:32:48 -07001312bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1313 if (mSnapshot->isIgnored()) {
1314 return true;
1315 }
1316
1317 Rect r(left, top, right, bottom);
1318 mSnapshot->transform->mapRect(r);
1319 r.snapToPixelBoundaries();
1320
1321 Rect clipRect(*mSnapshot->clipRect);
1322 clipRect.snapToPixelBoundaries();
1323
1324 return !clipRect.intersects(r);
1325}
1326
Romain Guy35643dd2012-09-18 15:40:58 -07001327bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1328 Rect& transformed, Rect& clip) {
1329 if (mSnapshot->isIgnored()) {
1330 return true;
1331 }
1332
1333 transformed.set(left, top, right, bottom);
1334 mSnapshot->transform->mapRect(transformed);
1335 transformed.snapToPixelBoundaries();
1336
1337 clip.set(*mSnapshot->clipRect);
1338 clip.snapToPixelBoundaries();
1339
1340 return !clip.intersects(transformed);
1341}
1342
Romain Guy672433d2013-01-04 19:05:13 -08001343bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1344 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001345 if (paint->getStyle() != SkPaint::kFill_Style) {
1346 float outset = paint->getStrokeWidth() * 0.5f;
1347 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1348 } else {
1349 return quickReject(left, top, right, bottom);
1350 }
1351}
1352
Romain Guyc7d53492010-06-25 13:41:57 -07001353bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001354 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001355 return true;
1356 }
1357
Romain Guy1d83e192010-08-17 11:37:00 -07001358 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001359 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001360 r.snapToPixelBoundaries();
1361
1362 Rect clipRect(*mSnapshot->clipRect);
1363 clipRect.snapToPixelBoundaries();
1364
Romain Guy586cae32012-07-13 15:28:31 -07001365 bool rejected = !clipRect.intersects(r);
1366 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001367 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001368 }
1369
1370 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001371}
1372
Romain Guy8ce00302013-01-15 18:51:42 -08001373void OpenGLRenderer::debugClip() {
1374#if DEBUG_CLIP_REGIONS
1375 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1376 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1377 }
1378#endif
1379}
1380
Romain Guy079ba2c2010-07-16 14:12:24 -07001381bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001382 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1383 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1384 if (clipped) {
1385 dirtyClip();
1386 }
1387 return !mSnapshot->clipRect->isEmpty();
1388 }
1389
1390 SkPath path;
1391 path.addRect(left, top, right, bottom);
1392
1393 return clipPath(&path, op);
1394}
1395
1396bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1397 SkMatrix transform;
1398 mSnapshot->transform->copyTo(transform);
1399
1400 SkPath transformed;
1401 path->transform(transform, &transformed);
1402
1403 SkRegion clip;
1404 if (!mSnapshot->clipRegion->isEmpty()) {
1405 clip.setRegion(*mSnapshot->clipRegion);
1406 } else {
1407 Rect* bounds = mSnapshot->clipRect;
1408 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1409 }
1410
1411 SkRegion region;
1412 region.setPath(transformed, clip);
1413
1414 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001415 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001416 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001417 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001418 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001419}
1420
Romain Guy735738c2012-12-03 12:34:51 -08001421bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001422 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1423 if (clipped) {
1424 dirtyClip();
1425 }
1426 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001427}
1428
Chet Haasea23eed82012-04-12 15:19:04 -07001429Rect* OpenGLRenderer::getClipRect() {
1430 return mSnapshot->clipRect;
1431}
1432
Romain Guyf6a11b82010-06-23 17:47:49 -07001433///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001434// Drawing commands
1435///////////////////////////////////////////////////////////////////////////////
1436
Romain Guy54be1cd2011-06-13 19:04:27 -07001437void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001438 // TODO: It would be best if we could do this before quickReject()
1439 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001440 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001441 // Make sure setScissor & setStencil happen at the beginning of
1442 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001443 if (mDirtyClip) {
1444 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001445 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001446 }
1447 mDescription.reset();
1448 mSetShaderColor = false;
1449 mColorSet = false;
1450 mColorA = mColorR = mColorG = mColorB = 0.0f;
1451 mTextureUnit = 0;
1452 mTrackDirtyRegions = true;
1453}
1454
1455void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1456 mDescription.hasTexture = true;
1457 mDescription.hasAlpha8Texture = isAlpha8;
1458}
1459
Romain Guyaa6c24c2011-04-28 18:40:04 -07001460void OpenGLRenderer::setupDrawWithExternalTexture() {
1461 mDescription.hasExternalTexture = true;
1462}
1463
Romain Guy15bc6432011-12-13 13:11:32 -08001464void OpenGLRenderer::setupDrawNoTexture() {
1465 mCaches.disbaleTexCoordsVertexArray();
1466}
1467
Chris Craik710f46d2012-09-17 17:25:49 -07001468void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001469 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001470}
1471
Chris Craik710f46d2012-09-17 17:25:49 -07001472void OpenGLRenderer::setupDrawVertexShape() {
1473 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001474}
1475
Romain Guyed6fcb02011-03-21 13:11:28 -07001476void OpenGLRenderer::setupDrawPoint(float pointSize) {
1477 mDescription.isPoint = true;
1478 mDescription.pointSize = pointSize;
1479}
1480
Romain Guy8d0d4782010-12-14 20:13:35 -08001481void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1482 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001483 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1484 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1485 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001486 mColorSet = true;
1487 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1488}
1489
Romain Guy86568192010-12-14 15:55:39 -08001490void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1491 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001492 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1493 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1494 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001495 mColorSet = true;
1496 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1497}
1498
Romain Guy41210632012-07-16 17:04:24 -07001499void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1500 mCaches.fontRenderer->describe(mDescription, paint);
1501}
1502
Romain Guy70ca14e2010-12-13 18:24:33 -08001503void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1504 mColorA = a;
1505 mColorR = r;
1506 mColorG = g;
1507 mColorB = b;
1508 mColorSet = true;
1509 mSetShaderColor = mDescription.setColor(r, g, b, a);
1510}
1511
1512void OpenGLRenderer::setupDrawShader() {
1513 if (mShader) {
1514 mShader->describe(mDescription, mCaches.extensions);
1515 }
1516}
1517
1518void OpenGLRenderer::setupDrawColorFilter() {
1519 if (mColorFilter) {
1520 mColorFilter->describe(mDescription, mCaches.extensions);
1521 }
1522}
1523
Romain Guyf09ef512011-05-27 11:43:46 -07001524void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1525 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1526 mColorA = 1.0f;
1527 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001528 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001529 }
1530}
1531
Romain Guy70ca14e2010-12-13 18:24:33 -08001532void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001533 // When the blending mode is kClear_Mode, we need to use a modulate color
1534 // argb=1,0,0,0
1535 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001536 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1537 mDescription, swapSrcDst);
1538}
1539
1540void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001541 // When the blending mode is kClear_Mode, we need to use a modulate color
1542 // argb=1,0,0,0
1543 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001544 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1545 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001546}
1547
1548void OpenGLRenderer::setupDrawProgram() {
1549 useProgram(mCaches.programCache.get(mDescription));
1550}
1551
1552void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1553 mTrackDirtyRegions = false;
1554}
1555
1556void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1557 bool ignoreTransform) {
1558 mModelView.loadTranslate(left, top, 0.0f);
1559 if (!ignoreTransform) {
1560 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1561 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1562 } else {
1563 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1564 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1565 }
1566}
1567
Chet Haase8a5cc922011-04-26 07:28:09 -07001568void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1569 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001570}
1571
Romain Guy70ca14e2010-12-13 18:24:33 -08001572void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1573 bool ignoreTransform, bool ignoreModelView) {
1574 if (!ignoreModelView) {
1575 mModelView.loadTranslate(left, top, 0.0f);
1576 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001577 } else {
1578 mModelView.loadIdentity();
1579 }
Romain Guy86568192010-12-14 15:55:39 -08001580 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1581 if (!ignoreTransform) {
1582 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1583 if (mTrackDirtyRegions && dirty) {
1584 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1585 }
1586 } else {
1587 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1588 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1589 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001590}
1591
Romain Guyed6fcb02011-03-21 13:11:28 -07001592void OpenGLRenderer::setupDrawPointUniforms() {
1593 int slot = mCaches.currentProgram->getUniform("pointSize");
1594 glUniform1f(slot, mDescription.pointSize);
1595}
1596
Romain Guy70ca14e2010-12-13 18:24:33 -08001597void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001598 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001599 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1600 }
1601}
1602
Romain Guy86568192010-12-14 15:55:39 -08001603void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001604 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001605 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001606 }
1607}
1608
Romain Guy70ca14e2010-12-13 18:24:33 -08001609void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1610 if (mShader) {
1611 if (ignoreTransform) {
1612 mModelView.loadInverse(*mSnapshot->transform);
1613 }
1614 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1615 }
1616}
1617
Romain Guy8d0d4782010-12-14 20:13:35 -08001618void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1619 if (mShader) {
1620 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1621 }
1622}
1623
Romain Guy70ca14e2010-12-13 18:24:33 -08001624void OpenGLRenderer::setupDrawColorFilterUniforms() {
1625 if (mColorFilter) {
1626 mColorFilter->setupProgram(mCaches.currentProgram);
1627 }
1628}
1629
Romain Guy41210632012-07-16 17:04:24 -07001630void OpenGLRenderer::setupDrawTextGammaUniforms() {
1631 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1632}
1633
Romain Guy70ca14e2010-12-13 18:24:33 -08001634void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001635 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001636 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001637 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001638}
1639
1640void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1641 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001642 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001643 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001644}
1645
Romain Guyaa6c24c2011-04-28 18:40:04 -07001646void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1647 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001648 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001649 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001650}
1651
Romain Guy8f0095c2011-05-02 17:24:22 -07001652void OpenGLRenderer::setupDrawTextureTransform() {
1653 mDescription.hasTextureTransform = true;
1654}
1655
1656void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001657 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1658 GL_FALSE, &transform.data[0]);
1659}
1660
Romain Guy70ca14e2010-12-13 18:24:33 -08001661void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001662 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001663 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001664 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001665 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001666 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001667 }
Romain Guyd71dd362011-12-12 19:03:35 -08001668
Chris Craikcb4d6002012-09-25 12:00:29 -07001669 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001670 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001671 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001672 }
1673
1674 mCaches.unbindIndicesBuffer();
1675}
1676
1677void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1678 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001679 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001680 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001681 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001682 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001683}
1684
Chet Haase5b0200b2011-04-13 17:58:08 -07001685void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001686 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001687 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001688 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001689}
1690
1691/**
1692 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001693 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1694 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1695 * attributes (one per vertex) are values from zero to one that tells the fragment
1696 * shader where the fragment is in relation to the line width/length overall; these values are
1697 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1698 * region of the line.
1699 * Note that we only pass down the width values in this setup function. The length coordinates
1700 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001701 */
Chet Haase99585ad2011-05-02 15:00:16 -07001702void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001703 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001704 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001705 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001706 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001707 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001708
Romain Guy7b631422012-04-04 11:38:54 -07001709 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001710 glEnableVertexAttribArray(widthSlot);
1711 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001712
Romain Guy7b631422012-04-04 11:38:54 -07001713 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001714 glEnableVertexAttribArray(lengthSlot);
1715 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001716
Chet Haase5b0200b2011-04-13 17:58:08 -07001717 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001718 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001719}
1720
1721void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1722 glDisableVertexAttribArray(widthSlot);
1723 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001724}
1725
Romain Guy70ca14e2010-12-13 18:24:33 -08001726void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001727}
1728
1729///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001730// Drawing
1731///////////////////////////////////////////////////////////////////////////////
1732
Chet Haase1271e2c2012-04-20 09:54:27 -07001733status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001734 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001735
Romain Guy0fe478e2010-11-08 12:08:41 -08001736 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1737 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001738 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001739 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001740 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001741
Romain Guy65549432012-03-26 16:45:05 -07001742 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001743}
1744
Chet Haaseed30fd82011-04-22 16:18:45 -07001745void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1746 if (displayList) {
1747 displayList->output(*this, level);
1748 }
1749}
1750
Romain Guya168d732011-03-18 16:50:13 -07001751void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1752 int alpha;
1753 SkXfermode::Mode mode;
1754 getAlphaAndMode(paint, &alpha, &mode);
1755
Romain Guy886b2752013-01-04 12:26:18 -08001756 int color = paint != NULL ? paint->getColor() : 0;
1757
Romain Guya168d732011-03-18 16:50:13 -07001758 float x = left;
1759 float y = top;
1760
Romain Guy886b2752013-01-04 12:26:18 -08001761 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1762
Romain Guya168d732011-03-18 16:50:13 -07001763 bool ignoreTransform = false;
1764 if (mSnapshot->transform->isPureTranslate()) {
1765 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1766 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1767 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001768
1769 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001770 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001771 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001772 }
1773
Romain Guy886b2752013-01-04 12:26:18 -08001774 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1775 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1776 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001777}
1778
Chet Haase48659092012-05-31 15:21:51 -07001779status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001780 const float right = left + bitmap->width();
1781 const float bottom = top + bitmap->height();
1782
1783 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001784 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001785 }
1786
Romain Guya1d3c912011-12-13 14:55:06 -08001787 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001788 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001789 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001790 const AutoTexture autoCleanup(texture);
1791
Romain Guy211370f2012-02-01 16:10:55 -08001792 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001793 drawAlphaBitmap(texture, left, top, paint);
1794 } else {
1795 drawTextureRect(left, top, right, bottom, texture, paint);
1796 }
Chet Haase48659092012-05-31 15:21:51 -07001797
1798 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001799}
1800
Chet Haase48659092012-05-31 15:21:51 -07001801status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001802 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1803 const mat4 transform(*matrix);
1804 transform.mapRect(r);
1805
Romain Guy6926c722010-07-12 20:20:03 -07001806 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001807 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001808 }
1809
Romain Guya1d3c912011-12-13 14:55:06 -08001810 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001811 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001812 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001813 const AutoTexture autoCleanup(texture);
1814
Romain Guy5b3b3522010-10-27 18:57:51 -07001815 // This could be done in a cheaper way, all we need is pass the matrix
1816 // to the vertex shader. The save/restore is a bit overkill.
1817 save(SkCanvas::kMatrix_SaveFlag);
1818 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001819 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1820 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1821 } else {
1822 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1823 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001824 restore();
Chet Haase48659092012-05-31 15:21:51 -07001825
1826 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001827}
1828
Chet Haase48659092012-05-31 15:21:51 -07001829status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001830 const float right = left + bitmap->width();
1831 const float bottom = top + bitmap->height();
1832
1833 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001834 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001835 }
1836
1837 mCaches.activeTexture(0);
1838 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1839 const AutoTexture autoCleanup(texture);
1840
Romain Guy886b2752013-01-04 12:26:18 -08001841 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1842 drawAlphaBitmap(texture, left, top, paint);
1843 } else {
1844 drawTextureRect(left, top, right, bottom, texture, paint);
1845 }
Chet Haase48659092012-05-31 15:21:51 -07001846
1847 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001848}
1849
Chet Haase48659092012-05-31 15:21:51 -07001850status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001851 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001852 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001853 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001854 }
1855
Romain Guyb18d2d02011-02-10 15:52:54 -08001856 float left = FLT_MAX;
1857 float top = FLT_MAX;
1858 float right = FLT_MIN;
1859 float bottom = FLT_MIN;
1860
Romain Guya92bb4d2012-10-16 11:08:44 -07001861 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001862
Romain Guya566b7c2011-01-23 16:36:11 -08001863 // TODO: Support the colors array
1864 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001865 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001866
Romain Guy5a7b4662011-01-20 19:09:30 -08001867 for (int32_t y = 0; y < meshHeight; y++) {
1868 for (int32_t x = 0; x < meshWidth; x++) {
1869 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1870
1871 float u1 = float(x) / meshWidth;
1872 float u2 = float(x + 1) / meshWidth;
1873 float v1 = float(y) / meshHeight;
1874 float v2 = float(y + 1) / meshHeight;
1875
1876 int ax = i + (meshWidth + 1) * 2;
1877 int ay = ax + 1;
1878 int bx = i;
1879 int by = bx + 1;
1880 int cx = i + 2;
1881 int cy = cx + 1;
1882 int dx = i + (meshWidth + 1) * 2 + 2;
1883 int dy = dx + 1;
1884
1885 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1886 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1887 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1888
1889 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1890 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1891 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001892
Romain Guya92bb4d2012-10-16 11:08:44 -07001893 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1894 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1895 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1896 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001897 }
1898 }
1899
Romain Guya92bb4d2012-10-16 11:08:44 -07001900 if (quickReject(left, top, right, bottom)) {
1901 return DrawGlInfo::kStatusDone;
1902 }
1903
1904 mCaches.activeTexture(0);
1905 Texture* texture = mCaches.textureCache.get(bitmap);
1906 if (!texture) return DrawGlInfo::kStatusDone;
1907 const AutoTexture autoCleanup(texture);
1908
1909 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1910 texture->setFilter(FILTER(paint), true);
1911
1912 int alpha;
1913 SkXfermode::Mode mode;
1914 getAlphaAndMode(paint, &alpha, &mode);
1915
1916 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001917 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1918 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001919
Romain Guy5a7b4662011-01-20 19:09:30 -08001920 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1921 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001922 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001923
1924 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001925}
1926
Chet Haase48659092012-05-31 15:21:51 -07001927status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001928 float srcLeft, float srcTop, float srcRight, float srcBottom,
1929 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001930 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001931 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001932 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001933 }
1934
Romain Guya1d3c912011-12-13 14:55:06 -08001935 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001936 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001937 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001938 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001939
Romain Guy8ba548f2010-06-30 19:21:21 -07001940 const float width = texture->width;
1941 const float height = texture->height;
1942
Romain Guy68169722011-08-22 17:33:33 -07001943 const float u1 = fmax(0.0f, srcLeft / width);
1944 const float v1 = fmax(0.0f, srcTop / height);
1945 const float u2 = fmin(1.0f, srcRight / width);
1946 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001947
Romain Guy03750a02010-10-18 14:06:08 -07001948 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001949 resetDrawTextureTexCoords(u1, v1, u2, v2);
1950
Romain Guy03750a02010-10-18 14:06:08 -07001951 int alpha;
1952 SkXfermode::Mode mode;
1953 getAlphaAndMode(paint, &alpha, &mode);
1954
Romain Guyd21b6e12011-11-30 20:21:23 -08001955 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1956
Romain Guy886b2752013-01-04 12:26:18 -08001957 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1958 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001959
Romain Guy886b2752013-01-04 12:26:18 -08001960 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1961 // Apply a scale transform on the canvas only when a shader is in use
1962 // Skia handles the ratio between the dst and src rects as a scale factor
1963 // when a shader is set
1964 bool useScaleTransform = mShader && scaled;
1965 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001966
Romain Guy886b2752013-01-04 12:26:18 -08001967 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1968 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1969 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1970
1971 dstRight = x + (dstRight - dstLeft);
1972 dstBottom = y + (dstBottom - dstTop);
1973
1974 dstLeft = x;
1975 dstTop = y;
1976
1977 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1978 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001979 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001980 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001981 }
1982
1983 if (CC_UNLIKELY(useScaleTransform)) {
1984 save(SkCanvas::kMatrix_SaveFlag);
1985 translate(dstLeft, dstTop);
1986 scale(scaleX, scaleY);
1987
1988 dstLeft = 0.0f;
1989 dstTop = 0.0f;
1990
1991 dstRight = srcRight - srcLeft;
1992 dstBottom = srcBottom - srcTop;
1993 }
1994
1995 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1996 int color = paint ? paint->getColor() : 0;
1997 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1998 texture->id, paint != NULL, color, alpha, mode,
1999 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2000 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2001 } else {
2002 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2003 texture->id, alpha / 255.0f, mode, texture->blend,
2004 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2005 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2006 }
2007
2008 if (CC_UNLIKELY(useScaleTransform)) {
2009 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002010 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002011
2012 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002013
2014 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002015}
2016
Chet Haase48659092012-05-31 15:21:51 -07002017status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002018 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002019 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002020 int alpha;
2021 SkXfermode::Mode mode;
2022 getAlphaAndModeDirect(paint, &alpha, &mode);
2023
2024 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2025 left, top, right, bottom, alpha, mode);
2026}
2027
2028status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2029 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2030 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002031 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002032 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002033 }
2034
Romain Guybe6f9dc2012-07-16 12:41:17 -07002035 alpha *= mSnapshot->alpha;
2036
Romain Guya1d3c912011-12-13 14:55:06 -08002037 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002038 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002039 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002040 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002041 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2042 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002043
Romain Guy2728f962010-10-08 18:36:15 -07002044 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002045 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002046
Romain Guy211370f2012-02-01 16:10:55 -08002047 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002048 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002049 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002050 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002051 const float offsetX = left + mSnapshot->transform->getTranslateX();
2052 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002053 const size_t count = mesh->quads.size();
2054 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002055 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002056 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002057 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2058 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2059 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002060 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002061 dirtyLayer(left + bounds.left, top + bounds.top,
2062 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002063 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002064 }
2065 }
2066
Romain Guy211370f2012-02-01 16:10:55 -08002067 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002068 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2069 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2070
2071 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2072 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2073 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2074 true, !mesh->hasEmptyQuads);
2075 } else {
2076 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2077 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2078 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2079 true, !mesh->hasEmptyQuads);
2080 }
Romain Guy054dc182010-10-15 17:55:25 -07002081 }
Chet Haase48659092012-05-31 15:21:51 -07002082
2083 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002084}
2085
Chet Haase99ecdc42011-05-06 12:06:34 -07002086/**
Chris Craikcb4d6002012-09-25 12:00:29 -07002087 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2088 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2089 * screen space in all directions. However, instead of using a fragment shader to compute the
2090 * translucency of the color from its position, we simply use a varying parameter to define how far
2091 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2092 *
2093 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07002094 */
Chris Craikcb4d6002012-09-25 12:00:29 -07002095void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2096 int color = paint->getColor();
2097 SkPaint::Style style = paint->getStyle();
2098 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2099 bool isAA = paint->isAntiAlias();
2100
Chris Craik710f46d2012-09-17 17:25:49 -07002101 VertexBuffer vertexBuffer;
2102 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07002103 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07002104
Chris Craikbf09ffb2012-10-01 13:50:37 -07002105 if (!vertexBuffer.getSize()) {
2106 // no vertices to draw
2107 return;
2108 }
2109
Chris Craik710f46d2012-09-17 17:25:49 -07002110 setupDraw();
2111 setupDrawNoTexture();
2112 if (isAA) setupDrawAA();
2113 setupDrawVertexShape();
2114 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2115 setupDrawColorFilter();
2116 setupDrawShader();
2117 setupDrawBlending(isAA, mode);
2118 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002119 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07002120 setupDrawColorUniforms();
2121 setupDrawColorFilterUniforms();
2122 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002123
Chris Craik710f46d2012-09-17 17:25:49 -07002124 void* vertices = vertexBuffer.getBuffer();
2125 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002126 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002127 mCaches.resetTexCoordsVertexPointer();
2128 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002129
Chris Craik710f46d2012-09-17 17:25:49 -07002130 int alphaSlot = -1;
2131 if (isAA) {
2132 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2133 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002134
Chris Craik710f46d2012-09-17 17:25:49 -07002135 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002136 glEnableVertexAttribArray(alphaSlot);
2137 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002138 }
Romain Guy04299382012-07-18 17:15:41 -07002139
Chris Craikcb4d6002012-09-25 12:00:29 -07002140 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002141 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002142
Chris Craik710f46d2012-09-17 17:25:49 -07002143 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002144
Chris Craik710f46d2012-09-17 17:25:49 -07002145 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002146 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002147 }
Chet Haase858aa932011-05-12 09:06:00 -07002148}
2149
2150/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002151 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2152 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2153 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2154 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2155 * of the line. Hairlines are more involved because we need to account for transform scaling
2156 * to end up with a one-pixel-wide line in screen space..
2157 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2158 * in combination with values that we calculate and pass down in this method. The basic approach
2159 * is that the quad we create contains both the core line area plus a bounding area in which
2160 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2161 * proportion of the width and the length of a given segment is represented by the boundary
2162 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2163 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2164 * on the inside). This ends up giving the result we want, with pixels that are completely
2165 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2166 * how far into the boundary region they are, which is determined by shader interpolation.
2167 */
Chet Haase48659092012-05-31 15:21:51 -07002168status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2169 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002170
2171 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002172 // We use half the stroke width here because we're going to position the quad
2173 // corner vertices half of the width away from the line endpoints
2174 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002175 // A stroke width of 0 has a special meaning in Skia:
2176 // it draws a line 1 px wide regardless of current transform
2177 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002178
Chet Haase99ecdc42011-05-06 12:06:34 -07002179 float inverseScaleX = 1.0f;
2180 float inverseScaleY = 1.0f;
2181 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002182
Chet Haase8a5cc922011-04-26 07:28:09 -07002183 int alpha;
2184 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002185
Chet Haase8a5cc922011-04-26 07:28:09 -07002186 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002187 int verticesCount = count;
2188 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002189 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002190 verticesCount += (count - 4);
2191 }
2192
2193 if (isHairLine || isAA) {
2194 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2195 // the line on the screen should always be one pixel wide regardless of scale. For
2196 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002197 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002198 Matrix4 *mat = mSnapshot->transform;
2199 float m00 = mat->data[Matrix4::kScaleX];
2200 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002201 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002202 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002203
Romain Guy211370f2012-02-01 16:10:55 -08002204 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2205 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002206
Chet Haase99ecdc42011-05-06 12:06:34 -07002207 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2208 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002209
Chet Haase99ecdc42011-05-06 12:06:34 -07002210 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2211 scaled = true;
2212 }
2213 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002214 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002215
2216 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002217
2218 mCaches.enableScissor();
2219
Chet Haase8a5cc922011-04-26 07:28:09 -07002220 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002221 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002222 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002223 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002224 }
2225 setupDrawColor(paint->getColor(), alpha);
2226 setupDrawColorFilter();
2227 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002228 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002229 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002230 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002231 setupDrawColorUniforms();
2232 setupDrawColorFilterUniforms();
2233 setupDrawShaderIdentityUniforms();
2234
2235 if (isHairLine) {
2236 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002237 halfStrokeWidth = isAA? 1 : .5;
2238 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002239 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002240 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002241 }
Romain Guy7b631422012-04-04 11:38:54 -07002242
2243 int widthSlot;
2244 int lengthSlot;
2245
Chet Haase5b0200b2011-04-13 17:58:08 -07002246 Vertex lines[verticesCount];
2247 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002248
Chet Haase99585ad2011-05-02 15:00:16 -07002249 AAVertex wLines[verticesCount];
2250 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002251
Romain Guy211370f2012-02-01 16:10:55 -08002252 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002253 setupDrawVertices(vertices);
2254 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002255 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2256 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002257 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002258 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2259 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002260 // We will need to calculate the actual width proportion on each segment for
2261 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002262 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002263 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2264 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002265 }
2266
Chet Haase99ecdc42011-05-06 12:06:34 -07002267 AAVertex* prevAAVertex = NULL;
2268 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002269
Chet Haase99585ad2011-05-02 15:00:16 -07002270 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002271 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002272
Chet Haase5b0200b2011-04-13 17:58:08 -07002273 for (int i = 0; i < count; i += 4) {
2274 // a = start point, b = end point
2275 vec2 a(points[i], points[i + 1]);
2276 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002277
Chet Haase99585ad2011-05-02 15:00:16 -07002278 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002279 float boundaryLengthProportion = 0;
2280 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002281
Chet Haase5b0200b2011-04-13 17:58:08 -07002282 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002283 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002284 float x = n.x;
2285 n.x = -n.y;
2286 n.y = x;
2287
Chet Haase8a5cc922011-04-26 07:28:09 -07002288 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002289 if (isAA) {
2290 float wideningFactor;
2291 if (fabs(n.x) >= fabs(n.y)) {
2292 wideningFactor = fabs(1.0f / n.x);
2293 } else {
2294 wideningFactor = fabs(1.0f / n.y);
2295 }
2296 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002297 }
Romain Guy7b631422012-04-04 11:38:54 -07002298
Chet Haase99ecdc42011-05-06 12:06:34 -07002299 if (scaled) {
2300 n.x *= inverseScaleX;
2301 n.y *= inverseScaleY;
2302 }
2303 } else if (scaled) {
2304 // Extend n by .5 pixel on each side, post-transform
2305 vec2 extendedN = n.copyNormalized();
2306 extendedN /= 2;
2307 extendedN.x *= inverseScaleX;
2308 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002309
Chet Haase99ecdc42011-05-06 12:06:34 -07002310 float extendedNLength = extendedN.length();
2311 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002312 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002313 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002314 }
Romain Guy7b631422012-04-04 11:38:54 -07002315
Chet Haase99585ad2011-05-02 15:00:16 -07002316 // aa lines expand the endpoint vertices to encompass the AA boundary
2317 if (isAA) {
2318 vec2 abVector = (b - a);
2319 length = abVector.length();
2320 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002321
Chet Haase99ecdc42011-05-06 12:06:34 -07002322 if (scaled) {
2323 abVector.x *= inverseScaleX;
2324 abVector.y *= inverseScaleY;
2325 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002326 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002327 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002328 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002329 }
Romain Guy7b631422012-04-04 11:38:54 -07002330
Chet Haase99ecdc42011-05-06 12:06:34 -07002331 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002332 a -= abVector;
2333 b += abVector;
2334 }
2335
Chet Haase5b0200b2011-04-13 17:58:08 -07002336 // Four corners of the rectangle defining a thick line
2337 vec2 p1 = a - n;
2338 vec2 p2 = a + n;
2339 vec2 p3 = b + n;
2340 vec2 p4 = b - n;
2341
Chet Haase99585ad2011-05-02 15:00:16 -07002342
Chet Haase5b0200b2011-04-13 17:58:08 -07002343 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2344 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2345 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2346 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2347
Romain Guy04299382012-07-18 17:15:41 -07002348 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002349 if (!isAA) {
2350 if (prevVertex != NULL) {
2351 // Issue two repeat vertices to create degenerate triangles to bridge
2352 // between the previous line and the new one. This is necessary because
2353 // we are creating a single triangle_strip which will contain
2354 // potentially discontinuous line segments.
2355 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2356 Vertex::set(vertices++, p1.x, p1.y);
2357 generatedVerticesCount += 2;
2358 }
Romain Guy7b631422012-04-04 11:38:54 -07002359
Chet Haase5b0200b2011-04-13 17:58:08 -07002360 Vertex::set(vertices++, p1.x, p1.y);
2361 Vertex::set(vertices++, p2.x, p2.y);
2362 Vertex::set(vertices++, p4.x, p4.y);
2363 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002364
Chet Haase5b0200b2011-04-13 17:58:08 -07002365 prevVertex = vertices - 1;
2366 generatedVerticesCount += 4;
2367 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002368 if (!isHairLine && scaled) {
2369 // Must set width proportions per-segment for scaled non-hairlines to use the
2370 // correct AA boundary dimensions
2371 if (boundaryWidthSlot < 0) {
2372 boundaryWidthSlot =
2373 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002374 }
Romain Guy7b631422012-04-04 11:38:54 -07002375
Chet Haase99ecdc42011-05-06 12:06:34 -07002376 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002377 }
Romain Guy7b631422012-04-04 11:38:54 -07002378
Chet Haase99585ad2011-05-02 15:00:16 -07002379 if (boundaryLengthSlot < 0) {
2380 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002381 }
Romain Guy7b631422012-04-04 11:38:54 -07002382
Chet Haase99ecdc42011-05-06 12:06:34 -07002383 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002384
Chet Haase5b0200b2011-04-13 17:58:08 -07002385 if (prevAAVertex != NULL) {
2386 // Issue two repeat vertices to create degenerate triangles to bridge
2387 // between the previous line and the new one. This is necessary because
2388 // we are creating a single triangle_strip which will contain
2389 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002390 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2391 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2392 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002393 generatedVerticesCount += 2;
2394 }
Romain Guy7b631422012-04-04 11:38:54 -07002395
Chet Haase99585ad2011-05-02 15:00:16 -07002396 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2397 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2398 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2399 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002400
Chet Haase5b0200b2011-04-13 17:58:08 -07002401 prevAAVertex = aaVertices - 1;
2402 generatedVerticesCount += 4;
2403 }
Romain Guy7b631422012-04-04 11:38:54 -07002404
Chet Haase99585ad2011-05-02 15:00:16 -07002405 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2406 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2407 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002408 }
2409 }
Romain Guy7b631422012-04-04 11:38:54 -07002410
Chet Haase5b0200b2011-04-13 17:58:08 -07002411 if (generatedVerticesCount > 0) {
2412 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2413 }
Romain Guy7b631422012-04-04 11:38:54 -07002414
2415 if (isAA) {
2416 finishDrawAALine(widthSlot, lengthSlot);
2417 }
Chet Haase48659092012-05-31 15:21:51 -07002418
2419 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002420}
2421
Chet Haase48659092012-05-31 15:21:51 -07002422status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2423 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002424
2425 // TODO: The paint's cap style defines whether the points are square or circular
2426 // TODO: Handle AA for round points
2427
Chet Haase5b0200b2011-04-13 17:58:08 -07002428 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002429 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002430 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002431 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002432 if (isHairLine) {
2433 // Now that we know it's hairline, we can set the effective width, to be used later
2434 strokeWidth = 1.0f;
2435 }
2436 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002437 int alpha;
2438 SkXfermode::Mode mode;
2439 getAlphaAndMode(paint, &alpha, &mode);
2440
2441 int verticesCount = count >> 1;
2442 int generatedVerticesCount = 0;
2443
2444 TextureVertex pointsData[verticesCount];
2445 TextureVertex* vertex = &pointsData[0];
2446
Romain Guy04299382012-07-18 17:15:41 -07002447 // TODO: We should optimize this method to not generate vertices for points
2448 // that lie outside of the clip.
2449 mCaches.enableScissor();
2450
Romain Guyed6fcb02011-03-21 13:11:28 -07002451 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002452 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002453 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002454 setupDrawColor(paint->getColor(), alpha);
2455 setupDrawColorFilter();
2456 setupDrawShader();
2457 setupDrawBlending(mode);
2458 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002459 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002460 setupDrawColorUniforms();
2461 setupDrawColorFilterUniforms();
2462 setupDrawPointUniforms();
2463 setupDrawShaderIdentityUniforms();
2464 setupDrawMesh(vertex);
2465
2466 for (int i = 0; i < count; i += 2) {
2467 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2468 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002469
Chet Haase8a5cc922011-04-26 07:28:09 -07002470 float left = points[i] - halfWidth;
2471 float right = points[i] + halfWidth;
2472 float top = points[i + 1] - halfWidth;
2473 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002474
Chet Haase8a5cc922011-04-26 07:28:09 -07002475 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002476 }
2477
2478 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002479
2480 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002481}
2482
Chet Haase48659092012-05-31 15:21:51 -07002483status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002484 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002485 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002486
Romain Guyae88e5e2010-10-22 17:49:18 -07002487 Rect& clip(*mSnapshot->clipRect);
2488 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002489
Romain Guy3d58c032010-07-14 16:34:53 -07002490 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002491
2492 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002493}
Romain Guy9d5316e2010-06-24 19:30:36 -07002494
Chet Haase48659092012-05-31 15:21:51 -07002495status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2496 SkPaint* paint) {
2497 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002498 const AutoTexture autoCleanup(texture);
2499
2500 const float x = left + texture->left - texture->offset;
2501 const float y = top + texture->top - texture->offset;
2502
2503 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002504
2505 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002506}
2507
Chet Haase48659092012-05-31 15:21:51 -07002508status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002509 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002510 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002511 return DrawGlInfo::kStatusDone;
2512 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002513
Chris Craikcb4d6002012-09-25 12:00:29 -07002514 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002515 mCaches.activeTexture(0);
2516 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2517 right - left, bottom - top, rx, ry, p);
2518 return drawShape(left, top, texture, p);
2519 }
2520
2521 SkPath path;
2522 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2524 float outset = p->getStrokeWidth() / 2;
2525 rect.outset(outset, outset);
2526 rx += outset;
2527 ry += outset;
2528 }
Chris Craik710f46d2012-09-17 17:25:49 -07002529 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002530 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002531
2532 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002533}
2534
Chris Craik710f46d2012-09-17 17:25:49 -07002535status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002536 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2537 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002538 return DrawGlInfo::kStatusDone;
2539 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002540 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002541 mCaches.activeTexture(0);
2542 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2543 return drawShape(x - radius, y - radius, texture, p);
2544 }
2545
2546 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002547 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2548 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2549 } else {
2550 path.addCircle(x, y, radius);
2551 }
2552 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002553
2554 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002555}
Romain Guy01d58e42011-01-19 21:54:02 -08002556
Chet Haase48659092012-05-31 15:21:51 -07002557status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002558 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002559 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002560 return DrawGlInfo::kStatusDone;
2561 }
Romain Guy01d58e42011-01-19 21:54:02 -08002562
Chris Craikcb4d6002012-09-25 12:00:29 -07002563 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002564 mCaches.activeTexture(0);
2565 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2566 return drawShape(left, top, texture, p);
2567 }
2568
2569 SkPath path;
2570 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002571 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2572 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2573 }
Chris Craik710f46d2012-09-17 17:25:49 -07002574 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002575 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002576
2577 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002578}
2579
Chet Haase48659092012-05-31 15:21:51 -07002580status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002581 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2582 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2583 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002584 }
2585
Chris Craik780c1282012-10-04 14:10:49 -07002586 if (fabs(sweepAngle) >= 360.0f) {
2587 return drawOval(left, top, right, bottom, p);
2588 }
2589
2590 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002591 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2592 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002593 mCaches.activeTexture(0);
2594 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2595 startAngle, sweepAngle, useCenter, p);
2596 return drawShape(left, top, texture, p);
2597 }
2598
2599 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2600 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2601 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2602 }
2603
2604 SkPath path;
2605 if (useCenter) {
2606 path.moveTo(rect.centerX(), rect.centerY());
2607 }
2608 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2609 if (useCenter) {
2610 path.close();
2611 }
2612 drawConvexPath(path, p);
2613
2614 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002615}
2616
Romain Guycf8675e2012-10-02 12:32:25 -07002617// See SkPaintDefaults.h
2618#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2619
Chet Haase48659092012-05-31 15:21:51 -07002620status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002621 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002622 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002623 }
2624
Chris Craik710f46d2012-09-17 17:25:49 -07002625 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002626 // only fill style is supported by drawConvexPath, since others have to handle joins
2627 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2628 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2629 mCaches.activeTexture(0);
2630 const PathTexture* texture =
2631 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2632 return drawShape(left, top, texture, p);
2633 }
2634
2635 SkPath path;
2636 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2637 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2638 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2639 }
2640 path.addRect(rect);
2641 drawConvexPath(path, p);
2642
2643 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002644 }
2645
Romain Guy181d0a62011-06-09 18:52:38 -07002646 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002647 SkPath path;
2648 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002649 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002650 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002651 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002652 }
Chet Haase48659092012-05-31 15:21:51 -07002653
2654 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002655}
Romain Guy9d5316e2010-06-24 19:30:36 -07002656
Raph Levien416a8472012-07-19 22:48:17 -07002657void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2658 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2659 float x, float y) {
2660 mCaches.activeTexture(0);
2661
2662 // NOTE: The drop shadow will not perform gamma correction
2663 // if shader-based correction is enabled
2664 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2665 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2666 paint, text, bytesCount, count, mShadowRadius, positions);
2667 const AutoTexture autoCleanup(shadow);
2668
2669 const float sx = x - shadow->left + mShadowDx;
2670 const float sy = y - shadow->top + mShadowDy;
2671
2672 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2673 int shadowColor = mShadowColor;
2674 if (mShader) {
2675 shadowColor = 0xffffffff;
2676 }
2677
2678 setupDraw();
2679 setupDrawWithTexture(true);
2680 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2681 setupDrawColorFilter();
2682 setupDrawShader();
2683 setupDrawBlending(true, mode);
2684 setupDrawProgram();
2685 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2686 setupDrawTexture(shadow->id);
2687 setupDrawPureColorUniforms();
2688 setupDrawColorFilterUniforms();
2689 setupDrawShaderUniforms();
2690 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2691
2692 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2693}
2694
Chet Haase48659092012-05-31 15:21:51 -07002695status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002696 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002697 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002698 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002699 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002700 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002701
Romain Guy671d6cf2012-01-18 12:39:17 -08002702 // NOTE: Skia does not support perspective transform on drawPosText yet
2703 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002704 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002705 }
2706
2707 float x = 0.0f;
2708 float y = 0.0f;
2709 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2710 if (pureTranslate) {
2711 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2712 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2713 }
2714
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002715 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002716 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002717
2718 int alpha;
2719 SkXfermode::Mode mode;
2720 getAlphaAndMode(paint, &alpha, &mode);
2721
Raph Levien416a8472012-07-19 22:48:17 -07002722 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002723 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2724 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002725 }
2726
Romain Guy671d6cf2012-01-18 12:39:17 -08002727 // Pick the appropriate texture filtering
2728 bool linearFilter = mSnapshot->transform->changesBounds();
2729 if (pureTranslate && !linearFilter) {
2730 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2731 }
2732
2733 mCaches.activeTexture(0);
2734 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002735 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 setupDrawDirtyRegionsDisabled();
2737 setupDrawWithTexture(true);
2738 setupDrawAlpha8Color(paint->getColor(), alpha);
2739 setupDrawColorFilter();
2740 setupDrawShader();
2741 setupDrawBlending(true, mode);
2742 setupDrawProgram();
2743 setupDrawModelView(x, y, x, y, pureTranslate, true);
2744 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2745 setupDrawPureColorUniforms();
2746 setupDrawColorFilterUniforms();
2747 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002748 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002749
2750 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2751 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2752
Romain Guy211370f2012-02-01 16:10:55 -08002753 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002754
2755 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2756 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002757 if (hasActiveLayer) {
2758 if (!pureTranslate) {
2759 mSnapshot->transform->mapRect(bounds);
2760 }
2761 dirtyLayerUnchecked(bounds, getRegion());
2762 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002763 }
Chet Haase48659092012-05-31 15:21:51 -07002764
2765 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002766}
2767
Romain Guyc2525952012-07-27 16:41:22 -07002768status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002769 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002770 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002771 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002772 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002773 }
2774
Chet Haasea1cff502012-02-21 13:43:44 -08002775 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002776 switch (paint->getTextAlign()) {
2777 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002778 x -= length / 2.0f;
2779 break;
2780 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002781 x -= length;
2782 break;
2783 default:
2784 break;
2785 }
2786
Romain Guycac5fd32011-12-01 20:08:50 -08002787 SkPaint::FontMetrics metrics;
2788 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002789 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002790 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002791 }
2792
Romain Guye3a9b242013-01-08 11:15:30 -08002793#if DEBUG_GLYPHS
2794 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2795 SkTypeface::UniqueID(paint->getTypeface()));
2796#endif
2797
2798 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2799 fontRenderer.setFont(paint, *mSnapshot->transform);
2800
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002801 const float oldX = x;
2802 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002803 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002804 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002805 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2806 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2807 }
2808
Romain Guy86568192010-12-14 15:55:39 -08002809 int alpha;
2810 SkXfermode::Mode mode;
2811 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002812
Romain Guy211370f2012-02-01 16:10:55 -08002813 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002814 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2815 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002816 }
2817
Romain Guy6620c6d2010-12-06 18:07:02 -08002818 // Pick the appropriate texture filtering
2819 bool linearFilter = mSnapshot->transform->changesBounds();
2820 if (pureTranslate && !linearFilter) {
2821 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2822 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002823
Romain Guy16c88082012-06-11 16:03:47 -07002824 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002825 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002826 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002827 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002828 setupDrawDirtyRegionsDisabled();
2829 setupDrawWithTexture(true);
2830 setupDrawAlpha8Color(paint->getColor(), alpha);
2831 setupDrawColorFilter();
2832 setupDrawShader();
2833 setupDrawBlending(true, mode);
2834 setupDrawProgram();
2835 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002836 // See comment above; the font renderer must use texture unit 0
2837 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002838 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2839 setupDrawPureColorUniforms();
2840 setupDrawColorFilterUniforms();
2841 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002842 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002843
Romain Guya3dc55f2012-09-28 13:55:44 -07002844 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2845 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002846 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2847
Romain Guy211370f2012-02-01 16:10:55 -08002848 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002849
Raph Levien996e57c2012-07-23 15:22:52 -07002850 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002851 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002852 SkPaint paintCopy(*paint);
2853 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2854 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002855 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002856 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002857 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002858 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002859 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002860
2861 if (status && hasActiveLayer) {
2862 if (!pureTranslate) {
2863 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002864 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002865 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002866 }
Romain Guy694b5192010-07-21 21:33:20 -07002867
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002868 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002869
2870 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002871}
2872
Chet Haase48659092012-05-31 15:21:51 -07002873status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002874 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002875 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2876 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002877 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002878 }
2879
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002880 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002881 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002882
2883 int alpha;
2884 SkXfermode::Mode mode;
2885 getAlphaAndMode(paint, &alpha, &mode);
2886
2887 mCaches.activeTexture(0);
2888 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002889 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002890 setupDrawDirtyRegionsDisabled();
2891 setupDrawWithTexture(true);
2892 setupDrawAlpha8Color(paint->getColor(), alpha);
2893 setupDrawColorFilter();
2894 setupDrawShader();
2895 setupDrawBlending(true, mode);
2896 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002897 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002898 setupDrawTexture(fontRenderer.getTexture(true));
2899 setupDrawPureColorUniforms();
2900 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002901 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002902 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002903
Romain Guy97771732012-02-28 18:17:02 -08002904 const Rect* clip = &mSnapshot->getLocalClip();
2905 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 -08002906
Romain Guy97771732012-02-28 18:17:02 -08002907 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002908
2909 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2910 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002911 if (hasActiveLayer) {
2912 mSnapshot->transform->mapRect(bounds);
2913 dirtyLayerUnchecked(bounds, getRegion());
2914 }
Romain Guy97771732012-02-28 18:17:02 -08002915 }
Chet Haase48659092012-05-31 15:21:51 -07002916
2917 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002918}
2919
Chet Haase48659092012-05-31 15:21:51 -07002920status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2921 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002922
Romain Guya1d3c912011-12-13 14:55:06 -08002923 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002924
Romain Guyfb8b7632010-08-23 21:05:08 -07002925 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002926 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002927 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002928
Romain Guy8b55f372010-08-18 17:10:07 -07002929 const float x = texture->left - texture->offset;
2930 const float y = texture->top - texture->offset;
2931
Romain Guy01d58e42011-01-19 21:54:02 -08002932 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002933
2934 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002935}
2936
Chet Haase48659092012-05-31 15:21:51 -07002937status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002938 if (!layer) {
2939 return DrawGlInfo::kStatusDone;
2940 }
2941
Romain Guyb2e2f242012-10-17 18:18:35 -07002942 mat4* transform = NULL;
2943 if (layer->isTextureLayer()) {
2944 transform = &layer->getTransform();
2945 if (!transform->isIdentity()) {
2946 save(0);
2947 mSnapshot->transform->multiply(*transform);
2948 }
2949 }
2950
Romain Guy35643dd2012-09-18 15:40:58 -07002951 Rect transformed;
2952 Rect clip;
2953 const bool rejected = quickRejectNoScissor(x, y,
2954 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2955
2956 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002957 if (transform && !transform->isIdentity()) {
2958 restore();
2959 }
Chet Haase48659092012-05-31 15:21:51 -07002960 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002961 }
2962
Romain Guy5bb3c732012-11-29 17:52:58 -08002963 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002964
Romain Guy87e2f7572012-09-24 11:37:12 -07002965 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002966 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002967
Romain Guy211370f2012-02-01 16:10:55 -08002968 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002969 SkiaColorFilter* oldFilter = mColorFilter;
2970 mColorFilter = layer->getColorFilter();
2971
Romain Guyc88e3572011-01-22 00:32:12 -08002972 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002973 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002974 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002975 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002976 setupDraw();
2977 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002978 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002979 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002980 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002981 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002982 setupDrawPureColorUniforms();
2983 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002984 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002985 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002986 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2987 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002988
Romain Guyd21b6e12011-11-30 20:21:23 -08002989 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002990 setupDrawModelViewTranslate(tx, ty,
2991 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002992 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002993 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002994 setupDrawModelViewTranslate(x, y,
2995 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2996 }
Romain Guyc88e3572011-01-22 00:32:12 -08002997 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002998
Romain Guyc88e3572011-01-22 00:32:12 -08002999 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3000 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003001
Romain Guyc88e3572011-01-22 00:32:12 -08003002 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003003
3004#if DEBUG_LAYERS_AS_REGIONS
3005 drawRegionRects(layer->region);
3006#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003007 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003008
Romain Guye529ece2012-09-26 11:23:17 -07003009 mColorFilter = oldFilter;
3010
Romain Guy5bb3c732012-11-29 17:52:58 -08003011 if (layer->debugDrawUpdate) {
3012 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003013 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3014 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3015 }
Romain Guyf219da52011-01-16 12:54:25 -08003016 }
Chet Haase48659092012-05-31 15:21:51 -07003017
Romain Guyb2e2f242012-10-17 18:18:35 -07003018 if (transform && !transform->isIdentity()) {
3019 restore();
3020 }
3021
Chet Haase48659092012-05-31 15:21:51 -07003022 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003023}
3024
Romain Guy6926c722010-07-12 20:20:03 -07003025///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003026// Shaders
3027///////////////////////////////////////////////////////////////////////////////
3028
3029void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07003030 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003031}
3032
Romain Guy06f96e22010-07-30 19:18:16 -07003033void OpenGLRenderer::setupShader(SkiaShader* shader) {
3034 mShader = shader;
3035 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003036 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003037 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003038}
3039
Romain Guyd27977d2010-07-14 19:18:51 -07003040///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003041// Color filters
3042///////////////////////////////////////////////////////////////////////////////
3043
3044void OpenGLRenderer::resetColorFilter() {
3045 mColorFilter = NULL;
3046}
3047
3048void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
3049 mColorFilter = filter;
3050}
3051
3052///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003053// Drop shadow
3054///////////////////////////////////////////////////////////////////////////////
3055
3056void OpenGLRenderer::resetShadow() {
3057 mHasShadow = false;
3058}
3059
3060void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
3061 mHasShadow = true;
3062 mShadowRadius = radius;
3063 mShadowDx = dx;
3064 mShadowDy = dy;
3065 mShadowColor = color;
3066}
3067
3068///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003069// Draw filters
3070///////////////////////////////////////////////////////////////////////////////
3071
3072void OpenGLRenderer::resetPaintFilter() {
3073 mHasDrawFilter = false;
3074}
3075
3076void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
3077 mHasDrawFilter = true;
3078 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3079 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
3080}
3081
3082SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08003083 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08003084
3085 uint32_t flags = paint->getFlags();
3086
3087 mFilteredPaint = *paint;
3088 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
3089
3090 return &mFilteredPaint;
3091}
3092
3093///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003094// Drawing implementation
3095///////////////////////////////////////////////////////////////////////////////
3096
Romain Guy01d58e42011-01-19 21:54:02 -08003097void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3098 float x, float y, SkPaint* paint) {
3099 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3100 return;
3101 }
3102
3103 int alpha;
3104 SkXfermode::Mode mode;
3105 getAlphaAndMode(paint, &alpha, &mode);
3106
3107 setupDraw();
3108 setupDrawWithTexture(true);
3109 setupDrawAlpha8Color(paint->getColor(), alpha);
3110 setupDrawColorFilter();
3111 setupDrawShader();
3112 setupDrawBlending(true, mode);
3113 setupDrawProgram();
3114 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3115 setupDrawTexture(texture->id);
3116 setupDrawPureColorUniforms();
3117 setupDrawColorFilterUniforms();
3118 setupDrawShaderUniforms();
3119 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3120
3121 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3122
3123 finishDrawTexture();
3124}
3125
Romain Guyf607bdc2010-09-10 19:20:06 -07003126// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003127#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3128#define kStdUnderline_Offset (1.0f / 9.0f)
3129#define kStdUnderline_Thickness (1.0f / 18.0f)
3130
3131void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3132 float x, float y, SkPaint* paint) {
3133 // Handle underline and strike-through
3134 uint32_t flags = paint->getFlags();
3135 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003136 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003137 float underlineWidth = length;
3138 // If length is > 0.0f, we already measured the text for the text alignment
3139 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003140 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003141 }
3142
Romain Guy211370f2012-02-01 16:10:55 -08003143 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003144 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003145 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003146
Raph Levien8b4072d2012-07-30 15:50:00 -07003147 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003148 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003149
Romain Guyf6834472011-01-23 13:32:12 -08003150 int linesCount = 0;
3151 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3152 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3153
3154 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003155 float points[pointsCount];
3156 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003157
3158 if (flags & SkPaint::kUnderlineText_Flag) {
3159 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003160 points[currentPoint++] = left;
3161 points[currentPoint++] = top;
3162 points[currentPoint++] = left + underlineWidth;
3163 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003164 }
3165
3166 if (flags & SkPaint::kStrikeThruText_Flag) {
3167 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003168 points[currentPoint++] = left;
3169 points[currentPoint++] = top;
3170 points[currentPoint++] = left + underlineWidth;
3171 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003172 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003173
Romain Guy726aeba2011-06-01 14:52:00 -07003174 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003175
Romain Guy726aeba2011-06-01 14:52:00 -07003176 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003177 }
3178 }
3179}
3180
Romain Guy672433d2013-01-04 19:05:13 -08003181status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3182 if (mSnapshot->isIgnored()) {
3183 return DrawGlInfo::kStatusDone;
3184 }
3185
Romain Guy735738c2012-12-03 12:34:51 -08003186 int color = paint->getColor();
3187 // If a shader is set, preserve only the alpha
3188 if (mShader) {
3189 color |= 0x00ffffff;
3190 }
3191 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3192
3193 return drawColorRects(rects, count, color, mode);
3194}
3195
3196status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy8ce00302013-01-15 18:51:42 -08003197 SkXfermode::Mode mode, bool ignoreTransform, bool dirty) {
Romain Guy735738c2012-12-03 12:34:51 -08003198
Romain Guy672433d2013-01-04 19:05:13 -08003199 float left = FLT_MAX;
3200 float top = FLT_MAX;
3201 float right = FLT_MIN;
3202 float bottom = FLT_MIN;
3203
3204 int vertexCount = 0;
3205 Vertex mesh[count * 6];
3206 Vertex* vertex = mesh;
3207
3208 for (int i = 0; i < count; i++) {
3209 int index = i * 4;
3210 float l = rects[index + 0];
3211 float t = rects[index + 1];
3212 float r = rects[index + 2];
3213 float b = rects[index + 3];
3214
Romain Guy8ce00302013-01-15 18:51:42 -08003215 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003216 Vertex::set(vertex++, l, b);
3217 Vertex::set(vertex++, l, t);
3218 Vertex::set(vertex++, r, t);
3219 Vertex::set(vertex++, l, b);
3220 Vertex::set(vertex++, r, t);
3221 Vertex::set(vertex++, r, b);
3222
3223 vertexCount += 6;
3224
3225 left = fminf(left, l);
3226 top = fminf(top, t);
3227 right = fmaxf(right, r);
3228 bottom = fmaxf(bottom, b);
3229 }
3230 }
3231
3232 if (count == 0) return DrawGlInfo::kStatusDone;
3233
Romain Guy672433d2013-01-04 19:05:13 -08003234 setupDraw();
3235 setupDrawNoTexture();
3236 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3237 setupDrawShader();
3238 setupDrawColorFilter();
3239 setupDrawBlending(mode);
3240 setupDrawProgram();
3241 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003242 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003243 setupDrawColorUniforms();
3244 setupDrawShaderUniforms();
3245 setupDrawColorFilterUniforms();
3246 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3247
Romain Guy8ce00302013-01-15 18:51:42 -08003248 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003249 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3250 }
3251
3252 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3253
3254 return DrawGlInfo::kStatusDrew;
3255}
3256
Romain Guy026c5e162010-06-28 17:12:22 -07003257void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003258 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003259 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003260 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003261 color |= 0x00ffffff;
3262 }
3263
Romain Guy70ca14e2010-12-13 18:24:33 -08003264 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003265 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003266 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDrawShader();
3268 setupDrawColorFilter();
3269 setupDrawBlending(mode);
3270 setupDrawProgram();
3271 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3272 setupDrawColorUniforms();
3273 setupDrawShaderUniforms(ignoreTransform);
3274 setupDrawColorFilterUniforms();
3275 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003276
Romain Guyc95c8d62010-09-17 15:31:32 -07003277 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3278}
3279
Romain Guy82ba8142010-07-09 13:25:56 -07003280void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003281 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003282 int alpha;
3283 SkXfermode::Mode mode;
3284 getAlphaAndMode(paint, &alpha, &mode);
3285
Romain Guyd21b6e12011-11-30 20:21:23 -08003286 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003287
Romain Guy211370f2012-02-01 16:10:55 -08003288 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003289 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3290 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3291
Romain Guyd21b6e12011-11-30 20:21:23 -08003292 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003293 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3294 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3295 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3296 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003297 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003298 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3299 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3300 GL_TRIANGLE_STRIP, gMeshCount);
3301 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003302}
3303
Romain Guybd6b79b2010-06-26 00:13:53 -07003304void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003305 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3306 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003307 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003308}
3309
3310void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003311 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003312 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003313 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003314
Romain Guy746b7402010-10-26 16:27:31 -07003315 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003316 setupDrawWithTexture();
3317 setupDrawColor(alpha, alpha, alpha, alpha);
3318 setupDrawColorFilter();
3319 setupDrawBlending(blend, mode, swapSrcDst);
3320 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003321 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003322 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003323 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003324 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003325 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003326 }
Romain Guy886b2752013-01-04 12:26:18 -08003327 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003328 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003329 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003331
Romain Guy6820ac82010-09-15 18:11:50 -07003332 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003333
3334 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003335}
3336
Romain Guy886b2752013-01-04 12:26:18 -08003337void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3338 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3339 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3340 bool ignoreTransform, bool dirty) {
3341
3342 setupDraw();
3343 setupDrawWithTexture(true);
3344 if (hasColor) {
3345 setupDrawAlpha8Color(color, alpha);
3346 }
3347 setupDrawColorFilter();
3348 setupDrawShader();
3349 setupDrawBlending(true, mode);
3350 setupDrawProgram();
3351 if (!dirty) setupDrawDirtyRegionsDisabled();
3352 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3353 setupDrawTexture(texture);
3354 setupDrawPureColorUniforms();
3355 setupDrawColorFilterUniforms();
3356 setupDrawShaderUniforms();
3357 setupDrawMesh(vertices, texCoords);
3358
3359 glDrawArrays(drawMode, 0, elementsCount);
3360
3361 finishDrawTexture();
3362}
3363
Romain Guya5aed0d2010-09-09 14:42:43 -07003364void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003365 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003366 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003367
Romain Guy82ba8142010-07-09 13:25:56 -07003368 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003369 // These blend modes are not supported by OpenGL directly and have
3370 // to be implemented using shaders. Since the shader will perform
3371 // the blending, turn blending off here
3372 // If the blend mode cannot be implemented using shaders, fall
3373 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003374 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003375 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003376 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003377 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003378
Romain Guy82bc7a72012-01-03 14:13:39 -08003379 if (mCaches.blend) {
3380 glDisable(GL_BLEND);
3381 mCaches.blend = false;
3382 }
3383
3384 return;
3385 } else {
3386 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003387 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003388 }
3389
3390 if (!mCaches.blend) {
3391 glEnable(GL_BLEND);
3392 }
3393
3394 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3395 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3396
3397 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3398 glBlendFunc(sourceMode, destMode);
3399 mCaches.lastSrcMode = sourceMode;
3400 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003401 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003402 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003403 glDisable(GL_BLEND);
3404 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003405 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003406}
3407
Romain Guy889f8d12010-07-29 14:37:42 -07003408bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003409 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003410 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003411 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003412 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003413 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003414 }
Romain Guy6926c722010-07-12 20:20:03 -07003415 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003416}
3417
Romain Guy026c5e162010-06-28 17:12:22 -07003418void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003419 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003420 TextureVertex::setUV(v++, u1, v1);
3421 TextureVertex::setUV(v++, u2, v1);
3422 TextureVertex::setUV(v++, u1, v2);
3423 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003424}
3425
Chet Haase5c13d892010-10-08 08:37:55 -07003426void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003427 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003428 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003429}
3430
Romain Guy9d5316e2010-06-24 19:30:36 -07003431}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003432}; // namespace android