blob: 8cda72989c707e5434c8c9dd883a9140682d4126 [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 Guy7c25aab2012-10-18 15:05:02 -0700172status_t OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom,
173 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 :
210 (const GLenum) GL_COLOR_ATTACHMENT0 };
211 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;
240 if (s->flags & Snapshot::kFlagIsFboLayer) {
241 clip = s->clipRect;
242 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700243
Romain Guy54c1a642012-09-27 17:55:46 -0700244 mCaches.startTiling(clip->left, s->height - clip->bottom,
245 clip->right - clip->left, clip->bottom - clip->top, opaque);
246 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700247}
248
249void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700250 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700251}
252
Romain Guyb025b9c2010-09-16 14:16:48 -0700253void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700254 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700255 endTiling();
256
Romain Guy11cb6422012-09-21 00:39:43 -0700257 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700258#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700259 GLenum status = GL_NO_ERROR;
260 while ((status = glGetError()) != GL_NO_ERROR) {
261 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
262 switch (status) {
263 case GL_INVALID_ENUM:
264 ALOGE(" GL_INVALID_ENUM");
265 break;
266 case GL_INVALID_VALUE:
267 ALOGE(" GL_INVALID_VALUE");
268 break;
269 case GL_INVALID_OPERATION:
270 ALOGE(" GL_INVALID_OPERATION");
271 break;
272 case GL_OUT_OF_MEMORY:
273 ALOGE(" Out of memory!");
274 break;
275 }
Romain Guya07105b2011-01-10 21:14:18 -0800276 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700277#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700278
Romain Guyc15008e2010-11-10 11:59:15 -0800279#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800280 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700281#else
282 if (mCaches.getDebugLevel() & kDebugMemory) {
283 mCaches.dumpMemoryUsage();
284 }
Romain Guyc15008e2010-11-10 11:59:15 -0800285#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700286 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700287}
288
Romain Guy6c319ca2011-01-11 14:29:25 -0800289void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700290 if (mCaches.currentProgram) {
291 if (mCaches.currentProgram->isInUse()) {
292 mCaches.currentProgram->remove();
293 mCaches.currentProgram = NULL;
294 }
295 }
Romain Guy50c0f092010-10-19 11:42:22 -0700296 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800297 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800298 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800299 mCaches.disbaleTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700300 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700301}
302
Romain Guy6c319ca2011-01-11 14:29:25 -0800303void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800304 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800305 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700306 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700307 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700308
Romain Guy3e263fa2011-12-12 16:47:48 -0800309 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
310
Chet Haase80250612012-08-15 13:46:54 -0700311 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700312 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800313 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700314 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700315
Romain Guya1d3c912011-12-13 14:55:06 -0800316 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700317
Romain Guy50c0f092010-10-19 11:42:22 -0700318 mCaches.blend = true;
319 glEnable(GL_BLEND);
320 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
321 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700322}
323
Romain Guy35643dd2012-09-18 15:40:58 -0700324void OpenGLRenderer::resumeAfterLayer() {
325 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
326 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
327 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700329
330 mCaches.resetScissor();
331 dirtyClip();
332}
333
Romain Guyba6be8a2012-04-23 18:22:09 -0700334void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700335 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700336}
337
338void OpenGLRenderer::attachFunctor(Functor* functor) {
339 mFunctors.add(functor);
340}
341
Romain Guy8f3b8e32012-03-27 16:33:45 -0700342status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
343 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700344 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700345
Romain Guyba6be8a2012-04-23 18:22:09 -0700346 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800347 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700348 SortedVector<Functor*> functors(mFunctors);
349 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700350
Romain Guyba6be8a2012-04-23 18:22:09 -0700351 DrawGlInfo info;
352 info.clipLeft = 0;
353 info.clipTop = 0;
354 info.clipRight = 0;
355 info.clipBottom = 0;
356 info.isLayer = false;
357 info.width = 0;
358 info.height = 0;
359 memset(info.transform, 0, sizeof(float) * 16);
360
361 for (size_t i = 0; i < count; i++) {
362 Functor* f = functors.itemAt(i);
363 result |= (*f)(DrawGlInfo::kModeProcess, &info);
364
Chris Craikc2c95432012-04-25 15:13:52 -0700365 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700366 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
367 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700368 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700369
Chris Craikc2c95432012-04-25 15:13:52 -0700370 if (result & DrawGlInfo::kStatusInvoke) {
371 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700372 }
373 }
Chris Craikd15321b2012-11-28 14:45:04 -0800374 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700375 }
376
377 return result;
378}
379
380status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800381 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700382 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700383
Romain Guy8a4ac612012-07-17 17:32:48 -0700384 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800385 if (mDirtyClip) {
386 setScissorFromClip();
387 }
Romain Guyd643bb52011-03-01 14:55:21 -0800388
Romain Guy80911b82011-03-16 15:30:12 -0700389 Rect clip(*mSnapshot->clipRect);
390 clip.snapToPixelBoundaries();
391
Romain Guyd643bb52011-03-01 14:55:21 -0800392 // Since we don't know what the functor will draw, let's dirty
393 // tne entire clip region
394 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800395 dirtyLayerUnchecked(clip, getRegion());
396 }
Romain Guyd643bb52011-03-01 14:55:21 -0800397
Romain Guy08aa2cb2011-03-17 11:06:57 -0700398 DrawGlInfo info;
399 info.clipLeft = clip.left;
400 info.clipTop = clip.top;
401 info.clipRight = clip.right;
402 info.clipBottom = clip.bottom;
403 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700404 info.width = getSnapshot()->viewport.getWidth();
405 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700406 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700407
Chet Haase48659092012-05-31 15:21:51 -0700408 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800409
Romain Guy8f3b8e32012-03-27 16:33:45 -0700410 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700411 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800412 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700413
Chris Craik65924a32012-04-05 17:52:11 -0700414 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700416 }
Romain Guycabfcc12011-03-07 18:06:46 -0800417 }
418
Chet Haasedaf98e92011-01-10 14:10:36 -0800419 resume();
Romain Guy65549432012-03-26 16:45:05 -0700420 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800421}
422
Romain Guyf6a11b82010-06-23 17:47:49 -0700423///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700424// Debug
425///////////////////////////////////////////////////////////////////////////////
426
427void OpenGLRenderer::startMark(const char* name) const {
428 mCaches.startMark(0, name);
429}
430
431void OpenGLRenderer::endMark() const {
432 mCaches.endMark();
433}
434
435void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
436 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
437 if (clear) {
438 mCaches.disableScissor();
439 mCaches.stencil.clear();
440 }
441 if (enable) {
442 mCaches.stencil.enableDebugWrite();
443 } else {
444 mCaches.stencil.disable();
445 }
446 }
447}
448
449void OpenGLRenderer::renderOverdraw() {
450 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
451 const Rect* clip = mTilingSnapshot->clipRect;
452
453 mCaches.enableScissor();
454 mCaches.setScissor(clip->left, mTilingSnapshot->height - clip->bottom,
455 clip->right - clip->left, clip->bottom - clip->top);
456
457 mCaches.stencil.enableDebugTest(2);
458 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
459 mCaches.stencil.enableDebugTest(3);
460 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
461 mCaches.stencil.enableDebugTest(4);
462 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
463 mCaches.stencil.enableDebugTest(4, true);
464 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
465 mCaches.stencil.disable();
466 }
467}
468
469///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700470// Layers
471///////////////////////////////////////////////////////////////////////////////
472
473bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
474 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
475 OpenGLRenderer* renderer = layer->renderer;
476 Rect& dirty = layer->dirtyRect;
477
Romain Guy7c450aa2012-09-21 19:15:00 -0700478 if (inFrame) {
479 endTiling();
480 debugOverdraw(false, false);
481 }
Romain Guy11cb6422012-09-21 00:39:43 -0700482
483 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
484 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
485 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
486 renderer->finish();
487
488 if (inFrame) {
489 resumeAfterLayer();
490 startTiling(mSnapshot);
491 }
492
493 dirty.setEmpty();
494 layer->deferredUpdateScheduled = false;
495 layer->renderer = NULL;
496 layer->displayList = NULL;
Romain Guy5bb3c732012-11-29 17:52:58 -0800497 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700498
499 return true;
500 }
501
502 return false;
503}
504
505void OpenGLRenderer::updateLayers() {
506 int count = mLayerUpdates.size();
507 if (count > 0) {
508 startMark("Layer Updates");
509
510 // Note: it is very important to update the layers in reverse order
511 for (int i = count - 1; i >= 0; i--) {
512 Layer* layer = mLayerUpdates.itemAt(i);
513 updateLayer(layer, false);
514 mCaches.resourceCache.decrementRefcount(layer);
515 }
516 mLayerUpdates.clear();
517
518 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
519 endMark();
520 }
521}
522
523void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
524 if (layer) {
525 mLayerUpdates.push_back(layer);
526 mCaches.resourceCache.incrementRefcount(layer);
527 }
528}
529
530void OpenGLRenderer::clearLayerUpdates() {
531 size_t count = mLayerUpdates.size();
532 if (count > 0) {
533 mCaches.resourceCache.lock();
534 for (size_t i = 0; i < count; i++) {
535 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
536 }
537 mCaches.resourceCache.unlock();
538 mLayerUpdates.clear();
539 }
540}
541
542///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700543// State management
544///////////////////////////////////////////////////////////////////////////////
545
Romain Guybb9524b2010-06-22 18:56:38 -0700546int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700547 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700548}
549
550int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700551 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700552}
553
554void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700555 if (mSaveCount > 1) {
556 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700557 }
Romain Guybb9524b2010-06-22 18:56:38 -0700558}
559
560void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700561 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700562
Romain Guy8fb95422010-08-17 18:38:51 -0700563 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700564 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700565 }
Romain Guybb9524b2010-06-22 18:56:38 -0700566}
567
Romain Guy8aef54f2010-09-01 15:13:49 -0700568int OpenGLRenderer::saveSnapshot(int flags) {
569 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700570 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700571}
572
573bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700574 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700575 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700576 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700577
Romain Guybd6b79b2010-06-26 00:13:53 -0700578 sp<Snapshot> current = mSnapshot;
579 sp<Snapshot> previous = mSnapshot->previous;
580
Romain Guyeb993562010-10-05 18:14:38 -0700581 if (restoreOrtho) {
582 Rect& r = previous->viewport;
583 glViewport(r.left, r.top, r.right, r.bottom);
584 mOrthoMatrix.load(current->orthoMatrix);
585 }
586
Romain Guy8b55f372010-08-18 17:10:07 -0700587 mSaveCount--;
588 mSnapshot = previous;
589
Romain Guy2542d192010-08-18 11:47:12 -0700590 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700591 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700592 }
Romain Guy2542d192010-08-18 11:47:12 -0700593
Romain Guy5ec99242010-11-03 16:19:08 -0700594 if (restoreLayer) {
595 composeLayer(current, previous);
596 }
597
Romain Guy2542d192010-08-18 11:47:12 -0700598 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700599}
600
Romain Guyf6a11b82010-06-23 17:47:49 -0700601///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700602// Layers
603///////////////////////////////////////////////////////////////////////////////
604
605int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700606 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700607 const GLuint previousFbo = mSnapshot->fbo;
608 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700609
Romain Guyaf636eb2010-12-09 17:47:21 -0800610 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700611 int alpha = 255;
612 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700613
Romain Guye45362c2010-11-03 19:58:32 -0700614 if (p) {
615 alpha = p->getAlpha();
Chris Craik710f46d2012-09-17 17:25:49 -0700616 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700617 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700618 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700619 }
Romain Guyd55a8612010-06-28 17:42:46 -0700620
Chet Haased48885a2012-08-28 17:43:28 -0700621 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700622 }
Romain Guyd55a8612010-06-28 17:42:46 -0700623
624 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700625}
626
627int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
628 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700629 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700630 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700631 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700632 SkPaint paint;
633 paint.setAlpha(alpha);
634 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700635 }
Romain Guyd55a8612010-06-28 17:42:46 -0700636}
Romain Guybd6b79b2010-06-26 00:13:53 -0700637
Romain Guy1c740bc2010-09-13 18:00:09 -0700638/**
639 * Layers are viewed by Skia are slightly different than layers in image editing
640 * programs (for instance.) When a layer is created, previously created layers
641 * and the frame buffer still receive every drawing command. For instance, if a
642 * layer is created and a shape intersecting the bounds of the layers and the
643 * framebuffer is draw, the shape will be drawn on both (unless the layer was
644 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
645 *
646 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
647 * texture. Unfortunately, this is inefficient as it requires every primitive to
648 * be drawn n + 1 times, where n is the number of active layers. In practice this
649 * means, for every primitive:
650 * - Switch active frame buffer
651 * - Change viewport, clip and projection matrix
652 * - Issue the drawing
653 *
654 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700655 * To avoid this, layers are implemented in a different way here, at least in the
656 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
657 * is set. When this flag is set we can redirect all drawing operations into a
658 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700659 *
660 * This implementation relies on the frame buffer being at least RGBA 8888. When
661 * a layer is created, only a texture is created, not an FBO. The content of the
662 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700663 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700664 * buffer and drawing continues as normal. This technique therefore treats the
665 * frame buffer as a scratch buffer for the layers.
666 *
667 * To compose the layers back onto the frame buffer, each layer texture
668 * (containing the original frame buffer data) is drawn as a simple quad over
669 * the frame buffer. The trick is that the quad is set as the composition
670 * destination in the blending equation, and the frame buffer becomes the source
671 * of the composition.
672 *
673 * Drawing layers with an alpha value requires an extra step before composition.
674 * An empty quad is drawn over the layer's region in the frame buffer. This quad
675 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
676 * quad is used to multiply the colors in the frame buffer. This is achieved by
677 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
678 * GL_ZERO, GL_SRC_ALPHA.
679 *
680 * Because glCopyTexImage2D() can be slow, an alternative implementation might
681 * be use to draw a single clipped layer. The implementation described above
682 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700683 *
684 * (1) The frame buffer is actually not cleared right away. To allow the GPU
685 * to potentially optimize series of calls to glCopyTexImage2D, the frame
686 * buffer is left untouched until the first drawing operation. Only when
687 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700688 */
Chet Haased48885a2012-08-28 17:43:28 -0700689bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
690 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700691 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700692 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700693
Romain Guyeb993562010-10-05 18:14:38 -0700694 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
695
Romain Guyf607bdc2010-09-10 19:20:06 -0700696 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700697 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700698 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700699 Rect untransformedBounds(bounds);
700 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700701
Chet Haased48885a2012-08-28 17:43:28 -0700702 // Layers only make sense if they are in the framebuffer's bounds
703 if (bounds.intersect(*mSnapshot->clipRect)) {
704 // We cannot work with sub-pixels in this case
705 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700706
Chet Haased48885a2012-08-28 17:43:28 -0700707 // When the layer is not an FBO, we may use glCopyTexImage so we
708 // need to make sure the layer does not extend outside the bounds
709 // of the framebuffer
710 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700711 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700712 } else if (fboLayer) {
713 clip.set(bounds);
714 mat4 inverse;
715 inverse.loadInverse(*mSnapshot->transform);
716 inverse.mapRect(clip);
717 clip.snapToPixelBoundaries();
718 if (clip.intersect(untransformedBounds)) {
719 clip.translate(-left, -top);
720 bounds.set(untransformedBounds);
721 } else {
722 clip.setEmpty();
723 }
Romain Guyad37cd32011-03-15 11:12:25 -0700724 }
Chet Haased48885a2012-08-28 17:43:28 -0700725 } else {
726 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700727 }
Romain Guybf434112010-09-16 14:40:17 -0700728
Romain Guy746b7402010-10-26 16:27:31 -0700729 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700730 bounds.getHeight() > mCaches.maxTextureSize ||
731 (fboLayer && clip.isEmpty())) {
732 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700733 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700734 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700735 }
736
737 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700738 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700739 return false;
740 }
Romain Guyf18fd992010-07-08 11:45:51 -0700741
Romain Guya1d3c912011-12-13 14:55:06 -0800742 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700743 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700744 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700745 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700746 }
747
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700749 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700750 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
751 bounds.getWidth() / float(layer->getWidth()), 0.0f);
752 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700753 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700754 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700755
Romain Guy8fb95422010-08-17 18:38:51 -0700756 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700757 mSnapshot->flags |= Snapshot::kFlagIsLayer;
758 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700759
Romain Guyeb993562010-10-05 18:14:38 -0700760 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700761 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700762 } else {
763 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800765 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 if (layer->isEmpty()) {
767 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700768 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 layer->getWidth(), layer->getHeight(), 0);
770 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800771 } else {
772 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700773 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800774 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700775
Romain Guy54be1cd2011-06-13 19:04:27 -0700776 // Enqueue the buffer coordinates to clear the corresponding region later
777 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700778 }
Romain Guyeb993562010-10-05 18:14:38 -0700779 }
Romain Guyf86ef572010-07-01 11:05:42 -0700780
Romain Guyd55a8612010-06-28 17:42:46 -0700781 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700782}
783
Chet Haased48885a2012-08-28 17:43:28 -0700784bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700786
Chet Haased48885a2012-08-28 17:43:28 -0700787 mSnapshot->region = &mSnapshot->layer->region;
788 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700789
Chet Haased48885a2012-08-28 17:43:28 -0700790 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
791 mSnapshot->fbo = layer->getFbo();
792 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
793 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
794 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
795 mSnapshot->height = bounds.getHeight();
796 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
797 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700798
Romain Guy2b7028e2012-09-19 17:25:38 -0700799 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700800 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700801 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
803 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700804
805 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700806 if (layer->isEmpty()) {
807 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
808 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809 }
810
811 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Romain Guy2b7028e2012-09-19 17:25:38 -0700814 startTiling(mSnapshot);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815
816 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700817 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800818 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700819 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820 glClear(GL_COLOR_BUFFER_BIT);
821
822 dirtyClip();
823
824 // Change the ortho projection
825 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
826 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
827
828 return true;
829}
830
Romain Guy1c740bc2010-09-13 18:00:09 -0700831/**
832 * Read the documentation of createLayer() before doing anything in this method.
833 */
Romain Guy1d83e192010-08-17 11:37:00 -0700834void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
835 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000836 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700837 return;
838 }
839
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700841
842 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700843 endTiling();
844
Romain Guye0aa84b2012-04-03 19:30:26 -0700845 // Detach the texture from the FBO
846 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guyeb993562010-10-05 18:14:38 -0700847 // Unbind current FBO and restore previous one
848 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700849 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700850
851 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700852 }
853
Romain Guy1d83e192010-08-17 11:37:00 -0700854 Layer* layer = current->layer;
855 const Rect& rect = layer->layer;
856
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700858 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700860 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700862 }
863
Romain Guy03750a02010-10-18 14:06:08 -0700864 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700865
Romain Guya1d3c912011-12-13 14:55:06 -0800866 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700867
Romain Guy5b3b3522010-10-27 18:57:51 -0700868 // When the layer is stored in an FBO, we can save a bit of fillrate by
869 // drawing only the dirty region
870 if (fboLayer) {
871 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700872 if (layer->getColorFilter()) {
873 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800874 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700875 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700876 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800877 resetColorFilter();
878 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700879 } else if (!rect.isEmpty()) {
880 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
881 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700882 }
Romain Guy8b55f372010-08-18 17:10:07 -0700883
Romain Guyeb993562010-10-05 18:14:38 -0700884 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800885 // Note: No need to use glDiscardFramebufferEXT() since we never
886 // create/compose layers that are not on screen with this
887 // code path
888 // See LayerRenderer::destroyLayer(Layer*)
889
Romain Guyeb993562010-10-05 18:14:38 -0700890 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
891 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700892 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700893 }
894
Romain Guy746b7402010-10-26 16:27:31 -0700895 dirtyClip();
896
Romain Guyeb993562010-10-05 18:14:38 -0700897 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700898 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700899 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700900 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700901 }
902}
903
Romain Guyaa6c24c2011-04-28 18:40:04 -0700904void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700906
907 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700909 setupDrawWithTexture();
910 } else {
911 setupDrawWithExternalTexture();
912 }
913 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700914 setupDrawColor(alpha, alpha, alpha, alpha);
915 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700917 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700918 setupDrawPureColorUniforms();
919 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700920 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
921 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700922 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700924 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700925 if (mSnapshot->transform->isPureTranslate() &&
926 layer->getWidth() == (uint32_t) rect.getWidth() &&
927 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
929 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
930
Romain Guyd21b6e12011-11-30 20:21:23 -0800931 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
933 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800934 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
936 }
937 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700938 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
939
940 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
941
942 finishDrawTexture();
943}
944
Romain Guy5b3b3522010-10-27 18:57:51 -0700945void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700947 const Rect& texCoords = layer->texCoords;
948 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
949 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 float x = rect.left;
952 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700953 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700954 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700955 layer->getHeight() == (uint32_t) rect.getHeight();
956
957 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 // When we're swapping, the layer is already in screen coordinates
959 if (!swap) {
960 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
961 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
962 }
963
Romain Guyd21b6e12011-11-30 20:21:23 -0800964 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800966 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700967 }
968
969 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
970 layer->getTexture(), layer->getAlpha() / 255.0f,
971 layer->getMode(), layer->isBlend(),
972 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
973 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700974
Romain Guyaa6c24c2011-04-28 18:40:04 -0700975 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
976 } else {
977 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
978 drawTextureLayer(layer, rect);
979 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
980 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700981}
982
983void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700984 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700985 layer->setRegionAsRect();
986
Romain Guy40667672011-03-18 14:34:03 -0700987 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700988
Romain Guy5b3b3522010-10-27 18:57:51 -0700989 layer->region.clear();
990 return;
991 }
992
Romain Guy8a3957d2011-09-07 17:55:15 -0700993 // TODO: See LayerRenderer.cpp::generateMesh() for important
994 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800995 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700996 size_t count;
997 const android::Rect* rects = layer->region.getArray(&count);
998
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 const float alpha = layer->getAlpha() / 255.0f;
1000 const float texX = 1.0f / float(layer->getWidth());
1001 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001002 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
1004 TextureVertex* mesh = mCaches.getRegionMesh();
1005 GLsizei numQuads = 0;
1006
Romain Guy7230a742011-01-10 22:26:16 -08001007 setupDraw();
1008 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 Guy5b3b3522010-10-27 18:57:51 -07001092void OpenGLRenderer::dirtyLayer(const float left, const float top,
1093 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001094 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001095 Rect bounds(left, top, right, bottom);
1096 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001097 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001098 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001099}
1100
1101void OpenGLRenderer::dirtyLayer(const float left, const float top,
1102 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001103 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001104 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001105 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001106 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001107}
1108
1109void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001110 if (bounds.intersect(*mSnapshot->clipRect)) {
1111 bounds.snapToPixelBoundaries();
1112 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1113 if (!dirty.isEmpty()) {
1114 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001115 }
1116 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001117}
1118
Romain Guy54be1cd2011-06-13 19:04:27 -07001119void OpenGLRenderer::clearLayerRegions() {
1120 const size_t count = mLayers.size();
1121 if (count == 0) return;
1122
1123 if (!mSnapshot->isIgnored()) {
1124 // Doing several glScissor/glClear here can negatively impact
1125 // GPUs with a tiler architecture, instead we draw quads with
1126 // the Clear blending mode
1127
1128 // The list contains bounds that have already been clipped
1129 // against their initial clip rect, and the current clip
1130 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001131 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001132
1133 Vertex mesh[count * 6];
1134 Vertex* vertex = mesh;
1135
1136 for (uint32_t i = 0; i < count; i++) {
1137 Rect* bounds = mLayers.itemAt(i);
1138
1139 Vertex::set(vertex++, bounds->left, bounds->bottom);
1140 Vertex::set(vertex++, bounds->left, bounds->top);
1141 Vertex::set(vertex++, bounds->right, bounds->top);
1142 Vertex::set(vertex++, bounds->left, bounds->bottom);
1143 Vertex::set(vertex++, bounds->right, bounds->top);
1144 Vertex::set(vertex++, bounds->right, bounds->bottom);
1145
1146 delete bounds;
1147 }
1148
1149 setupDraw(false);
1150 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1151 setupDrawBlending(true, SkXfermode::kClear_Mode);
1152 setupDrawProgram();
1153 setupDrawPureColorUniforms();
1154 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001155 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001156
Romain Guy54be1cd2011-06-13 19:04:27 -07001157 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001158
1159 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001160 } else {
1161 for (uint32_t i = 0; i < count; i++) {
1162 delete mLayers.itemAt(i);
1163 }
1164 }
1165
1166 mLayers.clear();
1167}
1168
Romain Guybd6b79b2010-06-26 00:13:53 -07001169///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001170// Transforms
1171///////////////////////////////////////////////////////////////////////////////
1172
1173void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001174 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001175}
1176
1177void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001178 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001179}
1180
1181void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001182 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001183}
1184
Romain Guy807daf72011-01-18 11:19:19 -08001185void OpenGLRenderer::skew(float sx, float sy) {
1186 mSnapshot->transform->skew(sx, sy);
1187}
1188
Romain Guyf6a11b82010-06-23 17:47:49 -07001189void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001190 if (matrix) {
1191 mSnapshot->transform->load(*matrix);
1192 } else {
1193 mSnapshot->transform->loadIdentity();
1194 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001195}
1196
1197void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001198 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001199}
1200
1201void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001202 SkMatrix transform;
1203 mSnapshot->transform->copyTo(transform);
1204 transform.preConcat(*matrix);
1205 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001206}
1207
1208///////////////////////////////////////////////////////////////////////////////
1209// Clipping
1210///////////////////////////////////////////////////////////////////////////////
1211
Romain Guybb9524b2010-06-22 18:56:38 -07001212void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001213 Rect clip(*mSnapshot->clipRect);
1214 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001215
Romain Guy8a4ac612012-07-17 17:32:48 -07001216 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1217 clip.getWidth(), clip.getHeight())) {
1218 mDirtyClip = false;
1219 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001220}
1221
1222const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001223 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001224}
1225
Romain Guy8a4ac612012-07-17 17:32:48 -07001226bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1227 if (mSnapshot->isIgnored()) {
1228 return true;
1229 }
1230
1231 Rect r(left, top, right, bottom);
1232 mSnapshot->transform->mapRect(r);
1233 r.snapToPixelBoundaries();
1234
1235 Rect clipRect(*mSnapshot->clipRect);
1236 clipRect.snapToPixelBoundaries();
1237
1238 return !clipRect.intersects(r);
1239}
1240
Romain Guy35643dd2012-09-18 15:40:58 -07001241bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1242 Rect& transformed, Rect& clip) {
1243 if (mSnapshot->isIgnored()) {
1244 return true;
1245 }
1246
1247 transformed.set(left, top, right, bottom);
1248 mSnapshot->transform->mapRect(transformed);
1249 transformed.snapToPixelBoundaries();
1250
1251 clip.set(*mSnapshot->clipRect);
1252 clip.snapToPixelBoundaries();
1253
1254 return !clip.intersects(transformed);
1255}
1256
Romain Guy672433d2013-01-04 19:05:13 -08001257bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1258 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001259 if (paint->getStyle() != SkPaint::kFill_Style) {
1260 float outset = paint->getStrokeWidth() * 0.5f;
1261 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1262 } else {
1263 return quickReject(left, top, right, bottom);
1264 }
1265}
1266
Romain Guyc7d53492010-06-25 13:41:57 -07001267bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001268 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001269 return true;
1270 }
1271
Romain Guy1d83e192010-08-17 11:37:00 -07001272 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001273 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001274 r.snapToPixelBoundaries();
1275
1276 Rect clipRect(*mSnapshot->clipRect);
1277 clipRect.snapToPixelBoundaries();
1278
Romain Guy586cae32012-07-13 15:28:31 -07001279 bool rejected = !clipRect.intersects(r);
1280 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001281 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001282 }
1283
1284 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001285}
1286
Romain Guy079ba2c2010-07-16 14:12:24 -07001287bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1288 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001289 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001290 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001291 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001292 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001293}
1294
Chet Haasea23eed82012-04-12 15:19:04 -07001295Rect* OpenGLRenderer::getClipRect() {
1296 return mSnapshot->clipRect;
1297}
1298
Romain Guyf6a11b82010-06-23 17:47:49 -07001299///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001300// Drawing commands
1301///////////////////////////////////////////////////////////////////////////////
1302
Romain Guy54be1cd2011-06-13 19:04:27 -07001303void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001304 // TODO: It would be best if we could do this before quickReject()
1305 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001306 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001307 if (mDirtyClip) {
1308 setScissorFromClip();
1309 }
1310 mDescription.reset();
1311 mSetShaderColor = false;
1312 mColorSet = false;
1313 mColorA = mColorR = mColorG = mColorB = 0.0f;
1314 mTextureUnit = 0;
1315 mTrackDirtyRegions = true;
1316}
1317
1318void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1319 mDescription.hasTexture = true;
1320 mDescription.hasAlpha8Texture = isAlpha8;
1321}
1322
Romain Guyaa6c24c2011-04-28 18:40:04 -07001323void OpenGLRenderer::setupDrawWithExternalTexture() {
1324 mDescription.hasExternalTexture = true;
1325}
1326
Romain Guy15bc6432011-12-13 13:11:32 -08001327void OpenGLRenderer::setupDrawNoTexture() {
1328 mCaches.disbaleTexCoordsVertexArray();
1329}
1330
Chris Craik710f46d2012-09-17 17:25:49 -07001331void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001332 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001333}
1334
Chris Craik710f46d2012-09-17 17:25:49 -07001335void OpenGLRenderer::setupDrawVertexShape() {
1336 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001337}
1338
Romain Guyed6fcb02011-03-21 13:11:28 -07001339void OpenGLRenderer::setupDrawPoint(float pointSize) {
1340 mDescription.isPoint = true;
1341 mDescription.pointSize = pointSize;
1342}
1343
Romain Guy8d0d4782010-12-14 20:13:35 -08001344void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1345 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001346 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1347 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1348 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 mColorSet = true;
1350 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1351}
1352
Romain Guy86568192010-12-14 15:55:39 -08001353void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1354 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001355 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1356 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1357 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001358 mColorSet = true;
1359 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1360}
1361
Romain Guy41210632012-07-16 17:04:24 -07001362void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1363 mCaches.fontRenderer->describe(mDescription, paint);
1364}
1365
Romain Guy70ca14e2010-12-13 18:24:33 -08001366void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1367 mColorA = a;
1368 mColorR = r;
1369 mColorG = g;
1370 mColorB = b;
1371 mColorSet = true;
1372 mSetShaderColor = mDescription.setColor(r, g, b, a);
1373}
1374
1375void OpenGLRenderer::setupDrawShader() {
1376 if (mShader) {
1377 mShader->describe(mDescription, mCaches.extensions);
1378 }
1379}
1380
1381void OpenGLRenderer::setupDrawColorFilter() {
1382 if (mColorFilter) {
1383 mColorFilter->describe(mDescription, mCaches.extensions);
1384 }
1385}
1386
Romain Guyf09ef512011-05-27 11:43:46 -07001387void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1388 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1389 mColorA = 1.0f;
1390 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001391 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001392 }
1393}
1394
Romain Guy70ca14e2010-12-13 18:24:33 -08001395void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001396 // When the blending mode is kClear_Mode, we need to use a modulate color
1397 // argb=1,0,0,0
1398 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001399 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1400 mDescription, swapSrcDst);
1401}
1402
1403void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001404 // When the blending mode is kClear_Mode, we need to use a modulate color
1405 // argb=1,0,0,0
1406 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001407 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1408 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001409}
1410
1411void OpenGLRenderer::setupDrawProgram() {
1412 useProgram(mCaches.programCache.get(mDescription));
1413}
1414
1415void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1416 mTrackDirtyRegions = false;
1417}
1418
1419void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1420 bool ignoreTransform) {
1421 mModelView.loadTranslate(left, top, 0.0f);
1422 if (!ignoreTransform) {
1423 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1424 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1425 } else {
1426 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1427 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1428 }
1429}
1430
Chet Haase8a5cc922011-04-26 07:28:09 -07001431void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1432 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001433}
1434
Romain Guy70ca14e2010-12-13 18:24:33 -08001435void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1436 bool ignoreTransform, bool ignoreModelView) {
1437 if (!ignoreModelView) {
1438 mModelView.loadTranslate(left, top, 0.0f);
1439 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001440 } else {
1441 mModelView.loadIdentity();
1442 }
Romain Guy86568192010-12-14 15:55:39 -08001443 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1444 if (!ignoreTransform) {
1445 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1446 if (mTrackDirtyRegions && dirty) {
1447 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1448 }
1449 } else {
1450 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1451 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1452 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001453}
1454
Romain Guyed6fcb02011-03-21 13:11:28 -07001455void OpenGLRenderer::setupDrawPointUniforms() {
1456 int slot = mCaches.currentProgram->getUniform("pointSize");
1457 glUniform1f(slot, mDescription.pointSize);
1458}
1459
Romain Guy70ca14e2010-12-13 18:24:33 -08001460void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001461 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001462 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1463 }
1464}
1465
Romain Guy86568192010-12-14 15:55:39 -08001466void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001467 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001468 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001469 }
1470}
1471
Romain Guy70ca14e2010-12-13 18:24:33 -08001472void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1473 if (mShader) {
1474 if (ignoreTransform) {
1475 mModelView.loadInverse(*mSnapshot->transform);
1476 }
1477 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1478 }
1479}
1480
Romain Guy8d0d4782010-12-14 20:13:35 -08001481void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1482 if (mShader) {
1483 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1484 }
1485}
1486
Romain Guy70ca14e2010-12-13 18:24:33 -08001487void OpenGLRenderer::setupDrawColorFilterUniforms() {
1488 if (mColorFilter) {
1489 mColorFilter->setupProgram(mCaches.currentProgram);
1490 }
1491}
1492
Romain Guy41210632012-07-16 17:04:24 -07001493void OpenGLRenderer::setupDrawTextGammaUniforms() {
1494 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1495}
1496
Romain Guy70ca14e2010-12-13 18:24:33 -08001497void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001498 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001499 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001500 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001501}
1502
1503void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1504 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001505 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001506 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001507}
1508
Romain Guyaa6c24c2011-04-28 18:40:04 -07001509void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1510 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001511 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001512 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001513}
1514
Romain Guy8f0095c2011-05-02 17:24:22 -07001515void OpenGLRenderer::setupDrawTextureTransform() {
1516 mDescription.hasTextureTransform = true;
1517}
1518
1519void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001520 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1521 GL_FALSE, &transform.data[0]);
1522}
1523
Romain Guy70ca14e2010-12-13 18:24:33 -08001524void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001525 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001526 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001527 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001528 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001529 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001530 }
Romain Guyd71dd362011-12-12 19:03:35 -08001531
Chris Craikcb4d6002012-09-25 12:00:29 -07001532 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001533 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001534 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001535 }
1536
1537 mCaches.unbindIndicesBuffer();
1538}
1539
1540void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1541 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001542 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001543 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001544 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001545 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001546}
1547
Chet Haase5b0200b2011-04-13 17:58:08 -07001548void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001549 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001550 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001551 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001552}
1553
1554/**
1555 * 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 -07001556 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1557 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1558 * attributes (one per vertex) are values from zero to one that tells the fragment
1559 * shader where the fragment is in relation to the line width/length overall; these values are
1560 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1561 * region of the line.
1562 * Note that we only pass down the width values in this setup function. The length coordinates
1563 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001564 */
Chet Haase99585ad2011-05-02 15:00:16 -07001565void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001566 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001567 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001568 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001569 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001570 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001571
Romain Guy7b631422012-04-04 11:38:54 -07001572 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001573 glEnableVertexAttribArray(widthSlot);
1574 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001575
Romain Guy7b631422012-04-04 11:38:54 -07001576 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001577 glEnableVertexAttribArray(lengthSlot);
1578 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001579
Chet Haase5b0200b2011-04-13 17:58:08 -07001580 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001581 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001582}
1583
1584void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1585 glDisableVertexAttribArray(widthSlot);
1586 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001587}
1588
Romain Guy70ca14e2010-12-13 18:24:33 -08001589void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001590}
1591
1592///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001593// Drawing
1594///////////////////////////////////////////////////////////////////////////////
1595
Chet Haase1271e2c2012-04-20 09:54:27 -07001596status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001597 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001598
Romain Guy0fe478e2010-11-08 12:08:41 -08001599 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1600 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001601 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001602 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001603 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001604
Romain Guy65549432012-03-26 16:45:05 -07001605 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001606}
1607
Chet Haaseed30fd82011-04-22 16:18:45 -07001608void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1609 if (displayList) {
1610 displayList->output(*this, level);
1611 }
1612}
1613
Romain Guya168d732011-03-18 16:50:13 -07001614void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1615 int alpha;
1616 SkXfermode::Mode mode;
1617 getAlphaAndMode(paint, &alpha, &mode);
1618
Romain Guy886b2752013-01-04 12:26:18 -08001619 int color = paint != NULL ? paint->getColor() : 0;
1620
Romain Guya168d732011-03-18 16:50:13 -07001621 float x = left;
1622 float y = top;
1623
Romain Guy886b2752013-01-04 12:26:18 -08001624 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1625
Romain Guya168d732011-03-18 16:50:13 -07001626 bool ignoreTransform = false;
1627 if (mSnapshot->transform->isPureTranslate()) {
1628 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1629 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1630 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001631
1632 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001633 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001634 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001635 }
1636
Romain Guy886b2752013-01-04 12:26:18 -08001637 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1638 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1639 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001640}
1641
Chet Haase48659092012-05-31 15:21:51 -07001642status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001643 const float right = left + bitmap->width();
1644 const float bottom = top + bitmap->height();
1645
1646 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001647 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001648 }
1649
Romain Guya1d3c912011-12-13 14:55:06 -08001650 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001651 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001652 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001653 const AutoTexture autoCleanup(texture);
1654
Romain Guy211370f2012-02-01 16:10:55 -08001655 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001656 drawAlphaBitmap(texture, left, top, paint);
1657 } else {
1658 drawTextureRect(left, top, right, bottom, texture, paint);
1659 }
Chet Haase48659092012-05-31 15:21:51 -07001660
1661 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001662}
1663
Chet Haase48659092012-05-31 15:21:51 -07001664status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001665 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1666 const mat4 transform(*matrix);
1667 transform.mapRect(r);
1668
Romain Guy6926c722010-07-12 20:20:03 -07001669 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001670 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001671 }
1672
Romain Guya1d3c912011-12-13 14:55:06 -08001673 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001674 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001675 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001676 const AutoTexture autoCleanup(texture);
1677
Romain Guy5b3b3522010-10-27 18:57:51 -07001678 // This could be done in a cheaper way, all we need is pass the matrix
1679 // to the vertex shader. The save/restore is a bit overkill.
1680 save(SkCanvas::kMatrix_SaveFlag);
1681 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001682 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1683 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1684 } else {
1685 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1686 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001687 restore();
Chet Haase48659092012-05-31 15:21:51 -07001688
1689 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001690}
1691
Chet Haase48659092012-05-31 15:21:51 -07001692status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001693 const float right = left + bitmap->width();
1694 const float bottom = top + bitmap->height();
1695
1696 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001697 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001698 }
1699
1700 mCaches.activeTexture(0);
1701 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1702 const AutoTexture autoCleanup(texture);
1703
Romain Guy886b2752013-01-04 12:26:18 -08001704 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1705 drawAlphaBitmap(texture, left, top, paint);
1706 } else {
1707 drawTextureRect(left, top, right, bottom, texture, paint);
1708 }
Chet Haase48659092012-05-31 15:21:51 -07001709
1710 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001711}
1712
Chet Haase48659092012-05-31 15:21:51 -07001713status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001714 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001715 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001716 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001717 }
1718
Romain Guyb18d2d02011-02-10 15:52:54 -08001719 float left = FLT_MAX;
1720 float top = FLT_MAX;
1721 float right = FLT_MIN;
1722 float bottom = FLT_MIN;
1723
Romain Guya92bb4d2012-10-16 11:08:44 -07001724 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001725
Romain Guya566b7c2011-01-23 16:36:11 -08001726 // TODO: Support the colors array
1727 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001728 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001729
Romain Guy5a7b4662011-01-20 19:09:30 -08001730 for (int32_t y = 0; y < meshHeight; y++) {
1731 for (int32_t x = 0; x < meshWidth; x++) {
1732 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1733
1734 float u1 = float(x) / meshWidth;
1735 float u2 = float(x + 1) / meshWidth;
1736 float v1 = float(y) / meshHeight;
1737 float v2 = float(y + 1) / meshHeight;
1738
1739 int ax = i + (meshWidth + 1) * 2;
1740 int ay = ax + 1;
1741 int bx = i;
1742 int by = bx + 1;
1743 int cx = i + 2;
1744 int cy = cx + 1;
1745 int dx = i + (meshWidth + 1) * 2 + 2;
1746 int dy = dx + 1;
1747
1748 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1749 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1750 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1751
1752 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1753 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1754 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001755
Romain Guya92bb4d2012-10-16 11:08:44 -07001756 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1757 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1758 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1759 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001760 }
1761 }
1762
Romain Guya92bb4d2012-10-16 11:08:44 -07001763 if (quickReject(left, top, right, bottom)) {
1764 return DrawGlInfo::kStatusDone;
1765 }
1766
1767 mCaches.activeTexture(0);
1768 Texture* texture = mCaches.textureCache.get(bitmap);
1769 if (!texture) return DrawGlInfo::kStatusDone;
1770 const AutoTexture autoCleanup(texture);
1771
1772 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1773 texture->setFilter(FILTER(paint), true);
1774
1775 int alpha;
1776 SkXfermode::Mode mode;
1777 getAlphaAndMode(paint, &alpha, &mode);
1778
1779 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001780 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1781 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001782
Romain Guy5a7b4662011-01-20 19:09:30 -08001783 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1784 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001785 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001786
1787 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001788}
1789
Chet Haase48659092012-05-31 15:21:51 -07001790status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001791 float srcLeft, float srcTop, float srcRight, float srcBottom,
1792 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001793 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001794 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001795 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001796 }
1797
Romain Guya1d3c912011-12-13 14:55:06 -08001798 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001799 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001800 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001801 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001802
Romain Guy8ba548f2010-06-30 19:21:21 -07001803 const float width = texture->width;
1804 const float height = texture->height;
1805
Romain Guy68169722011-08-22 17:33:33 -07001806 const float u1 = fmax(0.0f, srcLeft / width);
1807 const float v1 = fmax(0.0f, srcTop / height);
1808 const float u2 = fmin(1.0f, srcRight / width);
1809 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001810
Romain Guy03750a02010-10-18 14:06:08 -07001811 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001812 resetDrawTextureTexCoords(u1, v1, u2, v2);
1813
Romain Guy03750a02010-10-18 14:06:08 -07001814 int alpha;
1815 SkXfermode::Mode mode;
1816 getAlphaAndMode(paint, &alpha, &mode);
1817
Romain Guyd21b6e12011-11-30 20:21:23 -08001818 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1819
Romain Guy886b2752013-01-04 12:26:18 -08001820 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1821 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001822
Romain Guy886b2752013-01-04 12:26:18 -08001823 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1824 // Apply a scale transform on the canvas only when a shader is in use
1825 // Skia handles the ratio between the dst and src rects as a scale factor
1826 // when a shader is set
1827 bool useScaleTransform = mShader && scaled;
1828 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001829
Romain Guy886b2752013-01-04 12:26:18 -08001830 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1831 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1832 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1833
1834 dstRight = x + (dstRight - dstLeft);
1835 dstBottom = y + (dstBottom - dstTop);
1836
1837 dstLeft = x;
1838 dstTop = y;
1839
1840 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1841 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001842 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001843 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001844 }
1845
1846 if (CC_UNLIKELY(useScaleTransform)) {
1847 save(SkCanvas::kMatrix_SaveFlag);
1848 translate(dstLeft, dstTop);
1849 scale(scaleX, scaleY);
1850
1851 dstLeft = 0.0f;
1852 dstTop = 0.0f;
1853
1854 dstRight = srcRight - srcLeft;
1855 dstBottom = srcBottom - srcTop;
1856 }
1857
1858 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1859 int color = paint ? paint->getColor() : 0;
1860 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1861 texture->id, paint != NULL, color, alpha, mode,
1862 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1863 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
1864 } else {
1865 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1866 texture->id, alpha / 255.0f, mode, texture->blend,
1867 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1868 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
1869 }
1870
1871 if (CC_UNLIKELY(useScaleTransform)) {
1872 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08001873 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001874
1875 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001876
1877 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001878}
1879
Chet Haase48659092012-05-31 15:21:51 -07001880status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001881 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001882 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001883 int alpha;
1884 SkXfermode::Mode mode;
1885 getAlphaAndModeDirect(paint, &alpha, &mode);
1886
1887 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1888 left, top, right, bottom, alpha, mode);
1889}
1890
1891status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1892 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1893 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001894 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001895 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001896 }
1897
Romain Guybe6f9dc2012-07-16 12:41:17 -07001898 alpha *= mSnapshot->alpha;
1899
Romain Guya1d3c912011-12-13 14:55:06 -08001900 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001901 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001902 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001903 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001904 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1905 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001906
Romain Guy2728f962010-10-08 18:36:15 -07001907 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001908 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001909
Romain Guy211370f2012-02-01 16:10:55 -08001910 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001911 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001912 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001913 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001914 const float offsetX = left + mSnapshot->transform->getTranslateX();
1915 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001916 const size_t count = mesh->quads.size();
1917 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001918 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001919 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001920 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1921 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1922 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001923 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001924 dirtyLayer(left + bounds.left, top + bounds.top,
1925 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001926 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001927 }
1928 }
1929
Romain Guy211370f2012-02-01 16:10:55 -08001930 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001931 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1932 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1933
1934 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1935 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1936 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1937 true, !mesh->hasEmptyQuads);
1938 } else {
1939 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1940 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1941 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1942 true, !mesh->hasEmptyQuads);
1943 }
Romain Guy054dc182010-10-15 17:55:25 -07001944 }
Chet Haase48659092012-05-31 15:21:51 -07001945
1946 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001947}
1948
Chet Haase99ecdc42011-05-06 12:06:34 -07001949/**
Chris Craikcb4d6002012-09-25 12:00:29 -07001950 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
1951 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
1952 * screen space in all directions. However, instead of using a fragment shader to compute the
1953 * translucency of the color from its position, we simply use a varying parameter to define how far
1954 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
1955 *
1956 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07001957 */
Chris Craikcb4d6002012-09-25 12:00:29 -07001958void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
1959 int color = paint->getColor();
1960 SkPaint::Style style = paint->getStyle();
1961 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
1962 bool isAA = paint->isAntiAlias();
1963
Chris Craik710f46d2012-09-17 17:25:49 -07001964 VertexBuffer vertexBuffer;
1965 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07001966 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07001967
Chris Craikbf09ffb2012-10-01 13:50:37 -07001968 if (!vertexBuffer.getSize()) {
1969 // no vertices to draw
1970 return;
1971 }
1972
Chris Craik710f46d2012-09-17 17:25:49 -07001973 setupDraw();
1974 setupDrawNoTexture();
1975 if (isAA) setupDrawAA();
1976 setupDrawVertexShape();
1977 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1978 setupDrawColorFilter();
1979 setupDrawShader();
1980 setupDrawBlending(isAA, mode);
1981 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07001982 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07001983 setupDrawColorUniforms();
1984 setupDrawColorFilterUniforms();
1985 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07001986
Chris Craik710f46d2012-09-17 17:25:49 -07001987 void* vertices = vertexBuffer.getBuffer();
1988 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001989 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07001990 mCaches.resetTexCoordsVertexPointer();
1991 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07001992
Chris Craik710f46d2012-09-17 17:25:49 -07001993 int alphaSlot = -1;
1994 if (isAA) {
1995 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
1996 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07001997
Chris Craik710f46d2012-09-17 17:25:49 -07001998 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07001999 glEnableVertexAttribArray(alphaSlot);
2000 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002001 }
Romain Guy04299382012-07-18 17:15:41 -07002002
Chris Craikcb4d6002012-09-25 12:00:29 -07002003 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002004 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002005
Chris Craik710f46d2012-09-17 17:25:49 -07002006 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002007
Chris Craik710f46d2012-09-17 17:25:49 -07002008 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002009 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002010 }
Chet Haase858aa932011-05-12 09:06:00 -07002011}
2012
2013/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002014 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2015 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2016 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2017 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2018 * of the line. Hairlines are more involved because we need to account for transform scaling
2019 * to end up with a one-pixel-wide line in screen space..
2020 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2021 * in combination with values that we calculate and pass down in this method. The basic approach
2022 * is that the quad we create contains both the core line area plus a bounding area in which
2023 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2024 * proportion of the width and the length of a given segment is represented by the boundary
2025 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2026 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2027 * on the inside). This ends up giving the result we want, with pixels that are completely
2028 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2029 * how far into the boundary region they are, which is determined by shader interpolation.
2030 */
Chet Haase48659092012-05-31 15:21:51 -07002031status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2032 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002033
2034 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002035 // We use half the stroke width here because we're going to position the quad
2036 // corner vertices half of the width away from the line endpoints
2037 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002038 // A stroke width of 0 has a special meaning in Skia:
2039 // it draws a line 1 px wide regardless of current transform
2040 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002041
Chet Haase99ecdc42011-05-06 12:06:34 -07002042 float inverseScaleX = 1.0f;
2043 float inverseScaleY = 1.0f;
2044 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002045
Chet Haase8a5cc922011-04-26 07:28:09 -07002046 int alpha;
2047 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002048
Chet Haase8a5cc922011-04-26 07:28:09 -07002049 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002050 int verticesCount = count;
2051 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002052 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002053 verticesCount += (count - 4);
2054 }
2055
2056 if (isHairLine || isAA) {
2057 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2058 // the line on the screen should always be one pixel wide regardless of scale. For
2059 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002060 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002061 Matrix4 *mat = mSnapshot->transform;
2062 float m00 = mat->data[Matrix4::kScaleX];
2063 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002064 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002065 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002066
Romain Guy211370f2012-02-01 16:10:55 -08002067 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2068 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002069
Chet Haase99ecdc42011-05-06 12:06:34 -07002070 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2071 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002072
Chet Haase99ecdc42011-05-06 12:06:34 -07002073 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2074 scaled = true;
2075 }
2076 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002077 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002078
2079 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002080
2081 mCaches.enableScissor();
2082
Chet Haase8a5cc922011-04-26 07:28:09 -07002083 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002084 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002085 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002086 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002087 }
2088 setupDrawColor(paint->getColor(), alpha);
2089 setupDrawColorFilter();
2090 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002091 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002092 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002093 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002094 setupDrawColorUniforms();
2095 setupDrawColorFilterUniforms();
2096 setupDrawShaderIdentityUniforms();
2097
2098 if (isHairLine) {
2099 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002100 halfStrokeWidth = isAA? 1 : .5;
2101 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002102 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002103 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002104 }
Romain Guy7b631422012-04-04 11:38:54 -07002105
2106 int widthSlot;
2107 int lengthSlot;
2108
Chet Haase5b0200b2011-04-13 17:58:08 -07002109 Vertex lines[verticesCount];
2110 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002111
Chet Haase99585ad2011-05-02 15:00:16 -07002112 AAVertex wLines[verticesCount];
2113 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002114
Romain Guy211370f2012-02-01 16:10:55 -08002115 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002116 setupDrawVertices(vertices);
2117 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002118 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2119 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002120 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002121 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2122 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002123 // We will need to calculate the actual width proportion on each segment for
2124 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002125 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002126 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2127 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002128 }
2129
Chet Haase99ecdc42011-05-06 12:06:34 -07002130 AAVertex* prevAAVertex = NULL;
2131 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002132
Chet Haase99585ad2011-05-02 15:00:16 -07002133 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002134 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002135
Chet Haase5b0200b2011-04-13 17:58:08 -07002136 for (int i = 0; i < count; i += 4) {
2137 // a = start point, b = end point
2138 vec2 a(points[i], points[i + 1]);
2139 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002140
Chet Haase99585ad2011-05-02 15:00:16 -07002141 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002142 float boundaryLengthProportion = 0;
2143 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002144
Chet Haase5b0200b2011-04-13 17:58:08 -07002145 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002146 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002147 float x = n.x;
2148 n.x = -n.y;
2149 n.y = x;
2150
Chet Haase8a5cc922011-04-26 07:28:09 -07002151 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002152 if (isAA) {
2153 float wideningFactor;
2154 if (fabs(n.x) >= fabs(n.y)) {
2155 wideningFactor = fabs(1.0f / n.x);
2156 } else {
2157 wideningFactor = fabs(1.0f / n.y);
2158 }
2159 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002160 }
Romain Guy7b631422012-04-04 11:38:54 -07002161
Chet Haase99ecdc42011-05-06 12:06:34 -07002162 if (scaled) {
2163 n.x *= inverseScaleX;
2164 n.y *= inverseScaleY;
2165 }
2166 } else if (scaled) {
2167 // Extend n by .5 pixel on each side, post-transform
2168 vec2 extendedN = n.copyNormalized();
2169 extendedN /= 2;
2170 extendedN.x *= inverseScaleX;
2171 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002172
Chet Haase99ecdc42011-05-06 12:06:34 -07002173 float extendedNLength = extendedN.length();
2174 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002175 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002176 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002177 }
Romain Guy7b631422012-04-04 11:38:54 -07002178
Chet Haase99585ad2011-05-02 15:00:16 -07002179 // aa lines expand the endpoint vertices to encompass the AA boundary
2180 if (isAA) {
2181 vec2 abVector = (b - a);
2182 length = abVector.length();
2183 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002184
Chet Haase99ecdc42011-05-06 12:06:34 -07002185 if (scaled) {
2186 abVector.x *= inverseScaleX;
2187 abVector.y *= inverseScaleY;
2188 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002189 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002190 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002191 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002192 }
Romain Guy7b631422012-04-04 11:38:54 -07002193
Chet Haase99ecdc42011-05-06 12:06:34 -07002194 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002195 a -= abVector;
2196 b += abVector;
2197 }
2198
Chet Haase5b0200b2011-04-13 17:58:08 -07002199 // Four corners of the rectangle defining a thick line
2200 vec2 p1 = a - n;
2201 vec2 p2 = a + n;
2202 vec2 p3 = b + n;
2203 vec2 p4 = b - n;
2204
Chet Haase99585ad2011-05-02 15:00:16 -07002205
Chet Haase5b0200b2011-04-13 17:58:08 -07002206 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2207 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2208 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2209 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2210
Romain Guy04299382012-07-18 17:15:41 -07002211 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002212 if (!isAA) {
2213 if (prevVertex != NULL) {
2214 // Issue two repeat vertices to create degenerate triangles to bridge
2215 // between the previous line and the new one. This is necessary because
2216 // we are creating a single triangle_strip which will contain
2217 // potentially discontinuous line segments.
2218 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2219 Vertex::set(vertices++, p1.x, p1.y);
2220 generatedVerticesCount += 2;
2221 }
Romain Guy7b631422012-04-04 11:38:54 -07002222
Chet Haase5b0200b2011-04-13 17:58:08 -07002223 Vertex::set(vertices++, p1.x, p1.y);
2224 Vertex::set(vertices++, p2.x, p2.y);
2225 Vertex::set(vertices++, p4.x, p4.y);
2226 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002227
Chet Haase5b0200b2011-04-13 17:58:08 -07002228 prevVertex = vertices - 1;
2229 generatedVerticesCount += 4;
2230 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002231 if (!isHairLine && scaled) {
2232 // Must set width proportions per-segment for scaled non-hairlines to use the
2233 // correct AA boundary dimensions
2234 if (boundaryWidthSlot < 0) {
2235 boundaryWidthSlot =
2236 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002237 }
Romain Guy7b631422012-04-04 11:38:54 -07002238
Chet Haase99ecdc42011-05-06 12:06:34 -07002239 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002240 }
Romain Guy7b631422012-04-04 11:38:54 -07002241
Chet Haase99585ad2011-05-02 15:00:16 -07002242 if (boundaryLengthSlot < 0) {
2243 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002244 }
Romain Guy7b631422012-04-04 11:38:54 -07002245
Chet Haase99ecdc42011-05-06 12:06:34 -07002246 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002247
Chet Haase5b0200b2011-04-13 17:58:08 -07002248 if (prevAAVertex != NULL) {
2249 // Issue two repeat vertices to create degenerate triangles to bridge
2250 // between the previous line and the new one. This is necessary because
2251 // we are creating a single triangle_strip which will contain
2252 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002253 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2254 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2255 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002256 generatedVerticesCount += 2;
2257 }
Romain Guy7b631422012-04-04 11:38:54 -07002258
Chet Haase99585ad2011-05-02 15:00:16 -07002259 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2260 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2261 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2262 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002263
Chet Haase5b0200b2011-04-13 17:58:08 -07002264 prevAAVertex = aaVertices - 1;
2265 generatedVerticesCount += 4;
2266 }
Romain Guy7b631422012-04-04 11:38:54 -07002267
Chet Haase99585ad2011-05-02 15:00:16 -07002268 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2269 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2270 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002271 }
2272 }
Romain Guy7b631422012-04-04 11:38:54 -07002273
Chet Haase5b0200b2011-04-13 17:58:08 -07002274 if (generatedVerticesCount > 0) {
2275 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2276 }
Romain Guy7b631422012-04-04 11:38:54 -07002277
2278 if (isAA) {
2279 finishDrawAALine(widthSlot, lengthSlot);
2280 }
Chet Haase48659092012-05-31 15:21:51 -07002281
2282 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002283}
2284
Chet Haase48659092012-05-31 15:21:51 -07002285status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2286 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002287
2288 // TODO: The paint's cap style defines whether the points are square or circular
2289 // TODO: Handle AA for round points
2290
Chet Haase5b0200b2011-04-13 17:58:08 -07002291 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002292 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002293 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002294 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002295 if (isHairLine) {
2296 // Now that we know it's hairline, we can set the effective width, to be used later
2297 strokeWidth = 1.0f;
2298 }
2299 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002300 int alpha;
2301 SkXfermode::Mode mode;
2302 getAlphaAndMode(paint, &alpha, &mode);
2303
2304 int verticesCount = count >> 1;
2305 int generatedVerticesCount = 0;
2306
2307 TextureVertex pointsData[verticesCount];
2308 TextureVertex* vertex = &pointsData[0];
2309
Romain Guy04299382012-07-18 17:15:41 -07002310 // TODO: We should optimize this method to not generate vertices for points
2311 // that lie outside of the clip.
2312 mCaches.enableScissor();
2313
Romain Guyed6fcb02011-03-21 13:11:28 -07002314 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002315 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002316 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002317 setupDrawColor(paint->getColor(), alpha);
2318 setupDrawColorFilter();
2319 setupDrawShader();
2320 setupDrawBlending(mode);
2321 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002322 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002323 setupDrawColorUniforms();
2324 setupDrawColorFilterUniforms();
2325 setupDrawPointUniforms();
2326 setupDrawShaderIdentityUniforms();
2327 setupDrawMesh(vertex);
2328
2329 for (int i = 0; i < count; i += 2) {
2330 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2331 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002332
Chet Haase8a5cc922011-04-26 07:28:09 -07002333 float left = points[i] - halfWidth;
2334 float right = points[i] + halfWidth;
2335 float top = points[i + 1] - halfWidth;
2336 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002337
Chet Haase8a5cc922011-04-26 07:28:09 -07002338 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002339 }
2340
2341 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002342
2343 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002344}
2345
Chet Haase48659092012-05-31 15:21:51 -07002346status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002347 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002348 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002349
Romain Guyae88e5e2010-10-22 17:49:18 -07002350 Rect& clip(*mSnapshot->clipRect);
2351 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002352
Romain Guy3d58c032010-07-14 16:34:53 -07002353 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002354
2355 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002356}
Romain Guy9d5316e2010-06-24 19:30:36 -07002357
Chet Haase48659092012-05-31 15:21:51 -07002358status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2359 SkPaint* paint) {
2360 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002361 const AutoTexture autoCleanup(texture);
2362
2363 const float x = left + texture->left - texture->offset;
2364 const float y = top + texture->top - texture->offset;
2365
2366 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002367
2368 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002369}
2370
Chet Haase48659092012-05-31 15:21:51 -07002371status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002372 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002373 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002374 return DrawGlInfo::kStatusDone;
2375 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002376
Chris Craikcb4d6002012-09-25 12:00:29 -07002377 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002378 mCaches.activeTexture(0);
2379 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2380 right - left, bottom - top, rx, ry, p);
2381 return drawShape(left, top, texture, p);
2382 }
2383
2384 SkPath path;
2385 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002386 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2387 float outset = p->getStrokeWidth() / 2;
2388 rect.outset(outset, outset);
2389 rx += outset;
2390 ry += outset;
2391 }
Chris Craik710f46d2012-09-17 17:25:49 -07002392 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002393 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002394
2395 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002396}
2397
Chris Craik710f46d2012-09-17 17:25:49 -07002398status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002399 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2400 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002401 return DrawGlInfo::kStatusDone;
2402 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002403 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002404 mCaches.activeTexture(0);
2405 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2406 return drawShape(x - radius, y - radius, texture, p);
2407 }
2408
2409 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002410 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2411 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2412 } else {
2413 path.addCircle(x, y, radius);
2414 }
2415 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002416
2417 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002418}
Romain Guy01d58e42011-01-19 21:54:02 -08002419
Chet Haase48659092012-05-31 15:21:51 -07002420status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002421 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002422 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002423 return DrawGlInfo::kStatusDone;
2424 }
Romain Guy01d58e42011-01-19 21:54:02 -08002425
Chris Craikcb4d6002012-09-25 12:00:29 -07002426 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002427 mCaches.activeTexture(0);
2428 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2429 return drawShape(left, top, texture, p);
2430 }
2431
2432 SkPath path;
2433 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002434 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2435 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2436 }
Chris Craik710f46d2012-09-17 17:25:49 -07002437 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002438 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002439
2440 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002441}
2442
Chet Haase48659092012-05-31 15:21:51 -07002443status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002444 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2445 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2446 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002447 }
2448
Chris Craik780c1282012-10-04 14:10:49 -07002449 if (fabs(sweepAngle) >= 360.0f) {
2450 return drawOval(left, top, right, bottom, p);
2451 }
2452
2453 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002454 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2455 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002456 mCaches.activeTexture(0);
2457 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2458 startAngle, sweepAngle, useCenter, p);
2459 return drawShape(left, top, texture, p);
2460 }
2461
2462 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2463 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2464 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2465 }
2466
2467 SkPath path;
2468 if (useCenter) {
2469 path.moveTo(rect.centerX(), rect.centerY());
2470 }
2471 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2472 if (useCenter) {
2473 path.close();
2474 }
2475 drawConvexPath(path, p);
2476
2477 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002478}
2479
Romain Guycf8675e2012-10-02 12:32:25 -07002480// See SkPaintDefaults.h
2481#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2482
Chet Haase48659092012-05-31 15:21:51 -07002483status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002484 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002485 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002486 }
2487
Chris Craik710f46d2012-09-17 17:25:49 -07002488 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002489 // only fill style is supported by drawConvexPath, since others have to handle joins
2490 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2491 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2492 mCaches.activeTexture(0);
2493 const PathTexture* texture =
2494 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2495 return drawShape(left, top, texture, p);
2496 }
2497
2498 SkPath path;
2499 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2500 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2501 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2502 }
2503 path.addRect(rect);
2504 drawConvexPath(path, p);
2505
2506 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002507 }
2508
Romain Guy181d0a62011-06-09 18:52:38 -07002509 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002510 SkPath path;
2511 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002512 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002513 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002514 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002515 }
Chet Haase48659092012-05-31 15:21:51 -07002516
2517 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002518}
Romain Guy9d5316e2010-06-24 19:30:36 -07002519
Raph Levien416a8472012-07-19 22:48:17 -07002520void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2521 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2522 float x, float y) {
2523 mCaches.activeTexture(0);
2524
2525 // NOTE: The drop shadow will not perform gamma correction
2526 // if shader-based correction is enabled
2527 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2528 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2529 paint, text, bytesCount, count, mShadowRadius, positions);
2530 const AutoTexture autoCleanup(shadow);
2531
2532 const float sx = x - shadow->left + mShadowDx;
2533 const float sy = y - shadow->top + mShadowDy;
2534
2535 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2536 int shadowColor = mShadowColor;
2537 if (mShader) {
2538 shadowColor = 0xffffffff;
2539 }
2540
2541 setupDraw();
2542 setupDrawWithTexture(true);
2543 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2544 setupDrawColorFilter();
2545 setupDrawShader();
2546 setupDrawBlending(true, mode);
2547 setupDrawProgram();
2548 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2549 setupDrawTexture(shadow->id);
2550 setupDrawPureColorUniforms();
2551 setupDrawColorFilterUniforms();
2552 setupDrawShaderUniforms();
2553 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2554
2555 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2556}
2557
Chet Haase48659092012-05-31 15:21:51 -07002558status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002559 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002560 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002561 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002562 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002563 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002564
Romain Guy671d6cf2012-01-18 12:39:17 -08002565 // NOTE: Skia does not support perspective transform on drawPosText yet
2566 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002567 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002568 }
2569
2570 float x = 0.0f;
2571 float y = 0.0f;
2572 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2573 if (pureTranslate) {
2574 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2575 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2576 }
2577
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002578 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002579 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002580
2581 int alpha;
2582 SkXfermode::Mode mode;
2583 getAlphaAndMode(paint, &alpha, &mode);
2584
Raph Levien416a8472012-07-19 22:48:17 -07002585 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002586 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2587 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002588 }
2589
Romain Guy671d6cf2012-01-18 12:39:17 -08002590 // Pick the appropriate texture filtering
2591 bool linearFilter = mSnapshot->transform->changesBounds();
2592 if (pureTranslate && !linearFilter) {
2593 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2594 }
2595
2596 mCaches.activeTexture(0);
2597 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002598 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002599 setupDrawDirtyRegionsDisabled();
2600 setupDrawWithTexture(true);
2601 setupDrawAlpha8Color(paint->getColor(), alpha);
2602 setupDrawColorFilter();
2603 setupDrawShader();
2604 setupDrawBlending(true, mode);
2605 setupDrawProgram();
2606 setupDrawModelView(x, y, x, y, pureTranslate, true);
2607 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2608 setupDrawPureColorUniforms();
2609 setupDrawColorFilterUniforms();
2610 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002611 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002612
2613 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2614 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2615
Romain Guy211370f2012-02-01 16:10:55 -08002616 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002617
2618 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2619 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002620 if (hasActiveLayer) {
2621 if (!pureTranslate) {
2622 mSnapshot->transform->mapRect(bounds);
2623 }
2624 dirtyLayerUnchecked(bounds, getRegion());
2625 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002626 }
Chet Haase48659092012-05-31 15:21:51 -07002627
2628 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002629}
2630
Romain Guyc2525952012-07-27 16:41:22 -07002631status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002632 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002633 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002634 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002635 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002636 }
2637
Chet Haasea1cff502012-02-21 13:43:44 -08002638 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002639 switch (paint->getTextAlign()) {
2640 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002641 x -= length / 2.0f;
2642 break;
2643 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002644 x -= length;
2645 break;
2646 default:
2647 break;
2648 }
2649
Romain Guycac5fd32011-12-01 20:08:50 -08002650 SkPaint::FontMetrics metrics;
2651 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002652 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002653 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002654 }
2655
Romain Guye3a9b242013-01-08 11:15:30 -08002656#if DEBUG_GLYPHS
2657 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2658 SkTypeface::UniqueID(paint->getTypeface()));
2659#endif
2660
2661 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2662 fontRenderer.setFont(paint, *mSnapshot->transform);
2663
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002664 const float oldX = x;
2665 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002666 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002667 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002668 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2669 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2670 }
2671
Romain Guy86568192010-12-14 15:55:39 -08002672 int alpha;
2673 SkXfermode::Mode mode;
2674 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002675
Romain Guy211370f2012-02-01 16:10:55 -08002676 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002677 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2678 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002679 }
2680
Romain Guy6620c6d2010-12-06 18:07:02 -08002681 // Pick the appropriate texture filtering
2682 bool linearFilter = mSnapshot->transform->changesBounds();
2683 if (pureTranslate && !linearFilter) {
2684 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2685 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002686
Romain Guy16c88082012-06-11 16:03:47 -07002687 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002688 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002689 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002690 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002691 setupDrawDirtyRegionsDisabled();
2692 setupDrawWithTexture(true);
2693 setupDrawAlpha8Color(paint->getColor(), alpha);
2694 setupDrawColorFilter();
2695 setupDrawShader();
2696 setupDrawBlending(true, mode);
2697 setupDrawProgram();
2698 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002699 // See comment above; the font renderer must use texture unit 0
2700 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002701 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2702 setupDrawPureColorUniforms();
2703 setupDrawColorFilterUniforms();
2704 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002705 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002706
Romain Guya3dc55f2012-09-28 13:55:44 -07002707 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2708 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002709 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2710
Romain Guy211370f2012-02-01 16:10:55 -08002711 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002712
Raph Levien996e57c2012-07-23 15:22:52 -07002713 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002714 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002715 SkPaint paintCopy(*paint);
2716 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2717 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002718 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002719 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002720 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002721 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002722 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002723
2724 if (status && hasActiveLayer) {
2725 if (!pureTranslate) {
2726 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002727 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002728 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002729 }
Romain Guy694b5192010-07-21 21:33:20 -07002730
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002731 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002732
2733 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002734}
2735
Chet Haase48659092012-05-31 15:21:51 -07002736status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002737 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002738 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2739 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002740 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002741 }
2742
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002743 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002744 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002745
2746 int alpha;
2747 SkXfermode::Mode mode;
2748 getAlphaAndMode(paint, &alpha, &mode);
2749
2750 mCaches.activeTexture(0);
2751 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002752 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002753 setupDrawDirtyRegionsDisabled();
2754 setupDrawWithTexture(true);
2755 setupDrawAlpha8Color(paint->getColor(), alpha);
2756 setupDrawColorFilter();
2757 setupDrawShader();
2758 setupDrawBlending(true, mode);
2759 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002760 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002761 setupDrawTexture(fontRenderer.getTexture(true));
2762 setupDrawPureColorUniforms();
2763 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002764 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002765 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002766
Romain Guy97771732012-02-28 18:17:02 -08002767 const Rect* clip = &mSnapshot->getLocalClip();
2768 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 -08002769
Romain Guy97771732012-02-28 18:17:02 -08002770 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002771
2772 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2773 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002774 if (hasActiveLayer) {
2775 mSnapshot->transform->mapRect(bounds);
2776 dirtyLayerUnchecked(bounds, getRegion());
2777 }
Romain Guy97771732012-02-28 18:17:02 -08002778 }
Chet Haase48659092012-05-31 15:21:51 -07002779
2780 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002781}
2782
Chet Haase48659092012-05-31 15:21:51 -07002783status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2784 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002785
Romain Guya1d3c912011-12-13 14:55:06 -08002786 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002787
Romain Guyfb8b7632010-08-23 21:05:08 -07002788 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002789 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002790 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002791
Romain Guy8b55f372010-08-18 17:10:07 -07002792 const float x = texture->left - texture->offset;
2793 const float y = texture->top - texture->offset;
2794
Romain Guy01d58e42011-01-19 21:54:02 -08002795 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002796
2797 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002798}
2799
Chet Haase48659092012-05-31 15:21:51 -07002800status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002801 if (!layer) {
2802 return DrawGlInfo::kStatusDone;
2803 }
2804
Romain Guyb2e2f242012-10-17 18:18:35 -07002805 mat4* transform = NULL;
2806 if (layer->isTextureLayer()) {
2807 transform = &layer->getTransform();
2808 if (!transform->isIdentity()) {
2809 save(0);
2810 mSnapshot->transform->multiply(*transform);
2811 }
2812 }
2813
Romain Guy35643dd2012-09-18 15:40:58 -07002814 Rect transformed;
2815 Rect clip;
2816 const bool rejected = quickRejectNoScissor(x, y,
2817 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2818
2819 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002820 if (transform && !transform->isIdentity()) {
2821 restore();
2822 }
Chet Haase48659092012-05-31 15:21:51 -07002823 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002824 }
2825
Romain Guy5bb3c732012-11-29 17:52:58 -08002826 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002827
Romain Guy87e2f7572012-09-24 11:37:12 -07002828 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002829 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002830
Romain Guy211370f2012-02-01 16:10:55 -08002831 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002832 SkiaColorFilter* oldFilter = mColorFilter;
2833 mColorFilter = layer->getColorFilter();
2834
Romain Guyc88e3572011-01-22 00:32:12 -08002835 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002836 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002837 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002838 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002839 setupDraw();
2840 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002841 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002842 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002843 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002844 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002845 setupDrawPureColorUniforms();
2846 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002847 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002848 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002849 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2850 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002851
Romain Guyd21b6e12011-11-30 20:21:23 -08002852 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002853 setupDrawModelViewTranslate(tx, ty,
2854 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002855 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002856 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002857 setupDrawModelViewTranslate(x, y,
2858 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2859 }
Romain Guyc88e3572011-01-22 00:32:12 -08002860 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002861
Romain Guyc88e3572011-01-22 00:32:12 -08002862 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2863 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002864
Romain Guyc88e3572011-01-22 00:32:12 -08002865 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002866
2867#if DEBUG_LAYERS_AS_REGIONS
2868 drawRegionRects(layer->region);
2869#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002870 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002871
Romain Guye529ece2012-09-26 11:23:17 -07002872 mColorFilter = oldFilter;
2873
Romain Guy5bb3c732012-11-29 17:52:58 -08002874 if (layer->debugDrawUpdate) {
2875 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002876 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2877 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2878 }
Romain Guyf219da52011-01-16 12:54:25 -08002879 }
Chet Haase48659092012-05-31 15:21:51 -07002880
Romain Guyb2e2f242012-10-17 18:18:35 -07002881 if (transform && !transform->isIdentity()) {
2882 restore();
2883 }
2884
Chet Haase48659092012-05-31 15:21:51 -07002885 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002886}
2887
Romain Guy6926c722010-07-12 20:20:03 -07002888///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002889// Shaders
2890///////////////////////////////////////////////////////////////////////////////
2891
2892void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002893 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002894}
2895
Romain Guy06f96e22010-07-30 19:18:16 -07002896void OpenGLRenderer::setupShader(SkiaShader* shader) {
2897 mShader = shader;
2898 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002899 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002900 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002901}
2902
Romain Guyd27977d2010-07-14 19:18:51 -07002903///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002904// Color filters
2905///////////////////////////////////////////////////////////////////////////////
2906
2907void OpenGLRenderer::resetColorFilter() {
2908 mColorFilter = NULL;
2909}
2910
2911void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2912 mColorFilter = filter;
2913}
2914
2915///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002916// Drop shadow
2917///////////////////////////////////////////////////////////////////////////////
2918
2919void OpenGLRenderer::resetShadow() {
2920 mHasShadow = false;
2921}
2922
2923void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2924 mHasShadow = true;
2925 mShadowRadius = radius;
2926 mShadowDx = dx;
2927 mShadowDy = dy;
2928 mShadowColor = color;
2929}
2930
2931///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002932// Draw filters
2933///////////////////////////////////////////////////////////////////////////////
2934
2935void OpenGLRenderer::resetPaintFilter() {
2936 mHasDrawFilter = false;
2937}
2938
2939void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2940 mHasDrawFilter = true;
2941 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2942 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2943}
2944
2945SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002946 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002947
2948 uint32_t flags = paint->getFlags();
2949
2950 mFilteredPaint = *paint;
2951 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2952
2953 return &mFilteredPaint;
2954}
2955
2956///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002957// Drawing implementation
2958///////////////////////////////////////////////////////////////////////////////
2959
Romain Guy01d58e42011-01-19 21:54:02 -08002960void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2961 float x, float y, SkPaint* paint) {
2962 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2963 return;
2964 }
2965
2966 int alpha;
2967 SkXfermode::Mode mode;
2968 getAlphaAndMode(paint, &alpha, &mode);
2969
2970 setupDraw();
2971 setupDrawWithTexture(true);
2972 setupDrawAlpha8Color(paint->getColor(), alpha);
2973 setupDrawColorFilter();
2974 setupDrawShader();
2975 setupDrawBlending(true, mode);
2976 setupDrawProgram();
2977 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2978 setupDrawTexture(texture->id);
2979 setupDrawPureColorUniforms();
2980 setupDrawColorFilterUniforms();
2981 setupDrawShaderUniforms();
2982 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2983
2984 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2985
2986 finishDrawTexture();
2987}
2988
Romain Guyf607bdc2010-09-10 19:20:06 -07002989// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002990#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2991#define kStdUnderline_Offset (1.0f / 9.0f)
2992#define kStdUnderline_Thickness (1.0f / 18.0f)
2993
2994void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2995 float x, float y, SkPaint* paint) {
2996 // Handle underline and strike-through
2997 uint32_t flags = paint->getFlags();
2998 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002999 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003000 float underlineWidth = length;
3001 // If length is > 0.0f, we already measured the text for the text alignment
3002 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003003 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003004 }
3005
Romain Guy211370f2012-02-01 16:10:55 -08003006 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003007 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003008 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003009
Raph Levien8b4072d2012-07-30 15:50:00 -07003010 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003011 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003012
Romain Guyf6834472011-01-23 13:32:12 -08003013 int linesCount = 0;
3014 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3015 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3016
3017 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003018 float points[pointsCount];
3019 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003020
3021 if (flags & SkPaint::kUnderlineText_Flag) {
3022 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003023 points[currentPoint++] = left;
3024 points[currentPoint++] = top;
3025 points[currentPoint++] = left + underlineWidth;
3026 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003027 }
3028
3029 if (flags & SkPaint::kStrikeThruText_Flag) {
3030 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003031 points[currentPoint++] = left;
3032 points[currentPoint++] = top;
3033 points[currentPoint++] = left + underlineWidth;
3034 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003035 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003036
Romain Guy726aeba2011-06-01 14:52:00 -07003037 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003038
Romain Guy726aeba2011-06-01 14:52:00 -07003039 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003040 }
3041 }
3042}
3043
Romain Guy672433d2013-01-04 19:05:13 -08003044status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3045 if (mSnapshot->isIgnored()) {
3046 return DrawGlInfo::kStatusDone;
3047 }
3048
3049 float left = FLT_MAX;
3050 float top = FLT_MAX;
3051 float right = FLT_MIN;
3052 float bottom = FLT_MIN;
3053
3054 int vertexCount = 0;
3055 Vertex mesh[count * 6];
3056 Vertex* vertex = mesh;
3057
3058 for (int i = 0; i < count; i++) {
3059 int index = i * 4;
3060 float l = rects[index + 0];
3061 float t = rects[index + 1];
3062 float r = rects[index + 2];
3063 float b = rects[index + 3];
3064
3065 if (!quickRejectNoScissor(left, top, right, bottom)) {
3066 Vertex::set(vertex++, l, b);
3067 Vertex::set(vertex++, l, t);
3068 Vertex::set(vertex++, r, t);
3069 Vertex::set(vertex++, l, b);
3070 Vertex::set(vertex++, r, t);
3071 Vertex::set(vertex++, r, b);
3072
3073 vertexCount += 6;
3074
3075 left = fminf(left, l);
3076 top = fminf(top, t);
3077 right = fmaxf(right, r);
3078 bottom = fmaxf(bottom, b);
3079 }
3080 }
3081
3082 if (count == 0) return DrawGlInfo::kStatusDone;
3083
3084 int color = paint->getColor();
3085 // If a shader is set, preserve only the alpha
3086 if (mShader) {
3087 color |= 0x00ffffff;
3088 }
3089 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3090
3091 setupDraw();
3092 setupDrawNoTexture();
3093 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3094 setupDrawShader();
3095 setupDrawColorFilter();
3096 setupDrawBlending(mode);
3097 setupDrawProgram();
3098 setupDrawDirtyRegionsDisabled();
3099 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f);
3100 setupDrawColorUniforms();
3101 setupDrawShaderUniforms();
3102 setupDrawColorFilterUniforms();
3103 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3104
3105 if (hasLayer()) {
3106 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3107 }
3108
3109 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3110
3111 return DrawGlInfo::kStatusDrew;
3112}
3113
Romain Guy026c5e162010-06-28 17:12:22 -07003114void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003115 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003116 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003117 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003118 color |= 0x00ffffff;
3119 }
3120
Romain Guy70ca14e2010-12-13 18:24:33 -08003121 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003122 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003123 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003124 setupDrawShader();
3125 setupDrawColorFilter();
3126 setupDrawBlending(mode);
3127 setupDrawProgram();
3128 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3129 setupDrawColorUniforms();
3130 setupDrawShaderUniforms(ignoreTransform);
3131 setupDrawColorFilterUniforms();
3132 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003133
Romain Guyc95c8d62010-09-17 15:31:32 -07003134 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3135}
3136
Romain Guy82ba8142010-07-09 13:25:56 -07003137void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003138 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003139 int alpha;
3140 SkXfermode::Mode mode;
3141 getAlphaAndMode(paint, &alpha, &mode);
3142
Romain Guyd21b6e12011-11-30 20:21:23 -08003143 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003144
Romain Guy211370f2012-02-01 16:10:55 -08003145 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003146 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3147 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3148
Romain Guyd21b6e12011-11-30 20:21:23 -08003149 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003150 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3151 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3152 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3153 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003154 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003155 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3156 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3157 GL_TRIANGLE_STRIP, gMeshCount);
3158 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003159}
3160
Romain Guybd6b79b2010-06-26 00:13:53 -07003161void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003162 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3163 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003164 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003165}
3166
3167void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003168 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003169 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003170 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003171
Romain Guy746b7402010-10-26 16:27:31 -07003172 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003173 setupDrawWithTexture();
3174 setupDrawColor(alpha, alpha, alpha, alpha);
3175 setupDrawColorFilter();
3176 setupDrawBlending(blend, mode, swapSrcDst);
3177 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003178 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003179 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003180 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003181 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003182 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003183 }
Romain Guy886b2752013-01-04 12:26:18 -08003184 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003185 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003186 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003187 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003188
Romain Guy6820ac82010-09-15 18:11:50 -07003189 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003190
3191 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003192}
3193
Romain Guy886b2752013-01-04 12:26:18 -08003194void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3195 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3196 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3197 bool ignoreTransform, bool dirty) {
3198
3199 setupDraw();
3200 setupDrawWithTexture(true);
3201 if (hasColor) {
3202 setupDrawAlpha8Color(color, alpha);
3203 }
3204 setupDrawColorFilter();
3205 setupDrawShader();
3206 setupDrawBlending(true, mode);
3207 setupDrawProgram();
3208 if (!dirty) setupDrawDirtyRegionsDisabled();
3209 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3210 setupDrawTexture(texture);
3211 setupDrawPureColorUniforms();
3212 setupDrawColorFilterUniforms();
3213 setupDrawShaderUniforms();
3214 setupDrawMesh(vertices, texCoords);
3215
3216 glDrawArrays(drawMode, 0, elementsCount);
3217
3218 finishDrawTexture();
3219}
3220
Romain Guya5aed0d2010-09-09 14:42:43 -07003221void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003222 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003223 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003224
Romain Guy82ba8142010-07-09 13:25:56 -07003225 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003226 // These blend modes are not supported by OpenGL directly and have
3227 // to be implemented using shaders. Since the shader will perform
3228 // the blending, turn blending off here
3229 // If the blend mode cannot be implemented using shaders, fall
3230 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003231 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003232 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003233 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003234 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003235
Romain Guy82bc7a72012-01-03 14:13:39 -08003236 if (mCaches.blend) {
3237 glDisable(GL_BLEND);
3238 mCaches.blend = false;
3239 }
3240
3241 return;
3242 } else {
3243 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003244 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003245 }
3246
3247 if (!mCaches.blend) {
3248 glEnable(GL_BLEND);
3249 }
3250
3251 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3252 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3253
3254 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3255 glBlendFunc(sourceMode, destMode);
3256 mCaches.lastSrcMode = sourceMode;
3257 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003258 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003259 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003260 glDisable(GL_BLEND);
3261 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003262 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003263}
3264
Romain Guy889f8d12010-07-29 14:37:42 -07003265bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003266 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003267 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003268 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003269 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003270 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003271 }
Romain Guy6926c722010-07-12 20:20:03 -07003272 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003273}
3274
Romain Guy026c5e162010-06-28 17:12:22 -07003275void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003276 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003277 TextureVertex::setUV(v++, u1, v1);
3278 TextureVertex::setUV(v++, u2, v1);
3279 TextureVertex::setUV(v++, u1, v2);
3280 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003281}
3282
Chet Haase5c13d892010-10-08 08:37:55 -07003283void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003284 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003285 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003286}
3287
Romain Guy9d5316e2010-06-24 19:30:36 -07003288}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003289}; // namespace android