blob: be34b40ee42ffb43d25aa236895f85ed46843d27 [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 Guydda57022010-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 Guydda57022010-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 Guy8ce00302013-01-15 18:51:42 -0800840 Layer* layer = current->layer;
841 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700843
844 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700845 endTiling();
846
Romain Guye0aa84b2012-04-03 19:30:26 -0700847 // Detach the texture from the FBO
848 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800849
850 layer->removeFbo(false);
851
Romain Guyeb993562010-10-05 18:14:38 -0700852 // Unbind current FBO and restore previous one
853 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700854 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700855
856 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700857 }
858
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700860 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700862 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700864 }
865
Romain Guy03750a02010-10-18 14:06:08 -0700866 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700867
Romain Guya1d3c912011-12-13 14:55:06 -0800868 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700869
Romain Guy5b3b3522010-10-27 18:57:51 -0700870 // When the layer is stored in an FBO, we can save a bit of fillrate by
871 // drawing only the dirty region
872 if (fboLayer) {
873 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700874 if (layer->getColorFilter()) {
875 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800876 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700878 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800879 resetColorFilter();
880 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 } else if (!rect.isEmpty()) {
882 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
883 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700884 }
Romain Guy8b55f372010-08-18 17:10:07 -0700885
Romain Guy746b7402010-10-26 16:27:31 -0700886 dirtyClip();
887
Romain Guyeb993562010-10-05 18:14:38 -0700888 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700889 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700890 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700891 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700892 }
893}
894
Romain Guyaa6c24c2011-04-28 18:40:04 -0700895void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700896 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700897
898 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700899 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700900 setupDrawWithTexture();
901 } else {
902 setupDrawWithExternalTexture();
903 }
904 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700905 setupDrawColor(alpha, alpha, alpha, alpha);
906 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700908 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700909 setupDrawPureColorUniforms();
910 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700911 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
912 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700913 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700914 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700915 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700916 if (mSnapshot->transform->isPureTranslate() &&
917 layer->getWidth() == (uint32_t) rect.getWidth() &&
918 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
920 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
921
Romain Guyd21b6e12011-11-30 20:21:23 -0800922 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
924 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800925 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
927 }
928 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
930
931 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
932
933 finishDrawTexture();
934}
935
Romain Guy5b3b3522010-10-27 18:57:51 -0700936void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700938 const Rect& texCoords = layer->texCoords;
939 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
940 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700941
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 float x = rect.left;
943 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700944 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700945 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700946 layer->getHeight() == (uint32_t) rect.getHeight();
947
948 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 // When we're swapping, the layer is already in screen coordinates
950 if (!swap) {
951 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
952 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
953 }
954
Romain Guyd21b6e12011-11-30 20:21:23 -0800955 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800957 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 }
959
960 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
961 layer->getTexture(), layer->getAlpha() / 255.0f,
962 layer->getMode(), layer->isBlend(),
963 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
964 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700965
Romain Guyaa6c24c2011-04-28 18:40:04 -0700966 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
967 } else {
968 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
969 drawTextureLayer(layer, rect);
970 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
971 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700972}
973
974void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700975 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700976 layer->setRegionAsRect();
977
Romain Guy40667672011-03-18 14:34:03 -0700978 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700979
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 layer->region.clear();
981 return;
982 }
983
Romain Guy8a3957d2011-09-07 17:55:15 -0700984 // TODO: See LayerRenderer.cpp::generateMesh() for important
985 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800986 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700987 size_t count;
988 const android::Rect* rects = layer->region.getArray(&count);
989
Romain Guy9ace8f52011-07-07 20:50:11 -0700990 const float alpha = layer->getAlpha() / 255.0f;
991 const float texX = 1.0f / float(layer->getWidth());
992 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800993 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
Romain Guy8ce00302013-01-15 18:51:42 -0800995 setupDraw();
996
997 // We must get (and therefore bind) the region mesh buffer
998 // after we setup drawing in case we need to mess with the
999 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 TextureVertex* mesh = mCaches.getRegionMesh();
1001 GLsizei numQuads = 0;
1002
Romain Guy7230a742011-01-10 22:26:16 -08001003 setupDrawWithTexture();
1004 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001005 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001006 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001007 setupDrawProgram();
1008 setupDrawDirtyRegionsDisabled();
1009 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001010 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001011 setupDrawTexture(layer->getTexture());
1012 if (mSnapshot->transform->isPureTranslate()) {
1013 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
1014 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
1015
Romain Guyd21b6e12011-11-30 20:21:23 -08001016 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1018 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001019 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001020 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1021 }
Romain Guy15bc6432011-12-13 13:11:32 -08001022 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001023
1024 for (size_t i = 0; i < count; i++) {
1025 const android::Rect* r = &rects[i];
1026
1027 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001028 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001029 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001030 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001031
1032 // TODO: Reject quads outside of the clip
1033 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1034 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1035 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1036 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1037
1038 numQuads++;
1039
1040 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1041 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1042 numQuads = 0;
1043 mesh = mCaches.getRegionMesh();
1044 }
1045 }
1046
1047 if (numQuads > 0) {
1048 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1049 }
1050
Romain Guy7230a742011-01-10 22:26:16 -08001051 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001052
1053#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001054 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001055#endif
1056
1057 layer->region.clear();
1058 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001059}
1060
Romain Guy3a3133d2011-02-01 22:59:58 -08001061void OpenGLRenderer::drawRegionRects(const Region& region) {
1062#if DEBUG_LAYERS_AS_REGIONS
1063 size_t count;
1064 const android::Rect* rects = region.getArray(&count);
1065
1066 uint32_t colors[] = {
1067 0x7fff0000, 0x7f00ff00,
1068 0x7f0000ff, 0x7fff00ff,
1069 };
1070
1071 int offset = 0;
1072 int32_t top = rects[0].top;
1073
1074 for (size_t i = 0; i < count; i++) {
1075 if (top != rects[i].top) {
1076 offset ^= 0x2;
1077 top = rects[i].top;
1078 }
1079
1080 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1081 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1082 SkXfermode::kSrcOver_Mode);
1083 }
1084#endif
1085}
1086
Romain Guy8ce00302013-01-15 18:51:42 -08001087void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1088 SkXfermode::Mode mode, bool dirty) {
1089 int count = 0;
1090 Vector<float> rects;
1091
1092 SkRegion::Iterator it(region);
1093 while (!it.done()) {
1094 const SkIRect& r = it.rect();
1095 rects.push(r.fLeft);
1096 rects.push(r.fTop);
1097 rects.push(r.fRight);
1098 rects.push(r.fBottom);
1099 count++;
1100 it.next();
1101 }
1102
1103 drawColorRects(rects.array(), count, color, mode, true, dirty);
1104}
1105
Romain Guy5b3b3522010-10-27 18:57:51 -07001106void OpenGLRenderer::dirtyLayer(const float left, const float top,
1107 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001108 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001109 Rect bounds(left, top, right, bottom);
1110 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001111 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001112 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001113}
1114
1115void OpenGLRenderer::dirtyLayer(const float left, const float top,
1116 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001117 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001118 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001119 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001120 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001121}
1122
1123void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001124 if (bounds.intersect(*mSnapshot->clipRect)) {
1125 bounds.snapToPixelBoundaries();
1126 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1127 if (!dirty.isEmpty()) {
1128 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001129 }
1130 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001131}
1132
Romain Guy54be1cd2011-06-13 19:04:27 -07001133void OpenGLRenderer::clearLayerRegions() {
1134 const size_t count = mLayers.size();
1135 if (count == 0) return;
1136
1137 if (!mSnapshot->isIgnored()) {
1138 // Doing several glScissor/glClear here can negatively impact
1139 // GPUs with a tiler architecture, instead we draw quads with
1140 // the Clear blending mode
1141
1142 // The list contains bounds that have already been clipped
1143 // against their initial clip rect, and the current clip
1144 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001145 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001146
1147 Vertex mesh[count * 6];
1148 Vertex* vertex = mesh;
1149
1150 for (uint32_t i = 0; i < count; i++) {
1151 Rect* bounds = mLayers.itemAt(i);
1152
1153 Vertex::set(vertex++, bounds->left, bounds->bottom);
1154 Vertex::set(vertex++, bounds->left, bounds->top);
1155 Vertex::set(vertex++, bounds->right, bounds->top);
1156 Vertex::set(vertex++, bounds->left, bounds->bottom);
1157 Vertex::set(vertex++, bounds->right, bounds->top);
1158 Vertex::set(vertex++, bounds->right, bounds->bottom);
1159
1160 delete bounds;
1161 }
1162
1163 setupDraw(false);
1164 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1165 setupDrawBlending(true, SkXfermode::kClear_Mode);
1166 setupDrawProgram();
1167 setupDrawPureColorUniforms();
1168 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001169 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001170
Romain Guy54be1cd2011-06-13 19:04:27 -07001171 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001172
1173 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001174 } else {
1175 for (uint32_t i = 0; i < count; i++) {
1176 delete mLayers.itemAt(i);
1177 }
1178 }
1179
1180 mLayers.clear();
1181}
1182
Romain Guybd6b79b2010-06-26 00:13:53 -07001183///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001184// Transforms
1185///////////////////////////////////////////////////////////////////////////////
1186
1187void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001188 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001189}
1190
1191void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001192 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001193}
1194
1195void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001196 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001197}
1198
Romain Guy807daf72011-01-18 11:19:19 -08001199void OpenGLRenderer::skew(float sx, float sy) {
1200 mSnapshot->transform->skew(sx, sy);
1201}
1202
Romain Guyf6a11b82010-06-23 17:47:49 -07001203void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001204 if (matrix) {
1205 mSnapshot->transform->load(*matrix);
1206 } else {
1207 mSnapshot->transform->loadIdentity();
1208 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001209}
1210
1211void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001212 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001213}
1214
1215void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001216 SkMatrix transform;
1217 mSnapshot->transform->copyTo(transform);
1218 transform.preConcat(*matrix);
1219 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001220}
1221
1222///////////////////////////////////////////////////////////////////////////////
1223// Clipping
1224///////////////////////////////////////////////////////////////////////////////
1225
Romain Guybb9524b2010-06-22 18:56:38 -07001226void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001227 Rect clip(*mSnapshot->clipRect);
1228 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001229
Romain Guy8a4ac612012-07-17 17:32:48 -07001230 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1231 clip.getWidth(), clip.getHeight())) {
1232 mDirtyClip = false;
1233 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001234}
1235
Romain Guy8ce00302013-01-15 18:51:42 -08001236void OpenGLRenderer::ensureStencilBuffer() {
1237 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1238 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1239 // just hope we have one when hasLayer() returns false.
1240 if (hasLayer()) {
1241 attachStencilBufferToLayer(mSnapshot->layer);
1242 }
1243}
1244
1245void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1246 // The layer's FBO is already bound when we reach this stage
1247 if (!layer->getStencilRenderBuffer()) {
1248 // TODO: See Layer::removeFbo(). The stencil renderbuffer should be cached
1249 GLuint buffer;
1250 glGenRenderbuffers(1, &buffer);
1251 glBindRenderbuffer(GL_RENDERBUFFER, buffer);
1252 glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8,
1253 layer->getWidth(), layer->getHeight());
1254 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, buffer);
1255 layer->setStencilRenderBuffer(buffer);
1256 }
1257}
1258
1259void OpenGLRenderer::setStencilFromClip() {
1260 if (!mCaches.debugOverdraw) {
1261 if (!mSnapshot->clipRegion->isEmpty()) {
1262 // NOTE: The order here is important, we must set dirtyClip to false
1263 // before any draw call to avoid calling back into this method
1264 mDirtyClip = false;
1265
1266 ensureStencilBuffer();
1267
1268 mCaches.stencil.enableWrite();
1269
1270 // Clear the stencil but first make sure we restrict drawing
1271 // to the region's bounds
1272 bool resetScissor = mCaches.enableScissor();
1273 if (resetScissor) {
1274 // The scissor was not set so we now need to update it
1275 setScissorFromClip();
1276 }
1277 mCaches.stencil.clear();
1278 if (resetScissor) mCaches.disableScissor();
1279
1280 // NOTE: We could use the region contour path to generate a smaller mesh
1281 // Since we are using the stencil we could use the red book path
1282 // drawing technique. It might increase bandwidth usage though.
1283
1284 // The last parameter is important: we are not drawing in the color buffer
1285 // so we don't want to dirty the current layer, if any
1286 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1287
1288 mCaches.stencil.enableTest();
1289 } else {
1290 mCaches.stencil.disable();
1291 }
1292 }
1293}
1294
Romain Guy9d5316e2010-06-24 19:30:36 -07001295const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001296 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001297}
1298
Romain Guy8a4ac612012-07-17 17:32:48 -07001299bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1300 if (mSnapshot->isIgnored()) {
1301 return true;
1302 }
1303
1304 Rect r(left, top, right, bottom);
1305 mSnapshot->transform->mapRect(r);
1306 r.snapToPixelBoundaries();
1307
1308 Rect clipRect(*mSnapshot->clipRect);
1309 clipRect.snapToPixelBoundaries();
1310
1311 return !clipRect.intersects(r);
1312}
1313
Romain Guy35643dd2012-09-18 15:40:58 -07001314bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1315 Rect& transformed, Rect& clip) {
1316 if (mSnapshot->isIgnored()) {
1317 return true;
1318 }
1319
1320 transformed.set(left, top, right, bottom);
1321 mSnapshot->transform->mapRect(transformed);
1322 transformed.snapToPixelBoundaries();
1323
1324 clip.set(*mSnapshot->clipRect);
1325 clip.snapToPixelBoundaries();
1326
1327 return !clip.intersects(transformed);
1328}
1329
Romain Guy672433d2013-01-04 19:05:13 -08001330bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1331 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001332 if (paint->getStyle() != SkPaint::kFill_Style) {
1333 float outset = paint->getStrokeWidth() * 0.5f;
1334 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1335 } else {
1336 return quickReject(left, top, right, bottom);
1337 }
1338}
1339
Romain Guyc7d53492010-06-25 13:41:57 -07001340bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001341 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001342 return true;
1343 }
1344
Romain Guy1d83e192010-08-17 11:37:00 -07001345 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001346 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001347 r.snapToPixelBoundaries();
1348
1349 Rect clipRect(*mSnapshot->clipRect);
1350 clipRect.snapToPixelBoundaries();
1351
Romain Guy586cae32012-07-13 15:28:31 -07001352 bool rejected = !clipRect.intersects(r);
1353 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001354 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001355 }
1356
1357 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001358}
1359
Romain Guy8ce00302013-01-15 18:51:42 -08001360void OpenGLRenderer::debugClip() {
1361#if DEBUG_CLIP_REGIONS
1362 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1363 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1364 }
1365#endif
1366}
1367
Romain Guy079ba2c2010-07-16 14:12:24 -07001368bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001369 if (CC_LIKELY(mSnapshot->transform->rectToRect())) {
1370 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1371 if (clipped) {
1372 dirtyClip();
1373 }
1374 return !mSnapshot->clipRect->isEmpty();
1375 }
1376
1377 SkPath path;
1378 path.addRect(left, top, right, bottom);
1379
1380 return clipPath(&path, op);
1381}
1382
1383bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1384 SkMatrix transform;
1385 mSnapshot->transform->copyTo(transform);
1386
1387 SkPath transformed;
1388 path->transform(transform, &transformed);
1389
1390 SkRegion clip;
1391 if (!mSnapshot->clipRegion->isEmpty()) {
1392 clip.setRegion(*mSnapshot->clipRegion);
1393 } else {
1394 Rect* bounds = mSnapshot->clipRect;
1395 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1396 }
1397
1398 SkRegion region;
1399 region.setPath(transformed, clip);
1400
1401 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001402 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001403 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001404 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001405 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001406}
1407
Romain Guy735738c2012-12-03 12:34:51 -08001408bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001409 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1410 if (clipped) {
1411 dirtyClip();
1412 }
1413 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001414}
1415
Chet Haasea23eed82012-04-12 15:19:04 -07001416Rect* OpenGLRenderer::getClipRect() {
1417 return mSnapshot->clipRect;
1418}
1419
Romain Guyf6a11b82010-06-23 17:47:49 -07001420///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001421// Drawing commands
1422///////////////////////////////////////////////////////////////////////////////
1423
Romain Guy54be1cd2011-06-13 19:04:27 -07001424void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001425 // TODO: It would be best if we could do this before quickReject()
1426 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001427 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001428 // Make sure setScissor & setStencil happen at the beginning of
1429 // this method
Romain Guy70ca14e2010-12-13 18:24:33 -08001430 if (mDirtyClip) {
1431 setScissorFromClip();
Romain Guy8ce00302013-01-15 18:51:42 -08001432 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001433 }
1434 mDescription.reset();
1435 mSetShaderColor = false;
1436 mColorSet = false;
1437 mColorA = mColorR = mColorG = mColorB = 0.0f;
1438 mTextureUnit = 0;
1439 mTrackDirtyRegions = true;
1440}
1441
1442void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1443 mDescription.hasTexture = true;
1444 mDescription.hasAlpha8Texture = isAlpha8;
1445}
1446
Romain Guyaa6c24c2011-04-28 18:40:04 -07001447void OpenGLRenderer::setupDrawWithExternalTexture() {
1448 mDescription.hasExternalTexture = true;
1449}
1450
Romain Guy15bc6432011-12-13 13:11:32 -08001451void OpenGLRenderer::setupDrawNoTexture() {
1452 mCaches.disbaleTexCoordsVertexArray();
1453}
1454
Chris Craik710f46d2012-09-17 17:25:49 -07001455void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001456 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001457}
1458
Chris Craik710f46d2012-09-17 17:25:49 -07001459void OpenGLRenderer::setupDrawVertexShape() {
1460 mDescription.isVertexShape = true;
Chris Craik6ebdc112012-08-31 18:24:33 -07001461}
1462
Romain Guyed6fcb02011-03-21 13:11:28 -07001463void OpenGLRenderer::setupDrawPoint(float pointSize) {
1464 mDescription.isPoint = true;
1465 mDescription.pointSize = pointSize;
1466}
1467
Romain Guy8d0d4782010-12-14 20:13:35 -08001468void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1469 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001470 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1471 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1472 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001473 mColorSet = true;
1474 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1475}
1476
Romain Guy86568192010-12-14 15:55:39 -08001477void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1478 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001479 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1480 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1481 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001482 mColorSet = true;
1483 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1484}
1485
Romain Guy41210632012-07-16 17:04:24 -07001486void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1487 mCaches.fontRenderer->describe(mDescription, paint);
1488}
1489
Romain Guy70ca14e2010-12-13 18:24:33 -08001490void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1491 mColorA = a;
1492 mColorR = r;
1493 mColorG = g;
1494 mColorB = b;
1495 mColorSet = true;
1496 mSetShaderColor = mDescription.setColor(r, g, b, a);
1497}
1498
1499void OpenGLRenderer::setupDrawShader() {
1500 if (mShader) {
1501 mShader->describe(mDescription, mCaches.extensions);
1502 }
1503}
1504
1505void OpenGLRenderer::setupDrawColorFilter() {
1506 if (mColorFilter) {
1507 mColorFilter->describe(mDescription, mCaches.extensions);
1508 }
1509}
1510
Romain Guyf09ef512011-05-27 11:43:46 -07001511void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1512 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1513 mColorA = 1.0f;
1514 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001515 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001516 }
1517}
1518
Romain Guy70ca14e2010-12-13 18:24:33 -08001519void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001520 // When the blending mode is kClear_Mode, we need to use a modulate color
1521 // argb=1,0,0,0
1522 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001523 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1524 mDescription, swapSrcDst);
1525}
1526
1527void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001528 // When the blending mode is kClear_Mode, we need to use a modulate color
1529 // argb=1,0,0,0
1530 accountForClear(mode);
Romain Guye83221c2012-09-24 16:01:35 -07001531 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()) ||
1532 (mColorFilter && mColorFilter->blend()), mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001533}
1534
1535void OpenGLRenderer::setupDrawProgram() {
1536 useProgram(mCaches.programCache.get(mDescription));
1537}
1538
1539void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1540 mTrackDirtyRegions = false;
1541}
1542
1543void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1544 bool ignoreTransform) {
1545 mModelView.loadTranslate(left, top, 0.0f);
1546 if (!ignoreTransform) {
1547 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1548 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1549 } else {
1550 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1551 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1552 }
1553}
1554
Chet Haase8a5cc922011-04-26 07:28:09 -07001555void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1556 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001557}
1558
Romain Guy70ca14e2010-12-13 18:24:33 -08001559void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1560 bool ignoreTransform, bool ignoreModelView) {
1561 if (!ignoreModelView) {
1562 mModelView.loadTranslate(left, top, 0.0f);
1563 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001564 } else {
1565 mModelView.loadIdentity();
1566 }
Romain Guy86568192010-12-14 15:55:39 -08001567 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1568 if (!ignoreTransform) {
1569 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1570 if (mTrackDirtyRegions && dirty) {
1571 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1572 }
1573 } else {
1574 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1575 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1576 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001577}
1578
Romain Guyed6fcb02011-03-21 13:11:28 -07001579void OpenGLRenderer::setupDrawPointUniforms() {
1580 int slot = mCaches.currentProgram->getUniform("pointSize");
1581 glUniform1f(slot, mDescription.pointSize);
1582}
1583
Romain Guy70ca14e2010-12-13 18:24:33 -08001584void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001585 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001586 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1587 }
1588}
1589
Romain Guy86568192010-12-14 15:55:39 -08001590void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001591 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001592 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001593 }
1594}
1595
Romain Guy70ca14e2010-12-13 18:24:33 -08001596void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1597 if (mShader) {
1598 if (ignoreTransform) {
1599 mModelView.loadInverse(*mSnapshot->transform);
1600 }
1601 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1602 }
1603}
1604
Romain Guy8d0d4782010-12-14 20:13:35 -08001605void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1606 if (mShader) {
1607 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1608 }
1609}
1610
Romain Guy70ca14e2010-12-13 18:24:33 -08001611void OpenGLRenderer::setupDrawColorFilterUniforms() {
1612 if (mColorFilter) {
1613 mColorFilter->setupProgram(mCaches.currentProgram);
1614 }
1615}
1616
Romain Guy41210632012-07-16 17:04:24 -07001617void OpenGLRenderer::setupDrawTextGammaUniforms() {
1618 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1619}
1620
Romain Guy70ca14e2010-12-13 18:24:33 -08001621void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001622 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001623 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001624 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001625}
1626
1627void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1628 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001629 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001630 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001631}
1632
Romain Guyaa6c24c2011-04-28 18:40:04 -07001633void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1634 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001635 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001636 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001637}
1638
Romain Guy8f0095c2011-05-02 17:24:22 -07001639void OpenGLRenderer::setupDrawTextureTransform() {
1640 mDescription.hasTextureTransform = true;
1641}
1642
1643void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001644 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1645 GL_FALSE, &transform.data[0]);
1646}
1647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001649 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001651 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001652 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001653 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001654 }
Romain Guyd71dd362011-12-12 19:03:35 -08001655
Chris Craikcb4d6002012-09-25 12:00:29 -07001656 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001657 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001658 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001659 }
1660
1661 mCaches.unbindIndicesBuffer();
1662}
1663
1664void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1665 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001666 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001667 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001668 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001669 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001670}
1671
Chet Haase5b0200b2011-04-13 17:58:08 -07001672void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001673 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001674 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001675 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001676}
1677
1678/**
1679 * 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 -07001680 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1681 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1682 * attributes (one per vertex) are values from zero to one that tells the fragment
1683 * shader where the fragment is in relation to the line width/length overall; these values are
1684 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1685 * region of the line.
1686 * Note that we only pass down the width values in this setup function. The length coordinates
1687 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001688 */
Chet Haase99585ad2011-05-02 15:00:16 -07001689void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001690 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001691 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001692 mCaches.bindPositionVertexPointer(force, vertices, gAAVertexStride);
Romain Guyf3a910b42011-12-12 20:35:21 -08001693 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001694 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001695
Romain Guy7b631422012-04-04 11:38:54 -07001696 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001697 glEnableVertexAttribArray(widthSlot);
1698 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001699
Romain Guy7b631422012-04-04 11:38:54 -07001700 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001701 glEnableVertexAttribArray(lengthSlot);
1702 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001703
Chet Haase5b0200b2011-04-13 17:58:08 -07001704 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001705 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001706}
1707
1708void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1709 glDisableVertexAttribArray(widthSlot);
1710 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001711}
1712
Romain Guy70ca14e2010-12-13 18:24:33 -08001713void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001714}
1715
1716///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001717// Drawing
1718///////////////////////////////////////////////////////////////////////////////
1719
Chet Haase1271e2c2012-04-20 09:54:27 -07001720status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001721 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001722
Romain Guy0fe478e2010-11-08 12:08:41 -08001723 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1724 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001725 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001726 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001727 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001728
Romain Guy65549432012-03-26 16:45:05 -07001729 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001730}
1731
Chet Haaseed30fd82011-04-22 16:18:45 -07001732void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1733 if (displayList) {
1734 displayList->output(*this, level);
1735 }
1736}
1737
Romain Guya168d732011-03-18 16:50:13 -07001738void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1739 int alpha;
1740 SkXfermode::Mode mode;
1741 getAlphaAndMode(paint, &alpha, &mode);
1742
Romain Guy886b2752013-01-04 12:26:18 -08001743 int color = paint != NULL ? paint->getColor() : 0;
1744
Romain Guya168d732011-03-18 16:50:13 -07001745 float x = left;
1746 float y = top;
1747
Romain Guy886b2752013-01-04 12:26:18 -08001748 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1749
Romain Guya168d732011-03-18 16:50:13 -07001750 bool ignoreTransform = false;
1751 if (mSnapshot->transform->isPureTranslate()) {
1752 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1753 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1754 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001755
1756 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001757 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001758 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001759 }
1760
Romain Guy886b2752013-01-04 12:26:18 -08001761 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1762 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1763 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001764}
1765
Chet Haase48659092012-05-31 15:21:51 -07001766status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001767 const float right = left + bitmap->width();
1768 const float bottom = top + bitmap->height();
1769
1770 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001771 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001772 }
1773
Romain Guya1d3c912011-12-13 14:55:06 -08001774 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001775 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001776 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001777 const AutoTexture autoCleanup(texture);
1778
Romain Guy211370f2012-02-01 16:10:55 -08001779 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001780 drawAlphaBitmap(texture, left, top, paint);
1781 } else {
1782 drawTextureRect(left, top, right, bottom, texture, paint);
1783 }
Chet Haase48659092012-05-31 15:21:51 -07001784
1785 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001786}
1787
Chet Haase48659092012-05-31 15:21:51 -07001788status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001789 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1790 const mat4 transform(*matrix);
1791 transform.mapRect(r);
1792
Romain Guy6926c722010-07-12 20:20:03 -07001793 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001794 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001795 }
1796
Romain Guya1d3c912011-12-13 14:55:06 -08001797 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001798 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001799 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001800 const AutoTexture autoCleanup(texture);
1801
Romain Guy5b3b3522010-10-27 18:57:51 -07001802 // This could be done in a cheaper way, all we need is pass the matrix
1803 // to the vertex shader. The save/restore is a bit overkill.
1804 save(SkCanvas::kMatrix_SaveFlag);
1805 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08001806 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1807 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
1808 } else {
1809 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1810 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001811 restore();
Chet Haase48659092012-05-31 15:21:51 -07001812
1813 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001814}
1815
Chet Haase48659092012-05-31 15:21:51 -07001816status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001817 const float right = left + bitmap->width();
1818 const float bottom = top + bitmap->height();
1819
1820 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001821 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001822 }
1823
1824 mCaches.activeTexture(0);
1825 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1826 const AutoTexture autoCleanup(texture);
1827
Romain Guy886b2752013-01-04 12:26:18 -08001828 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1829 drawAlphaBitmap(texture, left, top, paint);
1830 } else {
1831 drawTextureRect(left, top, right, bottom, texture, paint);
1832 }
Chet Haase48659092012-05-31 15:21:51 -07001833
1834 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001835}
1836
Chet Haase48659092012-05-31 15:21:51 -07001837status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001838 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08001839 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001840 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001841 }
1842
Romain Guyb18d2d02011-02-10 15:52:54 -08001843 float left = FLT_MAX;
1844 float top = FLT_MAX;
1845 float right = FLT_MIN;
1846 float bottom = FLT_MIN;
1847
Romain Guya92bb4d2012-10-16 11:08:44 -07001848 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08001849
Romain Guya566b7c2011-01-23 16:36:11 -08001850 // TODO: Support the colors array
1851 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001852 TextureVertex* vertex = mesh;
Romain Guya92bb4d2012-10-16 11:08:44 -07001853
Romain Guy5a7b4662011-01-20 19:09:30 -08001854 for (int32_t y = 0; y < meshHeight; y++) {
1855 for (int32_t x = 0; x < meshWidth; x++) {
1856 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1857
1858 float u1 = float(x) / meshWidth;
1859 float u2 = float(x + 1) / meshWidth;
1860 float v1 = float(y) / meshHeight;
1861 float v2 = float(y + 1) / meshHeight;
1862
1863 int ax = i + (meshWidth + 1) * 2;
1864 int ay = ax + 1;
1865 int bx = i;
1866 int by = bx + 1;
1867 int cx = i + 2;
1868 int cy = cx + 1;
1869 int dx = i + (meshWidth + 1) * 2 + 2;
1870 int dy = dx + 1;
1871
1872 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1873 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1874 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1875
1876 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1877 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1878 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001879
Romain Guya92bb4d2012-10-16 11:08:44 -07001880 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1881 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1882 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1883 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08001884 }
1885 }
1886
Romain Guya92bb4d2012-10-16 11:08:44 -07001887 if (quickReject(left, top, right, bottom)) {
1888 return DrawGlInfo::kStatusDone;
1889 }
1890
1891 mCaches.activeTexture(0);
1892 Texture* texture = mCaches.textureCache.get(bitmap);
1893 if (!texture) return DrawGlInfo::kStatusDone;
1894 const AutoTexture autoCleanup(texture);
1895
1896 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1897 texture->setFilter(FILTER(paint), true);
1898
1899 int alpha;
1900 SkXfermode::Mode mode;
1901 getAlphaAndMode(paint, &alpha, &mode);
1902
1903 if (hasLayer()) {
Romain Guyb18d2d02011-02-10 15:52:54 -08001904 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1905 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001906
Romain Guy5a7b4662011-01-20 19:09:30 -08001907 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1908 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001909 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001910
1911 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001912}
1913
Chet Haase48659092012-05-31 15:21:51 -07001914status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001915 float srcLeft, float srcTop, float srcRight, float srcBottom,
1916 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001917 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001918 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001919 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001920 }
1921
Romain Guya1d3c912011-12-13 14:55:06 -08001922 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001923 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001924 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001925 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001926
Romain Guy8ba548f2010-06-30 19:21:21 -07001927 const float width = texture->width;
1928 const float height = texture->height;
1929
Romain Guy68169722011-08-22 17:33:33 -07001930 const float u1 = fmax(0.0f, srcLeft / width);
1931 const float v1 = fmax(0.0f, srcTop / height);
1932 const float u2 = fmin(1.0f, srcRight / width);
1933 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001934
Romain Guy03750a02010-10-18 14:06:08 -07001935 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001936 resetDrawTextureTexCoords(u1, v1, u2, v2);
1937
Romain Guy03750a02010-10-18 14:06:08 -07001938 int alpha;
1939 SkXfermode::Mode mode;
1940 getAlphaAndMode(paint, &alpha, &mode);
1941
Romain Guyd21b6e12011-11-30 20:21:23 -08001942 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1943
Romain Guy886b2752013-01-04 12:26:18 -08001944 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
1945 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08001946
Romain Guy886b2752013-01-04 12:26:18 -08001947 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
1948 // Apply a scale transform on the canvas only when a shader is in use
1949 // Skia handles the ratio between the dst and src rects as a scale factor
1950 // when a shader is set
1951 bool useScaleTransform = mShader && scaled;
1952 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07001953
Romain Guy886b2752013-01-04 12:26:18 -08001954 if (CC_LIKELY(mSnapshot->transform->isPureTranslate() && !useScaleTransform)) {
1955 float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1956 float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1957
1958 dstRight = x + (dstRight - dstLeft);
1959 dstBottom = y + (dstBottom - dstTop);
1960
1961 dstLeft = x;
1962 dstTop = y;
1963
1964 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
1965 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08001966 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001967 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08001968 }
1969
1970 if (CC_UNLIKELY(useScaleTransform)) {
1971 save(SkCanvas::kMatrix_SaveFlag);
1972 translate(dstLeft, dstTop);
1973 scale(scaleX, scaleY);
1974
1975 dstLeft = 0.0f;
1976 dstTop = 0.0f;
1977
1978 dstRight = srcRight - srcLeft;
1979 dstBottom = srcBottom - srcTop;
1980 }
1981
1982 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1983 int color = paint ? paint->getColor() : 0;
1984 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1985 texture->id, paint != NULL, color, alpha, mode,
1986 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1987 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
1988 } else {
1989 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
1990 texture->id, alpha / 255.0f, mode, texture->blend,
1991 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1992 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
1993 }
1994
1995 if (CC_UNLIKELY(useScaleTransform)) {
1996 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08001997 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001998
1999 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002000
2001 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002002}
2003
Chet Haase48659092012-05-31 15:21:51 -07002004status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002005 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002006 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002007 int alpha;
2008 SkXfermode::Mode mode;
2009 getAlphaAndModeDirect(paint, &alpha, &mode);
2010
2011 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2012 left, top, right, bottom, alpha, mode);
2013}
2014
2015status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2016 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2017 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002018 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002019 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002020 }
2021
Romain Guybe6f9dc2012-07-16 12:41:17 -07002022 alpha *= mSnapshot->alpha;
2023
Romain Guya1d3c912011-12-13 14:55:06 -08002024 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002025 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002026 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002027 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08002028 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2029 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07002030
Romain Guy2728f962010-10-08 18:36:15 -07002031 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002032 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002033
Romain Guy211370f2012-02-01 16:10:55 -08002034 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002035 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002036 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002037 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002038 const float offsetX = left + mSnapshot->transform->getTranslateX();
2039 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002040 const size_t count = mesh->quads.size();
2041 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002042 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002043 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002044 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2045 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2046 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002047 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002048 dirtyLayer(left + bounds.left, top + bounds.top,
2049 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08002050 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002051 }
2052 }
2053
Romain Guy211370f2012-02-01 16:10:55 -08002054 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002055 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2056 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2057
2058 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2059 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2060 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2061 true, !mesh->hasEmptyQuads);
2062 } else {
2063 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2064 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2065 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2066 true, !mesh->hasEmptyQuads);
2067 }
Romain Guy054dc182010-10-15 17:55:25 -07002068 }
Chet Haase48659092012-05-31 15:21:51 -07002069
2070 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002071}
2072
Chet Haase99ecdc42011-05-06 12:06:34 -07002073/**
Chris Craikcb4d6002012-09-25 12:00:29 -07002074 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2075 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2076 * screen space in all directions. However, instead of using a fragment shader to compute the
2077 * translucency of the color from its position, we simply use a varying parameter to define how far
2078 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2079 *
2080 * Doesn't yet support joins, caps, or path effects.
Chet Haase858aa932011-05-12 09:06:00 -07002081 */
Chris Craikcb4d6002012-09-25 12:00:29 -07002082void OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2083 int color = paint->getColor();
2084 SkPaint::Style style = paint->getStyle();
2085 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2086 bool isAA = paint->isAntiAlias();
2087
Chris Craik710f46d2012-09-17 17:25:49 -07002088 VertexBuffer vertexBuffer;
2089 // TODO: try clipping large paths to viewport
Chris Craikcb4d6002012-09-25 12:00:29 -07002090 PathRenderer::convexPathVertices(path, paint, mSnapshot->transform, vertexBuffer);
Romain Guy04299382012-07-18 17:15:41 -07002091
Chris Craikbf09ffb2012-10-01 13:50:37 -07002092 if (!vertexBuffer.getSize()) {
2093 // no vertices to draw
2094 return;
2095 }
2096
Chris Craik710f46d2012-09-17 17:25:49 -07002097 setupDraw();
2098 setupDrawNoTexture();
2099 if (isAA) setupDrawAA();
2100 setupDrawVertexShape();
2101 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2102 setupDrawColorFilter();
2103 setupDrawShader();
2104 setupDrawBlending(isAA, mode);
2105 setupDrawProgram();
Chris Craikcb4d6002012-09-25 12:00:29 -07002106 setupDrawModelViewIdentity();
Chris Craik710f46d2012-09-17 17:25:49 -07002107 setupDrawColorUniforms();
2108 setupDrawColorFilterUniforms();
2109 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002110
Chris Craik710f46d2012-09-17 17:25:49 -07002111 void* vertices = vertexBuffer.getBuffer();
2112 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002113 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002114 mCaches.resetTexCoordsVertexPointer();
2115 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002116
Chris Craik710f46d2012-09-17 17:25:49 -07002117 int alphaSlot = -1;
2118 if (isAA) {
2119 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2120 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002121
Chris Craik710f46d2012-09-17 17:25:49 -07002122 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002123 glEnableVertexAttribArray(alphaSlot);
2124 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002125 }
Romain Guy04299382012-07-18 17:15:41 -07002126
Chris Craikcb4d6002012-09-25 12:00:29 -07002127 SkRect bounds = PathRenderer::computePathBounds(path, paint);
Chris Craik710f46d2012-09-17 17:25:49 -07002128 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *mSnapshot->transform);
Romain Guy04299382012-07-18 17:15:41 -07002129
Chris Craik710f46d2012-09-17 17:25:49 -07002130 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002131
Chris Craik710f46d2012-09-17 17:25:49 -07002132 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002133 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002134 }
Chet Haase858aa932011-05-12 09:06:00 -07002135}
2136
2137/**
Chet Haase99ecdc42011-05-06 12:06:34 -07002138 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
2139 * rules for those lines produces some unexpected results, and may vary between hardware devices.
2140 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
2141 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
2142 * of the line. Hairlines are more involved because we need to account for transform scaling
2143 * to end up with a one-pixel-wide line in screen space..
2144 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
2145 * in combination with values that we calculate and pass down in this method. The basic approach
2146 * is that the quad we create contains both the core line area plus a bounding area in which
2147 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
2148 * proportion of the width and the length of a given segment is represented by the boundary
2149 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
2150 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
2151 * on the inside). This ends up giving the result we want, with pixels that are completely
2152 * 'inside' the line area being filled opaquely and the other pixels being filled according to
2153 * how far into the boundary region they are, which is determined by shader interpolation.
2154 */
Chet Haase48659092012-05-31 15:21:51 -07002155status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
2156 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002157
2158 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07002159 // We use half the stroke width here because we're going to position the quad
2160 // corner vertices half of the width away from the line endpoints
2161 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002162 // A stroke width of 0 has a special meaning in Skia:
2163 // it draws a line 1 px wide regardless of current transform
2164 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07002165
Chet Haase99ecdc42011-05-06 12:06:34 -07002166 float inverseScaleX = 1.0f;
2167 float inverseScaleY = 1.0f;
2168 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07002169
Chet Haase8a5cc922011-04-26 07:28:09 -07002170 int alpha;
2171 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07002172
Chet Haase8a5cc922011-04-26 07:28:09 -07002173 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002174 int verticesCount = count;
2175 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07002176 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07002177 verticesCount += (count - 4);
2178 }
2179
2180 if (isHairLine || isAA) {
2181 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
2182 // the line on the screen should always be one pixel wide regardless of scale. For
2183 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08002184 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07002185 Matrix4 *mat = mSnapshot->transform;
2186 float m00 = mat->data[Matrix4::kScaleX];
2187 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07002188 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07002189 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07002190
Romain Guy211370f2012-02-01 16:10:55 -08002191 float scaleX = sqrtf(m00 * m00 + m01 * m01);
2192 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07002193
Chet Haase99ecdc42011-05-06 12:06:34 -07002194 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
2195 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07002196
Chet Haase99ecdc42011-05-06 12:06:34 -07002197 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
2198 scaled = true;
2199 }
2200 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002201 }
Chet Haase8a5cc922011-04-26 07:28:09 -07002202
2203 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07002204
2205 mCaches.enableScissor();
2206
Chet Haase8a5cc922011-04-26 07:28:09 -07002207 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002208 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002209 if (isAA) {
Chris Craik710f46d2012-09-17 17:25:49 -07002210 setupDrawAA();
Chet Haase8a5cc922011-04-26 07:28:09 -07002211 }
2212 setupDrawColor(paint->getColor(), alpha);
2213 setupDrawColorFilter();
2214 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08002215 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07002216 setupDrawProgram();
Chris Craikb30cb102012-10-05 19:11:37 -07002217 setupDrawModelViewIdentity(true);
Chet Haase8a5cc922011-04-26 07:28:09 -07002218 setupDrawColorUniforms();
2219 setupDrawColorFilterUniforms();
2220 setupDrawShaderIdentityUniforms();
2221
2222 if (isHairLine) {
2223 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07002224 halfStrokeWidth = isAA? 1 : .5;
2225 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002226 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07002227 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07002228 }
Romain Guy7b631422012-04-04 11:38:54 -07002229
2230 int widthSlot;
2231 int lengthSlot;
2232
Chet Haase5b0200b2011-04-13 17:58:08 -07002233 Vertex lines[verticesCount];
2234 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002235
Chet Haase99585ad2011-05-02 15:00:16 -07002236 AAVertex wLines[verticesCount];
2237 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07002238
Romain Guy211370f2012-02-01 16:10:55 -08002239 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002240 setupDrawVertices(vertices);
2241 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07002242 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
2243 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07002244 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07002245 // AA stroke width (the base stroke width expanded by a half pixel on either side).
2246 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07002247 // We will need to calculate the actual width proportion on each segment for
2248 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07002249 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07002250 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
2251 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07002252 }
2253
Chet Haase99ecdc42011-05-06 12:06:34 -07002254 AAVertex* prevAAVertex = NULL;
2255 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07002256
Chet Haase99585ad2011-05-02 15:00:16 -07002257 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07002258 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07002259
Chet Haase5b0200b2011-04-13 17:58:08 -07002260 for (int i = 0; i < count; i += 4) {
2261 // a = start point, b = end point
2262 vec2 a(points[i], points[i + 1]);
2263 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07002264
Chet Haase99585ad2011-05-02 15:00:16 -07002265 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07002266 float boundaryLengthProportion = 0;
2267 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07002268
Chet Haase5b0200b2011-04-13 17:58:08 -07002269 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07002270 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07002271 float x = n.x;
2272 n.x = -n.y;
2273 n.y = x;
2274
Chet Haase8a5cc922011-04-26 07:28:09 -07002275 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07002276 if (isAA) {
2277 float wideningFactor;
2278 if (fabs(n.x) >= fabs(n.y)) {
2279 wideningFactor = fabs(1.0f / n.x);
2280 } else {
2281 wideningFactor = fabs(1.0f / n.y);
2282 }
2283 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07002284 }
Romain Guy7b631422012-04-04 11:38:54 -07002285
Chet Haase99ecdc42011-05-06 12:06:34 -07002286 if (scaled) {
2287 n.x *= inverseScaleX;
2288 n.y *= inverseScaleY;
2289 }
2290 } else if (scaled) {
2291 // Extend n by .5 pixel on each side, post-transform
2292 vec2 extendedN = n.copyNormalized();
2293 extendedN /= 2;
2294 extendedN.x *= inverseScaleX;
2295 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002296
Chet Haase99ecdc42011-05-06 12:06:34 -07002297 float extendedNLength = extendedN.length();
2298 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002299 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002300 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002301 }
Romain Guy7b631422012-04-04 11:38:54 -07002302
Chet Haase99585ad2011-05-02 15:00:16 -07002303 // aa lines expand the endpoint vertices to encompass the AA boundary
2304 if (isAA) {
2305 vec2 abVector = (b - a);
2306 length = abVector.length();
2307 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002308
Chet Haase99ecdc42011-05-06 12:06:34 -07002309 if (scaled) {
2310 abVector.x *= inverseScaleX;
2311 abVector.y *= inverseScaleY;
2312 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002313 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002314 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002315 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002316 }
Romain Guy7b631422012-04-04 11:38:54 -07002317
Chet Haase99ecdc42011-05-06 12:06:34 -07002318 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002319 a -= abVector;
2320 b += abVector;
2321 }
2322
Chet Haase5b0200b2011-04-13 17:58:08 -07002323 // Four corners of the rectangle defining a thick line
2324 vec2 p1 = a - n;
2325 vec2 p2 = a + n;
2326 vec2 p3 = b + n;
2327 vec2 p4 = b - n;
2328
Chet Haase99585ad2011-05-02 15:00:16 -07002329
Chet Haase5b0200b2011-04-13 17:58:08 -07002330 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2331 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2332 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2333 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2334
Romain Guy04299382012-07-18 17:15:41 -07002335 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002336 if (!isAA) {
2337 if (prevVertex != NULL) {
2338 // Issue two repeat vertices to create degenerate triangles to bridge
2339 // between the previous line and the new one. This is necessary because
2340 // we are creating a single triangle_strip which will contain
2341 // potentially discontinuous line segments.
2342 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2343 Vertex::set(vertices++, p1.x, p1.y);
2344 generatedVerticesCount += 2;
2345 }
Romain Guy7b631422012-04-04 11:38:54 -07002346
Chet Haase5b0200b2011-04-13 17:58:08 -07002347 Vertex::set(vertices++, p1.x, p1.y);
2348 Vertex::set(vertices++, p2.x, p2.y);
2349 Vertex::set(vertices++, p4.x, p4.y);
2350 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002351
Chet Haase5b0200b2011-04-13 17:58:08 -07002352 prevVertex = vertices - 1;
2353 generatedVerticesCount += 4;
2354 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002355 if (!isHairLine && scaled) {
2356 // Must set width proportions per-segment for scaled non-hairlines to use the
2357 // correct AA boundary dimensions
2358 if (boundaryWidthSlot < 0) {
2359 boundaryWidthSlot =
2360 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002361 }
Romain Guy7b631422012-04-04 11:38:54 -07002362
Chet Haase99ecdc42011-05-06 12:06:34 -07002363 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002364 }
Romain Guy7b631422012-04-04 11:38:54 -07002365
Chet Haase99585ad2011-05-02 15:00:16 -07002366 if (boundaryLengthSlot < 0) {
2367 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002368 }
Romain Guy7b631422012-04-04 11:38:54 -07002369
Chet Haase99ecdc42011-05-06 12:06:34 -07002370 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002371
Chet Haase5b0200b2011-04-13 17:58:08 -07002372 if (prevAAVertex != NULL) {
2373 // Issue two repeat vertices to create degenerate triangles to bridge
2374 // between the previous line and the new one. This is necessary because
2375 // we are creating a single triangle_strip which will contain
2376 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002377 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2378 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2379 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002380 generatedVerticesCount += 2;
2381 }
Romain Guy7b631422012-04-04 11:38:54 -07002382
Chet Haase99585ad2011-05-02 15:00:16 -07002383 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2384 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2385 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2386 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002387
Chet Haase5b0200b2011-04-13 17:58:08 -07002388 prevAAVertex = aaVertices - 1;
2389 generatedVerticesCount += 4;
2390 }
Romain Guy7b631422012-04-04 11:38:54 -07002391
Chet Haase99585ad2011-05-02 15:00:16 -07002392 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2393 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2394 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002395 }
2396 }
Romain Guy7b631422012-04-04 11:38:54 -07002397
Chet Haase5b0200b2011-04-13 17:58:08 -07002398 if (generatedVerticesCount > 0) {
2399 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2400 }
Romain Guy7b631422012-04-04 11:38:54 -07002401
2402 if (isAA) {
2403 finishDrawAALine(widthSlot, lengthSlot);
2404 }
Chet Haase48659092012-05-31 15:21:51 -07002405
2406 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002407}
2408
Chet Haase48659092012-05-31 15:21:51 -07002409status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2410 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002411
2412 // TODO: The paint's cap style defines whether the points are square or circular
2413 // TODO: Handle AA for round points
2414
Chet Haase5b0200b2011-04-13 17:58:08 -07002415 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002416 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002417 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002418 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002419 if (isHairLine) {
2420 // Now that we know it's hairline, we can set the effective width, to be used later
2421 strokeWidth = 1.0f;
2422 }
2423 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002424 int alpha;
2425 SkXfermode::Mode mode;
2426 getAlphaAndMode(paint, &alpha, &mode);
2427
2428 int verticesCount = count >> 1;
2429 int generatedVerticesCount = 0;
2430
2431 TextureVertex pointsData[verticesCount];
2432 TextureVertex* vertex = &pointsData[0];
2433
Romain Guy04299382012-07-18 17:15:41 -07002434 // TODO: We should optimize this method to not generate vertices for points
2435 // that lie outside of the clip.
2436 mCaches.enableScissor();
2437
Romain Guyed6fcb02011-03-21 13:11:28 -07002438 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002439 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002440 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002441 setupDrawColor(paint->getColor(), alpha);
2442 setupDrawColorFilter();
2443 setupDrawShader();
2444 setupDrawBlending(mode);
2445 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002446 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002447 setupDrawColorUniforms();
2448 setupDrawColorFilterUniforms();
2449 setupDrawPointUniforms();
2450 setupDrawShaderIdentityUniforms();
2451 setupDrawMesh(vertex);
2452
2453 for (int i = 0; i < count; i += 2) {
2454 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2455 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002456
Chet Haase8a5cc922011-04-26 07:28:09 -07002457 float left = points[i] - halfWidth;
2458 float right = points[i] + halfWidth;
2459 float top = points[i + 1] - halfWidth;
2460 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002461
Chet Haase8a5cc922011-04-26 07:28:09 -07002462 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002463 }
2464
2465 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002466
2467 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002468}
2469
Chet Haase48659092012-05-31 15:21:51 -07002470status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002471 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002472 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002473
Romain Guyae88e5e2010-10-22 17:49:18 -07002474 Rect& clip(*mSnapshot->clipRect);
2475 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002476
Romain Guy3d58c032010-07-14 16:34:53 -07002477 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002478
2479 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002480}
Romain Guy9d5316e2010-06-24 19:30:36 -07002481
Chet Haase48659092012-05-31 15:21:51 -07002482status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2483 SkPaint* paint) {
2484 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002485 const AutoTexture autoCleanup(texture);
2486
2487 const float x = left + texture->left - texture->offset;
2488 const float y = top + texture->top - texture->offset;
2489
2490 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002491
2492 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002493}
2494
Chet Haase48659092012-05-31 15:21:51 -07002495status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002496 float rx, float ry, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002497 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002498 return DrawGlInfo::kStatusDone;
2499 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002500
Chris Craikcb4d6002012-09-25 12:00:29 -07002501 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002502 mCaches.activeTexture(0);
2503 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2504 right - left, bottom - top, rx, ry, p);
2505 return drawShape(left, top, texture, p);
2506 }
2507
2508 SkPath path;
2509 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002510 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2511 float outset = p->getStrokeWidth() / 2;
2512 rect.outset(outset, outset);
2513 rx += outset;
2514 ry += outset;
2515 }
Chris Craik710f46d2012-09-17 17:25:49 -07002516 path.addRoundRect(rect, rx, ry);
Chris Craikcb4d6002012-09-25 12:00:29 -07002517 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002518
2519 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002520}
2521
Chris Craik710f46d2012-09-17 17:25:49 -07002522status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
2524 x + radius, y + radius, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002525 return DrawGlInfo::kStatusDone;
2526 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002527 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002528 mCaches.activeTexture(0);
2529 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, p);
2530 return drawShape(x - radius, y - radius, texture, p);
2531 }
2532
2533 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002534 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2535 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2536 } else {
2537 path.addCircle(x, y, radius);
2538 }
2539 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002540
2541 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002542}
Romain Guy01d58e42011-01-19 21:54:02 -08002543
Chet Haase48659092012-05-31 15:21:51 -07002544status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002545 SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002546 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002547 return DrawGlInfo::kStatusDone;
2548 }
Romain Guy01d58e42011-01-19 21:54:02 -08002549
Chris Craikcb4d6002012-09-25 12:00:29 -07002550 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002551 mCaches.activeTexture(0);
2552 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, p);
2553 return drawShape(left, top, texture, p);
2554 }
2555
2556 SkPath path;
2557 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002558 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2559 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2560 }
Chris Craik710f46d2012-09-17 17:25:49 -07002561 path.addOval(rect);
Chris Craikcb4d6002012-09-25 12:00:29 -07002562 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002563
2564 return DrawGlInfo::kStatusDrew;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002565}
2566
Chet Haase48659092012-05-31 15:21:51 -07002567status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002568 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
2569 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
2570 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002571 }
2572
Chris Craik780c1282012-10-04 14:10:49 -07002573 if (fabs(sweepAngle) >= 360.0f) {
2574 return drawOval(left, top, right, bottom, p);
2575 }
2576
2577 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Romain Guye3a9b242013-01-08 11:15:30 -08002578 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 ||
2579 p->getStrokeCap() != SkPaint::kButt_Cap || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002580 mCaches.activeTexture(0);
2581 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2582 startAngle, sweepAngle, useCenter, p);
2583 return drawShape(left, top, texture, p);
2584 }
2585
2586 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2587 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2588 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2589 }
2590
2591 SkPath path;
2592 if (useCenter) {
2593 path.moveTo(rect.centerX(), rect.centerY());
2594 }
2595 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2596 if (useCenter) {
2597 path.close();
2598 }
2599 drawConvexPath(path, p);
2600
2601 return DrawGlInfo::kStatusDrew;
Romain Guy8b2f5262011-01-23 16:15:02 -08002602}
2603
Romain Guycf8675e2012-10-02 12:32:25 -07002604// See SkPaintDefaults.h
2605#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2606
Chet Haase48659092012-05-31 15:21:51 -07002607status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002608 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p)) {
Chet Haase48659092012-05-31 15:21:51 -07002609 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002610 }
2611
Chris Craik710f46d2012-09-17 17:25:49 -07002612 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002613 // only fill style is supported by drawConvexPath, since others have to handle joins
2614 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2615 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2616 mCaches.activeTexture(0);
2617 const PathTexture* texture =
2618 mCaches.rectShapeCache.getRect(right - left, bottom - top, p);
2619 return drawShape(left, top, texture, p);
2620 }
2621
2622 SkPath path;
2623 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2624 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2625 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2626 }
2627 path.addRect(rect);
2628 drawConvexPath(path, p);
2629
2630 return DrawGlInfo::kStatusDrew;
Romain Guy026c5e162010-06-28 17:12:22 -07002631 }
2632
Romain Guy181d0a62011-06-09 18:52:38 -07002633 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002634 SkPath path;
2635 path.addRect(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002636 drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002637 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002638 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chet Haase858aa932011-05-12 09:06:00 -07002639 }
Chet Haase48659092012-05-31 15:21:51 -07002640
2641 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002642}
Romain Guy9d5316e2010-06-24 19:30:36 -07002643
Raph Levien416a8472012-07-19 22:48:17 -07002644void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2645 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2646 float x, float y) {
2647 mCaches.activeTexture(0);
2648
2649 // NOTE: The drop shadow will not perform gamma correction
2650 // if shader-based correction is enabled
2651 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2652 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2653 paint, text, bytesCount, count, mShadowRadius, positions);
2654 const AutoTexture autoCleanup(shadow);
2655
2656 const float sx = x - shadow->left + mShadowDx;
2657 const float sy = y - shadow->top + mShadowDy;
2658
2659 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2660 int shadowColor = mShadowColor;
2661 if (mShader) {
2662 shadowColor = 0xffffffff;
2663 }
2664
2665 setupDraw();
2666 setupDrawWithTexture(true);
2667 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2668 setupDrawColorFilter();
2669 setupDrawShader();
2670 setupDrawBlending(true, mode);
2671 setupDrawProgram();
2672 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2673 setupDrawTexture(shadow->id);
2674 setupDrawPureColorUniforms();
2675 setupDrawColorFilterUniforms();
2676 setupDrawShaderUniforms();
2677 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2678
2679 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2680}
2681
Chet Haase48659092012-05-31 15:21:51 -07002682status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002683 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002684 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002685 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002686 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002687 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002688
Romain Guy671d6cf2012-01-18 12:39:17 -08002689 // NOTE: Skia does not support perspective transform on drawPosText yet
2690 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002691 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002692 }
2693
2694 float x = 0.0f;
2695 float y = 0.0f;
2696 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2697 if (pureTranslate) {
2698 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2699 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2700 }
2701
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002702 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002703 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy671d6cf2012-01-18 12:39:17 -08002704
2705 int alpha;
2706 SkXfermode::Mode mode;
2707 getAlphaAndMode(paint, &alpha, &mode);
2708
Raph Levien416a8472012-07-19 22:48:17 -07002709 if (CC_UNLIKELY(mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002710 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2711 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002712 }
2713
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 // Pick the appropriate texture filtering
2715 bool linearFilter = mSnapshot->transform->changesBounds();
2716 if (pureTranslate && !linearFilter) {
2717 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2718 }
2719
2720 mCaches.activeTexture(0);
2721 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002722 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002723 setupDrawDirtyRegionsDisabled();
2724 setupDrawWithTexture(true);
2725 setupDrawAlpha8Color(paint->getColor(), alpha);
2726 setupDrawColorFilter();
2727 setupDrawShader();
2728 setupDrawBlending(true, mode);
2729 setupDrawProgram();
2730 setupDrawModelView(x, y, x, y, pureTranslate, true);
2731 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2732 setupDrawPureColorUniforms();
2733 setupDrawColorFilterUniforms();
2734 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002735 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002736
2737 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2738 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2739
Romain Guy211370f2012-02-01 16:10:55 -08002740 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002741
2742 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2743 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002744 if (hasActiveLayer) {
2745 if (!pureTranslate) {
2746 mSnapshot->transform->mapRect(bounds);
2747 }
2748 dirtyLayerUnchecked(bounds, getRegion());
2749 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002750 }
Chet Haase48659092012-05-31 15:21:51 -07002751
2752 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002753}
2754
Romain Guyc2525952012-07-27 16:41:22 -07002755status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002756 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002757 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002758 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002759 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002760 }
2761
Chet Haasea1cff502012-02-21 13:43:44 -08002762 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002763 switch (paint->getTextAlign()) {
2764 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002765 x -= length / 2.0f;
2766 break;
2767 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002768 x -= length;
2769 break;
2770 default:
2771 break;
2772 }
2773
Romain Guycac5fd32011-12-01 20:08:50 -08002774 SkPaint::FontMetrics metrics;
2775 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002776 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002777 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002778 }
2779
Romain Guye3a9b242013-01-08 11:15:30 -08002780#if DEBUG_GLYPHS
2781 ALOGD("OpenGLRenderer drawText() with FontID=%d",
2782 SkTypeface::UniqueID(paint->getTypeface()));
2783#endif
2784
2785 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2786 fontRenderer.setFont(paint, *mSnapshot->transform);
2787
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002788 const float oldX = x;
2789 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002790 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002791 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002792 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2793 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2794 }
2795
Romain Guy86568192010-12-14 15:55:39 -08002796 int alpha;
2797 SkXfermode::Mode mode;
2798 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002799
Romain Guy211370f2012-02-01 16:10:55 -08002800 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002801 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2802 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002803 }
2804
Romain Guy6620c6d2010-12-06 18:07:02 -08002805 // Pick the appropriate texture filtering
2806 bool linearFilter = mSnapshot->transform->changesBounds();
2807 if (pureTranslate && !linearFilter) {
2808 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2809 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002810
Romain Guy16c88082012-06-11 16:03:47 -07002811 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002812 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002813 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002814 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002815 setupDrawDirtyRegionsDisabled();
2816 setupDrawWithTexture(true);
2817 setupDrawAlpha8Color(paint->getColor(), alpha);
2818 setupDrawColorFilter();
2819 setupDrawShader();
2820 setupDrawBlending(true, mode);
2821 setupDrawProgram();
2822 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002823 // See comment above; the font renderer must use texture unit 0
2824 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002825 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2826 setupDrawPureColorUniforms();
2827 setupDrawColorFilterUniforms();
2828 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002829 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002830
Romain Guya3dc55f2012-09-28 13:55:44 -07002831 const Rect* clip = pureTranslate ? mSnapshot->clipRect :
2832 (mSnapshot->hasPerspectiveTransform() ? NULL : &mSnapshot->getLocalClip());
Romain Guy5b3b3522010-10-27 18:57:51 -07002833 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2834
Romain Guy211370f2012-02-01 16:10:55 -08002835 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002836
Raph Levien996e57c2012-07-23 15:22:52 -07002837 bool status;
Romain Guya3dc55f2012-09-28 13:55:44 -07002838 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002839 SkPaint paintCopy(*paint);
2840 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2841 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002842 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002843 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002844 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guya3dc55f2012-09-28 13:55:44 -07002845 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002846 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002847
2848 if (status && hasActiveLayer) {
2849 if (!pureTranslate) {
2850 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002851 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002852 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002853 }
Romain Guy694b5192010-07-21 21:33:20 -07002854
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002855 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002856
2857 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002858}
2859
Chet Haase48659092012-05-31 15:21:51 -07002860status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002861 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002862 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2863 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002864 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002865 }
2866
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002867 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guye3a9b242013-01-08 11:15:30 -08002868 fontRenderer.setFont(paint, *mSnapshot->transform);
Romain Guy03d58522012-02-24 17:54:07 -08002869
2870 int alpha;
2871 SkXfermode::Mode mode;
2872 getAlphaAndMode(paint, &alpha, &mode);
2873
2874 mCaches.activeTexture(0);
2875 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002876 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002877 setupDrawDirtyRegionsDisabled();
2878 setupDrawWithTexture(true);
2879 setupDrawAlpha8Color(paint->getColor(), alpha);
2880 setupDrawColorFilter();
2881 setupDrawShader();
2882 setupDrawBlending(true, mode);
2883 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002884 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002885 setupDrawTexture(fontRenderer.getTexture(true));
2886 setupDrawPureColorUniforms();
2887 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002888 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002889 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002890
Romain Guy97771732012-02-28 18:17:02 -08002891 const Rect* clip = &mSnapshot->getLocalClip();
2892 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 -08002893
Romain Guy97771732012-02-28 18:17:02 -08002894 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002895
2896 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2897 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002898 if (hasActiveLayer) {
2899 mSnapshot->transform->mapRect(bounds);
2900 dirtyLayerUnchecked(bounds, getRegion());
2901 }
Romain Guy97771732012-02-28 18:17:02 -08002902 }
Chet Haase48659092012-05-31 15:21:51 -07002903
2904 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002905}
2906
Chet Haase48659092012-05-31 15:21:51 -07002907status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2908 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002909
Romain Guya1d3c912011-12-13 14:55:06 -08002910 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002911
Romain Guyfb8b7632010-08-23 21:05:08 -07002912 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002913 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002914 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002915
Romain Guy8b55f372010-08-18 17:10:07 -07002916 const float x = texture->left - texture->offset;
2917 const float y = texture->top - texture->offset;
2918
Romain Guy01d58e42011-01-19 21:54:02 -08002919 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002920
2921 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002922}
2923
Chet Haase48659092012-05-31 15:21:51 -07002924status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy35643dd2012-09-18 15:40:58 -07002925 if (!layer) {
2926 return DrawGlInfo::kStatusDone;
2927 }
2928
Romain Guyb2e2f242012-10-17 18:18:35 -07002929 mat4* transform = NULL;
2930 if (layer->isTextureLayer()) {
2931 transform = &layer->getTransform();
2932 if (!transform->isIdentity()) {
2933 save(0);
2934 mSnapshot->transform->multiply(*transform);
2935 }
2936 }
2937
Romain Guy35643dd2012-09-18 15:40:58 -07002938 Rect transformed;
2939 Rect clip;
2940 const bool rejected = quickRejectNoScissor(x, y,
2941 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2942
2943 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002944 if (transform && !transform->isIdentity()) {
2945 restore();
2946 }
Chet Haase48659092012-05-31 15:21:51 -07002947 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002948 }
2949
Romain Guy5bb3c732012-11-29 17:52:58 -08002950 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002951
Romain Guy87e2f7572012-09-24 11:37:12 -07002952 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002953 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002954
Romain Guy211370f2012-02-01 16:10:55 -08002955 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guye529ece2012-09-26 11:23:17 -07002956 SkiaColorFilter* oldFilter = mColorFilter;
2957 mColorFilter = layer->getColorFilter();
2958
Romain Guyc88e3572011-01-22 00:32:12 -08002959 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002960 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002961 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002962 const float a = layer->getAlpha() / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002963 setupDraw();
2964 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002965 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002966 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002967 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002968 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002969 setupDrawPureColorUniforms();
2970 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002971 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002972 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002973 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2974 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002975
Romain Guyd21b6e12011-11-30 20:21:23 -08002976 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002977 setupDrawModelViewTranslate(tx, ty,
2978 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002979 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002980 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002981 setupDrawModelViewTranslate(x, y,
2982 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2983 }
Romain Guyc88e3572011-01-22 00:32:12 -08002984 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002985
Romain Guyc88e3572011-01-22 00:32:12 -08002986 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2987 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002988
Romain Guyc88e3572011-01-22 00:32:12 -08002989 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002990
2991#if DEBUG_LAYERS_AS_REGIONS
2992 drawRegionRects(layer->region);
2993#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002994 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002995
Romain Guye529ece2012-09-26 11:23:17 -07002996 mColorFilter = oldFilter;
2997
Romain Guy5bb3c732012-11-29 17:52:58 -08002998 if (layer->debugDrawUpdate) {
2999 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003000 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3001 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3002 }
Romain Guyf219da52011-01-16 12:54:25 -08003003 }
Chet Haase48659092012-05-31 15:21:51 -07003004
Romain Guyb2e2f242012-10-17 18:18:35 -07003005 if (transform && !transform->isIdentity()) {
3006 restore();
3007 }
3008
Chet Haase48659092012-05-31 15:21:51 -07003009 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003010}
3011
Romain Guy6926c722010-07-12 20:20:03 -07003012///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003013// Shaders
3014///////////////////////////////////////////////////////////////////////////////
3015
3016void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07003017 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003018}
3019
Romain Guy06f96e22010-07-30 19:18:16 -07003020void OpenGLRenderer::setupShader(SkiaShader* shader) {
3021 mShader = shader;
3022 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003023 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003024 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003025}
3026
Romain Guyd27977d2010-07-14 19:18:51 -07003027///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003028// Color filters
3029///////////////////////////////////////////////////////////////////////////////
3030
3031void OpenGLRenderer::resetColorFilter() {
3032 mColorFilter = NULL;
3033}
3034
3035void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
3036 mColorFilter = filter;
3037}
3038
3039///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003040// Drop shadow
3041///////////////////////////////////////////////////////////////////////////////
3042
3043void OpenGLRenderer::resetShadow() {
3044 mHasShadow = false;
3045}
3046
3047void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
3048 mHasShadow = true;
3049 mShadowRadius = radius;
3050 mShadowDx = dx;
3051 mShadowDy = dy;
3052 mShadowColor = color;
3053}
3054
3055///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003056// Draw filters
3057///////////////////////////////////////////////////////////////////////////////
3058
3059void OpenGLRenderer::resetPaintFilter() {
3060 mHasDrawFilter = false;
3061}
3062
3063void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
3064 mHasDrawFilter = true;
3065 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3066 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
3067}
3068
3069SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08003070 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08003071
3072 uint32_t flags = paint->getFlags();
3073
3074 mFilteredPaint = *paint;
3075 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
3076
3077 return &mFilteredPaint;
3078}
3079
3080///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003081// Drawing implementation
3082///////////////////////////////////////////////////////////////////////////////
3083
Romain Guy01d58e42011-01-19 21:54:02 -08003084void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3085 float x, float y, SkPaint* paint) {
3086 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3087 return;
3088 }
3089
3090 int alpha;
3091 SkXfermode::Mode mode;
3092 getAlphaAndMode(paint, &alpha, &mode);
3093
3094 setupDraw();
3095 setupDrawWithTexture(true);
3096 setupDrawAlpha8Color(paint->getColor(), alpha);
3097 setupDrawColorFilter();
3098 setupDrawShader();
3099 setupDrawBlending(true, mode);
3100 setupDrawProgram();
3101 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3102 setupDrawTexture(texture->id);
3103 setupDrawPureColorUniforms();
3104 setupDrawColorFilterUniforms();
3105 setupDrawShaderUniforms();
3106 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3107
3108 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3109
3110 finishDrawTexture();
3111}
3112
Romain Guyf607bdc2010-09-10 19:20:06 -07003113// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003114#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3115#define kStdUnderline_Offset (1.0f / 9.0f)
3116#define kStdUnderline_Thickness (1.0f / 18.0f)
3117
3118void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3119 float x, float y, SkPaint* paint) {
3120 // Handle underline and strike-through
3121 uint32_t flags = paint->getFlags();
3122 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003123 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003124 float underlineWidth = length;
3125 // If length is > 0.0f, we already measured the text for the text alignment
3126 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003127 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003128 }
3129
Romain Guy211370f2012-02-01 16:10:55 -08003130 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003131 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003132 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003133
Raph Levien8b4072d2012-07-30 15:50:00 -07003134 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003135 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003136
Romain Guyf6834472011-01-23 13:32:12 -08003137 int linesCount = 0;
3138 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3139 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3140
3141 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003142 float points[pointsCount];
3143 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003144
3145 if (flags & SkPaint::kUnderlineText_Flag) {
3146 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003147 points[currentPoint++] = left;
3148 points[currentPoint++] = top;
3149 points[currentPoint++] = left + underlineWidth;
3150 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003151 }
3152
3153 if (flags & SkPaint::kStrikeThruText_Flag) {
3154 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003155 points[currentPoint++] = left;
3156 points[currentPoint++] = top;
3157 points[currentPoint++] = left + underlineWidth;
3158 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003159 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003160
Romain Guy726aeba2011-06-01 14:52:00 -07003161 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003162
Romain Guy726aeba2011-06-01 14:52:00 -07003163 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003164 }
3165 }
3166}
3167
Romain Guy672433d2013-01-04 19:05:13 -08003168status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3169 if (mSnapshot->isIgnored()) {
3170 return DrawGlInfo::kStatusDone;
3171 }
3172
Romain Guy735738c2012-12-03 12:34:51 -08003173 int color = paint->getColor();
3174 // If a shader is set, preserve only the alpha
3175 if (mShader) {
3176 color |= 0x00ffffff;
3177 }
3178 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3179
3180 return drawColorRects(rects, count, color, mode);
3181}
3182
3183status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy8ce00302013-01-15 18:51:42 -08003184 SkXfermode::Mode mode, bool ignoreTransform, bool dirty) {
Romain Guy735738c2012-12-03 12:34:51 -08003185
Romain Guy672433d2013-01-04 19:05:13 -08003186 float left = FLT_MAX;
3187 float top = FLT_MAX;
3188 float right = FLT_MIN;
3189 float bottom = FLT_MIN;
3190
3191 int vertexCount = 0;
3192 Vertex mesh[count * 6];
3193 Vertex* vertex = mesh;
3194
3195 for (int i = 0; i < count; i++) {
3196 int index = i * 4;
3197 float l = rects[index + 0];
3198 float t = rects[index + 1];
3199 float r = rects[index + 2];
3200 float b = rects[index + 3];
3201
Romain Guy8ce00302013-01-15 18:51:42 -08003202 if (ignoreTransform || !quickRejectNoScissor(left, top, right, bottom)) {
Romain Guy672433d2013-01-04 19:05:13 -08003203 Vertex::set(vertex++, l, b);
3204 Vertex::set(vertex++, l, t);
3205 Vertex::set(vertex++, r, t);
3206 Vertex::set(vertex++, l, b);
3207 Vertex::set(vertex++, r, t);
3208 Vertex::set(vertex++, r, b);
3209
3210 vertexCount += 6;
3211
3212 left = fminf(left, l);
3213 top = fminf(top, t);
3214 right = fmaxf(right, r);
3215 bottom = fmaxf(bottom, b);
3216 }
3217 }
3218
3219 if (count == 0) return DrawGlInfo::kStatusDone;
3220
Romain Guy672433d2013-01-04 19:05:13 -08003221 setupDraw();
3222 setupDrawNoTexture();
3223 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3224 setupDrawShader();
3225 setupDrawColorFilter();
3226 setupDrawBlending(mode);
3227 setupDrawProgram();
3228 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003229 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003230 setupDrawColorUniforms();
3231 setupDrawShaderUniforms();
3232 setupDrawColorFilterUniforms();
3233 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3234
Romain Guy8ce00302013-01-15 18:51:42 -08003235 if (dirty && hasLayer()) {
Romain Guy672433d2013-01-04 19:05:13 -08003236 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
3237 }
3238
3239 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3240
3241 return DrawGlInfo::kStatusDrew;
3242}
3243
Romain Guy026c5e162010-06-28 17:12:22 -07003244void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003245 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003246 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07003247 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003248 color |= 0x00ffffff;
3249 }
3250
Romain Guy70ca14e2010-12-13 18:24:33 -08003251 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003252 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003253 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003254 setupDrawShader();
3255 setupDrawColorFilter();
3256 setupDrawBlending(mode);
3257 setupDrawProgram();
3258 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3259 setupDrawColorUniforms();
3260 setupDrawShaderUniforms(ignoreTransform);
3261 setupDrawColorFilterUniforms();
3262 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003263
Romain Guyc95c8d62010-09-17 15:31:32 -07003264 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3265}
3266
Romain Guy82ba8142010-07-09 13:25:56 -07003267void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003268 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003269 int alpha;
3270 SkXfermode::Mode mode;
3271 getAlphaAndMode(paint, &alpha, &mode);
3272
Romain Guyd21b6e12011-11-30 20:21:23 -08003273 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003274
Romain Guy211370f2012-02-01 16:10:55 -08003275 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08003276 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
3277 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
3278
Romain Guyd21b6e12011-11-30 20:21:23 -08003279 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003280 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3281 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3282 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3283 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003284 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003285 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3286 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3287 GL_TRIANGLE_STRIP, gMeshCount);
3288 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003289}
3290
Romain Guybd6b79b2010-06-26 00:13:53 -07003291void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003292 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3293 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003294 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003295}
3296
3297void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003298 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003299 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003300 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003301
Romain Guy746b7402010-10-26 16:27:31 -07003302 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003303 setupDrawWithTexture();
3304 setupDrawColor(alpha, alpha, alpha, alpha);
3305 setupDrawColorFilter();
3306 setupDrawBlending(blend, mode, swapSrcDst);
3307 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003308 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003309 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003310 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003311 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003312 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003313 }
Romain Guy886b2752013-01-04 12:26:18 -08003314 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003315 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003316 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003317 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003318
Romain Guy6820ac82010-09-15 18:11:50 -07003319 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003320
3321 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003322}
3323
Romain Guy886b2752013-01-04 12:26:18 -08003324void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3325 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3326 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3327 bool ignoreTransform, bool dirty) {
3328
3329 setupDraw();
3330 setupDrawWithTexture(true);
3331 if (hasColor) {
3332 setupDrawAlpha8Color(color, alpha);
3333 }
3334 setupDrawColorFilter();
3335 setupDrawShader();
3336 setupDrawBlending(true, mode);
3337 setupDrawProgram();
3338 if (!dirty) setupDrawDirtyRegionsDisabled();
3339 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3340 setupDrawTexture(texture);
3341 setupDrawPureColorUniforms();
3342 setupDrawColorFilterUniforms();
3343 setupDrawShaderUniforms();
3344 setupDrawMesh(vertices, texCoords);
3345
3346 glDrawArrays(drawMode, 0, elementsCount);
3347
3348 finishDrawTexture();
3349}
3350
Romain Guya5aed0d2010-09-09 14:42:43 -07003351void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003352 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003353 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003354
Romain Guy82ba8142010-07-09 13:25:56 -07003355 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003356 // These blend modes are not supported by OpenGL directly and have
3357 // to be implemented using shaders. Since the shader will perform
3358 // the blending, turn blending off here
3359 // If the blend mode cannot be implemented using shaders, fall
3360 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003361 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08003362 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003363 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003364 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003365
Romain Guy82bc7a72012-01-03 14:13:39 -08003366 if (mCaches.blend) {
3367 glDisable(GL_BLEND);
3368 mCaches.blend = false;
3369 }
3370
3371 return;
3372 } else {
3373 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003374 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003375 }
3376
3377 if (!mCaches.blend) {
3378 glEnable(GL_BLEND);
3379 }
3380
3381 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3382 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3383
3384 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3385 glBlendFunc(sourceMode, destMode);
3386 mCaches.lastSrcMode = sourceMode;
3387 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003388 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003389 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003390 glDisable(GL_BLEND);
3391 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003392 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003393}
3394
Romain Guy889f8d12010-07-29 14:37:42 -07003395bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003396 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003397 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003398 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003399 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003400 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003401 }
Romain Guy6926c722010-07-12 20:20:03 -07003402 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003403}
3404
Romain Guy026c5e162010-06-28 17:12:22 -07003405void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003406 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003407 TextureVertex::setUV(v++, u1, v1);
3408 TextureVertex::setUV(v++, u2, v1);
3409 TextureVertex::setUV(v++, u1, v2);
3410 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003411}
3412
Chet Haase5c13d892010-10-08 08:37:55 -07003413void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003414 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07003415 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003416}
3417
Romain Guy9d5316e2010-06-24 19:30:36 -07003418}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003419}; // namespace android