blob: cab82de27aea2ce14a407428087917d005468b72 [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 Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700148 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700149
150 mWidth = width;
151 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700152
153 mFirstSnapshot->height = height;
154 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700155
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
165}
166
167void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800168 mCaches.clearGarbage();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700169 mFunctors.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800170
Romain Guy8aef54f2010-09-01 15:13:49 -0700171 mSnapshot = new Snapshot(mFirstSnapshot,
172 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800173 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700174 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700175
Romain Guy39d252a2011-12-12 18:14:06 -0800176 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800177 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800178
Romain Guy7d7b5492011-01-24 16:33:45 -0800179 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800180 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800181
Romain Guy6b7bd242010-10-06 19:49:23 -0700182 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700183 glClear(GL_COLOR_BUFFER_BIT);
184 }
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
Romain Guyb025b9c2010-09-16 14:16:48 -0700187void OpenGLRenderer::finish() {
188#if DEBUG_OPENGL
189 GLenum status = GL_NO_ERROR;
190 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000191 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800192 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700193 case GL_INVALID_ENUM:
194 ALOGE(" GL_INVALID_ENUM");
195 break;
196 case GL_INVALID_VALUE:
197 ALOGE(" GL_INVALID_VALUE");
198 break;
199 case GL_INVALID_OPERATION:
200 ALOGE(" GL_INVALID_OPERATION");
201 break;
Romain Guya07105b2011-01-10 21:14:18 -0800202 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700203 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800204 break;
205 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700206 }
207#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800208#if DEBUG_MEMORY_USAGE
209 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800210#else
211 if (mCaches.getDebugLevel() & kDebugMemory) {
212 mCaches.dumpMemoryUsage();
213 }
Romain Guyc15008e2010-11-10 11:59:15 -0800214#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700215}
216
Romain Guy6c319ca2011-01-11 14:29:25 -0800217void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700218 if (mCaches.currentProgram) {
219 if (mCaches.currentProgram->isInUse()) {
220 mCaches.currentProgram->remove();
221 mCaches.currentProgram = NULL;
222 }
223 }
Romain Guy50c0f092010-10-19 11:42:22 -0700224 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800225 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800226 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800227 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700228}
229
Romain Guy6c319ca2011-01-11 14:29:25 -0800230void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800231 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
232
233 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800234 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
235
Romain Guyda8532c2010-08-31 11:50:35 -0700236 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800237 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700238 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700239
Romain Guya1d3c912011-12-13 14:55:06 -0800240 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800241 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700242
Romain Guy50c0f092010-10-19 11:42:22 -0700243 mCaches.blend = true;
244 glEnable(GL_BLEND);
245 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
246 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700247}
248
Romain Guyba6be8a2012-04-23 18:22:09 -0700249void OpenGLRenderer::detachFunctor(Functor* functor) {
250 mFunctors.remove(functor);
251}
252
253void OpenGLRenderer::attachFunctor(Functor* functor) {
254 mFunctors.add(functor);
255}
256
Romain Guy8f3b8e32012-03-27 16:33:45 -0700257status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
258 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700259 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700260
Romain Guyba6be8a2012-04-23 18:22:09 -0700261 if (count > 0) {
262 SortedVector<Functor*> functors(mFunctors);
263 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700264
Romain Guyba6be8a2012-04-23 18:22:09 -0700265 DrawGlInfo info;
266 info.clipLeft = 0;
267 info.clipTop = 0;
268 info.clipRight = 0;
269 info.clipBottom = 0;
270 info.isLayer = false;
271 info.width = 0;
272 info.height = 0;
273 memset(info.transform, 0, sizeof(float) * 16);
274
275 for (size_t i = 0; i < count; i++) {
276 Functor* f = functors.itemAt(i);
277 result |= (*f)(DrawGlInfo::kModeProcess, &info);
278
279 if (result != DrawGlInfo::kStatusDone) {
280 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
281 dirty.unionWith(localDirty);
282
283 if (result & DrawGlInfo::kStatusInvoke) {
284 mFunctors.add(f);
285 }
Romain Guy8f3b8e32012-03-27 16:33:45 -0700286 }
287 }
288 }
289
Romain Guyc189ef52012-04-25 20:02:53 -0700290 // Restore state possibly changed by the functors in process mode
291 GLboolean value;
292 glGetBooleanv(GL_BLEND, &value);
293 mCaches.blend = value;
294
295 mCaches.activeTexture(0);
296
Romain Guy8f3b8e32012-03-27 16:33:45 -0700297 return result;
298}
299
300status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800301 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800302 if (mDirtyClip) {
303 setScissorFromClip();
304 }
Romain Guyd643bb52011-03-01 14:55:21 -0800305
Romain Guy80911b82011-03-16 15:30:12 -0700306 Rect clip(*mSnapshot->clipRect);
307 clip.snapToPixelBoundaries();
308
Romain Guyd643bb52011-03-01 14:55:21 -0800309#if RENDER_LAYERS_AS_REGIONS
310 // Since we don't know what the functor will draw, let's dirty
311 // tne entire clip region
312 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800313 dirtyLayerUnchecked(clip, getRegion());
314 }
315#endif
316
Romain Guy08aa2cb2011-03-17 11:06:57 -0700317 DrawGlInfo info;
318 info.clipLeft = clip.left;
319 info.clipTop = clip.top;
320 info.clipRight = clip.right;
321 info.clipBottom = clip.bottom;
322 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700323 info.width = getSnapshot()->viewport.getWidth();
324 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700325 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700326
Romain Guy8f3b8e32012-03-27 16:33:45 -0700327 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800328
Romain Guy8f3b8e32012-03-27 16:33:45 -0700329 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700330 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800331 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700332
Chris Craik65924a32012-04-05 17:52:11 -0700333 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700334 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700335 }
Romain Guycabfcc12011-03-07 18:06:46 -0800336 }
337
Chet Haasedaf98e92011-01-10 14:10:36 -0800338 resume();
Romain Guy65549432012-03-26 16:45:05 -0700339 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800340}
341
Romain Guyf6a11b82010-06-23 17:47:49 -0700342///////////////////////////////////////////////////////////////////////////////
343// State management
344///////////////////////////////////////////////////////////////////////////////
345
Romain Guybb9524b2010-06-22 18:56:38 -0700346int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700347 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700348}
349
350int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700351 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700352}
353
354void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700355 if (mSaveCount > 1) {
356 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700357 }
Romain Guybb9524b2010-06-22 18:56:38 -0700358}
359
360void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700361 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700362
Romain Guy8fb95422010-08-17 18:38:51 -0700363 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700364 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700365 }
Romain Guybb9524b2010-06-22 18:56:38 -0700366}
367
Romain Guy8aef54f2010-09-01 15:13:49 -0700368int OpenGLRenderer::saveSnapshot(int flags) {
369 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700370 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700371}
372
373bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700375 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700376 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700377
Romain Guybd6b79b2010-06-26 00:13:53 -0700378 sp<Snapshot> current = mSnapshot;
379 sp<Snapshot> previous = mSnapshot->previous;
380
Romain Guyeb993562010-10-05 18:14:38 -0700381 if (restoreOrtho) {
382 Rect& r = previous->viewport;
383 glViewport(r.left, r.top, r.right, r.bottom);
384 mOrthoMatrix.load(current->orthoMatrix);
385 }
386
Romain Guy8b55f372010-08-18 17:10:07 -0700387 mSaveCount--;
388 mSnapshot = previous;
389
Romain Guy2542d192010-08-18 11:47:12 -0700390 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700391 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700392 }
Romain Guy2542d192010-08-18 11:47:12 -0700393
Romain Guy5ec99242010-11-03 16:19:08 -0700394 if (restoreLayer) {
395 composeLayer(current, previous);
396 }
397
Romain Guy2542d192010-08-18 11:47:12 -0700398 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700399}
400
Romain Guyf6a11b82010-06-23 17:47:49 -0700401///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700402// Layers
403///////////////////////////////////////////////////////////////////////////////
404
405int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700406 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700407 const GLuint previousFbo = mSnapshot->fbo;
408 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700409
Romain Guyaf636eb2010-12-09 17:47:21 -0800410 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700411 int alpha = 255;
412 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700413
Romain Guye45362c2010-11-03 19:58:32 -0700414 if (p) {
415 alpha = p->getAlpha();
416 if (!mCaches.extensions.hasFramebufferFetch()) {
417 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
418 if (!isMode) {
419 // Assume SRC_OVER
420 mode = SkXfermode::kSrcOver_Mode;
421 }
422 } else {
423 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700424 }
425 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700426 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700427 }
Romain Guyd55a8612010-06-28 17:42:46 -0700428
Romain Guydbc26d22010-10-11 17:58:29 -0700429 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
430 }
Romain Guyd55a8612010-06-28 17:42:46 -0700431
432 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700433}
434
435int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
436 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700437 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700438 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700439 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700440 SkPaint paint;
441 paint.setAlpha(alpha);
442 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700443 }
Romain Guyd55a8612010-06-28 17:42:46 -0700444}
Romain Guybd6b79b2010-06-26 00:13:53 -0700445
Romain Guy1c740bc2010-09-13 18:00:09 -0700446/**
447 * Layers are viewed by Skia are slightly different than layers in image editing
448 * programs (for instance.) When a layer is created, previously created layers
449 * and the frame buffer still receive every drawing command. For instance, if a
450 * layer is created and a shape intersecting the bounds of the layers and the
451 * framebuffer is draw, the shape will be drawn on both (unless the layer was
452 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
453 *
454 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
455 * texture. Unfortunately, this is inefficient as it requires every primitive to
456 * be drawn n + 1 times, where n is the number of active layers. In practice this
457 * means, for every primitive:
458 * - Switch active frame buffer
459 * - Change viewport, clip and projection matrix
460 * - Issue the drawing
461 *
462 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700463 * To avoid this, layers are implemented in a different way here, at least in the
464 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
465 * is set. When this flag is set we can redirect all drawing operations into a
466 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700467 *
468 * This implementation relies on the frame buffer being at least RGBA 8888. When
469 * a layer is created, only a texture is created, not an FBO. The content of the
470 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700471 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700472 * buffer and drawing continues as normal. This technique therefore treats the
473 * frame buffer as a scratch buffer for the layers.
474 *
475 * To compose the layers back onto the frame buffer, each layer texture
476 * (containing the original frame buffer data) is drawn as a simple quad over
477 * the frame buffer. The trick is that the quad is set as the composition
478 * destination in the blending equation, and the frame buffer becomes the source
479 * of the composition.
480 *
481 * Drawing layers with an alpha value requires an extra step before composition.
482 * An empty quad is drawn over the layer's region in the frame buffer. This quad
483 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
484 * quad is used to multiply the colors in the frame buffer. This is achieved by
485 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
486 * GL_ZERO, GL_SRC_ALPHA.
487 *
488 * Because glCopyTexImage2D() can be slow, an alternative implementation might
489 * be use to draw a single clipped layer. The implementation described above
490 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700491 *
492 * (1) The frame buffer is actually not cleared right away. To allow the GPU
493 * to potentially optimize series of calls to glCopyTexImage2D, the frame
494 * buffer is left untouched until the first drawing operation. Only when
495 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700496 */
Romain Guyd55a8612010-06-28 17:42:46 -0700497bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700498 float right, float bottom, int alpha, SkXfermode::Mode mode,
499 int flags, GLuint previousFbo) {
500 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700501 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700502
Romain Guyeb993562010-10-05 18:14:38 -0700503 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
504
Romain Guyf607bdc2010-09-10 19:20:06 -0700505 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700506 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700507 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700508 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700509
Romain Guyeb993562010-10-05 18:14:38 -0700510 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700511 if (bounds.intersect(*snapshot->clipRect)) {
512 // We cannot work with sub-pixels in this case
513 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700514
Romain Guyad37cd32011-03-15 11:12:25 -0700515 // When the layer is not an FBO, we may use glCopyTexImage so we
516 // need to make sure the layer does not extend outside the bounds
517 // of the framebuffer
518 if (!bounds.intersect(snapshot->previous->viewport)) {
519 bounds.setEmpty();
520 }
521 } else {
522 bounds.setEmpty();
523 }
Romain Guyeb993562010-10-05 18:14:38 -0700524 }
Romain Guybf434112010-09-16 14:40:17 -0700525
Romain Guy746b7402010-10-26 16:27:31 -0700526 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
527 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800528 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700529 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700530 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700531 }
532
533 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800534 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700535 return false;
536 }
Romain Guyf18fd992010-07-08 11:45:51 -0700537
Romain Guya1d3c912011-12-13 14:55:06 -0800538 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700539 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700540 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700541 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700542 }
543
Romain Guy9ace8f52011-07-07 20:50:11 -0700544 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700545 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700546 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
547 bounds.getWidth() / float(layer->getWidth()), 0.0f);
548 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700549 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700550
Romain Guy8fb95422010-08-17 18:38:51 -0700551 // Save the layer in the snapshot
552 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700553 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700554
Romain Guyeb993562010-10-05 18:14:38 -0700555 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700556 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700557 } else {
558 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700559 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800560 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700561 if (layer->isEmpty()) {
562 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
563 bounds.left, snapshot->height - bounds.bottom,
564 layer->getWidth(), layer->getHeight(), 0);
565 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800566 } else {
567 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
568 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
569 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700570
Romain Guy54be1cd2011-06-13 19:04:27 -0700571 // Enqueue the buffer coordinates to clear the corresponding region later
572 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700573 }
Romain Guyeb993562010-10-05 18:14:38 -0700574 }
Romain Guyf86ef572010-07-01 11:05:42 -0700575
Romain Guyd55a8612010-06-28 17:42:46 -0700576 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700577}
578
Romain Guy5b3b3522010-10-27 18:57:51 -0700579bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
580 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700581 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700582
583#if RENDER_LAYERS_AS_REGIONS
584 snapshot->region = &snapshot->layer->region;
585 snapshot->flags |= Snapshot::kFlagFboTarget;
586#endif
587
588 Rect clip(bounds);
589 snapshot->transform->mapRect(clip);
590 clip.intersect(*snapshot->clipRect);
591 clip.snapToPixelBoundaries();
592 clip.intersect(snapshot->previous->viewport);
593
594 mat4 inverse;
595 inverse.loadInverse(*mSnapshot->transform);
596
597 inverse.mapRect(clip);
598 clip.snapToPixelBoundaries();
599 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700600 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700601
602 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700603 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700605 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
606 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
607 snapshot->height = bounds.getHeight();
608 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
609 snapshot->orthoMatrix.load(mOrthoMatrix);
610
611 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700612 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
613 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700614
615 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700616 if (layer->isEmpty()) {
617 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
618 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700619 }
620
621 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700622 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700623
624#if DEBUG_LAYERS_AS_REGIONS
625 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
626 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000627 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700628
629 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700630 layer->deleteTexture();
631 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700632
633 delete layer;
634
635 return false;
636 }
637#endif
638
639 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800640 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700641 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700642 glClear(GL_COLOR_BUFFER_BIT);
643
644 dirtyClip();
645
646 // Change the ortho projection
647 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
648 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
649
650 return true;
651}
652
Romain Guy1c740bc2010-09-13 18:00:09 -0700653/**
654 * Read the documentation of createLayer() before doing anything in this method.
655 */
Romain Guy1d83e192010-08-17 11:37:00 -0700656void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
657 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000658 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700659 return;
660 }
661
Romain Guy5b3b3522010-10-27 18:57:51 -0700662 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700663
664 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700665 // Detach the texture from the FBO
666 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
667
Romain Guyeb993562010-10-05 18:14:38 -0700668 // Unbind current FBO and restore previous one
669 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
670 }
671
Romain Guy1d83e192010-08-17 11:37:00 -0700672 Layer* layer = current->layer;
673 const Rect& rect = layer->layer;
674
Romain Guy9ace8f52011-07-07 20:50:11 -0700675 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700676 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700678 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700679 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700680 }
681
Romain Guy03750a02010-10-18 14:06:08 -0700682 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700683
Romain Guya1d3c912011-12-13 14:55:06 -0800684 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700685
Romain Guy5b3b3522010-10-27 18:57:51 -0700686 // When the layer is stored in an FBO, we can save a bit of fillrate by
687 // drawing only the dirty region
688 if (fboLayer) {
689 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 if (layer->getColorFilter()) {
691 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800692 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700693 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800695 resetColorFilter();
696 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 } else if (!rect.isEmpty()) {
698 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
699 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700700 }
Romain Guy8b55f372010-08-18 17:10:07 -0700701
Romain Guyeb993562010-10-05 18:14:38 -0700702 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800703 // Note: No need to use glDiscardFramebufferEXT() since we never
704 // create/compose layers that are not on screen with this
705 // code path
706 // See LayerRenderer::destroyLayer(Layer*)
707
Romain Guyeb993562010-10-05 18:14:38 -0700708 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
709 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700710 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700711 }
712
Romain Guy746b7402010-10-26 16:27:31 -0700713 dirtyClip();
714
Romain Guyeb993562010-10-05 18:14:38 -0700715 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700716 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700717 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700718 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700719 delete layer;
720 }
721}
722
Romain Guyaa6c24c2011-04-28 18:40:04 -0700723void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700725
Romain Guy302a9df2011-08-16 13:55:02 -0700726 mat4& transform = layer->getTransform();
727 if (!transform.isIdentity()) {
728 save(0);
729 mSnapshot->transform->multiply(transform);
730 }
731
Romain Guyaa6c24c2011-04-28 18:40:04 -0700732 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700733 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700734 setupDrawWithTexture();
735 } else {
736 setupDrawWithExternalTexture();
737 }
738 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700739 setupDrawColor(alpha, alpha, alpha, alpha);
740 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700742 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700743 setupDrawPureColorUniforms();
744 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
746 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700747 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700749 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700750 if (mSnapshot->transform->isPureTranslate() &&
751 layer->getWidth() == (uint32_t) rect.getWidth() &&
752 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
754 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
755
Romain Guyd21b6e12011-11-30 20:21:23 -0800756 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
758 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800759 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
761 }
762 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700763 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
764
765 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
766
767 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700768
769 if (!transform.isIdentity()) {
770 restore();
771 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700772}
773
Romain Guy5b3b3522010-10-27 18:57:51 -0700774void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700776 const Rect& texCoords = layer->texCoords;
777 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
778 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700779
Romain Guy9ace8f52011-07-07 20:50:11 -0700780 float x = rect.left;
781 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700782 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700783 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700784 layer->getHeight() == (uint32_t) rect.getHeight();
785
786 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 // When we're swapping, the layer is already in screen coordinates
788 if (!swap) {
789 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
790 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
791 }
792
Romain Guyd21b6e12011-11-30 20:21:23 -0800793 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800795 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700796 }
797
798 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
799 layer->getTexture(), layer->getAlpha() / 255.0f,
800 layer->getMode(), layer->isBlend(),
801 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
802 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700803
Romain Guyaa6c24c2011-04-28 18:40:04 -0700804 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
805 } else {
806 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
807 drawTextureLayer(layer, rect);
808 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
809 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700810}
811
812void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
813#if RENDER_LAYERS_AS_REGIONS
814 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700815 layer->setRegionAsRect();
816
Romain Guy40667672011-03-18 14:34:03 -0700817 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700818
Romain Guy5b3b3522010-10-27 18:57:51 -0700819 layer->region.clear();
820 return;
821 }
822
Romain Guy8a3957d2011-09-07 17:55:15 -0700823 // TODO: See LayerRenderer.cpp::generateMesh() for important
824 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800825 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700826 size_t count;
827 const android::Rect* rects = layer->region.getArray(&count);
828
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 const float alpha = layer->getAlpha() / 255.0f;
830 const float texX = 1.0f / float(layer->getWidth());
831 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800832 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
834 TextureVertex* mesh = mCaches.getRegionMesh();
835 GLsizei numQuads = 0;
836
Romain Guy7230a742011-01-10 22:26:16 -0800837 setupDraw();
838 setupDrawWithTexture();
839 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800840 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700841 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800842 setupDrawProgram();
843 setupDrawDirtyRegionsDisabled();
844 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800845 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 setupDrawTexture(layer->getTexture());
847 if (mSnapshot->transform->isPureTranslate()) {
848 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
849 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
850
Romain Guyd21b6e12011-11-30 20:21:23 -0800851 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
853 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800854 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700855 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
856 }
Romain Guy15bc6432011-12-13 13:11:32 -0800857 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700858
859 for (size_t i = 0; i < count; i++) {
860 const android::Rect* r = &rects[i];
861
862 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800863 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800865 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700866
867 // TODO: Reject quads outside of the clip
868 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
869 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
870 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
871 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
872
873 numQuads++;
874
875 if (numQuads >= REGION_MESH_QUAD_COUNT) {
876 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
877 numQuads = 0;
878 mesh = mCaches.getRegionMesh();
879 }
880 }
881
882 if (numQuads > 0) {
883 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
884 }
885
Romain Guy7230a742011-01-10 22:26:16 -0800886 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700887
888#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800889 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700890#endif
891
892 layer->region.clear();
893 }
894#else
895 composeLayerRect(layer, rect);
896#endif
897}
898
Romain Guy3a3133d2011-02-01 22:59:58 -0800899void OpenGLRenderer::drawRegionRects(const Region& region) {
900#if DEBUG_LAYERS_AS_REGIONS
901 size_t count;
902 const android::Rect* rects = region.getArray(&count);
903
904 uint32_t colors[] = {
905 0x7fff0000, 0x7f00ff00,
906 0x7f0000ff, 0x7fff00ff,
907 };
908
909 int offset = 0;
910 int32_t top = rects[0].top;
911
912 for (size_t i = 0; i < count; i++) {
913 if (top != rects[i].top) {
914 offset ^= 0x2;
915 top = rects[i].top;
916 }
917
918 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
919 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
920 SkXfermode::kSrcOver_Mode);
921 }
922#endif
923}
924
Romain Guy5b3b3522010-10-27 18:57:51 -0700925void OpenGLRenderer::dirtyLayer(const float left, const float top,
926 const float right, const float bottom, const mat4 transform) {
927#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800928 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700929 Rect bounds(left, top, right, bottom);
930 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800931 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700932 }
933#endif
934}
935
936void OpenGLRenderer::dirtyLayer(const float left, const float top,
937 const float right, const float bottom) {
938#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800939 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700940 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800941 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800942 }
943#endif
944}
945
946void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
947#if RENDER_LAYERS_AS_REGIONS
948 if (bounds.intersect(*mSnapshot->clipRect)) {
949 bounds.snapToPixelBoundaries();
950 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
951 if (!dirty.isEmpty()) {
952 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700953 }
954 }
955#endif
956}
957
Romain Guy54be1cd2011-06-13 19:04:27 -0700958void OpenGLRenderer::clearLayerRegions() {
959 const size_t count = mLayers.size();
960 if (count == 0) return;
961
962 if (!mSnapshot->isIgnored()) {
963 // Doing several glScissor/glClear here can negatively impact
964 // GPUs with a tiler architecture, instead we draw quads with
965 // the Clear blending mode
966
967 // The list contains bounds that have already been clipped
968 // against their initial clip rect, and the current clip
969 // is likely different so we need to disable clipping here
970 glDisable(GL_SCISSOR_TEST);
971
972 Vertex mesh[count * 6];
973 Vertex* vertex = mesh;
974
975 for (uint32_t i = 0; i < count; i++) {
976 Rect* bounds = mLayers.itemAt(i);
977
978 Vertex::set(vertex++, bounds->left, bounds->bottom);
979 Vertex::set(vertex++, bounds->left, bounds->top);
980 Vertex::set(vertex++, bounds->right, bounds->top);
981 Vertex::set(vertex++, bounds->left, bounds->bottom);
982 Vertex::set(vertex++, bounds->right, bounds->top);
983 Vertex::set(vertex++, bounds->right, bounds->bottom);
984
985 delete bounds;
986 }
987
988 setupDraw(false);
989 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
990 setupDrawBlending(true, SkXfermode::kClear_Mode);
991 setupDrawProgram();
992 setupDrawPureColorUniforms();
993 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800994 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700995
Romain Guy54be1cd2011-06-13 19:04:27 -0700996 glDrawArrays(GL_TRIANGLES, 0, count * 6);
997
998 glEnable(GL_SCISSOR_TEST);
999 } else {
1000 for (uint32_t i = 0; i < count; i++) {
1001 delete mLayers.itemAt(i);
1002 }
1003 }
1004
1005 mLayers.clear();
1006}
1007
Romain Guybd6b79b2010-06-26 00:13:53 -07001008///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001009// Transforms
1010///////////////////////////////////////////////////////////////////////////////
1011
1012void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001013 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001014}
1015
1016void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001021 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001022}
1023
Romain Guy807daf72011-01-18 11:19:19 -08001024void OpenGLRenderer::skew(float sx, float sy) {
1025 mSnapshot->transform->skew(sx, sy);
1026}
1027
Romain Guyf6a11b82010-06-23 17:47:49 -07001028void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001029 if (matrix) {
1030 mSnapshot->transform->load(*matrix);
1031 } else {
1032 mSnapshot->transform->loadIdentity();
1033 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001034}
1035
1036void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001037 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001038}
1039
1040void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001041 SkMatrix transform;
1042 mSnapshot->transform->copyTo(transform);
1043 transform.preConcat(*matrix);
1044 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001045}
1046
1047///////////////////////////////////////////////////////////////////////////////
1048// Clipping
1049///////////////////////////////////////////////////////////////////////////////
1050
Romain Guybb9524b2010-06-22 18:56:38 -07001051void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001052 Rect clip(*mSnapshot->clipRect);
1053 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001054
1055 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1056 clip.getWidth(), clip.getHeight());
1057
Romain Guy746b7402010-10-26 16:27:31 -07001058 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001059}
1060
1061const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001062 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001063}
1064
Romain Guyc7d53492010-06-25 13:41:57 -07001065bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001066 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001067 return true;
1068 }
1069
Romain Guy1d83e192010-08-17 11:37:00 -07001070 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001071 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001072 r.snapToPixelBoundaries();
1073
1074 Rect clipRect(*mSnapshot->clipRect);
1075 clipRect.snapToPixelBoundaries();
1076
1077 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001078}
1079
Romain Guy079ba2c2010-07-16 14:12:24 -07001080bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1081 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001082 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001083 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001084 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001085 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001086}
1087
Chet Haasea23eed82012-04-12 15:19:04 -07001088Rect* OpenGLRenderer::getClipRect() {
1089 return mSnapshot->clipRect;
1090}
1091
Romain Guyf6a11b82010-06-23 17:47:49 -07001092///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001093// Drawing commands
1094///////////////////////////////////////////////////////////////////////////////
1095
Romain Guy54be1cd2011-06-13 19:04:27 -07001096void OpenGLRenderer::setupDraw(bool clear) {
1097 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001098 if (mDirtyClip) {
1099 setScissorFromClip();
1100 }
1101 mDescription.reset();
1102 mSetShaderColor = false;
1103 mColorSet = false;
1104 mColorA = mColorR = mColorG = mColorB = 0.0f;
1105 mTextureUnit = 0;
1106 mTrackDirtyRegions = true;
1107}
1108
1109void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1110 mDescription.hasTexture = true;
1111 mDescription.hasAlpha8Texture = isAlpha8;
1112}
1113
Romain Guyaa6c24c2011-04-28 18:40:04 -07001114void OpenGLRenderer::setupDrawWithExternalTexture() {
1115 mDescription.hasExternalTexture = true;
1116}
1117
Romain Guy15bc6432011-12-13 13:11:32 -08001118void OpenGLRenderer::setupDrawNoTexture() {
1119 mCaches.disbaleTexCoordsVertexArray();
1120}
1121
Chet Haase5b0200b2011-04-13 17:58:08 -07001122void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001123 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001124}
1125
Romain Guyed6fcb02011-03-21 13:11:28 -07001126void OpenGLRenderer::setupDrawPoint(float pointSize) {
1127 mDescription.isPoint = true;
1128 mDescription.pointSize = pointSize;
1129}
1130
Romain Guy70ca14e2010-12-13 18:24:33 -08001131void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001132 setupDrawColor(color, (color >> 24) & 0xFF);
1133}
1134
1135void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1136 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001137 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001138 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1139 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001140 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001141 mColorR = a * ((color >> 16) & 0xFF);
1142 mColorG = a * ((color >> 8) & 0xFF);
1143 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001144 mColorSet = true;
1145 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1146}
1147
Romain Guy86568192010-12-14 15:55:39 -08001148void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1149 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001150 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1151 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001152 const float a = mColorA / 255.0f;
1153 mColorR = a * ((color >> 16) & 0xFF);
1154 mColorG = a * ((color >> 8) & 0xFF);
1155 mColorB = a * ((color ) & 0xFF);
1156 mColorSet = true;
1157 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1158}
1159
Romain Guy70ca14e2010-12-13 18:24:33 -08001160void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1161 mColorA = a;
1162 mColorR = r;
1163 mColorG = g;
1164 mColorB = b;
1165 mColorSet = true;
1166 mSetShaderColor = mDescription.setColor(r, g, b, a);
1167}
1168
Romain Guy86568192010-12-14 15:55:39 -08001169void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1170 mColorA = a;
1171 mColorR = r;
1172 mColorG = g;
1173 mColorB = b;
1174 mColorSet = true;
1175 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1176}
1177
Romain Guy70ca14e2010-12-13 18:24:33 -08001178void OpenGLRenderer::setupDrawShader() {
1179 if (mShader) {
1180 mShader->describe(mDescription, mCaches.extensions);
1181 }
1182}
1183
1184void OpenGLRenderer::setupDrawColorFilter() {
1185 if (mColorFilter) {
1186 mColorFilter->describe(mDescription, mCaches.extensions);
1187 }
1188}
1189
Romain Guyf09ef512011-05-27 11:43:46 -07001190void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1191 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1192 mColorA = 1.0f;
1193 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001194 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001195 }
1196}
1197
Romain Guy70ca14e2010-12-13 18:24:33 -08001198void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001199 // When the blending mode is kClear_Mode, we need to use a modulate color
1200 // argb=1,0,0,0
1201 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001202 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1203 mDescription, swapSrcDst);
1204}
1205
1206void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001207 // When the blending mode is kClear_Mode, we need to use a modulate color
1208 // argb=1,0,0,0
1209 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001210 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1211 mDescription, swapSrcDst);
1212}
1213
1214void OpenGLRenderer::setupDrawProgram() {
1215 useProgram(mCaches.programCache.get(mDescription));
1216}
1217
1218void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1219 mTrackDirtyRegions = false;
1220}
1221
1222void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1223 bool ignoreTransform) {
1224 mModelView.loadTranslate(left, top, 0.0f);
1225 if (!ignoreTransform) {
1226 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1227 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1228 } else {
1229 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1230 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1231 }
1232}
1233
Chet Haase8a5cc922011-04-26 07:28:09 -07001234void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1235 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001236}
1237
Romain Guy70ca14e2010-12-13 18:24:33 -08001238void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1239 bool ignoreTransform, bool ignoreModelView) {
1240 if (!ignoreModelView) {
1241 mModelView.loadTranslate(left, top, 0.0f);
1242 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001243 } else {
1244 mModelView.loadIdentity();
1245 }
Romain Guy86568192010-12-14 15:55:39 -08001246 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1247 if (!ignoreTransform) {
1248 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1249 if (mTrackDirtyRegions && dirty) {
1250 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1251 }
1252 } else {
1253 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1254 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1255 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001256}
1257
Romain Guyed6fcb02011-03-21 13:11:28 -07001258void OpenGLRenderer::setupDrawPointUniforms() {
1259 int slot = mCaches.currentProgram->getUniform("pointSize");
1260 glUniform1f(slot, mDescription.pointSize);
1261}
1262
Romain Guy70ca14e2010-12-13 18:24:33 -08001263void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001264 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001265 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1266 }
1267}
1268
Romain Guy86568192010-12-14 15:55:39 -08001269void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001270 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001271 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001272 }
1273}
1274
Romain Guy70ca14e2010-12-13 18:24:33 -08001275void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1276 if (mShader) {
1277 if (ignoreTransform) {
1278 mModelView.loadInverse(*mSnapshot->transform);
1279 }
1280 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1281 }
1282}
1283
Romain Guy8d0d4782010-12-14 20:13:35 -08001284void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1285 if (mShader) {
1286 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1287 }
1288}
1289
Romain Guy70ca14e2010-12-13 18:24:33 -08001290void OpenGLRenderer::setupDrawColorFilterUniforms() {
1291 if (mColorFilter) {
1292 mColorFilter->setupProgram(mCaches.currentProgram);
1293 }
1294}
1295
1296void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001297 bool force = mCaches.bindMeshBuffer();
1298 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001299 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001300}
1301
1302void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1303 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001304 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001305 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001306}
1307
Romain Guyaa6c24c2011-04-28 18:40:04 -07001308void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1309 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001310 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001311 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001312}
1313
Romain Guy8f0095c2011-05-02 17:24:22 -07001314void OpenGLRenderer::setupDrawTextureTransform() {
1315 mDescription.hasTextureTransform = true;
1316}
1317
1318void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001319 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1320 GL_FALSE, &transform.data[0]);
1321}
1322
Romain Guy70ca14e2010-12-13 18:24:33 -08001323void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001324 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001325 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001326 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001327 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001328 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001329 }
Romain Guyd71dd362011-12-12 19:03:35 -08001330
Romain Guyf3a910b42011-12-12 20:35:21 -08001331 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001332 if (mCaches.currentProgram->texCoords >= 0) {
1333 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1334 }
1335
1336 mCaches.unbindIndicesBuffer();
1337}
1338
1339void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1340 bool force = mCaches.unbindMeshBuffer();
1341 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1342 if (mCaches.currentProgram->texCoords >= 0) {
1343 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001344 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001345}
1346
Chet Haase5b0200b2011-04-13 17:58:08 -07001347void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001348 bool force = mCaches.unbindMeshBuffer();
1349 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1350 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001351 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001352}
1353
1354/**
1355 * 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 -07001356 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1357 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1358 * attributes (one per vertex) are values from zero to one that tells the fragment
1359 * shader where the fragment is in relation to the line width/length overall; these values are
1360 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1361 * region of the line.
1362 * Note that we only pass down the width values in this setup function. The length coordinates
1363 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001364 */
Chet Haase99585ad2011-05-02 15:00:16 -07001365void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001366 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001367 bool force = mCaches.unbindMeshBuffer();
1368 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1369 vertices, gAAVertexStride);
1370 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001371 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001372
Romain Guy7b631422012-04-04 11:38:54 -07001373 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001374 glEnableVertexAttribArray(widthSlot);
1375 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001376
Romain Guy7b631422012-04-04 11:38:54 -07001377 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001378 glEnableVertexAttribArray(lengthSlot);
1379 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001380
Chet Haase5b0200b2011-04-13 17:58:08 -07001381 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001382 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001383
Chet Haase99585ad2011-05-02 15:00:16 -07001384 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001385 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001386 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1387}
1388
1389void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1390 glDisableVertexAttribArray(widthSlot);
1391 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001392}
1393
Romain Guy70ca14e2010-12-13 18:24:33 -08001394void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001395}
1396
1397///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001398// Drawing
1399///////////////////////////////////////////////////////////////////////////////
1400
Chet Haase1271e2c2012-04-20 09:54:27 -07001401status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001402 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001403
Romain Guy0fe478e2010-11-08 12:08:41 -08001404 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1405 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001406 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001407 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001408 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001409
Romain Guy65549432012-03-26 16:45:05 -07001410 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001411}
1412
Chet Haaseed30fd82011-04-22 16:18:45 -07001413void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1414 if (displayList) {
1415 displayList->output(*this, level);
1416 }
1417}
1418
Romain Guya168d732011-03-18 16:50:13 -07001419void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1420 int alpha;
1421 SkXfermode::Mode mode;
1422 getAlphaAndMode(paint, &alpha, &mode);
1423
Romain Guya168d732011-03-18 16:50:13 -07001424 float x = left;
1425 float y = top;
1426
Romain Guye3c26852011-07-25 16:36:01 -07001427 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001428 bool ignoreTransform = false;
1429 if (mSnapshot->transform->isPureTranslate()) {
1430 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1431 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1432 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001433 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001434 } else {
1435 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001436 }
1437
1438 setupDraw();
1439 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001440 if (paint) {
1441 setupDrawAlpha8Color(paint->getColor(), alpha);
1442 }
Romain Guya168d732011-03-18 16:50:13 -07001443 setupDrawColorFilter();
1444 setupDrawShader();
1445 setupDrawBlending(true, mode);
1446 setupDrawProgram();
1447 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001448
Romain Guya168d732011-03-18 16:50:13 -07001449 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001450 texture->setWrap(GL_CLAMP_TO_EDGE);
1451 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001452
Romain Guya168d732011-03-18 16:50:13 -07001453 setupDrawPureColorUniforms();
1454 setupDrawColorFilterUniforms();
1455 setupDrawShaderUniforms();
1456 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1457
1458 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1459
1460 finishDrawTexture();
1461}
1462
Chet Haase5c13d892010-10-08 08:37:55 -07001463void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001464 const float right = left + bitmap->width();
1465 const float bottom = top + bitmap->height();
1466
1467 if (quickReject(left, top, right, bottom)) {
1468 return;
1469 }
1470
Romain Guya1d3c912011-12-13 14:55:06 -08001471 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001472 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001473 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001474 const AutoTexture autoCleanup(texture);
1475
Romain Guy211370f2012-02-01 16:10:55 -08001476 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001477 drawAlphaBitmap(texture, left, top, paint);
1478 } else {
1479 drawTextureRect(left, top, right, bottom, texture, paint);
1480 }
Romain Guyce0537b2010-06-29 21:05:21 -07001481}
1482
Chet Haase5c13d892010-10-08 08:37:55 -07001483void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001484 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1485 const mat4 transform(*matrix);
1486 transform.mapRect(r);
1487
Romain Guy6926c722010-07-12 20:20:03 -07001488 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1489 return;
1490 }
1491
Romain Guya1d3c912011-12-13 14:55:06 -08001492 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001493 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001494 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001495 const AutoTexture autoCleanup(texture);
1496
Romain Guy5b3b3522010-10-27 18:57:51 -07001497 // This could be done in a cheaper way, all we need is pass the matrix
1498 // to the vertex shader. The save/restore is a bit overkill.
1499 save(SkCanvas::kMatrix_SaveFlag);
1500 concatMatrix(matrix);
1501 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1502 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001503}
1504
Romain Guy5a7b4662011-01-20 19:09:30 -08001505void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1506 float* vertices, int* colors, SkPaint* paint) {
1507 // TODO: Do a quickReject
1508 if (!vertices || mSnapshot->isIgnored()) {
1509 return;
1510 }
1511
Romain Guya1d3c912011-12-13 14:55:06 -08001512 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001513 Texture* texture = mCaches.textureCache.get(bitmap);
1514 if (!texture) return;
1515 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001516
Romain Guyd21b6e12011-11-30 20:21:23 -08001517 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1518 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001519
1520 int alpha;
1521 SkXfermode::Mode mode;
1522 getAlphaAndMode(paint, &alpha, &mode);
1523
Romain Guy5a7b4662011-01-20 19:09:30 -08001524 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001525
Romain Guyb18d2d02011-02-10 15:52:54 -08001526 float left = FLT_MAX;
1527 float top = FLT_MAX;
1528 float right = FLT_MIN;
1529 float bottom = FLT_MIN;
1530
1531#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001532 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001533#else
Romain Guy211370f2012-02-01 16:10:55 -08001534 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001535#endif
1536
Romain Guya566b7c2011-01-23 16:36:11 -08001537 // TODO: Support the colors array
1538 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001539 TextureVertex* vertex = mesh;
1540 for (int32_t y = 0; y < meshHeight; y++) {
1541 for (int32_t x = 0; x < meshWidth; x++) {
1542 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1543
1544 float u1 = float(x) / meshWidth;
1545 float u2 = float(x + 1) / meshWidth;
1546 float v1 = float(y) / meshHeight;
1547 float v2 = float(y + 1) / meshHeight;
1548
1549 int ax = i + (meshWidth + 1) * 2;
1550 int ay = ax + 1;
1551 int bx = i;
1552 int by = bx + 1;
1553 int cx = i + 2;
1554 int cy = cx + 1;
1555 int dx = i + (meshWidth + 1) * 2 + 2;
1556 int dy = dx + 1;
1557
1558 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1559 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1560 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1561
1562 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1563 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1564 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001565
1566#if RENDER_LAYERS_AS_REGIONS
1567 if (hasActiveLayer) {
1568 // TODO: This could be optimized to avoid unnecessary ops
1569 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1570 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1571 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1572 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1573 }
1574#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001575 }
1576 }
1577
Romain Guyb18d2d02011-02-10 15:52:54 -08001578#if RENDER_LAYERS_AS_REGIONS
1579 if (hasActiveLayer) {
1580 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1581 }
1582#endif
1583
Romain Guy5a7b4662011-01-20 19:09:30 -08001584 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1585 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001586 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001587}
1588
Romain Guy8ba548f2010-06-30 19:21:21 -07001589void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1590 float srcLeft, float srcTop, float srcRight, float srcBottom,
1591 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001592 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001593 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1594 return;
1595 }
1596
Romain Guya1d3c912011-12-13 14:55:06 -08001597 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001598 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001599 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001600 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001601
Romain Guy8ba548f2010-06-30 19:21:21 -07001602 const float width = texture->width;
1603 const float height = texture->height;
1604
Romain Guy68169722011-08-22 17:33:33 -07001605 const float u1 = fmax(0.0f, srcLeft / width);
1606 const float v1 = fmax(0.0f, srcTop / height);
1607 const float u2 = fmin(1.0f, srcRight / width);
1608 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001609
Romain Guy03750a02010-10-18 14:06:08 -07001610 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001611 resetDrawTextureTexCoords(u1, v1, u2, v2);
1612
Romain Guy03750a02010-10-18 14:06:08 -07001613 int alpha;
1614 SkXfermode::Mode mode;
1615 getAlphaAndMode(paint, &alpha, &mode);
1616
Romain Guyd21b6e12011-11-30 20:21:23 -08001617 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1618
Romain Guy211370f2012-02-01 16:10:55 -08001619 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001620 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1621 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1622
Romain Guyb5014982011-07-28 15:39:12 -07001623 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001624 // Enable linear filtering if the source rectangle is scaled
1625 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001626 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001627 }
Romain Guyb5014982011-07-28 15:39:12 -07001628
Romain Guyd21b6e12011-11-30 20:21:23 -08001629 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001630 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1631 texture->id, alpha / 255.0f, mode, texture->blend,
1632 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1633 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1634 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001635 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001636 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1637 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1638 GL_TRIANGLE_STRIP, gMeshCount);
1639 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001640
1641 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1642}
1643
Romain Guy4aa90572010-09-26 18:40:37 -07001644void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001645 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001646 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001647 if (quickReject(left, top, right, bottom)) {
1648 return;
1649 }
1650
Romain Guya1d3c912011-12-13 14:55:06 -08001651 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001652 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001653 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001654 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001655 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1656 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001657
1658 int alpha;
1659 SkXfermode::Mode mode;
1660 getAlphaAndMode(paint, &alpha, &mode);
1661
Romain Guy2728f962010-10-08 18:36:15 -07001662 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001663 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001664
Romain Guy211370f2012-02-01 16:10:55 -08001665 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001666 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001667#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001668 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001669 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001670 const float offsetX = left + mSnapshot->transform->getTranslateX();
1671 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001672 const size_t count = mesh->quads.size();
1673 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001674 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001675 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001676 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1677 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1678 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001679 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001680 dirtyLayer(left + bounds.left, top + bounds.top,
1681 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001682 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001683 }
1684 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001685#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001686
Romain Guy211370f2012-02-01 16:10:55 -08001687 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001688 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1689 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1690
1691 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1692 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1693 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1694 true, !mesh->hasEmptyQuads);
1695 } else {
1696 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1697 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1698 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1699 true, !mesh->hasEmptyQuads);
1700 }
Romain Guy054dc182010-10-15 17:55:25 -07001701 }
Romain Guyf7f93552010-07-08 19:17:03 -07001702}
1703
Chet Haase99ecdc42011-05-06 12:06:34 -07001704/**
Chet Haase858aa932011-05-12 09:06:00 -07001705 * This function uses a similar approach to that of AA lines in the drawLines() function.
1706 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1707 * shader to compute the translucency of the color, determined by whether a given pixel is
1708 * within that boundary region and how far into the region it is.
1709 */
1710void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001711 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001712 float inverseScaleX = 1.0f;
1713 float inverseScaleY = 1.0f;
1714 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001715 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001716 Matrix4 *mat = mSnapshot->transform;
1717 float m00 = mat->data[Matrix4::kScaleX];
1718 float m01 = mat->data[Matrix4::kSkewY];
1719 float m02 = mat->data[2];
1720 float m10 = mat->data[Matrix4::kSkewX];
1721 float m11 = mat->data[Matrix4::kScaleX];
1722 float m12 = mat->data[6];
1723 float scaleX = sqrt(m00 * m00 + m01 * m01);
1724 float scaleY = sqrt(m10 * m10 + m11 * m11);
1725 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1726 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1727 }
1728
1729 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001730 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001731 setupDrawAALine();
1732 setupDrawColor(color);
1733 setupDrawColorFilter();
1734 setupDrawShader();
1735 setupDrawBlending(true, mode);
1736 setupDrawProgram();
1737 setupDrawModelViewIdentity(true);
1738 setupDrawColorUniforms();
1739 setupDrawColorFilterUniforms();
1740 setupDrawShaderIdentityUniforms();
1741
1742 AAVertex rects[4];
1743 AAVertex* aaVertices = &rects[0];
1744 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1745 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1746
1747 float boundarySizeX = .5 * inverseScaleX;
1748 float boundarySizeY = .5 * inverseScaleY;
1749
1750 // Adjust the rect by the AA boundary padding
1751 left -= boundarySizeX;
1752 right += boundarySizeX;
1753 top -= boundarySizeY;
1754 bottom += boundarySizeY;
1755
1756 float width = right - left;
1757 float height = bottom - top;
1758
Romain Guy7b631422012-04-04 11:38:54 -07001759 int widthSlot;
1760 int lengthSlot;
1761
Chet Haase858aa932011-05-12 09:06:00 -07001762 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1763 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001764 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1765 boundaryWidthProportion, widthSlot, lengthSlot);
1766
Chet Haase858aa932011-05-12 09:06:00 -07001767 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1768 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1769 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001770 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001771
1772 if (!quickReject(left, top, right, bottom)) {
1773 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1774 AAVertex::set(aaVertices++, left, top, 1, 0);
1775 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1776 AAVertex::set(aaVertices++, right, top, 0, 0);
1777 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1778 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1779 }
Romain Guy7b631422012-04-04 11:38:54 -07001780
1781 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001782}
1783
1784/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001785 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1786 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1787 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1788 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1789 * of the line. Hairlines are more involved because we need to account for transform scaling
1790 * to end up with a one-pixel-wide line in screen space..
1791 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1792 * in combination with values that we calculate and pass down in this method. The basic approach
1793 * is that the quad we create contains both the core line area plus a bounding area in which
1794 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1795 * proportion of the width and the length of a given segment is represented by the boundary
1796 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1797 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1798 * on the inside). This ends up giving the result we want, with pixels that are completely
1799 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1800 * how far into the boundary region they are, which is determined by shader interpolation.
1801 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001802void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1803 if (mSnapshot->isIgnored()) return;
1804
1805 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001806 // We use half the stroke width here because we're going to position the quad
1807 // corner vertices half of the width away from the line endpoints
1808 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001809 // A stroke width of 0 has a special meaning in Skia:
1810 // it draws a line 1 px wide regardless of current transform
1811 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001812
Chet Haase99ecdc42011-05-06 12:06:34 -07001813 float inverseScaleX = 1.0f;
1814 float inverseScaleY = 1.0f;
1815 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001816
Chet Haase8a5cc922011-04-26 07:28:09 -07001817 int alpha;
1818 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001819
Chet Haase8a5cc922011-04-26 07:28:09 -07001820 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001821 int verticesCount = count;
1822 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001823 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001824 verticesCount += (count - 4);
1825 }
1826
1827 if (isHairLine || isAA) {
1828 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1829 // the line on the screen should always be one pixel wide regardless of scale. For
1830 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001831 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001832 Matrix4 *mat = mSnapshot->transform;
1833 float m00 = mat->data[Matrix4::kScaleX];
1834 float m01 = mat->data[Matrix4::kSkewY];
1835 float m02 = mat->data[2];
1836 float m10 = mat->data[Matrix4::kSkewX];
1837 float m11 = mat->data[Matrix4::kScaleX];
1838 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001839
Romain Guy211370f2012-02-01 16:10:55 -08001840 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1841 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001842
Chet Haase99ecdc42011-05-06 12:06:34 -07001843 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1844 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001845
Chet Haase99ecdc42011-05-06 12:06:34 -07001846 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1847 scaled = true;
1848 }
1849 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001850 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001851
1852 getAlphaAndMode(paint, &alpha, &mode);
1853 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001854 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001855 if (isAA) {
1856 setupDrawAALine();
1857 }
1858 setupDrawColor(paint->getColor(), alpha);
1859 setupDrawColorFilter();
1860 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001861 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001862 setupDrawProgram();
1863 setupDrawModelViewIdentity(true);
1864 setupDrawColorUniforms();
1865 setupDrawColorFilterUniforms();
1866 setupDrawShaderIdentityUniforms();
1867
1868 if (isHairLine) {
1869 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001870 halfStrokeWidth = isAA? 1 : .5;
1871 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001872 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001874 }
Romain Guy7b631422012-04-04 11:38:54 -07001875
1876 int widthSlot;
1877 int lengthSlot;
1878
Chet Haase5b0200b2011-04-13 17:58:08 -07001879 Vertex lines[verticesCount];
1880 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001881
Chet Haase99585ad2011-05-02 15:00:16 -07001882 AAVertex wLines[verticesCount];
1883 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001884
Romain Guy211370f2012-02-01 16:10:55 -08001885 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001886 setupDrawVertices(vertices);
1887 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001888 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1889 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001890 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001891 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1892 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001893 // We will need to calculate the actual width proportion on each segment for
1894 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1895 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001896 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1897 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001898 }
1899
Chet Haase99ecdc42011-05-06 12:06:34 -07001900 AAVertex* prevAAVertex = NULL;
1901 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001902
Chet Haase99585ad2011-05-02 15:00:16 -07001903 int boundaryLengthSlot = -1;
1904 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001905 int boundaryWidthSlot = -1;
1906 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001907
Chet Haase5b0200b2011-04-13 17:58:08 -07001908 for (int i = 0; i < count; i += 4) {
1909 // a = start point, b = end point
1910 vec2 a(points[i], points[i + 1]);
1911 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001912
Chet Haase99585ad2011-05-02 15:00:16 -07001913 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001914 float boundaryLengthProportion = 0;
1915 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001916
Chet Haase5b0200b2011-04-13 17:58:08 -07001917 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001918 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001919 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001920 if (isAA) {
1921 float wideningFactor;
1922 if (fabs(n.x) >= fabs(n.y)) {
1923 wideningFactor = fabs(1.0f / n.x);
1924 } else {
1925 wideningFactor = fabs(1.0f / n.y);
1926 }
1927 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001928 }
Romain Guy7b631422012-04-04 11:38:54 -07001929
Chet Haase99ecdc42011-05-06 12:06:34 -07001930 if (scaled) {
1931 n.x *= inverseScaleX;
1932 n.y *= inverseScaleY;
1933 }
1934 } else if (scaled) {
1935 // Extend n by .5 pixel on each side, post-transform
1936 vec2 extendedN = n.copyNormalized();
1937 extendedN /= 2;
1938 extendedN.x *= inverseScaleX;
1939 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001940
Chet Haase99ecdc42011-05-06 12:06:34 -07001941 float extendedNLength = extendedN.length();
1942 // We need to set this value on the shader prior to drawing
1943 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1944 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001945 }
Romain Guy7b631422012-04-04 11:38:54 -07001946
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 float x = n.x;
1948 n.x = -n.y;
1949 n.y = x;
1950
Chet Haase99585ad2011-05-02 15:00:16 -07001951 // aa lines expand the endpoint vertices to encompass the AA boundary
1952 if (isAA) {
1953 vec2 abVector = (b - a);
1954 length = abVector.length();
1955 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001956
Chet Haase99ecdc42011-05-06 12:06:34 -07001957 if (scaled) {
1958 abVector.x *= inverseScaleX;
1959 abVector.y *= inverseScaleY;
1960 float abLength = abVector.length();
1961 boundaryLengthProportion = abLength / (length + abLength);
1962 } else {
1963 boundaryLengthProportion = .5 / (length + 1);
1964 }
Romain Guy7b631422012-04-04 11:38:54 -07001965
Chet Haase99ecdc42011-05-06 12:06:34 -07001966 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001967 a -= abVector;
1968 b += abVector;
1969 }
1970
Chet Haase5b0200b2011-04-13 17:58:08 -07001971 // Four corners of the rectangle defining a thick line
1972 vec2 p1 = a - n;
1973 vec2 p2 = a + n;
1974 vec2 p3 = b + n;
1975 vec2 p4 = b - n;
1976
Chet Haase99585ad2011-05-02 15:00:16 -07001977
Chet Haase5b0200b2011-04-13 17:58:08 -07001978 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1979 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1980 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1981 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1982
1983 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001984 if (!isAA) {
1985 if (prevVertex != NULL) {
1986 // Issue two repeat vertices to create degenerate triangles to bridge
1987 // between the previous line and the new one. This is necessary because
1988 // we are creating a single triangle_strip which will contain
1989 // potentially discontinuous line segments.
1990 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1991 Vertex::set(vertices++, p1.x, p1.y);
1992 generatedVerticesCount += 2;
1993 }
Romain Guy7b631422012-04-04 11:38:54 -07001994
Chet Haase5b0200b2011-04-13 17:58:08 -07001995 Vertex::set(vertices++, p1.x, p1.y);
1996 Vertex::set(vertices++, p2.x, p2.y);
1997 Vertex::set(vertices++, p4.x, p4.y);
1998 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07001999
Chet Haase5b0200b2011-04-13 17:58:08 -07002000 prevVertex = vertices - 1;
2001 generatedVerticesCount += 4;
2002 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002003 if (!isHairLine && scaled) {
2004 // Must set width proportions per-segment for scaled non-hairlines to use the
2005 // correct AA boundary dimensions
2006 if (boundaryWidthSlot < 0) {
2007 boundaryWidthSlot =
2008 mCaches.currentProgram->getUniform("boundaryWidth");
2009 inverseBoundaryWidthSlot =
2010 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2011 }
Romain Guy7b631422012-04-04 11:38:54 -07002012
Chet Haase99ecdc42011-05-06 12:06:34 -07002013 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2014 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2015 }
Romain Guy7b631422012-04-04 11:38:54 -07002016
Chet Haase99585ad2011-05-02 15:00:16 -07002017 if (boundaryLengthSlot < 0) {
2018 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2019 inverseBoundaryLengthSlot =
2020 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2021 }
Romain Guy7b631422012-04-04 11:38:54 -07002022
Chet Haase99ecdc42011-05-06 12:06:34 -07002023 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2024 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002025
Chet Haase5b0200b2011-04-13 17:58:08 -07002026 if (prevAAVertex != NULL) {
2027 // Issue two repeat vertices to create degenerate triangles to bridge
2028 // between the previous line and the new one. This is necessary because
2029 // we are creating a single triangle_strip which will contain
2030 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002031 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2032 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2033 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002034 generatedVerticesCount += 2;
2035 }
Romain Guy7b631422012-04-04 11:38:54 -07002036
Chet Haase99585ad2011-05-02 15:00:16 -07002037 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2038 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2039 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2040 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002041
Chet Haase5b0200b2011-04-13 17:58:08 -07002042 prevAAVertex = aaVertices - 1;
2043 generatedVerticesCount += 4;
2044 }
Romain Guy7b631422012-04-04 11:38:54 -07002045
Chet Haase99585ad2011-05-02 15:00:16 -07002046 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2047 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2048 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002049 }
2050 }
Romain Guy7b631422012-04-04 11:38:54 -07002051
Chet Haase5b0200b2011-04-13 17:58:08 -07002052 if (generatedVerticesCount > 0) {
2053 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2054 }
Romain Guy7b631422012-04-04 11:38:54 -07002055
2056 if (isAA) {
2057 finishDrawAALine(widthSlot, lengthSlot);
2058 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002059}
2060
Romain Guyed6fcb02011-03-21 13:11:28 -07002061void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2062 if (mSnapshot->isIgnored()) return;
2063
2064 // TODO: The paint's cap style defines whether the points are square or circular
2065 // TODO: Handle AA for round points
2066
Chet Haase5b0200b2011-04-13 17:58:08 -07002067 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002068 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002069 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002070 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002071 if (isHairLine) {
2072 // Now that we know it's hairline, we can set the effective width, to be used later
2073 strokeWidth = 1.0f;
2074 }
2075 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002076 int alpha;
2077 SkXfermode::Mode mode;
2078 getAlphaAndMode(paint, &alpha, &mode);
2079
2080 int verticesCount = count >> 1;
2081 int generatedVerticesCount = 0;
2082
2083 TextureVertex pointsData[verticesCount];
2084 TextureVertex* vertex = &pointsData[0];
2085
2086 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002087 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002088 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002089 setupDrawColor(paint->getColor(), alpha);
2090 setupDrawColorFilter();
2091 setupDrawShader();
2092 setupDrawBlending(mode);
2093 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002094 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002095 setupDrawColorUniforms();
2096 setupDrawColorFilterUniforms();
2097 setupDrawPointUniforms();
2098 setupDrawShaderIdentityUniforms();
2099 setupDrawMesh(vertex);
2100
2101 for (int i = 0; i < count; i += 2) {
2102 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2103 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002104
Chet Haase8a5cc922011-04-26 07:28:09 -07002105 float left = points[i] - halfWidth;
2106 float right = points[i] + halfWidth;
2107 float top = points[i + 1] - halfWidth;
2108 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002109
Chet Haase8a5cc922011-04-26 07:28:09 -07002110 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002111 }
2112
2113 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2114}
2115
Romain Guy85bf02f2010-06-22 13:11:24 -07002116void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002117 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002118 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002119
Romain Guyae88e5e2010-10-22 17:49:18 -07002120 Rect& clip(*mSnapshot->clipRect);
2121 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002122
Romain Guy3d58c032010-07-14 16:34:53 -07002123 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002124}
Romain Guy9d5316e2010-06-24 19:30:36 -07002125
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002126void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002127 if (!texture) return;
2128 const AutoTexture autoCleanup(texture);
2129
2130 const float x = left + texture->left - texture->offset;
2131 const float y = top + texture->top - texture->offset;
2132
2133 drawPathTexture(texture, x, y, paint);
2134}
2135
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002136void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2137 float rx, float ry, SkPaint* paint) {
2138 if (mSnapshot->isIgnored()) return;
2139
Romain Guya1d3c912011-12-13 14:55:06 -08002140 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002141 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2142 right - left, bottom - top, rx, ry, paint);
2143 drawShape(left, top, texture, paint);
2144}
2145
Romain Guy01d58e42011-01-19 21:54:02 -08002146void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2147 if (mSnapshot->isIgnored()) return;
2148
Romain Guya1d3c912011-12-13 14:55:06 -08002149 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002150 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002151 drawShape(x - radius, y - radius, texture, paint);
2152}
Romain Guy01d58e42011-01-19 21:54:02 -08002153
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002154void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2155 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002156
Romain Guya1d3c912011-12-13 14:55:06 -08002157 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002158 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2159 drawShape(left, top, texture, paint);
2160}
2161
Romain Guy8b2f5262011-01-23 16:15:02 -08002162void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2163 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2164 if (mSnapshot->isIgnored()) return;
2165
2166 if (fabs(sweepAngle) >= 360.0f) {
2167 drawOval(left, top, right, bottom, paint);
2168 return;
2169 }
2170
Romain Guya1d3c912011-12-13 14:55:06 -08002171 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002172 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2173 startAngle, sweepAngle, useCenter, paint);
2174 drawShape(left, top, texture, paint);
2175}
2176
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002177void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2178 SkPaint* paint) {
2179 if (mSnapshot->isIgnored()) return;
2180
Romain Guya1d3c912011-12-13 14:55:06 -08002181 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002182 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2183 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002184}
2185
Chet Haase5c13d892010-10-08 08:37:55 -07002186void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002187 if (p->getStyle() != SkPaint::kFill_Style) {
2188 drawRectAsShape(left, top, right, bottom, p);
2189 return;
2190 }
2191
Romain Guy6926c722010-07-12 20:20:03 -07002192 if (quickReject(left, top, right, bottom)) {
2193 return;
2194 }
2195
Romain Guy026c5e162010-06-28 17:12:22 -07002196 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002197 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002198 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2199 if (!isMode) {
2200 // Assume SRC_OVER
2201 mode = SkXfermode::kSrcOver_Mode;
2202 }
2203 } else {
2204 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002205 }
2206
Romain Guy026c5e162010-06-28 17:12:22 -07002207 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002208 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002209 drawAARect(left, top, right, bottom, color, mode);
2210 } else {
2211 drawColorRect(left, top, right, bottom, color, mode);
2212 }
Romain Guyc7d53492010-06-25 13:41:57 -07002213}
Romain Guy9d5316e2010-06-24 19:30:36 -07002214
Romain Guyeb9a5362012-01-17 17:39:26 -08002215void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2216 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002217 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2218 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002219 return;
2220 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002221
Romain Guy671d6cf2012-01-18 12:39:17 -08002222 // NOTE: Skia does not support perspective transform on drawPosText yet
2223 if (!mSnapshot->transform->isSimple()) {
2224 return;
2225 }
2226
2227 float x = 0.0f;
2228 float y = 0.0f;
2229 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2230 if (pureTranslate) {
2231 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2232 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2233 }
2234
2235 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2236 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2237 paint->getTextSize());
2238
2239 int alpha;
2240 SkXfermode::Mode mode;
2241 getAlphaAndMode(paint, &alpha, &mode);
2242
2243 // Pick the appropriate texture filtering
2244 bool linearFilter = mSnapshot->transform->changesBounds();
2245 if (pureTranslate && !linearFilter) {
2246 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2247 }
2248
2249 mCaches.activeTexture(0);
2250 setupDraw();
2251 setupDrawDirtyRegionsDisabled();
2252 setupDrawWithTexture(true);
2253 setupDrawAlpha8Color(paint->getColor(), alpha);
2254 setupDrawColorFilter();
2255 setupDrawShader();
2256 setupDrawBlending(true, mode);
2257 setupDrawProgram();
2258 setupDrawModelView(x, y, x, y, pureTranslate, true);
2259 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2260 setupDrawPureColorUniforms();
2261 setupDrawColorFilterUniforms();
2262 setupDrawShaderUniforms(pureTranslate);
2263
2264 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2265 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2266
2267#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002268 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002269#else
Romain Guy211370f2012-02-01 16:10:55 -08002270 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002271#endif
2272
2273 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2274 positions, hasActiveLayer ? &bounds : NULL)) {
2275#if RENDER_LAYERS_AS_REGIONS
2276 if (hasActiveLayer) {
2277 if (!pureTranslate) {
2278 mSnapshot->transform->mapRect(bounds);
2279 }
2280 dirtyLayerUnchecked(bounds, getRegion());
2281 }
2282#endif
2283 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002284}
2285
Romain Guye8e62a42010-07-23 18:55:21 -07002286void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002287 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002288 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2289 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002290 return;
2291 }
2292
Chet Haasea1cff502012-02-21 13:43:44 -08002293 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002294 switch (paint->getTextAlign()) {
2295 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002296 x -= length / 2.0f;
2297 break;
2298 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002299 x -= length;
2300 break;
2301 default:
2302 break;
2303 }
2304
Romain Guycac5fd32011-12-01 20:08:50 -08002305 SkPaint::FontMetrics metrics;
2306 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002307 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002308 return;
2309 }
2310
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002311 const float oldX = x;
2312 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002313 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002314 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002315 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2316 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2317 }
2318
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002319#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002320 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002321#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002322
2323 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002324 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002325 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002326
Romain Guy86568192010-12-14 15:55:39 -08002327 int alpha;
2328 SkXfermode::Mode mode;
2329 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002330
Romain Guy211370f2012-02-01 16:10:55 -08002331 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002332 mCaches.activeTexture(0);
2333
Romain Guyb45c0c92010-08-26 20:35:23 -07002334 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002335 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2336 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002337 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002338
Romain Guy740bf2b2011-04-26 15:33:10 -07002339 const float sx = oldX - shadow->left + mShadowDx;
2340 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002341
Romain Guy86568192010-12-14 15:55:39 -08002342 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002343 int shadowColor = mShadowColor;
2344 if (mShader) {
2345 shadowColor = 0xffffffff;
2346 }
Romain Guy86568192010-12-14 15:55:39 -08002347
Romain Guy86568192010-12-14 15:55:39 -08002348 setupDraw();
2349 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002350 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2351 setupDrawColorFilter();
2352 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002353 setupDrawBlending(true, mode);
2354 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002355 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002356 setupDrawTexture(shadow->id);
2357 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002358 setupDrawColorFilterUniforms();
2359 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002360 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2361
Romain Guy0a417492010-08-16 20:26:20 -07002362 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002363 }
2364
Romain Guy6620c6d2010-12-06 18:07:02 -08002365 // Pick the appropriate texture filtering
2366 bool linearFilter = mSnapshot->transform->changesBounds();
2367 if (pureTranslate && !linearFilter) {
2368 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2369 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002370
Romain Guya1d3c912011-12-13 14:55:06 -08002371 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002372 setupDraw();
2373 setupDrawDirtyRegionsDisabled();
2374 setupDrawWithTexture(true);
2375 setupDrawAlpha8Color(paint->getColor(), alpha);
2376 setupDrawColorFilter();
2377 setupDrawShader();
2378 setupDrawBlending(true, mode);
2379 setupDrawProgram();
2380 setupDrawModelView(x, y, x, y, pureTranslate, true);
2381 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2382 setupDrawPureColorUniforms();
2383 setupDrawColorFilterUniforms();
2384 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002385
Romain Guy6620c6d2010-12-06 18:07:02 -08002386 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002387 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2388
2389#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002390 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002391#else
Romain Guy211370f2012-02-01 16:10:55 -08002392 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002393#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002394
Romain Guy6620c6d2010-12-06 18:07:02 -08002395 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002396 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002397#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002398 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002399 if (!pureTranslate) {
2400 mSnapshot->transform->mapRect(bounds);
2401 }
Romain Guyf219da52011-01-16 12:54:25 -08002402 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002403 }
2404#endif
2405 }
Romain Guy694b5192010-07-21 21:33:20 -07002406
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002407 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002408}
2409
Romain Guy325740f2012-02-24 16:48:34 -08002410void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2411 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002412 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2413 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2414 return;
2415 }
2416
Romain Guy03d58522012-02-24 17:54:07 -08002417 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2418 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2419 paint->getTextSize());
2420
2421 int alpha;
2422 SkXfermode::Mode mode;
2423 getAlphaAndMode(paint, &alpha, &mode);
2424
2425 mCaches.activeTexture(0);
2426 setupDraw();
2427 setupDrawDirtyRegionsDisabled();
2428 setupDrawWithTexture(true);
2429 setupDrawAlpha8Color(paint->getColor(), alpha);
2430 setupDrawColorFilter();
2431 setupDrawShader();
2432 setupDrawBlending(true, mode);
2433 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002434 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002435 setupDrawTexture(fontRenderer.getTexture(true));
2436 setupDrawPureColorUniforms();
2437 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002438 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002439
Romain Guy97771732012-02-28 18:17:02 -08002440 const Rect* clip = &mSnapshot->getLocalClip();
2441 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 -08002442
Romain Guy97771732012-02-28 18:17:02 -08002443#if RENDER_LAYERS_AS_REGIONS
2444 const bool hasActiveLayer = hasLayer();
2445#else
2446 const bool hasActiveLayer = false;
2447#endif
2448
2449 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2450 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2451#if RENDER_LAYERS_AS_REGIONS
2452 if (hasActiveLayer) {
2453 mSnapshot->transform->mapRect(bounds);
2454 dirtyLayerUnchecked(bounds, getRegion());
2455 }
2456#endif
2457 }
Romain Guy325740f2012-02-24 16:48:34 -08002458}
2459
Romain Guy7fbcc042010-08-04 15:40:07 -07002460void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002461 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002462
Romain Guya1d3c912011-12-13 14:55:06 -08002463 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002464
Romain Guy33f6beb2012-02-16 19:24:51 -08002465 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002466 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002467 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002468 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002469
Romain Guy8b55f372010-08-18 17:10:07 -07002470 const float x = texture->left - texture->offset;
2471 const float y = texture->top - texture->offset;
2472
Romain Guy01d58e42011-01-19 21:54:02 -08002473 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002474}
2475
Romain Guyada830f2011-01-13 12:13:20 -08002476void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2477 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002478 return;
2479 }
2480
Romain Guy2bf68f02012-03-02 13:37:47 -08002481 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2482 OpenGLRenderer* renderer = layer->renderer;
2483 Rect& dirty = layer->dirtyRect;
2484
2485 interrupt();
2486 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2487 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002488 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002489 renderer->finish();
2490 resume();
2491
2492 dirty.setEmpty();
2493 layer->deferredUpdateScheduled = false;
2494 layer->renderer = NULL;
2495 layer->displayList = NULL;
2496 }
2497
Romain Guya1d3c912011-12-13 14:55:06 -08002498 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002499
2500 int alpha;
2501 SkXfermode::Mode mode;
2502 getAlphaAndMode(paint, &alpha, &mode);
2503
Romain Guy9ace8f52011-07-07 20:50:11 -07002504 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002505
Romain Guyf219da52011-01-16 12:54:25 -08002506#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002507 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002508 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002509 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002510 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002511 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002512 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002513
Romain Guyc88e3572011-01-22 00:32:12 -08002514 setupDraw();
2515 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002516 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002517 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002518 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002519 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002520 setupDrawPureColorUniforms();
2521 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002522 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002523 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002524 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2525 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2526
Romain Guyd21b6e12011-11-30 20:21:23 -08002527 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002528 setupDrawModelViewTranslate(x, y,
2529 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2530 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002531 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002532 setupDrawModelViewTranslate(x, y,
2533 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2534 }
Romain Guyc88e3572011-01-22 00:32:12 -08002535 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002536
Romain Guyc88e3572011-01-22 00:32:12 -08002537 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2538 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002539
Romain Guyc88e3572011-01-22 00:32:12 -08002540 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002541
2542#if DEBUG_LAYERS_AS_REGIONS
2543 drawRegionRects(layer->region);
2544#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002545 }
Romain Guyf219da52011-01-16 12:54:25 -08002546 }
2547#else
Romain Guyada830f2011-01-13 12:13:20 -08002548 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2549 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002550#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002551}
2552
Romain Guy6926c722010-07-12 20:20:03 -07002553///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002554// Shaders
2555///////////////////////////////////////////////////////////////////////////////
2556
2557void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002558 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002559}
2560
Romain Guy06f96e22010-07-30 19:18:16 -07002561void OpenGLRenderer::setupShader(SkiaShader* shader) {
2562 mShader = shader;
2563 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002564 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002565 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002566}
2567
Romain Guyd27977d2010-07-14 19:18:51 -07002568///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002569// Color filters
2570///////////////////////////////////////////////////////////////////////////////
2571
2572void OpenGLRenderer::resetColorFilter() {
2573 mColorFilter = NULL;
2574}
2575
2576void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2577 mColorFilter = filter;
2578}
2579
2580///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002581// Drop shadow
2582///////////////////////////////////////////////////////////////////////////////
2583
2584void OpenGLRenderer::resetShadow() {
2585 mHasShadow = false;
2586}
2587
2588void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2589 mHasShadow = true;
2590 mShadowRadius = radius;
2591 mShadowDx = dx;
2592 mShadowDy = dy;
2593 mShadowColor = color;
2594}
2595
2596///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002597// Draw filters
2598///////////////////////////////////////////////////////////////////////////////
2599
2600void OpenGLRenderer::resetPaintFilter() {
2601 mHasDrawFilter = false;
2602}
2603
2604void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2605 mHasDrawFilter = true;
2606 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2607 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2608}
2609
2610SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002611 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002612
2613 uint32_t flags = paint->getFlags();
2614
2615 mFilteredPaint = *paint;
2616 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2617
2618 return &mFilteredPaint;
2619}
2620
2621///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002622// Drawing implementation
2623///////////////////////////////////////////////////////////////////////////////
2624
Romain Guy01d58e42011-01-19 21:54:02 -08002625void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2626 float x, float y, SkPaint* paint) {
2627 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2628 return;
2629 }
2630
2631 int alpha;
2632 SkXfermode::Mode mode;
2633 getAlphaAndMode(paint, &alpha, &mode);
2634
2635 setupDraw();
2636 setupDrawWithTexture(true);
2637 setupDrawAlpha8Color(paint->getColor(), alpha);
2638 setupDrawColorFilter();
2639 setupDrawShader();
2640 setupDrawBlending(true, mode);
2641 setupDrawProgram();
2642 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2643 setupDrawTexture(texture->id);
2644 setupDrawPureColorUniforms();
2645 setupDrawColorFilterUniforms();
2646 setupDrawShaderUniforms();
2647 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2648
2649 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2650
2651 finishDrawTexture();
2652}
2653
Romain Guyf607bdc2010-09-10 19:20:06 -07002654// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002655#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2656#define kStdUnderline_Offset (1.0f / 9.0f)
2657#define kStdUnderline_Thickness (1.0f / 18.0f)
2658
2659void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2660 float x, float y, SkPaint* paint) {
2661 // Handle underline and strike-through
2662 uint32_t flags = paint->getFlags();
2663 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002664 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002665 float underlineWidth = length;
2666 // If length is > 0.0f, we already measured the text for the text alignment
2667 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002668 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002669 }
2670
2671 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002672 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002673 case SkPaint::kCenter_Align:
2674 offsetX = underlineWidth * 0.5f;
2675 break;
2676 case SkPaint::kRight_Align:
2677 offsetX = underlineWidth;
2678 break;
2679 default:
2680 break;
2681 }
2682
Romain Guy211370f2012-02-01 16:10:55 -08002683 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002684 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002685 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002686
Romain Guye20ecbd2010-09-22 19:49:04 -07002687 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002688 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002689
Romain Guyf6834472011-01-23 13:32:12 -08002690 int linesCount = 0;
2691 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2692 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2693
2694 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002695 float points[pointsCount];
2696 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002697
2698 if (flags & SkPaint::kUnderlineText_Flag) {
2699 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002700 points[currentPoint++] = left;
2701 points[currentPoint++] = top;
2702 points[currentPoint++] = left + underlineWidth;
2703 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002704 }
2705
2706 if (flags & SkPaint::kStrikeThruText_Flag) {
2707 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002708 points[currentPoint++] = left;
2709 points[currentPoint++] = top;
2710 points[currentPoint++] = left + underlineWidth;
2711 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002712 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002713
Romain Guy726aeba2011-06-01 14:52:00 -07002714 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002715
Romain Guy726aeba2011-06-01 14:52:00 -07002716 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002717 }
2718 }
2719}
2720
Romain Guy026c5e162010-06-28 17:12:22 -07002721void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002722 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002723 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002724 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002725 color |= 0x00ffffff;
2726 }
2727
Romain Guy70ca14e2010-12-13 18:24:33 -08002728 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002729 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002730 setupDrawColor(color);
2731 setupDrawShader();
2732 setupDrawColorFilter();
2733 setupDrawBlending(mode);
2734 setupDrawProgram();
2735 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2736 setupDrawColorUniforms();
2737 setupDrawShaderUniforms(ignoreTransform);
2738 setupDrawColorFilterUniforms();
2739 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002740
Romain Guyc95c8d62010-09-17 15:31:32 -07002741 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2742}
2743
Romain Guy82ba8142010-07-09 13:25:56 -07002744void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002745 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002746 int alpha;
2747 SkXfermode::Mode mode;
2748 getAlphaAndMode(paint, &alpha, &mode);
2749
Romain Guyd21b6e12011-11-30 20:21:23 -08002750 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002751
Romain Guy211370f2012-02-01 16:10:55 -08002752 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002753 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2754 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2755
Romain Guyd21b6e12011-11-30 20:21:23 -08002756 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002757 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2758 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2759 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2760 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002761 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002762 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2763 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2764 GL_TRIANGLE_STRIP, gMeshCount);
2765 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002766}
2767
Romain Guybd6b79b2010-06-26 00:13:53 -07002768void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002769 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2770 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002771 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002772}
2773
2774void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002775 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002776 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002777 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002778
Romain Guy746b7402010-10-26 16:27:31 -07002779 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002780 setupDrawWithTexture();
2781 setupDrawColor(alpha, alpha, alpha, alpha);
2782 setupDrawColorFilter();
2783 setupDrawBlending(blend, mode, swapSrcDst);
2784 setupDrawProgram();
2785 if (!dirty) {
2786 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002787 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002788 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002789 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002790 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002791 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002792 }
Romain Guy86568192010-12-14 15:55:39 -08002793 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002794 setupDrawColorFilterUniforms();
2795 setupDrawTexture(texture);
2796 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002797
Romain Guy6820ac82010-09-15 18:11:50 -07002798 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002799
2800 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002801}
2802
Romain Guya5aed0d2010-09-09 14:42:43 -07002803void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002804 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002805 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002806
Romain Guy82ba8142010-07-09 13:25:56 -07002807 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002808 // These blend modes are not supported by OpenGL directly and have
2809 // to be implemented using shaders. Since the shader will perform
2810 // the blending, turn blending off here
2811 // If the blend mode cannot be implemented using shaders, fall
2812 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002813 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2814 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002815 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002816 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002817
Romain Guy82bc7a72012-01-03 14:13:39 -08002818 if (mCaches.blend) {
2819 glDisable(GL_BLEND);
2820 mCaches.blend = false;
2821 }
2822
2823 return;
2824 } else {
2825 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002826 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002827 }
2828
2829 if (!mCaches.blend) {
2830 glEnable(GL_BLEND);
2831 }
2832
2833 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2834 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2835
2836 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2837 glBlendFunc(sourceMode, destMode);
2838 mCaches.lastSrcMode = sourceMode;
2839 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002840 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002841 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002842 glDisable(GL_BLEND);
2843 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002844 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002845}
2846
Romain Guy889f8d12010-07-29 14:37:42 -07002847bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002848 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002849 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002850 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002851 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002852 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002853 }
Romain Guy6926c722010-07-12 20:20:03 -07002854 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002855}
2856
Romain Guy026c5e162010-06-28 17:12:22 -07002857void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002858 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002859 TextureVertex::setUV(v++, u1, v1);
2860 TextureVertex::setUV(v++, u2, v1);
2861 TextureVertex::setUV(v++, u1, v2);
2862 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002863}
2864
Chet Haase5c13d892010-10-08 08:37:55 -07002865void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002866 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002867 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002868
2869 // Skia draws using the color's alpha channel if < 255
2870 // Otherwise, it uses the paint's alpha
2871 int color = paint->getColor();
2872 *alpha = (color >> 24) & 0xFF;
2873 if (*alpha == 255) {
2874 *alpha = paint->getAlpha();
2875 }
2876 } else {
2877 *mode = SkXfermode::kSrcOver_Mode;
2878 *alpha = 255;
2879 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002880 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002881}
2882
Romain Guya5aed0d2010-09-09 14:42:43 -07002883SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002884 SkXfermode::Mode resultMode;
2885 if (!SkXfermode::AsMode(mode, &resultMode)) {
2886 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002887 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002888 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002889}
2890
Romain Guy9d5316e2010-06-24 19:30:36 -07002891}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002892}; // namespace android