blob: 9f8b87c8109637bd5605cf41622a7ef03fb57a39 [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"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guyf8773082012-07-12 18:01:00 -070048#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070049
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800127// Debug
128///////////////////////////////////////////////////////////////////////////////
129
130void OpenGLRenderer::startMark(const char* name) const {
131 mCaches.startMark(0, name);
132}
133
134void OpenGLRenderer::endMark() const {
135 mCaches.endMark();
136}
137
138///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700139// Setup
140///////////////////////////////////////////////////////////////////////////////
141
Romain Guy49c5fc02012-05-15 11:10:01 -0700142bool OpenGLRenderer::isDeferred() {
143 return false;
144}
145
Romain Guy85bf02f2010-06-22 13:11:24 -0700146void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700147 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
149 mWidth = width;
150 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700151
152 mFirstSnapshot->height = height;
153 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700154
Romain Guy3e263fa2011-12-12 16:47:48 -0800155 glDisable(GL_DITHER);
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800157
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700159}
160
Chet Haase44b2fe32012-06-06 19:03:58 -0700161int OpenGLRenderer::prepare(bool opaque) {
162 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
Romain Guy7d7b5492011-01-24 16:33:45 -0800163}
164
Chet Haase44b2fe32012-06-06 19:03:58 -0700165int OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800166 mCaches.clearGarbage();
167
Romain Guy8aef54f2010-09-01 15:13:49 -0700168 mSnapshot = new Snapshot(mFirstSnapshot,
169 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800170 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700171 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700172
Romain Guy7d7b5492011-01-24 16:33:45 -0800173 mSnapshot->setClip(left, top, right, bottom);
Romain Guyddf74372012-05-22 14:07:07 -0700174 mDirtyClip = opaque;
175
176 syncState();
Romain Guy7d7b5492011-01-24 16:33:45 -0800177
Romain Guy6b7bd242010-10-06 19:49:23 -0700178 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700179 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700180 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700181 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700182 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700183 } else {
184 mCaches.resetScissor();
185 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700186
187 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700188}
189
190void OpenGLRenderer::syncState() {
191 glViewport(0, 0, mWidth, mHeight);
192
193 if (mCaches.blend) {
194 glEnable(GL_BLEND);
195 } else {
196 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700197 }
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
Romain Guyb025b9c2010-09-16 14:16:48 -0700200void OpenGLRenderer::finish() {
201#if DEBUG_OPENGL
202 GLenum status = GL_NO_ERROR;
203 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000204 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800205 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700206 case GL_INVALID_ENUM:
207 ALOGE(" GL_INVALID_ENUM");
208 break;
209 case GL_INVALID_VALUE:
210 ALOGE(" GL_INVALID_VALUE");
211 break;
212 case GL_INVALID_OPERATION:
213 ALOGE(" GL_INVALID_OPERATION");
214 break;
Romain Guya07105b2011-01-10 21:14:18 -0800215 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700216 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800217 break;
218 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700219 }
220#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800221#if DEBUG_MEMORY_USAGE
222 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800223#else
224 if (mCaches.getDebugLevel() & kDebugMemory) {
225 mCaches.dumpMemoryUsage();
226 }
Romain Guyc15008e2010-11-10 11:59:15 -0800227#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700228}
229
Romain Guy6c319ca2011-01-11 14:29:25 -0800230void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700231 if (mCaches.currentProgram) {
232 if (mCaches.currentProgram->isInUse()) {
233 mCaches.currentProgram->remove();
234 mCaches.currentProgram = NULL;
235 }
236 }
Romain Guy50c0f092010-10-19 11:42:22 -0700237 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800238 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800239 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800240 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700241}
242
Romain Guy6c319ca2011-01-11 14:29:25 -0800243void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800244 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
245
246 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800247 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
248
Chet Haase80250612012-08-15 13:46:54 -0700249 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700250 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800251 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700252 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700253
Romain Guya1d3c912011-12-13 14:55:06 -0800254 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800255 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700256
Romain Guy50c0f092010-10-19 11:42:22 -0700257 mCaches.blend = true;
258 glEnable(GL_BLEND);
259 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
260 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700261}
262
Romain Guyba6be8a2012-04-23 18:22:09 -0700263void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700264 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700265}
266
267void OpenGLRenderer::attachFunctor(Functor* functor) {
268 mFunctors.add(functor);
269}
270
Romain Guy8f3b8e32012-03-27 16:33:45 -0700271status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
272 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700273 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700274
Romain Guyba6be8a2012-04-23 18:22:09 -0700275 if (count > 0) {
276 SortedVector<Functor*> functors(mFunctors);
277 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700278
Romain Guyba6be8a2012-04-23 18:22:09 -0700279 DrawGlInfo info;
280 info.clipLeft = 0;
281 info.clipTop = 0;
282 info.clipRight = 0;
283 info.clipBottom = 0;
284 info.isLayer = false;
285 info.width = 0;
286 info.height = 0;
287 memset(info.transform, 0, sizeof(float) * 16);
288
289 for (size_t i = 0; i < count; i++) {
290 Functor* f = functors.itemAt(i);
291 result |= (*f)(DrawGlInfo::kModeProcess, &info);
292
Chris Craikc2c95432012-04-25 15:13:52 -0700293 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700294 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
295 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700296 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700297
Chris Craikc2c95432012-04-25 15:13:52 -0700298 if (result & DrawGlInfo::kStatusInvoke) {
299 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700300 }
301 }
302 }
303
Romain Guyc189ef52012-04-25 20:02:53 -0700304 mCaches.activeTexture(0);
305
Romain Guy8f3b8e32012-03-27 16:33:45 -0700306 return result;
307}
308
309status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800310 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700311 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700312
Romain Guy8a4ac612012-07-17 17:32:48 -0700313 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800314 if (mDirtyClip) {
315 setScissorFromClip();
316 }
Romain Guyd643bb52011-03-01 14:55:21 -0800317
Romain Guy80911b82011-03-16 15:30:12 -0700318 Rect clip(*mSnapshot->clipRect);
319 clip.snapToPixelBoundaries();
320
Romain Guyd643bb52011-03-01 14:55:21 -0800321 // Since we don't know what the functor will draw, let's dirty
322 // tne entire clip region
323 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800324 dirtyLayerUnchecked(clip, getRegion());
325 }
Romain Guyd643bb52011-03-01 14:55:21 -0800326
Romain Guy08aa2cb2011-03-17 11:06:57 -0700327 DrawGlInfo info;
328 info.clipLeft = clip.left;
329 info.clipTop = clip.top;
330 info.clipRight = clip.right;
331 info.clipBottom = clip.bottom;
332 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700333 info.width = getSnapshot()->viewport.getWidth();
334 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700335 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700336
Chet Haase48659092012-05-31 15:21:51 -0700337 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800338
Romain Guy8f3b8e32012-03-27 16:33:45 -0700339 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700340 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800341 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700342
Chris Craik65924a32012-04-05 17:52:11 -0700343 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700344 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700345 }
Romain Guycabfcc12011-03-07 18:06:46 -0800346 }
347
Chet Haasedaf98e92011-01-10 14:10:36 -0800348 resume();
Romain Guy65549432012-03-26 16:45:05 -0700349 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800350}
351
Romain Guyf6a11b82010-06-23 17:47:49 -0700352///////////////////////////////////////////////////////////////////////////////
353// State management
354///////////////////////////////////////////////////////////////////////////////
355
Romain Guybb9524b2010-06-22 18:56:38 -0700356int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700357 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700358}
359
360int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700361 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700362}
363
364void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700365 if (mSaveCount > 1) {
366 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700367 }
Romain Guybb9524b2010-06-22 18:56:38 -0700368}
369
370void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700371 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700372
Romain Guy8fb95422010-08-17 18:38:51 -0700373 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700374 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700375 }
Romain Guybb9524b2010-06-22 18:56:38 -0700376}
377
Romain Guy8aef54f2010-09-01 15:13:49 -0700378int OpenGLRenderer::saveSnapshot(int flags) {
379 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700380 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700381}
382
383bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700384 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700385 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700386 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700387
Romain Guybd6b79b2010-06-26 00:13:53 -0700388 sp<Snapshot> current = mSnapshot;
389 sp<Snapshot> previous = mSnapshot->previous;
390
Romain Guyeb993562010-10-05 18:14:38 -0700391 if (restoreOrtho) {
392 Rect& r = previous->viewport;
393 glViewport(r.left, r.top, r.right, r.bottom);
394 mOrthoMatrix.load(current->orthoMatrix);
395 }
396
Romain Guy8b55f372010-08-18 17:10:07 -0700397 mSaveCount--;
398 mSnapshot = previous;
399
Romain Guy2542d192010-08-18 11:47:12 -0700400 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700401 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700402 }
Romain Guy2542d192010-08-18 11:47:12 -0700403
Romain Guy5ec99242010-11-03 16:19:08 -0700404 if (restoreLayer) {
405 composeLayer(current, previous);
406 }
407
Romain Guy2542d192010-08-18 11:47:12 -0700408 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700409}
410
Romain Guyf6a11b82010-06-23 17:47:49 -0700411///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700412// Layers
413///////////////////////////////////////////////////////////////////////////////
414
415int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700416 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700417 const GLuint previousFbo = mSnapshot->fbo;
418 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700419
Romain Guyaf636eb2010-12-09 17:47:21 -0800420 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700421 int alpha = 255;
422 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700423
Romain Guye45362c2010-11-03 19:58:32 -0700424 if (p) {
425 alpha = p->getAlpha();
426 if (!mCaches.extensions.hasFramebufferFetch()) {
427 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
428 if (!isMode) {
429 // Assume SRC_OVER
430 mode = SkXfermode::kSrcOver_Mode;
431 }
432 } else {
433 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700434 }
435 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700436 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700437 }
Romain Guyd55a8612010-06-28 17:42:46 -0700438
Chet Haased48885a2012-08-28 17:43:28 -0700439 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700440 }
Romain Guyd55a8612010-06-28 17:42:46 -0700441
442 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700443}
444
445int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
446 int alpha, int flags) {
Romain Guyf8773082012-07-12 18:01:00 -0700447 if (alpha >= 255) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700448 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700449 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700450 SkPaint paint;
451 paint.setAlpha(alpha);
452 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700453 }
Romain Guyd55a8612010-06-28 17:42:46 -0700454}
Romain Guybd6b79b2010-06-26 00:13:53 -0700455
Romain Guy1c740bc2010-09-13 18:00:09 -0700456/**
457 * Layers are viewed by Skia are slightly different than layers in image editing
458 * programs (for instance.) When a layer is created, previously created layers
459 * and the frame buffer still receive every drawing command. For instance, if a
460 * layer is created and a shape intersecting the bounds of the layers and the
461 * framebuffer is draw, the shape will be drawn on both (unless the layer was
462 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
463 *
464 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
465 * texture. Unfortunately, this is inefficient as it requires every primitive to
466 * be drawn n + 1 times, where n is the number of active layers. In practice this
467 * means, for every primitive:
468 * - Switch active frame buffer
469 * - Change viewport, clip and projection matrix
470 * - Issue the drawing
471 *
472 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700473 * To avoid this, layers are implemented in a different way here, at least in the
474 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
475 * is set. When this flag is set we can redirect all drawing operations into a
476 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700477 *
478 * This implementation relies on the frame buffer being at least RGBA 8888. When
479 * a layer is created, only a texture is created, not an FBO. The content of the
480 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700481 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700482 * buffer and drawing continues as normal. This technique therefore treats the
483 * frame buffer as a scratch buffer for the layers.
484 *
485 * To compose the layers back onto the frame buffer, each layer texture
486 * (containing the original frame buffer data) is drawn as a simple quad over
487 * the frame buffer. The trick is that the quad is set as the composition
488 * destination in the blending equation, and the frame buffer becomes the source
489 * of the composition.
490 *
491 * Drawing layers with an alpha value requires an extra step before composition.
492 * An empty quad is drawn over the layer's region in the frame buffer. This quad
493 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
494 * quad is used to multiply the colors in the frame buffer. This is achieved by
495 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
496 * GL_ZERO, GL_SRC_ALPHA.
497 *
498 * Because glCopyTexImage2D() can be slow, an alternative implementation might
499 * be use to draw a single clipped layer. The implementation described above
500 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700501 *
502 * (1) The frame buffer is actually not cleared right away. To allow the GPU
503 * to potentially optimize series of calls to glCopyTexImage2D, the frame
504 * buffer is left untouched until the first drawing operation. Only when
505 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700506 */
Chet Haased48885a2012-08-28 17:43:28 -0700507bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
508 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700509 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700510 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700511
Romain Guyeb993562010-10-05 18:14:38 -0700512 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
513
Romain Guyf607bdc2010-09-10 19:20:06 -0700514 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700515 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700516 Rect bounds(left, top, right, bottom);
Chet Haased48885a2012-08-28 17:43:28 -0700517 Rect untransformedBounds(bounds);
518 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700519
Chet Haased48885a2012-08-28 17:43:28 -0700520 // Layers only make sense if they are in the framebuffer's bounds
521 if (bounds.intersect(*mSnapshot->clipRect)) {
522 // We cannot work with sub-pixels in this case
523 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700524
Chet Haased48885a2012-08-28 17:43:28 -0700525 // When the layer is not an FBO, we may use glCopyTexImage so we
526 // need to make sure the layer does not extend outside the bounds
527 // of the framebuffer
528 if (!bounds.intersect(mSnapshot->previous->viewport)) {
Romain Guyad37cd32011-03-15 11:12:25 -0700529 bounds.setEmpty();
Chet Haased48885a2012-08-28 17:43:28 -0700530 } else if (fboLayer) {
531 clip.set(bounds);
532 mat4 inverse;
533 inverse.loadInverse(*mSnapshot->transform);
534 inverse.mapRect(clip);
535 clip.snapToPixelBoundaries();
536 if (clip.intersect(untransformedBounds)) {
537 clip.translate(-left, -top);
538 bounds.set(untransformedBounds);
539 } else {
540 clip.setEmpty();
541 }
Romain Guyad37cd32011-03-15 11:12:25 -0700542 }
Chet Haased48885a2012-08-28 17:43:28 -0700543 } else {
544 bounds.setEmpty();
Romain Guyeb993562010-10-05 18:14:38 -0700545 }
Romain Guybf434112010-09-16 14:40:17 -0700546
Romain Guy746b7402010-10-26 16:27:31 -0700547 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
Chet Haased48885a2012-08-28 17:43:28 -0700548 bounds.getHeight() > mCaches.maxTextureSize ||
549 (fboLayer && clip.isEmpty())) {
550 mSnapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700551 } else {
Chet Haased48885a2012-08-28 17:43:28 -0700552 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700553 }
554
555 // Bail out if we won't draw in this snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700556 if (mSnapshot->invisible || mSnapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700557 return false;
558 }
Romain Guyf18fd992010-07-08 11:45:51 -0700559
Romain Guya1d3c912011-12-13 14:55:06 -0800560 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700561 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700562 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700563 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700564 }
565
Romain Guy9ace8f52011-07-07 20:50:11 -0700566 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700567 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700568 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
569 bounds.getWidth() / float(layer->getWidth()), 0.0f);
570 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700571 layer->setBlend(true);
Romain Guydda57022010-07-06 11:39:32 -0700572
Romain Guy8fb95422010-08-17 18:38:51 -0700573 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700574 mSnapshot->flags |= Snapshot::kFlagIsLayer;
575 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700576
Romain Guyeb993562010-10-05 18:14:38 -0700577 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700578 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700579 } else {
580 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700581 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800582 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700583 if (layer->isEmpty()) {
584 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700585 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 layer->getWidth(), layer->getHeight(), 0);
587 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800588 } else {
589 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700590 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800591 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700592
Romain Guy54be1cd2011-06-13 19:04:27 -0700593 // Enqueue the buffer coordinates to clear the corresponding region later
594 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700595 }
Romain Guyeb993562010-10-05 18:14:38 -0700596 }
Romain Guyf86ef572010-07-01 11:05:42 -0700597
Romain Guyd55a8612010-06-28 17:42:46 -0700598 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700599}
600
Chet Haased48885a2012-08-28 17:43:28 -0700601bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700602 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700603
Chet Haased48885a2012-08-28 17:43:28 -0700604 mSnapshot->region = &mSnapshot->layer->region;
605 mSnapshot->flags |= Snapshot::kFlagFboTarget;
Romain Guy5b3b3522010-10-27 18:57:51 -0700606
Chet Haased48885a2012-08-28 17:43:28 -0700607 mSnapshot->flags |= Snapshot::kFlagIsFboLayer;
608 mSnapshot->fbo = layer->getFbo();
609 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
610 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
611 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
612 mSnapshot->height = bounds.getHeight();
613 mSnapshot->flags |= Snapshot::kFlagDirtyOrtho;
614 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700615
616 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700617 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
618 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700619
620 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700621 if (layer->isEmpty()) {
622 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
623 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700624 }
625
626 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700627 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700628
629#if DEBUG_LAYERS_AS_REGIONS
630 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
631 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000632 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700633
634 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700635 layer->deleteTexture();
636 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700637
638 delete layer;
639
640 return false;
641 }
642#endif
643
644 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700645 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800646 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700647 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700648 glClear(GL_COLOR_BUFFER_BIT);
649
650 dirtyClip();
651
652 // Change the ortho projection
653 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
654 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
655
656 return true;
657}
658
Romain Guy1c740bc2010-09-13 18:00:09 -0700659/**
660 * Read the documentation of createLayer() before doing anything in this method.
661 */
Romain Guy1d83e192010-08-17 11:37:00 -0700662void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
663 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000664 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700665 return;
666 }
667
Romain Guy5b3b3522010-10-27 18:57:51 -0700668 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700669
670 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700671 // Detach the texture from the FBO
672 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
673
Romain Guyeb993562010-10-05 18:14:38 -0700674 // Unbind current FBO and restore previous one
675 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
676 }
677
Romain Guy1d83e192010-08-17 11:37:00 -0700678 Layer* layer = current->layer;
679 const Rect& rect = layer->layer;
680
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700682 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700684 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700686 }
687
Romain Guy03750a02010-10-18 14:06:08 -0700688 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700689
Romain Guya1d3c912011-12-13 14:55:06 -0800690 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700691
Romain Guy5b3b3522010-10-27 18:57:51 -0700692 // When the layer is stored in an FBO, we can save a bit of fillrate by
693 // drawing only the dirty region
694 if (fboLayer) {
695 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 if (layer->getColorFilter()) {
697 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800698 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700700 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800701 resetColorFilter();
702 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 } else if (!rect.isEmpty()) {
704 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
705 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 }
Romain Guy8b55f372010-08-18 17:10:07 -0700707
Romain Guyeb993562010-10-05 18:14:38 -0700708 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800709 // Note: No need to use glDiscardFramebufferEXT() since we never
710 // create/compose layers that are not on screen with this
711 // code path
712 // See LayerRenderer::destroyLayer(Layer*)
713
Romain Guyeb993562010-10-05 18:14:38 -0700714 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
715 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700716 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700717 }
718
Romain Guy746b7402010-10-26 16:27:31 -0700719 dirtyClip();
720
Romain Guyeb993562010-10-05 18:14:38 -0700721 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700722 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700723 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700725 delete layer;
726 }
727}
728
Romain Guyaa6c24c2011-04-28 18:40:04 -0700729void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700731
Romain Guy302a9df2011-08-16 13:55:02 -0700732 mat4& transform = layer->getTransform();
733 if (!transform.isIdentity()) {
734 save(0);
735 mSnapshot->transform->multiply(transform);
736 }
737
Romain Guyaa6c24c2011-04-28 18:40:04 -0700738 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700740 setupDrawWithTexture();
741 } else {
742 setupDrawWithExternalTexture();
743 }
744 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700745 setupDrawColor(alpha, alpha, alpha, alpha);
746 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700748 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 setupDrawPureColorUniforms();
750 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
752 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700753 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700754 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700755 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700756 if (mSnapshot->transform->isPureTranslate() &&
757 layer->getWidth() == (uint32_t) rect.getWidth() &&
758 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
760 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
761
Romain Guyd21b6e12011-11-30 20:21:23 -0800762 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
764 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800765 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
767 }
768 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700769 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
770
771 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
772
773 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700774
775 if (!transform.isIdentity()) {
776 restore();
777 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778}
779
Romain Guy5b3b3522010-10-27 18:57:51 -0700780void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700781 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700782 const Rect& texCoords = layer->texCoords;
783 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
784 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 float x = rect.left;
787 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700788 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700789 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700790 layer->getHeight() == (uint32_t) rect.getHeight();
791
792 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700793 // When we're swapping, the layer is already in screen coordinates
794 if (!swap) {
795 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
796 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
797 }
798
Romain Guyd21b6e12011-11-30 20:21:23 -0800799 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700800 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800801 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 }
803
804 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
805 layer->getTexture(), layer->getAlpha() / 255.0f,
806 layer->getMode(), layer->isBlend(),
807 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
808 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
Romain Guyaa6c24c2011-04-28 18:40:04 -0700810 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
811 } else {
812 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
813 drawTextureLayer(layer, rect);
814 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
815 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700816}
817
818void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700819 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700820 layer->setRegionAsRect();
821
Romain Guy40667672011-03-18 14:34:03 -0700822 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700823
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 layer->region.clear();
825 return;
826 }
827
Romain Guy8a3957d2011-09-07 17:55:15 -0700828 // TODO: See LayerRenderer.cpp::generateMesh() for important
829 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800830 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 size_t count;
832 const android::Rect* rects = layer->region.getArray(&count);
833
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 const float alpha = layer->getAlpha() / 255.0f;
835 const float texX = 1.0f / float(layer->getWidth());
836 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800837 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
839 TextureVertex* mesh = mCaches.getRegionMesh();
840 GLsizei numQuads = 0;
841
Romain Guy7230a742011-01-10 22:26:16 -0800842 setupDraw();
843 setupDrawWithTexture();
844 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800845 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800847 setupDrawProgram();
848 setupDrawDirtyRegionsDisabled();
849 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800850 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 setupDrawTexture(layer->getTexture());
852 if (mSnapshot->transform->isPureTranslate()) {
853 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
854 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
855
Romain Guyd21b6e12011-11-30 20:21:23 -0800856 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
858 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800859 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700860 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
861 }
Romain Guy15bc6432011-12-13 13:11:32 -0800862 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700863
864 for (size_t i = 0; i < count; i++) {
865 const android::Rect* r = &rects[i];
866
867 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800868 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800870 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700871
872 // TODO: Reject quads outside of the clip
873 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
874 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
875 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
876 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
877
878 numQuads++;
879
880 if (numQuads >= REGION_MESH_QUAD_COUNT) {
881 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
882 numQuads = 0;
883 mesh = mCaches.getRegionMesh();
884 }
885 }
886
887 if (numQuads > 0) {
888 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
889 }
890
Romain Guy7230a742011-01-10 22:26:16 -0800891 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700892
893#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800894 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700895#endif
896
897 layer->region.clear();
898 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700899}
900
Romain Guy3a3133d2011-02-01 22:59:58 -0800901void OpenGLRenderer::drawRegionRects(const Region& region) {
902#if DEBUG_LAYERS_AS_REGIONS
903 size_t count;
904 const android::Rect* rects = region.getArray(&count);
905
906 uint32_t colors[] = {
907 0x7fff0000, 0x7f00ff00,
908 0x7f0000ff, 0x7fff00ff,
909 };
910
911 int offset = 0;
912 int32_t top = rects[0].top;
913
914 for (size_t i = 0; i < count; i++) {
915 if (top != rects[i].top) {
916 offset ^= 0x2;
917 top = rects[i].top;
918 }
919
920 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
921 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
922 SkXfermode::kSrcOver_Mode);
923 }
924#endif
925}
926
Romain Guy5b3b3522010-10-27 18:57:51 -0700927void OpenGLRenderer::dirtyLayer(const float left, const float top,
928 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -0800929 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700930 Rect bounds(left, top, right, bottom);
931 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800932 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700933 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700934}
935
936void OpenGLRenderer::dirtyLayer(const float left, const float top,
937 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -0800938 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700939 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800940 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800941 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800942}
943
944void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -0800945 if (bounds.intersect(*mSnapshot->clipRect)) {
946 bounds.snapToPixelBoundaries();
947 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
948 if (!dirty.isEmpty()) {
949 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950 }
951 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700952}
953
Romain Guy54be1cd2011-06-13 19:04:27 -0700954void OpenGLRenderer::clearLayerRegions() {
955 const size_t count = mLayers.size();
956 if (count == 0) return;
957
958 if (!mSnapshot->isIgnored()) {
959 // Doing several glScissor/glClear here can negatively impact
960 // GPUs with a tiler architecture, instead we draw quads with
961 // the Clear blending mode
962
963 // The list contains bounds that have already been clipped
964 // against their initial clip rect, and the current clip
965 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -0700966 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700967
968 Vertex mesh[count * 6];
969 Vertex* vertex = mesh;
970
971 for (uint32_t i = 0; i < count; i++) {
972 Rect* bounds = mLayers.itemAt(i);
973
974 Vertex::set(vertex++, bounds->left, bounds->bottom);
975 Vertex::set(vertex++, bounds->left, bounds->top);
976 Vertex::set(vertex++, bounds->right, bounds->top);
977 Vertex::set(vertex++, bounds->left, bounds->bottom);
978 Vertex::set(vertex++, bounds->right, bounds->top);
979 Vertex::set(vertex++, bounds->right, bounds->bottom);
980
981 delete bounds;
982 }
983
984 setupDraw(false);
985 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
986 setupDrawBlending(true, SkXfermode::kClear_Mode);
987 setupDrawProgram();
988 setupDrawPureColorUniforms();
989 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800990 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700991
Romain Guy54be1cd2011-06-13 19:04:27 -0700992 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -0700993
994 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -0700995 } else {
996 for (uint32_t i = 0; i < count; i++) {
997 delete mLayers.itemAt(i);
998 }
999 }
1000
1001 mLayers.clear();
1002}
1003
Romain Guybd6b79b2010-06-26 00:13:53 -07001004///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001005// Transforms
1006///////////////////////////////////////////////////////////////////////////////
1007
1008void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001009 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001010}
1011
1012void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001013 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001014}
1015
1016void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
Romain Guy807daf72011-01-18 11:19:19 -08001020void OpenGLRenderer::skew(float sx, float sy) {
1021 mSnapshot->transform->skew(sx, sy);
1022}
1023
Romain Guyf6a11b82010-06-23 17:47:49 -07001024void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001025 if (matrix) {
1026 mSnapshot->transform->load(*matrix);
1027 } else {
1028 mSnapshot->transform->loadIdentity();
1029 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001030}
1031
1032void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001033 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
1036void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001037 SkMatrix transform;
1038 mSnapshot->transform->copyTo(transform);
1039 transform.preConcat(*matrix);
1040 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001041}
1042
1043///////////////////////////////////////////////////////////////////////////////
1044// Clipping
1045///////////////////////////////////////////////////////////////////////////////
1046
Romain Guybb9524b2010-06-22 18:56:38 -07001047void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001048 Rect clip(*mSnapshot->clipRect);
1049 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001050
Romain Guy8a4ac612012-07-17 17:32:48 -07001051 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1052 clip.getWidth(), clip.getHeight())) {
1053 mDirtyClip = false;
1054 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001055}
1056
1057const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001058 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001059}
1060
Romain Guy8a4ac612012-07-17 17:32:48 -07001061bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1062 if (mSnapshot->isIgnored()) {
1063 return true;
1064 }
1065
1066 Rect r(left, top, right, bottom);
1067 mSnapshot->transform->mapRect(r);
1068 r.snapToPixelBoundaries();
1069
1070 Rect clipRect(*mSnapshot->clipRect);
1071 clipRect.snapToPixelBoundaries();
1072
1073 return !clipRect.intersects(r);
1074}
1075
Romain Guyc7d53492010-06-25 13:41:57 -07001076bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001077 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001078 return true;
1079 }
1080
Romain Guy1d83e192010-08-17 11:37:00 -07001081 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001082 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001083 r.snapToPixelBoundaries();
1084
1085 Rect clipRect(*mSnapshot->clipRect);
1086 clipRect.snapToPixelBoundaries();
1087
Romain Guy586cae32012-07-13 15:28:31 -07001088 bool rejected = !clipRect.intersects(r);
1089 if (!isDeferred() && !rejected) {
1090 mCaches.setScissorEnabled(!clipRect.contains(r));
1091 }
1092
1093 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001094}
1095
Romain Guy079ba2c2010-07-16 14:12:24 -07001096bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1097 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001098 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001099 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001100 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001101 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001102}
1103
Chet Haasea23eed82012-04-12 15:19:04 -07001104Rect* OpenGLRenderer::getClipRect() {
1105 return mSnapshot->clipRect;
1106}
1107
Romain Guyf6a11b82010-06-23 17:47:49 -07001108///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001109// Drawing commands
1110///////////////////////////////////////////////////////////////////////////////
1111
Romain Guy54be1cd2011-06-13 19:04:27 -07001112void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001113 // TODO: It would be best if we could do this before quickReject()
1114 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001115 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001116 if (mDirtyClip) {
1117 setScissorFromClip();
1118 }
1119 mDescription.reset();
1120 mSetShaderColor = false;
1121 mColorSet = false;
1122 mColorA = mColorR = mColorG = mColorB = 0.0f;
1123 mTextureUnit = 0;
1124 mTrackDirtyRegions = true;
1125}
1126
1127void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1128 mDescription.hasTexture = true;
1129 mDescription.hasAlpha8Texture = isAlpha8;
1130}
1131
Romain Guyaa6c24c2011-04-28 18:40:04 -07001132void OpenGLRenderer::setupDrawWithExternalTexture() {
1133 mDescription.hasExternalTexture = true;
1134}
1135
Romain Guy15bc6432011-12-13 13:11:32 -08001136void OpenGLRenderer::setupDrawNoTexture() {
1137 mCaches.disbaleTexCoordsVertexArray();
1138}
1139
Chet Haase5b0200b2011-04-13 17:58:08 -07001140void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001141 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001142}
1143
Chris Craik6ebdc112012-08-31 18:24:33 -07001144void OpenGLRenderer::setupDrawAARect() {
1145 mDescription.isAARect = true;
1146}
1147
Romain Guyed6fcb02011-03-21 13:11:28 -07001148void OpenGLRenderer::setupDrawPoint(float pointSize) {
1149 mDescription.isPoint = true;
1150 mDescription.pointSize = pointSize;
1151}
1152
Romain Guy70ca14e2010-12-13 18:24:33 -08001153void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001154 setupDrawColor(color, (color >> 24) & 0xFF);
1155}
1156
1157void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1158 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001159 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1160 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001161 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001162 mColorR = a * ((color >> 16) & 0xFF);
1163 mColorG = a * ((color >> 8) & 0xFF);
1164 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001165 mColorSet = true;
1166 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1167}
1168
Romain Guy86568192010-12-14 15:55:39 -08001169void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1170 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001171 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1172 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001173 const float a = mColorA / 255.0f;
1174 mColorR = a * ((color >> 16) & 0xFF);
1175 mColorG = a * ((color >> 8) & 0xFF);
1176 mColorB = a * ((color ) & 0xFF);
1177 mColorSet = true;
1178 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1179}
1180
Romain Guy41210632012-07-16 17:04:24 -07001181void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1182 mCaches.fontRenderer->describe(mDescription, paint);
1183}
1184
Romain Guy70ca14e2010-12-13 18:24:33 -08001185void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1186 mColorA = a;
1187 mColorR = r;
1188 mColorG = g;
1189 mColorB = b;
1190 mColorSet = true;
1191 mSetShaderColor = mDescription.setColor(r, g, b, a);
1192}
1193
1194void OpenGLRenderer::setupDrawShader() {
1195 if (mShader) {
1196 mShader->describe(mDescription, mCaches.extensions);
1197 }
1198}
1199
1200void OpenGLRenderer::setupDrawColorFilter() {
1201 if (mColorFilter) {
1202 mColorFilter->describe(mDescription, mCaches.extensions);
1203 }
1204}
1205
Romain Guyf09ef512011-05-27 11:43:46 -07001206void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1207 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1208 mColorA = 1.0f;
1209 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001210 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001211 }
1212}
1213
Romain Guy70ca14e2010-12-13 18:24:33 -08001214void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001215 // When the blending mode is kClear_Mode, we need to use a modulate color
1216 // argb=1,0,0,0
1217 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001218 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1219 mDescription, swapSrcDst);
1220}
1221
1222void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001223 // When the blending mode is kClear_Mode, we need to use a modulate color
1224 // argb=1,0,0,0
1225 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001226 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1227 mDescription, swapSrcDst);
1228}
1229
1230void OpenGLRenderer::setupDrawProgram() {
1231 useProgram(mCaches.programCache.get(mDescription));
1232}
1233
1234void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1235 mTrackDirtyRegions = false;
1236}
1237
1238void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1239 bool ignoreTransform) {
1240 mModelView.loadTranslate(left, top, 0.0f);
1241 if (!ignoreTransform) {
1242 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1243 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1244 } else {
1245 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1246 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1247 }
1248}
1249
Chet Haase8a5cc922011-04-26 07:28:09 -07001250void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1251 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001252}
1253
Romain Guy70ca14e2010-12-13 18:24:33 -08001254void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1255 bool ignoreTransform, bool ignoreModelView) {
1256 if (!ignoreModelView) {
1257 mModelView.loadTranslate(left, top, 0.0f);
1258 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001259 } else {
1260 mModelView.loadIdentity();
1261 }
Romain Guy86568192010-12-14 15:55:39 -08001262 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1263 if (!ignoreTransform) {
1264 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1265 if (mTrackDirtyRegions && dirty) {
1266 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1267 }
1268 } else {
1269 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1270 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1271 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001272}
1273
Romain Guyed6fcb02011-03-21 13:11:28 -07001274void OpenGLRenderer::setupDrawPointUniforms() {
1275 int slot = mCaches.currentProgram->getUniform("pointSize");
1276 glUniform1f(slot, mDescription.pointSize);
1277}
1278
Romain Guy70ca14e2010-12-13 18:24:33 -08001279void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001280 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001281 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1282 }
1283}
1284
Romain Guy86568192010-12-14 15:55:39 -08001285void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001286 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001287 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001288 }
1289}
1290
Romain Guy70ca14e2010-12-13 18:24:33 -08001291void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1292 if (mShader) {
1293 if (ignoreTransform) {
1294 mModelView.loadInverse(*mSnapshot->transform);
1295 }
1296 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1297 }
1298}
1299
Romain Guy8d0d4782010-12-14 20:13:35 -08001300void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1301 if (mShader) {
1302 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1303 }
1304}
1305
Romain Guy70ca14e2010-12-13 18:24:33 -08001306void OpenGLRenderer::setupDrawColorFilterUniforms() {
1307 if (mColorFilter) {
1308 mColorFilter->setupProgram(mCaches.currentProgram);
1309 }
1310}
1311
Romain Guy41210632012-07-16 17:04:24 -07001312void OpenGLRenderer::setupDrawTextGammaUniforms() {
1313 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1314}
1315
Romain Guy70ca14e2010-12-13 18:24:33 -08001316void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001317 bool force = mCaches.bindMeshBuffer();
1318 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001319 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001320}
1321
1322void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1323 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001324 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001325 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001326}
1327
Romain Guyaa6c24c2011-04-28 18:40:04 -07001328void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1329 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001330 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001331 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001332}
1333
Romain Guy8f0095c2011-05-02 17:24:22 -07001334void OpenGLRenderer::setupDrawTextureTransform() {
1335 mDescription.hasTextureTransform = true;
1336}
1337
1338void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001339 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1340 GL_FALSE, &transform.data[0]);
1341}
1342
Romain Guy70ca14e2010-12-13 18:24:33 -08001343void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001344 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001345 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001346 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001347 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001348 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001349 }
Romain Guyd71dd362011-12-12 19:03:35 -08001350
Romain Guyf3a910b42011-12-12 20:35:21 -08001351 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001352 if (mCaches.currentProgram->texCoords >= 0) {
1353 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1354 }
1355
1356 mCaches.unbindIndicesBuffer();
1357}
1358
1359void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1360 bool force = mCaches.unbindMeshBuffer();
1361 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1362 if (mCaches.currentProgram->texCoords >= 0) {
1363 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001364 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001365}
1366
Chet Haase5b0200b2011-04-13 17:58:08 -07001367void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001368 bool force = mCaches.unbindMeshBuffer();
1369 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1370 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001371 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001372}
1373
1374/**
1375 * 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 -07001376 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1377 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1378 * attributes (one per vertex) are values from zero to one that tells the fragment
1379 * shader where the fragment is in relation to the line width/length overall; these values are
1380 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1381 * region of the line.
1382 * Note that we only pass down the width values in this setup function. The length coordinates
1383 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001384 */
Chet Haase99585ad2011-05-02 15:00:16 -07001385void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001386 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001387 bool force = mCaches.unbindMeshBuffer();
1388 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1389 vertices, gAAVertexStride);
1390 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001391 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001392
Romain Guy7b631422012-04-04 11:38:54 -07001393 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001394 glEnableVertexAttribArray(widthSlot);
1395 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001396
Romain Guy7b631422012-04-04 11:38:54 -07001397 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001398 glEnableVertexAttribArray(lengthSlot);
1399 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001400
Chet Haase5b0200b2011-04-13 17:58:08 -07001401 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001402 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001403}
1404
1405void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1406 glDisableVertexAttribArray(widthSlot);
1407 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001408}
1409
Romain Guy70ca14e2010-12-13 18:24:33 -08001410void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001411}
1412
1413///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001414// Drawing
1415///////////////////////////////////////////////////////////////////////////////
1416
Chet Haase1271e2c2012-04-20 09:54:27 -07001417status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001418 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001419
Romain Guy0fe478e2010-11-08 12:08:41 -08001420 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1421 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001422 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001423 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001424 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001425
Romain Guy65549432012-03-26 16:45:05 -07001426 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001427}
1428
Chet Haaseed30fd82011-04-22 16:18:45 -07001429void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1430 if (displayList) {
1431 displayList->output(*this, level);
1432 }
1433}
1434
Romain Guya168d732011-03-18 16:50:13 -07001435void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1436 int alpha;
1437 SkXfermode::Mode mode;
1438 getAlphaAndMode(paint, &alpha, &mode);
1439
Romain Guya168d732011-03-18 16:50:13 -07001440 float x = left;
1441 float y = top;
1442
Romain Guye3c26852011-07-25 16:36:01 -07001443 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001444 bool ignoreTransform = false;
1445 if (mSnapshot->transform->isPureTranslate()) {
1446 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1447 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1448 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001449 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001450 } else {
1451 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001452 }
1453
1454 setupDraw();
1455 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001456 if (paint) {
1457 setupDrawAlpha8Color(paint->getColor(), alpha);
1458 }
Romain Guya168d732011-03-18 16:50:13 -07001459 setupDrawColorFilter();
1460 setupDrawShader();
1461 setupDrawBlending(true, mode);
1462 setupDrawProgram();
1463 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001464
Romain Guya168d732011-03-18 16:50:13 -07001465 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001466 texture->setWrap(GL_CLAMP_TO_EDGE);
1467 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001468
Romain Guya168d732011-03-18 16:50:13 -07001469 setupDrawPureColorUniforms();
1470 setupDrawColorFilterUniforms();
1471 setupDrawShaderUniforms();
1472 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1473
1474 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1475
1476 finishDrawTexture();
1477}
1478
Chet Haase48659092012-05-31 15:21:51 -07001479status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001480 const float right = left + bitmap->width();
1481 const float bottom = top + bitmap->height();
1482
1483 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001484 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001485 }
1486
Romain Guya1d3c912011-12-13 14:55:06 -08001487 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001488 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001489 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001490 const AutoTexture autoCleanup(texture);
1491
Romain Guy211370f2012-02-01 16:10:55 -08001492 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001493 drawAlphaBitmap(texture, left, top, paint);
1494 } else {
1495 drawTextureRect(left, top, right, bottom, texture, paint);
1496 }
Chet Haase48659092012-05-31 15:21:51 -07001497
1498 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001499}
1500
Chet Haase48659092012-05-31 15:21:51 -07001501status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001502 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1503 const mat4 transform(*matrix);
1504 transform.mapRect(r);
1505
Romain Guy6926c722010-07-12 20:20:03 -07001506 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001507 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001508 }
1509
Romain Guya1d3c912011-12-13 14:55:06 -08001510 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001511 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001512 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001513 const AutoTexture autoCleanup(texture);
1514
Romain Guy5b3b3522010-10-27 18:57:51 -07001515 // This could be done in a cheaper way, all we need is pass the matrix
1516 // to the vertex shader. The save/restore is a bit overkill.
1517 save(SkCanvas::kMatrix_SaveFlag);
1518 concatMatrix(matrix);
1519 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1520 restore();
Chet Haase48659092012-05-31 15:21:51 -07001521
1522 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07001523}
1524
Chet Haase48659092012-05-31 15:21:51 -07001525status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07001526 const float right = left + bitmap->width();
1527 const float bottom = top + bitmap->height();
1528
1529 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001530 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07001531 }
1532
1533 mCaches.activeTexture(0);
1534 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1535 const AutoTexture autoCleanup(texture);
1536
1537 drawTextureRect(left, top, right, bottom, texture, paint);
Chet Haase48659092012-05-31 15:21:51 -07001538
1539 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07001540}
1541
Chet Haase48659092012-05-31 15:21:51 -07001542status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08001543 float* vertices, int* colors, SkPaint* paint) {
1544 // TODO: Do a quickReject
1545 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07001546 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001547 }
1548
Romain Guya1d3c912011-12-13 14:55:06 -08001549 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001550 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001551 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08001552 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001553
Romain Guyd21b6e12011-11-30 20:21:23 -08001554 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1555 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001556
1557 int alpha;
1558 SkXfermode::Mode mode;
1559 getAlphaAndMode(paint, &alpha, &mode);
1560
Romain Guy5a7b4662011-01-20 19:09:30 -08001561 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001562
Romain Guyb18d2d02011-02-10 15:52:54 -08001563 float left = FLT_MAX;
1564 float top = FLT_MAX;
1565 float right = FLT_MIN;
1566 float bottom = FLT_MIN;
1567
Romain Guy211370f2012-02-01 16:10:55 -08001568 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001569
Romain Guya566b7c2011-01-23 16:36:11 -08001570 // TODO: Support the colors array
1571 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001572 TextureVertex* vertex = mesh;
1573 for (int32_t y = 0; y < meshHeight; y++) {
1574 for (int32_t x = 0; x < meshWidth; x++) {
1575 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1576
1577 float u1 = float(x) / meshWidth;
1578 float u2 = float(x + 1) / meshWidth;
1579 float v1 = float(y) / meshHeight;
1580 float v2 = float(y + 1) / meshHeight;
1581
1582 int ax = i + (meshWidth + 1) * 2;
1583 int ay = ax + 1;
1584 int bx = i;
1585 int by = bx + 1;
1586 int cx = i + 2;
1587 int cy = cx + 1;
1588 int dx = i + (meshWidth + 1) * 2 + 2;
1589 int dy = dx + 1;
1590
1591 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1592 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1593 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1594
1595 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1596 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1597 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001598
Romain Guyb18d2d02011-02-10 15:52:54 -08001599 if (hasActiveLayer) {
1600 // TODO: This could be optimized to avoid unnecessary ops
1601 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1602 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1603 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1604 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1605 }
Romain Guy5a7b4662011-01-20 19:09:30 -08001606 }
1607 }
1608
Romain Guyb18d2d02011-02-10 15:52:54 -08001609 if (hasActiveLayer) {
1610 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1611 }
Romain Guyb18d2d02011-02-10 15:52:54 -08001612
Romain Guy5a7b4662011-01-20 19:09:30 -08001613 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1614 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001615 GL_TRIANGLES, count, false, false, 0, false, false);
Chet Haase48659092012-05-31 15:21:51 -07001616
1617 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08001618}
1619
Chet Haase48659092012-05-31 15:21:51 -07001620status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07001621 float srcLeft, float srcTop, float srcRight, float srcBottom,
1622 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001623 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001624 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001625 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001626 }
1627
Romain Guya1d3c912011-12-13 14:55:06 -08001628 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001629 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001630 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001631 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001632
Romain Guy8ba548f2010-06-30 19:21:21 -07001633 const float width = texture->width;
1634 const float height = texture->height;
1635
Romain Guy68169722011-08-22 17:33:33 -07001636 const float u1 = fmax(0.0f, srcLeft / width);
1637 const float v1 = fmax(0.0f, srcTop / height);
1638 const float u2 = fmin(1.0f, srcRight / width);
1639 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001640
Romain Guy03750a02010-10-18 14:06:08 -07001641 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001642 resetDrawTextureTexCoords(u1, v1, u2, v2);
1643
Romain Guy03750a02010-10-18 14:06:08 -07001644 int alpha;
1645 SkXfermode::Mode mode;
1646 getAlphaAndMode(paint, &alpha, &mode);
1647
Romain Guyd21b6e12011-11-30 20:21:23 -08001648 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1649
Romain Guy211370f2012-02-01 16:10:55 -08001650 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001651 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1652 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1653
Romain Guyb5014982011-07-28 15:39:12 -07001654 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001655 // Enable linear filtering if the source rectangle is scaled
1656 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001657 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001658 }
Romain Guyb5014982011-07-28 15:39:12 -07001659
Romain Guyd21b6e12011-11-30 20:21:23 -08001660 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001661 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1662 texture->id, alpha / 255.0f, mode, texture->blend,
1663 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1664 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1665 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001666 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001667 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1668 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1669 GL_TRIANGLE_STRIP, gMeshCount);
1670 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001671
1672 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07001673
1674 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07001675}
1676
Chet Haase48659092012-05-31 15:21:51 -07001677status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001678 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001679 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07001680 int alpha;
1681 SkXfermode::Mode mode;
1682 getAlphaAndModeDirect(paint, &alpha, &mode);
1683
1684 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
1685 left, top, right, bottom, alpha, mode);
1686}
1687
1688status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
1689 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
1690 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07001691 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001692 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001693 }
1694
Romain Guybe6f9dc2012-07-16 12:41:17 -07001695 alpha *= mSnapshot->alpha;
1696
Romain Guya1d3c912011-12-13 14:55:06 -08001697 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001698 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001699 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001700 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001701 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1702 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001703
Romain Guy2728f962010-10-08 18:36:15 -07001704 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001705 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001706
Romain Guy211370f2012-02-01 16:10:55 -08001707 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001708 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07001709 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001710 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001711 const float offsetX = left + mSnapshot->transform->getTranslateX();
1712 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001713 const size_t count = mesh->quads.size();
1714 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001715 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001716 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001717 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1718 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1719 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001720 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001721 dirtyLayer(left + bounds.left, top + bounds.top,
1722 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001723 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001724 }
1725 }
1726
Romain Guy211370f2012-02-01 16:10:55 -08001727 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001728 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1729 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1730
1731 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1732 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1733 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1734 true, !mesh->hasEmptyQuads);
1735 } else {
1736 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1737 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1738 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1739 true, !mesh->hasEmptyQuads);
1740 }
Romain Guy054dc182010-10-15 17:55:25 -07001741 }
Chet Haase48659092012-05-31 15:21:51 -07001742
1743 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07001744}
1745
Chet Haase99ecdc42011-05-06 12:06:34 -07001746/**
Chet Haase858aa932011-05-12 09:06:00 -07001747 * This function uses a similar approach to that of AA lines in the drawLines() function.
Chris Craik6ebdc112012-08-31 18:24:33 -07001748 * We expand the rectangle by a half pixel in screen space on all sides. However, instead of using
1749 * a fragment shader to compute the translucency of the color from its position, we simply use a
1750 * varying parameter to define how far a given pixel is into the region.
Chet Haase858aa932011-05-12 09:06:00 -07001751 */
1752void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001753 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001754 float inverseScaleX = 1.0f;
1755 float inverseScaleY = 1.0f;
Romain Guy04299382012-07-18 17:15:41 -07001756
Chet Haase858aa932011-05-12 09:06:00 -07001757 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001758 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001759 Matrix4 *mat = mSnapshot->transform;
1760 float m00 = mat->data[Matrix4::kScaleX];
1761 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase858aa932011-05-12 09:06:00 -07001762 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001763 float m11 = mat->data[Matrix4::kScaleY];
Chet Haase858aa932011-05-12 09:06:00 -07001764 float scaleX = sqrt(m00 * m00 + m01 * m01);
1765 float scaleY = sqrt(m10 * m10 + m11 * m11);
1766 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1767 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1768 }
1769
Chet Haase858aa932011-05-12 09:06:00 -07001770 float boundarySizeX = .5 * inverseScaleX;
1771 float boundarySizeY = .5 * inverseScaleY;
1772
Chris Craik6ebdc112012-08-31 18:24:33 -07001773 float innerLeft = left + boundarySizeX;
1774 float innerRight = right - boundarySizeX;
1775 float innerTop = top + boundarySizeY;
1776 float innerBottom = bottom - boundarySizeY;
1777
Chet Haase858aa932011-05-12 09:06:00 -07001778 // Adjust the rect by the AA boundary padding
1779 left -= boundarySizeX;
1780 right += boundarySizeX;
1781 top -= boundarySizeY;
1782 bottom += boundarySizeY;
1783
Chet Haase858aa932011-05-12 09:06:00 -07001784 if (!quickReject(left, top, right, bottom)) {
Romain Guy04299382012-07-18 17:15:41 -07001785 setupDraw();
1786 setupDrawNoTexture();
Chris Craik6ebdc112012-08-31 18:24:33 -07001787 setupDrawAARect();
Romain Guy04299382012-07-18 17:15:41 -07001788 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
1789 setupDrawColorFilter();
1790 setupDrawShader();
1791 setupDrawBlending(true, mode);
1792 setupDrawProgram();
1793 setupDrawModelViewIdentity(true);
1794 setupDrawColorUniforms();
1795 setupDrawColorFilterUniforms();
1796 setupDrawShaderIdentityUniforms();
1797
Chris Craik6ebdc112012-08-31 18:24:33 -07001798 AlphaVertex rects[14];
1799 AlphaVertex* aVertices = &rects[0];
1800 void* alphaCoords = ((GLbyte*) aVertices) + gVertexAlphaOffset;
Romain Guy04299382012-07-18 17:15:41 -07001801
Chris Craik6ebdc112012-08-31 18:24:33 -07001802 bool force = mCaches.unbindMeshBuffer();
1803 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1804 aVertices, gAlphaVertexStride);
1805 mCaches.resetTexCoordsVertexPointer();
1806 mCaches.unbindIndicesBuffer();
Romain Guy04299382012-07-18 17:15:41 -07001807
Chris Craik6ebdc112012-08-31 18:24:33 -07001808 int alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
1809 glEnableVertexAttribArray(alphaSlot);
1810 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Romain Guy04299382012-07-18 17:15:41 -07001811
Chris Craik6ebdc112012-08-31 18:24:33 -07001812 // draw left
1813 AlphaVertex::set(aVertices++, left, bottom, 0);
1814 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1815 AlphaVertex::set(aVertices++, left, top, 0);
1816 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001817
Chris Craik6ebdc112012-08-31 18:24:33 -07001818 // draw top
1819 AlphaVertex::set(aVertices++, right, top, 0);
1820 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
Romain Guy04299382012-07-18 17:15:41 -07001821
Chris Craik6ebdc112012-08-31 18:24:33 -07001822 // draw right
1823 AlphaVertex::set(aVertices++, right, bottom, 0);
1824 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1825
1826 // draw bottom
1827 AlphaVertex::set(aVertices++, left, bottom, 0);
1828 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1829
1830 // draw inner rect (repeating last vertex to create degenerate bridge triangles)
1831 // TODO: also consider drawing the inner rect without the blending-forced shader, if
1832 // blending is expensive. Note: can't use drawColorRect() since it doesn't use vertex
1833 // buffers like below, resulting in slightly different transformed coordinates.
1834 AlphaVertex::set(aVertices++, innerLeft, innerBottom, 1);
1835 AlphaVertex::set(aVertices++, innerLeft, innerTop, 1);
1836 AlphaVertex::set(aVertices++, innerRight, innerBottom, 1);
1837 AlphaVertex::set(aVertices++, innerRight, innerTop, 1);
1838
Chet Haase858aa932011-05-12 09:06:00 -07001839 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guy7b631422012-04-04 11:38:54 -07001840
Chris Craik6ebdc112012-08-31 18:24:33 -07001841 glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);
Romain Guy04299382012-07-18 17:15:41 -07001842
Chris Craik6ebdc112012-08-31 18:24:33 -07001843 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07001844 }
Chet Haase858aa932011-05-12 09:06:00 -07001845}
1846
1847/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001848 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1849 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1850 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1851 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1852 * of the line. Hairlines are more involved because we need to account for transform scaling
1853 * to end up with a one-pixel-wide line in screen space..
1854 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1855 * in combination with values that we calculate and pass down in this method. The basic approach
1856 * is that the quad we create contains both the core line area plus a bounding area in which
1857 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1858 * proportion of the width and the length of a given segment is represented by the boundary
1859 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1860 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1861 * on the inside). This ends up giving the result we want, with pixels that are completely
1862 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1863 * how far into the boundary region they are, which is determined by shader interpolation.
1864 */
Chet Haase48659092012-05-31 15:21:51 -07001865status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1866 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07001867
1868 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001869 // We use half the stroke width here because we're going to position the quad
1870 // corner vertices half of the width away from the line endpoints
1871 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001872 // A stroke width of 0 has a special meaning in Skia:
1873 // it draws a line 1 px wide regardless of current transform
1874 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001875
Chet Haase99ecdc42011-05-06 12:06:34 -07001876 float inverseScaleX = 1.0f;
1877 float inverseScaleY = 1.0f;
1878 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001879
Chet Haase8a5cc922011-04-26 07:28:09 -07001880 int alpha;
1881 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001882
Chet Haase8a5cc922011-04-26 07:28:09 -07001883 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001884 int verticesCount = count;
1885 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001886 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 verticesCount += (count - 4);
1888 }
1889
1890 if (isHairLine || isAA) {
1891 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1892 // the line on the screen should always be one pixel wide regardless of scale. For
1893 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001894 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001895 Matrix4 *mat = mSnapshot->transform;
1896 float m00 = mat->data[Matrix4::kScaleX];
1897 float m01 = mat->data[Matrix4::kSkewY];
Chet Haase99ecdc42011-05-06 12:06:34 -07001898 float m10 = mat->data[Matrix4::kSkewX];
Chris Craik9147cd42012-09-06 16:44:51 -07001899 float m11 = mat->data[Matrix4::kScaleY];
Romain Guy7b631422012-04-04 11:38:54 -07001900
Romain Guy211370f2012-02-01 16:10:55 -08001901 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1902 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001903
Chet Haase99ecdc42011-05-06 12:06:34 -07001904 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1905 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001906
Chet Haase99ecdc42011-05-06 12:06:34 -07001907 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1908 scaled = true;
1909 }
1910 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001912
1913 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy04299382012-07-18 17:15:41 -07001914
1915 mCaches.enableScissor();
1916
Chet Haase8a5cc922011-04-26 07:28:09 -07001917 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001918 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001919 if (isAA) {
1920 setupDrawAALine();
1921 }
1922 setupDrawColor(paint->getColor(), alpha);
1923 setupDrawColorFilter();
1924 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001925 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001926 setupDrawProgram();
1927 setupDrawModelViewIdentity(true);
1928 setupDrawColorUniforms();
1929 setupDrawColorFilterUniforms();
1930 setupDrawShaderIdentityUniforms();
1931
1932 if (isHairLine) {
1933 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001934 halfStrokeWidth = isAA? 1 : .5;
1935 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001936 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001937 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001938 }
Romain Guy7b631422012-04-04 11:38:54 -07001939
1940 int widthSlot;
1941 int lengthSlot;
1942
Chet Haase5b0200b2011-04-13 17:58:08 -07001943 Vertex lines[verticesCount];
1944 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001945
Chet Haase99585ad2011-05-02 15:00:16 -07001946 AAVertex wLines[verticesCount];
1947 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001948
Romain Guy211370f2012-02-01 16:10:55 -08001949 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001950 setupDrawVertices(vertices);
1951 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001952 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1953 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001954 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001955 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1956 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001957 // We will need to calculate the actual width proportion on each segment for
1958 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
Chris Craik8f5ad762012-09-04 14:35:19 -07001959 float boundaryWidthProportion = .5 - 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001960 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1961 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001962 }
1963
Chet Haase99ecdc42011-05-06 12:06:34 -07001964 AAVertex* prevAAVertex = NULL;
1965 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001966
Chet Haase99585ad2011-05-02 15:00:16 -07001967 int boundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001968 int boundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001969
Chet Haase5b0200b2011-04-13 17:58:08 -07001970 for (int i = 0; i < count; i += 4) {
1971 // a = start point, b = end point
1972 vec2 a(points[i], points[i + 1]);
1973 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001974
Chet Haase99585ad2011-05-02 15:00:16 -07001975 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001976 float boundaryLengthProportion = 0;
1977 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001978
Chet Haase5b0200b2011-04-13 17:58:08 -07001979 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001980 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chris Craik75040f82012-09-07 13:56:43 -07001981 float x = n.x;
1982 n.x = -n.y;
1983 n.y = x;
1984
Chet Haase8a5cc922011-04-26 07:28:09 -07001985 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001986 if (isAA) {
1987 float wideningFactor;
1988 if (fabs(n.x) >= fabs(n.y)) {
1989 wideningFactor = fabs(1.0f / n.x);
1990 } else {
1991 wideningFactor = fabs(1.0f / n.y);
1992 }
1993 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001994 }
Romain Guy7b631422012-04-04 11:38:54 -07001995
Chet Haase99ecdc42011-05-06 12:06:34 -07001996 if (scaled) {
1997 n.x *= inverseScaleX;
1998 n.y *= inverseScaleY;
1999 }
2000 } else if (scaled) {
2001 // Extend n by .5 pixel on each side, post-transform
2002 vec2 extendedN = n.copyNormalized();
2003 extendedN /= 2;
2004 extendedN.x *= inverseScaleX;
2005 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07002006
Chet Haase99ecdc42011-05-06 12:06:34 -07002007 float extendedNLength = extendedN.length();
2008 // We need to set this value on the shader prior to drawing
Chris Craik75040f82012-09-07 13:56:43 -07002009 boundaryWidthProportion = .5 - extendedNLength / (halfStrokeWidth + extendedNLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002010 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07002011 }
Romain Guy7b631422012-04-04 11:38:54 -07002012
Chet Haase99585ad2011-05-02 15:00:16 -07002013 // aa lines expand the endpoint vertices to encompass the AA boundary
2014 if (isAA) {
2015 vec2 abVector = (b - a);
2016 length = abVector.length();
2017 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07002018
Chet Haase99ecdc42011-05-06 12:06:34 -07002019 if (scaled) {
2020 abVector.x *= inverseScaleX;
2021 abVector.y *= inverseScaleY;
2022 float abLength = abVector.length();
Chris Craik8f5ad762012-09-04 14:35:19 -07002023 boundaryLengthProportion = .5 - abLength / (length + abLength);
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 } else {
Chris Craik8f5ad762012-09-04 14:35:19 -07002025 boundaryLengthProportion = .5 - .5 / (length + 1);
Chet Haase99ecdc42011-05-06 12:06:34 -07002026 }
Romain Guy7b631422012-04-04 11:38:54 -07002027
Chet Haase99ecdc42011-05-06 12:06:34 -07002028 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07002029 a -= abVector;
2030 b += abVector;
2031 }
2032
Chet Haase5b0200b2011-04-13 17:58:08 -07002033 // Four corners of the rectangle defining a thick line
2034 vec2 p1 = a - n;
2035 vec2 p2 = a + n;
2036 vec2 p3 = b + n;
2037 vec2 p4 = b - n;
2038
Chet Haase99585ad2011-05-02 15:00:16 -07002039
Chet Haase5b0200b2011-04-13 17:58:08 -07002040 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2041 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2042 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2043 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2044
Romain Guy04299382012-07-18 17:15:41 -07002045 if (!quickRejectNoScissor(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002046 if (!isAA) {
2047 if (prevVertex != NULL) {
2048 // Issue two repeat vertices to create degenerate triangles to bridge
2049 // between the previous line and the new one. This is necessary because
2050 // we are creating a single triangle_strip which will contain
2051 // potentially discontinuous line segments.
2052 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2053 Vertex::set(vertices++, p1.x, p1.y);
2054 generatedVerticesCount += 2;
2055 }
Romain Guy7b631422012-04-04 11:38:54 -07002056
Chet Haase5b0200b2011-04-13 17:58:08 -07002057 Vertex::set(vertices++, p1.x, p1.y);
2058 Vertex::set(vertices++, p2.x, p2.y);
2059 Vertex::set(vertices++, p4.x, p4.y);
2060 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002061
Chet Haase5b0200b2011-04-13 17:58:08 -07002062 prevVertex = vertices - 1;
2063 generatedVerticesCount += 4;
2064 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002065 if (!isHairLine && scaled) {
2066 // Must set width proportions per-segment for scaled non-hairlines to use the
2067 // correct AA boundary dimensions
2068 if (boundaryWidthSlot < 0) {
2069 boundaryWidthSlot =
2070 mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07002071 }
Romain Guy7b631422012-04-04 11:38:54 -07002072
Chet Haase99ecdc42011-05-06 12:06:34 -07002073 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99ecdc42011-05-06 12:06:34 -07002074 }
Romain Guy7b631422012-04-04 11:38:54 -07002075
Chet Haase99585ad2011-05-02 15:00:16 -07002076 if (boundaryLengthSlot < 0) {
2077 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
Chet Haase99585ad2011-05-02 15:00:16 -07002078 }
Romain Guy7b631422012-04-04 11:38:54 -07002079
Chet Haase99ecdc42011-05-06 12:06:34 -07002080 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07002081
Chet Haase5b0200b2011-04-13 17:58:08 -07002082 if (prevAAVertex != NULL) {
2083 // Issue two repeat vertices to create degenerate triangles to bridge
2084 // between the previous line and the new one. This is necessary because
2085 // we are creating a single triangle_strip which will contain
2086 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002087 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2088 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2089 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002090 generatedVerticesCount += 2;
2091 }
Romain Guy7b631422012-04-04 11:38:54 -07002092
Chet Haase99585ad2011-05-02 15:00:16 -07002093 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2094 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2095 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2096 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002097
Chet Haase5b0200b2011-04-13 17:58:08 -07002098 prevAAVertex = aaVertices - 1;
2099 generatedVerticesCount += 4;
2100 }
Romain Guy7b631422012-04-04 11:38:54 -07002101
Chet Haase99585ad2011-05-02 15:00:16 -07002102 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2103 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2104 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002105 }
2106 }
Romain Guy7b631422012-04-04 11:38:54 -07002107
Chet Haase5b0200b2011-04-13 17:58:08 -07002108 if (generatedVerticesCount > 0) {
2109 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2110 }
Romain Guy7b631422012-04-04 11:38:54 -07002111
2112 if (isAA) {
2113 finishDrawAALine(widthSlot, lengthSlot);
2114 }
Chet Haase48659092012-05-31 15:21:51 -07002115
2116 return DrawGlInfo::kStatusDrew;
Chet Haase5b0200b2011-04-13 17:58:08 -07002117}
2118
Chet Haase48659092012-05-31 15:21:51 -07002119status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2120 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002121
2122 // TODO: The paint's cap style defines whether the points are square or circular
2123 // TODO: Handle AA for round points
2124
Chet Haase5b0200b2011-04-13 17:58:08 -07002125 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002126 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002127 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002128 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002129 if (isHairLine) {
2130 // Now that we know it's hairline, we can set the effective width, to be used later
2131 strokeWidth = 1.0f;
2132 }
2133 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002134 int alpha;
2135 SkXfermode::Mode mode;
2136 getAlphaAndMode(paint, &alpha, &mode);
2137
2138 int verticesCount = count >> 1;
2139 int generatedVerticesCount = 0;
2140
2141 TextureVertex pointsData[verticesCount];
2142 TextureVertex* vertex = &pointsData[0];
2143
Romain Guy04299382012-07-18 17:15:41 -07002144 // TODO: We should optimize this method to not generate vertices for points
2145 // that lie outside of the clip.
2146 mCaches.enableScissor();
2147
Romain Guyed6fcb02011-03-21 13:11:28 -07002148 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002149 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002150 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002151 setupDrawColor(paint->getColor(), alpha);
2152 setupDrawColorFilter();
2153 setupDrawShader();
2154 setupDrawBlending(mode);
2155 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002156 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002157 setupDrawColorUniforms();
2158 setupDrawColorFilterUniforms();
2159 setupDrawPointUniforms();
2160 setupDrawShaderIdentityUniforms();
2161 setupDrawMesh(vertex);
2162
2163 for (int i = 0; i < count; i += 2) {
2164 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2165 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002166
Chet Haase8a5cc922011-04-26 07:28:09 -07002167 float left = points[i] - halfWidth;
2168 float right = points[i] + halfWidth;
2169 float top = points[i + 1] - halfWidth;
2170 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002171
Chet Haase8a5cc922011-04-26 07:28:09 -07002172 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002173 }
2174
2175 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002176
2177 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002178}
2179
Chet Haase48659092012-05-31 15:21:51 -07002180status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002181 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002182 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002183
Romain Guyae88e5e2010-10-22 17:49:18 -07002184 Rect& clip(*mSnapshot->clipRect);
2185 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002186
Romain Guy3d58c032010-07-14 16:34:53 -07002187 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002188
2189 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002190}
Romain Guy9d5316e2010-06-24 19:30:36 -07002191
Chet Haase48659092012-05-31 15:21:51 -07002192status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2193 SkPaint* paint) {
2194 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002195 const AutoTexture autoCleanup(texture);
2196
2197 const float x = left + texture->left - texture->offset;
2198 const float y = top + texture->top - texture->offset;
2199
2200 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002201
2202 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002203}
2204
Chet Haase48659092012-05-31 15:21:51 -07002205status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002206 float rx, float ry, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002207 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002208
Romain Guya1d3c912011-12-13 14:55:06 -08002209 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002210 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2211 right - left, bottom - top, rx, ry, paint);
Chet Haase48659092012-05-31 15:21:51 -07002212 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002213}
2214
Chet Haase48659092012-05-31 15:21:51 -07002215status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2216 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002217
Romain Guya1d3c912011-12-13 14:55:06 -08002218 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002219 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Chet Haase48659092012-05-31 15:21:51 -07002220 return drawShape(x - radius, y - radius, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002221}
Romain Guy01d58e42011-01-19 21:54:02 -08002222
Chet Haase48659092012-05-31 15:21:51 -07002223status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
2224 SkPaint* paint) {
2225 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002226
Romain Guya1d3c912011-12-13 14:55:06 -08002227 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002228 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002229 return drawShape(left, top, texture, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002230}
2231
Chet Haase48659092012-05-31 15:21:51 -07002232status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -08002233 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002234 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002235
2236 if (fabs(sweepAngle) >= 360.0f) {
Chet Haase48659092012-05-31 15:21:51 -07002237 return drawOval(left, top, right, bottom, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002238 }
2239
Romain Guya1d3c912011-12-13 14:55:06 -08002240 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002241 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2242 startAngle, sweepAngle, useCenter, paint);
Chet Haase48659092012-05-31 15:21:51 -07002243 return drawShape(left, top, texture, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -08002244}
2245
Chet Haase48659092012-05-31 15:21:51 -07002246status_t OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002247 SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002248 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002249
Romain Guya1d3c912011-12-13 14:55:06 -08002250 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002251 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
Chet Haase48659092012-05-31 15:21:51 -07002252 return drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002253}
2254
Chet Haase48659092012-05-31 15:21:51 -07002255status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002256 if (p->getStyle() != SkPaint::kFill_Style) {
Chet Haase48659092012-05-31 15:21:51 -07002257 return drawRectAsShape(left, top, right, bottom, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002258 }
2259
Romain Guy6926c722010-07-12 20:20:03 -07002260 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002261 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002262 }
2263
Romain Guy026c5e162010-06-28 17:12:22 -07002264 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002265 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002266 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2267 if (!isMode) {
2268 // Assume SRC_OVER
2269 mode = SkXfermode::kSrcOver_Mode;
2270 }
2271 } else {
2272 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002273 }
2274
Romain Guy026c5e162010-06-28 17:12:22 -07002275 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002276 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002277 drawAARect(left, top, right, bottom, color, mode);
2278 } else {
2279 drawColorRect(left, top, right, bottom, color, mode);
2280 }
Chet Haase48659092012-05-31 15:21:51 -07002281
2282 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002283}
Romain Guy9d5316e2010-06-24 19:30:36 -07002284
Raph Levien416a8472012-07-19 22:48:17 -07002285void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2286 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2287 float x, float y) {
2288 mCaches.activeTexture(0);
2289
2290 // NOTE: The drop shadow will not perform gamma correction
2291 // if shader-based correction is enabled
2292 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2293 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2294 paint, text, bytesCount, count, mShadowRadius, positions);
2295 const AutoTexture autoCleanup(shadow);
2296
2297 const float sx = x - shadow->left + mShadowDx;
2298 const float sy = y - shadow->top + mShadowDy;
2299
2300 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2301 int shadowColor = mShadowColor;
2302 if (mShader) {
2303 shadowColor = 0xffffffff;
2304 }
2305
2306 setupDraw();
2307 setupDrawWithTexture(true);
2308 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2309 setupDrawColorFilter();
2310 setupDrawShader();
2311 setupDrawBlending(true, mode);
2312 setupDrawProgram();
2313 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2314 setupDrawTexture(shadow->id);
2315 setupDrawPureColorUniforms();
2316 setupDrawColorFilterUniforms();
2317 setupDrawShaderUniforms();
2318 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2319
2320 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2321}
2322
Chet Haase48659092012-05-31 15:21:51 -07002323status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002324 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002325 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002326 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002327 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002328 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002329
Romain Guy671d6cf2012-01-18 12:39:17 -08002330 // NOTE: Skia does not support perspective transform on drawPosText yet
2331 if (!mSnapshot->transform->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002332 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002333 }
2334
2335 float x = 0.0f;
2336 float y = 0.0f;
2337 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2338 if (pureTranslate) {
2339 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2340 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2341 }
2342
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002343 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002344 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2345 paint->getTextSize());
2346
2347 int alpha;
2348 SkXfermode::Mode mode;
2349 getAlphaAndMode(paint, &alpha, &mode);
2350
Raph Levien416a8472012-07-19 22:48:17 -07002351 if (CC_UNLIKELY(mHasShadow)) {
2352 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2353 0.0f, 0.0f);
2354 }
2355
Romain Guy671d6cf2012-01-18 12:39:17 -08002356 // Pick the appropriate texture filtering
2357 bool linearFilter = mSnapshot->transform->changesBounds();
2358 if (pureTranslate && !linearFilter) {
2359 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2360 }
2361
2362 mCaches.activeTexture(0);
2363 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002364 setupDrawTextGamma(paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002365 setupDrawDirtyRegionsDisabled();
2366 setupDrawWithTexture(true);
2367 setupDrawAlpha8Color(paint->getColor(), alpha);
2368 setupDrawColorFilter();
2369 setupDrawShader();
2370 setupDrawBlending(true, mode);
2371 setupDrawProgram();
2372 setupDrawModelView(x, y, x, y, pureTranslate, true);
2373 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2374 setupDrawPureColorUniforms();
2375 setupDrawColorFilterUniforms();
2376 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002377 setupDrawTextGammaUniforms();
Romain Guy671d6cf2012-01-18 12:39:17 -08002378
2379 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2380 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2381
Romain Guy211370f2012-02-01 16:10:55 -08002382 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002383
2384 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2385 positions, hasActiveLayer ? &bounds : NULL)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002386 if (hasActiveLayer) {
2387 if (!pureTranslate) {
2388 mSnapshot->transform->mapRect(bounds);
2389 }
2390 dirtyLayerUnchecked(bounds, getRegion());
2391 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002392 }
Chet Haase48659092012-05-31 15:21:51 -07002393
2394 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002395}
2396
Romain Guyc2525952012-07-27 16:41:22 -07002397status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002398 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002399 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
Romain Guy9c0b1882012-07-13 12:13:07 -07002400 (paint->getAlpha() * mSnapshot->alpha == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002401 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002402 }
2403
Chet Haasea1cff502012-02-21 13:43:44 -08002404 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002405 switch (paint->getTextAlign()) {
2406 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002407 x -= length / 2.0f;
2408 break;
2409 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002410 x -= length;
2411 break;
2412 default:
2413 break;
2414 }
2415
Romain Guycac5fd32011-12-01 20:08:50 -08002416 SkPaint::FontMetrics metrics;
2417 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002418 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002419 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002420 }
2421
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002422 const float oldX = x;
2423 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002424 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002425 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002426 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2427 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2428 }
2429
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002430#if DEBUG_GLYPHS
Romain Guyc2525952012-07-27 16:41:22 -07002431 ALOGD("OpenGLRenderer drawText() with FontID=%d",
Raph Levien996e57c2012-07-23 15:22:52 -07002432 SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002433#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002434
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002435 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002436 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002437 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002438
Romain Guy86568192010-12-14 15:55:39 -08002439 int alpha;
2440 SkXfermode::Mode mode;
2441 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002442
Romain Guy211370f2012-02-01 16:10:55 -08002443 if (CC_UNLIKELY(mHasShadow)) {
Raph Levien996e57c2012-07-23 15:22:52 -07002444 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, alpha, mode,
2445 oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002446 }
2447
Romain Guy6620c6d2010-12-06 18:07:02 -08002448 // Pick the appropriate texture filtering
2449 bool linearFilter = mSnapshot->transform->changesBounds();
2450 if (pureTranslate && !linearFilter) {
2451 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2452 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002453
Romain Guy16c88082012-06-11 16:03:47 -07002454 // The font renderer will always use texture unit 0
Romain Guya1d3c912011-12-13 14:55:06 -08002455 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002456 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002457 setupDrawTextGamma(paint);
Romain Guy86568192010-12-14 15:55:39 -08002458 setupDrawDirtyRegionsDisabled();
2459 setupDrawWithTexture(true);
2460 setupDrawAlpha8Color(paint->getColor(), alpha);
2461 setupDrawColorFilter();
2462 setupDrawShader();
2463 setupDrawBlending(true, mode);
2464 setupDrawProgram();
2465 setupDrawModelView(x, y, x, y, pureTranslate, true);
Romain Guy16c88082012-06-11 16:03:47 -07002466 // See comment above; the font renderer must use texture unit 0
2467 // assert(mTextureUnit == 0)
Romain Guy86568192010-12-14 15:55:39 -08002468 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2469 setupDrawPureColorUniforms();
2470 setupDrawColorFilterUniforms();
2471 setupDrawShaderUniforms(pureTranslate);
Romain Guy41210632012-07-16 17:04:24 -07002472 setupDrawTextGammaUniforms();
Romain Guy06f96e22010-07-30 19:18:16 -07002473
Romain Guy6620c6d2010-12-06 18:07:02 -08002474 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002475 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2476
Romain Guy211370f2012-02-01 16:10:55 -08002477 const bool hasActiveLayer = hasLayer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002478
Raph Levien996e57c2012-07-23 15:22:52 -07002479 bool status;
Raph Levien8b4072d2012-07-30 15:50:00 -07002480 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
2481 SkPaint paintCopy(*paint);
2482 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2483 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Raph Levien996e57c2012-07-23 15:22:52 -07002484 positions, hasActiveLayer ? &bounds : NULL);
2485 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002486 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2487 positions, hasActiveLayer ? &bounds : NULL);
Raph Levien996e57c2012-07-23 15:22:52 -07002488 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002489
2490 if (status && hasActiveLayer) {
2491 if (!pureTranslate) {
2492 mSnapshot->transform->mapRect(bounds);
Romain Guy5b3b3522010-10-27 18:57:51 -07002493 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002494 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002495 }
Romain Guy694b5192010-07-21 21:33:20 -07002496
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002497 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002498
2499 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002500}
2501
Chet Haase48659092012-05-31 15:21:51 -07002502status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002503 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002504 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2505 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Chet Haase48659092012-05-31 15:21:51 -07002506 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002507 }
2508
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002509 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002510 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2511 paint->getTextSize());
2512
2513 int alpha;
2514 SkXfermode::Mode mode;
2515 getAlphaAndMode(paint, &alpha, &mode);
2516
2517 mCaches.activeTexture(0);
2518 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002519 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002520 setupDrawDirtyRegionsDisabled();
2521 setupDrawWithTexture(true);
2522 setupDrawAlpha8Color(paint->getColor(), alpha);
2523 setupDrawColorFilter();
2524 setupDrawShader();
2525 setupDrawBlending(true, mode);
2526 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002527 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002528 setupDrawTexture(fontRenderer.getTexture(true));
2529 setupDrawPureColorUniforms();
2530 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002531 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002532 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002533
Romain Guy97771732012-02-28 18:17:02 -08002534 const Rect* clip = &mSnapshot->getLocalClip();
2535 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 -08002536
Romain Guy97771732012-02-28 18:17:02 -08002537 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002538
2539 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2540 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002541 if (hasActiveLayer) {
2542 mSnapshot->transform->mapRect(bounds);
2543 dirtyLayerUnchecked(bounds, getRegion());
2544 }
Romain Guy97771732012-02-28 18:17:02 -08002545 }
Chet Haase48659092012-05-31 15:21:51 -07002546
2547 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002548}
2549
Chet Haase48659092012-05-31 15:21:51 -07002550status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2551 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002552
Romain Guya1d3c912011-12-13 14:55:06 -08002553 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002554
Romain Guy33f6beb2012-02-16 19:24:51 -08002555 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002556 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002557 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002558 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002559
Romain Guy8b55f372010-08-18 17:10:07 -07002560 const float x = texture->left - texture->offset;
2561 const float y = texture->top - texture->offset;
2562
Romain Guy01d58e42011-01-19 21:54:02 -08002563 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002564
2565 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002566}
2567
Chet Haase48659092012-05-31 15:21:51 -07002568status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guyada830f2011-01-13 12:13:20 -08002569 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Chet Haase48659092012-05-31 15:21:51 -07002570 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002571 }
2572
Romain Guy4ff0cf42012-08-06 14:51:10 -07002573 bool debugLayerUpdate = false;
2574
Romain Guy2bf68f02012-03-02 13:37:47 -08002575 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2576 OpenGLRenderer* renderer = layer->renderer;
2577 Rect& dirty = layer->dirtyRect;
2578
2579 interrupt();
2580 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2581 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002582 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002583 renderer->finish();
2584 resume();
2585
2586 dirty.setEmpty();
2587 layer->deferredUpdateScheduled = false;
2588 layer->renderer = NULL;
2589 layer->displayList = NULL;
Romain Guy4ff0cf42012-08-06 14:51:10 -07002590
2591 debugLayerUpdate = mCaches.debugLayersUpdates;
Romain Guy2bf68f02012-03-02 13:37:47 -08002592 }
2593
Romain Guya1d3c912011-12-13 14:55:06 -08002594 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002595
Romain Guy211370f2012-02-01 16:10:55 -08002596 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002597 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002598 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002599 } else if (layer->mesh) {
Chet Haased15ebf22012-09-05 11:40:29 -07002600 const float a = layer->getAlpha() / 255.0f;
2601 SkiaColorFilter *oldFilter = mColorFilter;
2602 mColorFilter = layer->getColorFilter();
Romain Guyf219da52011-01-16 12:54:25 -08002603
Romain Guyc88e3572011-01-22 00:32:12 -08002604 setupDraw();
2605 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002606 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002607 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002608 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002609 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002610 setupDrawPureColorUniforms();
2611 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002612 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002613 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy4ff0cf42012-08-06 14:51:10 -07002614 int tx = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2615 int ty = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002616
Romain Guyd21b6e12011-11-30 20:21:23 -08002617 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002618 setupDrawModelViewTranslate(tx, ty,
2619 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002620 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002621 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002622 setupDrawModelViewTranslate(x, y,
2623 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2624 }
Romain Guyc88e3572011-01-22 00:32:12 -08002625 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002626
Romain Guyc88e3572011-01-22 00:32:12 -08002627 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2628 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002629
Romain Guyc88e3572011-01-22 00:32:12 -08002630 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002631
Chet Haased15ebf22012-09-05 11:40:29 -07002632 mColorFilter = oldFilter;
2633
Romain Guy3a3133d2011-02-01 22:59:58 -08002634#if DEBUG_LAYERS_AS_REGIONS
2635 drawRegionRects(layer->region);
2636#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002637 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002638
2639 if (debugLayerUpdate) {
2640 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2641 0x7f00ff00, SkXfermode::kSrcOver_Mode);
2642 }
Romain Guyf219da52011-01-16 12:54:25 -08002643 }
Chet Haase48659092012-05-31 15:21:51 -07002644
2645 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002646}
2647
Romain Guy6926c722010-07-12 20:20:03 -07002648///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002649// Shaders
2650///////////////////////////////////////////////////////////////////////////////
2651
2652void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002653 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002654}
2655
Romain Guy06f96e22010-07-30 19:18:16 -07002656void OpenGLRenderer::setupShader(SkiaShader* shader) {
2657 mShader = shader;
2658 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002659 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002660 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002661}
2662
Romain Guyd27977d2010-07-14 19:18:51 -07002663///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002664// Color filters
2665///////////////////////////////////////////////////////////////////////////////
2666
2667void OpenGLRenderer::resetColorFilter() {
2668 mColorFilter = NULL;
2669}
2670
2671void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2672 mColorFilter = filter;
2673}
2674
2675///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002676// Drop shadow
2677///////////////////////////////////////////////////////////////////////////////
2678
2679void OpenGLRenderer::resetShadow() {
2680 mHasShadow = false;
2681}
2682
2683void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2684 mHasShadow = true;
2685 mShadowRadius = radius;
2686 mShadowDx = dx;
2687 mShadowDy = dy;
2688 mShadowColor = color;
2689}
2690
2691///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002692// Draw filters
2693///////////////////////////////////////////////////////////////////////////////
2694
2695void OpenGLRenderer::resetPaintFilter() {
2696 mHasDrawFilter = false;
2697}
2698
2699void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2700 mHasDrawFilter = true;
2701 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2702 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2703}
2704
2705SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002706 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002707
2708 uint32_t flags = paint->getFlags();
2709
2710 mFilteredPaint = *paint;
2711 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2712
2713 return &mFilteredPaint;
2714}
2715
2716///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002717// Drawing implementation
2718///////////////////////////////////////////////////////////////////////////////
2719
Romain Guy01d58e42011-01-19 21:54:02 -08002720void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2721 float x, float y, SkPaint* paint) {
2722 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2723 return;
2724 }
2725
2726 int alpha;
2727 SkXfermode::Mode mode;
2728 getAlphaAndMode(paint, &alpha, &mode);
2729
2730 setupDraw();
2731 setupDrawWithTexture(true);
2732 setupDrawAlpha8Color(paint->getColor(), alpha);
2733 setupDrawColorFilter();
2734 setupDrawShader();
2735 setupDrawBlending(true, mode);
2736 setupDrawProgram();
2737 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2738 setupDrawTexture(texture->id);
2739 setupDrawPureColorUniforms();
2740 setupDrawColorFilterUniforms();
2741 setupDrawShaderUniforms();
2742 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2743
2744 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2745
2746 finishDrawTexture();
2747}
2748
Romain Guyf607bdc2010-09-10 19:20:06 -07002749// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002750#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2751#define kStdUnderline_Offset (1.0f / 9.0f)
2752#define kStdUnderline_Thickness (1.0f / 18.0f)
2753
2754void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2755 float x, float y, SkPaint* paint) {
2756 // Handle underline and strike-through
2757 uint32_t flags = paint->getFlags();
2758 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002759 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002760 float underlineWidth = length;
2761 // If length is > 0.0f, we already measured the text for the text alignment
2762 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002763 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002764 }
2765
Romain Guy211370f2012-02-01 16:10:55 -08002766 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002767 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002768 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002769
Raph Levien8b4072d2012-07-30 15:50:00 -07002770 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07002771 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002772
Romain Guyf6834472011-01-23 13:32:12 -08002773 int linesCount = 0;
2774 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2775 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2776
2777 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002778 float points[pointsCount];
2779 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002780
2781 if (flags & SkPaint::kUnderlineText_Flag) {
2782 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002783 points[currentPoint++] = left;
2784 points[currentPoint++] = top;
2785 points[currentPoint++] = left + underlineWidth;
2786 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002787 }
2788
2789 if (flags & SkPaint::kStrikeThruText_Flag) {
2790 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002791 points[currentPoint++] = left;
2792 points[currentPoint++] = top;
2793 points[currentPoint++] = left + underlineWidth;
2794 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002795 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002796
Romain Guy726aeba2011-06-01 14:52:00 -07002797 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002798
Romain Guy726aeba2011-06-01 14:52:00 -07002799 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002800 }
2801 }
2802}
2803
Romain Guy026c5e162010-06-28 17:12:22 -07002804void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002805 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002806 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002807 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002808 color |= 0x00ffffff;
2809 }
2810
Romain Guy70ca14e2010-12-13 18:24:33 -08002811 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002812 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07002813 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08002814 setupDrawShader();
2815 setupDrawColorFilter();
2816 setupDrawBlending(mode);
2817 setupDrawProgram();
2818 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2819 setupDrawColorUniforms();
2820 setupDrawShaderUniforms(ignoreTransform);
2821 setupDrawColorFilterUniforms();
2822 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002823
Romain Guyc95c8d62010-09-17 15:31:32 -07002824 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2825}
2826
Romain Guy82ba8142010-07-09 13:25:56 -07002827void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002828 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002829 int alpha;
2830 SkXfermode::Mode mode;
2831 getAlphaAndMode(paint, &alpha, &mode);
2832
Romain Guyd21b6e12011-11-30 20:21:23 -08002833 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002834
Romain Guy211370f2012-02-01 16:10:55 -08002835 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002836 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2837 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2838
Romain Guyd21b6e12011-11-30 20:21:23 -08002839 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002840 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2841 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2842 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2843 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002844 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002845 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2846 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2847 GL_TRIANGLE_STRIP, gMeshCount);
2848 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002849}
2850
Romain Guybd6b79b2010-06-26 00:13:53 -07002851void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002852 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2853 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002854 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002855}
2856
2857void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002858 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002859 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002860 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002861
Romain Guy746b7402010-10-26 16:27:31 -07002862 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002863 setupDrawWithTexture();
2864 setupDrawColor(alpha, alpha, alpha, alpha);
2865 setupDrawColorFilter();
2866 setupDrawBlending(blend, mode, swapSrcDst);
2867 setupDrawProgram();
2868 if (!dirty) {
2869 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002870 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002871 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002872 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002873 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002874 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002875 }
Romain Guy86568192010-12-14 15:55:39 -08002876 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002877 setupDrawColorFilterUniforms();
2878 setupDrawTexture(texture);
2879 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002880
Romain Guy6820ac82010-09-15 18:11:50 -07002881 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002882
2883 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002884}
2885
Romain Guya5aed0d2010-09-09 14:42:43 -07002886void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002887 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002888 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002889
Romain Guy82ba8142010-07-09 13:25:56 -07002890 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002891 // These blend modes are not supported by OpenGL directly and have
2892 // to be implemented using shaders. Since the shader will perform
2893 // the blending, turn blending off here
2894 // If the blend mode cannot be implemented using shaders, fall
2895 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07002896 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy211370f2012-02-01 16:10:55 -08002897 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002898 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002899 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002900
Romain Guy82bc7a72012-01-03 14:13:39 -08002901 if (mCaches.blend) {
2902 glDisable(GL_BLEND);
2903 mCaches.blend = false;
2904 }
2905
2906 return;
2907 } else {
2908 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002909 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002910 }
2911
2912 if (!mCaches.blend) {
2913 glEnable(GL_BLEND);
2914 }
2915
2916 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2917 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2918
2919 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2920 glBlendFunc(sourceMode, destMode);
2921 mCaches.lastSrcMode = sourceMode;
2922 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002923 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002924 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002925 glDisable(GL_BLEND);
2926 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002927 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002928}
2929
Romain Guy889f8d12010-07-29 14:37:42 -07002930bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002931 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002932 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002933 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002934 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002935 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002936 }
Romain Guy6926c722010-07-12 20:20:03 -07002937 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002938}
2939
Romain Guy026c5e162010-06-28 17:12:22 -07002940void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002941 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002942 TextureVertex::setUV(v++, u1, v1);
2943 TextureVertex::setUV(v++, u2, v1);
2944 TextureVertex::setUV(v++, u1, v2);
2945 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002946}
2947
Chet Haase5c13d892010-10-08 08:37:55 -07002948void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002949 getAlphaAndModeDirect(paint, alpha, mode);
Chet Haasedb8c9a62012-03-21 18:54:18 -07002950 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002951}
2952
Romain Guy9d5316e2010-06-24 19:30:36 -07002953}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002954}; // namespace android