blob: a6e786274f1744798a927aa4338755aad3f5886e [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 Guy49c5fc02012-05-15 11:10:01 -0700147bool OpenGLRenderer::isDeferred() {
148 return false;
149}
150
Romain Guy85bf02f2010-06-22 13:11:24 -0700151void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700152 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700153
154 mWidth = width;
155 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700156
157 mFirstSnapshot->height = height;
158 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800163
Romain Guy3e263fa2011-12-12 16:47:48 -0800164 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700165}
166
Romain Guy6b7bd242010-10-06 19:49:23 -0700167void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800168 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
169}
170
171void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800172 mCaches.clearGarbage();
173
Romain Guy8aef54f2010-09-01 15:13:49 -0700174 mSnapshot = new Snapshot(mFirstSnapshot,
175 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800176 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700177 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700178
Romain Guy39d252a2011-12-12 18:14:06 -0800179 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800180 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800181
Romain Guy7d7b5492011-01-24 16:33:45 -0800182 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800183 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800184
Romain Guy6b7bd242010-10-06 19:49:23 -0700185 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 glClear(GL_COLOR_BUFFER_BIT);
187 }
Romain Guybb9524b2010-06-22 18:56:38 -0700188}
189
Romain Guyb025b9c2010-09-16 14:16:48 -0700190void OpenGLRenderer::finish() {
191#if DEBUG_OPENGL
192 GLenum status = GL_NO_ERROR;
193 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000194 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800195 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700196 case GL_INVALID_ENUM:
197 ALOGE(" GL_INVALID_ENUM");
198 break;
199 case GL_INVALID_VALUE:
200 ALOGE(" GL_INVALID_VALUE");
201 break;
202 case GL_INVALID_OPERATION:
203 ALOGE(" GL_INVALID_OPERATION");
204 break;
Romain Guya07105b2011-01-10 21:14:18 -0800205 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700206 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800207 break;
208 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700209 }
210#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800211#if DEBUG_MEMORY_USAGE
212 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800213#else
214 if (mCaches.getDebugLevel() & kDebugMemory) {
215 mCaches.dumpMemoryUsage();
216 }
Romain Guyc15008e2010-11-10 11:59:15 -0800217#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700218}
219
Romain Guy6c319ca2011-01-11 14:29:25 -0800220void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700221 if (mCaches.currentProgram) {
222 if (mCaches.currentProgram->isInUse()) {
223 mCaches.currentProgram->remove();
224 mCaches.currentProgram = NULL;
225 }
226 }
Romain Guy50c0f092010-10-19 11:42:22 -0700227 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800228 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800229 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800230 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700231}
232
Romain Guy6c319ca2011-01-11 14:29:25 -0800233void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800234 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
235
236 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800237 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
238
Romain Guyda8532c2010-08-31 11:50:35 -0700239 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800240 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700241 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700242
Romain Guya1d3c912011-12-13 14:55:06 -0800243 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800244 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700245
Romain Guy50c0f092010-10-19 11:42:22 -0700246 mCaches.blend = true;
247 glEnable(GL_BLEND);
248 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
249 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700250}
251
Romain Guyba6be8a2012-04-23 18:22:09 -0700252void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craikf8dafa12012-05-22 13:29:40 -0700253 ALOGD("opengl renderer %p detaching functor %p", this, functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700254 mFunctors.remove(functor);
255}
256
257void OpenGLRenderer::attachFunctor(Functor* functor) {
258 mFunctors.add(functor);
259}
260
Romain Guy8f3b8e32012-03-27 16:33:45 -0700261status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
262 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700263 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700264
Romain Guyba6be8a2012-04-23 18:22:09 -0700265 if (count > 0) {
266 SortedVector<Functor*> functors(mFunctors);
267 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700268
Romain Guyba6be8a2012-04-23 18:22:09 -0700269 DrawGlInfo info;
270 info.clipLeft = 0;
271 info.clipTop = 0;
272 info.clipRight = 0;
273 info.clipBottom = 0;
274 info.isLayer = false;
275 info.width = 0;
276 info.height = 0;
277 memset(info.transform, 0, sizeof(float) * 16);
278
279 for (size_t i = 0; i < count; i++) {
280 Functor* f = functors.itemAt(i);
281 result |= (*f)(DrawGlInfo::kModeProcess, &info);
282
Chris Craikc2c95432012-04-25 15:13:52 -0700283 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700284 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
285 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700286 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700287
Chris Craikc2c95432012-04-25 15:13:52 -0700288 if (result & DrawGlInfo::kStatusInvoke) {
289 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700290 }
291 }
292 }
293
Romain Guyc189ef52012-04-25 20:02:53 -0700294 // Restore state possibly changed by the functors in process mode
295 GLboolean value;
296 glGetBooleanv(GL_BLEND, &value);
297 mCaches.blend = value;
298
299 mCaches.activeTexture(0);
300
Romain Guy8f3b8e32012-03-27 16:33:45 -0700301 return result;
302}
303
304status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800305 interrupt();
Chris Craikf8dafa12012-05-22 13:29:40 -0700306 mFunctors.remove(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700307
Romain Guyf90f8172011-01-25 22:53:24 -0800308 if (mDirtyClip) {
309 setScissorFromClip();
310 }
Romain Guyd643bb52011-03-01 14:55:21 -0800311
Romain Guy80911b82011-03-16 15:30:12 -0700312 Rect clip(*mSnapshot->clipRect);
313 clip.snapToPixelBoundaries();
314
Romain Guyd643bb52011-03-01 14:55:21 -0800315#if RENDER_LAYERS_AS_REGIONS
316 // Since we don't know what the functor will draw, let's dirty
317 // tne entire clip region
318 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800319 dirtyLayerUnchecked(clip, getRegion());
320 }
321#endif
322
Romain Guy08aa2cb2011-03-17 11:06:57 -0700323 DrawGlInfo info;
324 info.clipLeft = clip.left;
325 info.clipTop = clip.top;
326 info.clipRight = clip.right;
327 info.clipBottom = clip.bottom;
328 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700329 info.width = getSnapshot()->viewport.getWidth();
330 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700331 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700332
Romain Guy8f3b8e32012-03-27 16:33:45 -0700333 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800334
Romain Guy8f3b8e32012-03-27 16:33:45 -0700335 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700336 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800337 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700338
Chris Craik65924a32012-04-05 17:52:11 -0700339 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700340 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700341 }
Romain Guycabfcc12011-03-07 18:06:46 -0800342 }
343
Chet Haasedaf98e92011-01-10 14:10:36 -0800344 resume();
Romain Guy65549432012-03-26 16:45:05 -0700345 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800346}
347
Romain Guyf6a11b82010-06-23 17:47:49 -0700348///////////////////////////////////////////////////////////////////////////////
349// State management
350///////////////////////////////////////////////////////////////////////////////
351
Romain Guybb9524b2010-06-22 18:56:38 -0700352int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700353 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700354}
355
356int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700358}
359
360void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700361 if (mSaveCount > 1) {
362 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700363 }
Romain Guybb9524b2010-06-22 18:56:38 -0700364}
365
366void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700367 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700368
Romain Guy8fb95422010-08-17 18:38:51 -0700369 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700370 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700371 }
Romain Guybb9524b2010-06-22 18:56:38 -0700372}
373
Romain Guy8aef54f2010-09-01 15:13:49 -0700374int OpenGLRenderer::saveSnapshot(int flags) {
375 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700376 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700377}
378
379bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700380 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700381 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700382 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700383
Romain Guybd6b79b2010-06-26 00:13:53 -0700384 sp<Snapshot> current = mSnapshot;
385 sp<Snapshot> previous = mSnapshot->previous;
386
Romain Guyeb993562010-10-05 18:14:38 -0700387 if (restoreOrtho) {
388 Rect& r = previous->viewport;
389 glViewport(r.left, r.top, r.right, r.bottom);
390 mOrthoMatrix.load(current->orthoMatrix);
391 }
392
Romain Guy8b55f372010-08-18 17:10:07 -0700393 mSaveCount--;
394 mSnapshot = previous;
395
Romain Guy2542d192010-08-18 11:47:12 -0700396 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700397 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700398 }
Romain Guy2542d192010-08-18 11:47:12 -0700399
Romain Guy5ec99242010-11-03 16:19:08 -0700400 if (restoreLayer) {
401 composeLayer(current, previous);
402 }
403
Romain Guy2542d192010-08-18 11:47:12 -0700404 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700405}
406
Romain Guyf6a11b82010-06-23 17:47:49 -0700407///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700408// Layers
409///////////////////////////////////////////////////////////////////////////////
410
411int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700412 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700413 const GLuint previousFbo = mSnapshot->fbo;
414 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700415
Romain Guyaf636eb2010-12-09 17:47:21 -0800416 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700417 int alpha = 255;
418 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700419
Romain Guye45362c2010-11-03 19:58:32 -0700420 if (p) {
421 alpha = p->getAlpha();
422 if (!mCaches.extensions.hasFramebufferFetch()) {
423 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
424 if (!isMode) {
425 // Assume SRC_OVER
426 mode = SkXfermode::kSrcOver_Mode;
427 }
428 } else {
429 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700430 }
431 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700432 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700433 }
Romain Guyd55a8612010-06-28 17:42:46 -0700434
Romain Guydbc26d22010-10-11 17:58:29 -0700435 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
436 }
Romain Guyd55a8612010-06-28 17:42:46 -0700437
438 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700439}
440
441int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
442 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700443 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700444 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700445 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700446 SkPaint paint;
447 paint.setAlpha(alpha);
448 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700449 }
Romain Guyd55a8612010-06-28 17:42:46 -0700450}
Romain Guybd6b79b2010-06-26 00:13:53 -0700451
Romain Guy1c740bc2010-09-13 18:00:09 -0700452/**
453 * Layers are viewed by Skia are slightly different than layers in image editing
454 * programs (for instance.) When a layer is created, previously created layers
455 * and the frame buffer still receive every drawing command. For instance, if a
456 * layer is created and a shape intersecting the bounds of the layers and the
457 * framebuffer is draw, the shape will be drawn on both (unless the layer was
458 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
459 *
460 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
461 * texture. Unfortunately, this is inefficient as it requires every primitive to
462 * be drawn n + 1 times, where n is the number of active layers. In practice this
463 * means, for every primitive:
464 * - Switch active frame buffer
465 * - Change viewport, clip and projection matrix
466 * - Issue the drawing
467 *
468 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700469 * To avoid this, layers are implemented in a different way here, at least in the
470 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
471 * is set. When this flag is set we can redirect all drawing operations into a
472 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700473 *
474 * This implementation relies on the frame buffer being at least RGBA 8888. When
475 * a layer is created, only a texture is created, not an FBO. The content of the
476 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700477 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700478 * buffer and drawing continues as normal. This technique therefore treats the
479 * frame buffer as a scratch buffer for the layers.
480 *
481 * To compose the layers back onto the frame buffer, each layer texture
482 * (containing the original frame buffer data) is drawn as a simple quad over
483 * the frame buffer. The trick is that the quad is set as the composition
484 * destination in the blending equation, and the frame buffer becomes the source
485 * of the composition.
486 *
487 * Drawing layers with an alpha value requires an extra step before composition.
488 * An empty quad is drawn over the layer's region in the frame buffer. This quad
489 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
490 * quad is used to multiply the colors in the frame buffer. This is achieved by
491 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
492 * GL_ZERO, GL_SRC_ALPHA.
493 *
494 * Because glCopyTexImage2D() can be slow, an alternative implementation might
495 * be use to draw a single clipped layer. The implementation described above
496 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700497 *
498 * (1) The frame buffer is actually not cleared right away. To allow the GPU
499 * to potentially optimize series of calls to glCopyTexImage2D, the frame
500 * buffer is left untouched until the first drawing operation. Only when
501 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700502 */
Romain Guyd55a8612010-06-28 17:42:46 -0700503bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700504 float right, float bottom, int alpha, SkXfermode::Mode mode,
505 int flags, GLuint previousFbo) {
506 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700507 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700508
Romain Guyeb993562010-10-05 18:14:38 -0700509 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
510
Romain Guyf607bdc2010-09-10 19:20:06 -0700511 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700512 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700513 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700514 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700515
Romain Guyeb993562010-10-05 18:14:38 -0700516 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700517 if (bounds.intersect(*snapshot->clipRect)) {
518 // We cannot work with sub-pixels in this case
519 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700520
Romain Guyad37cd32011-03-15 11:12:25 -0700521 // When the layer is not an FBO, we may use glCopyTexImage so we
522 // need to make sure the layer does not extend outside the bounds
523 // of the framebuffer
524 if (!bounds.intersect(snapshot->previous->viewport)) {
525 bounds.setEmpty();
526 }
527 } else {
528 bounds.setEmpty();
529 }
Romain Guyeb993562010-10-05 18:14:38 -0700530 }
Romain Guybf434112010-09-16 14:40:17 -0700531
Romain Guy746b7402010-10-26 16:27:31 -0700532 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
533 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800534 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700535 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700536 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700537 }
538
539 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800540 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700541 return false;
542 }
Romain Guyf18fd992010-07-08 11:45:51 -0700543
Romain Guya1d3c912011-12-13 14:55:06 -0800544 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700545 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700546 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700547 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700548 }
549
Romain Guy9ace8f52011-07-07 20:50:11 -0700550 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700551 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700552 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
553 bounds.getWidth() / float(layer->getWidth()), 0.0f);
554 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700555 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700556
Romain Guy8fb95422010-08-17 18:38:51 -0700557 // Save the layer in the snapshot
558 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700559 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700560
Romain Guyeb993562010-10-05 18:14:38 -0700561 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700562 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700563 } else {
564 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700565 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800566 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700567 if (layer->isEmpty()) {
568 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
569 bounds.left, snapshot->height - bounds.bottom,
570 layer->getWidth(), layer->getHeight(), 0);
571 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800572 } else {
573 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
574 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
575 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700576
Romain Guy54be1cd2011-06-13 19:04:27 -0700577 // Enqueue the buffer coordinates to clear the corresponding region later
578 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700579 }
Romain Guyeb993562010-10-05 18:14:38 -0700580 }
Romain Guyf86ef572010-07-01 11:05:42 -0700581
Romain Guyd55a8612010-06-28 17:42:46 -0700582 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700583}
584
Romain Guy5b3b3522010-10-27 18:57:51 -0700585bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
586 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700587 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700588
589#if RENDER_LAYERS_AS_REGIONS
590 snapshot->region = &snapshot->layer->region;
591 snapshot->flags |= Snapshot::kFlagFboTarget;
592#endif
593
594 Rect clip(bounds);
595 snapshot->transform->mapRect(clip);
596 clip.intersect(*snapshot->clipRect);
597 clip.snapToPixelBoundaries();
598 clip.intersect(snapshot->previous->viewport);
599
600 mat4 inverse;
601 inverse.loadInverse(*mSnapshot->transform);
602
603 inverse.mapRect(clip);
604 clip.snapToPixelBoundaries();
605 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700606 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700607
608 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700611 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
612 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
613 snapshot->height = bounds.getHeight();
614 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
615 snapshot->orthoMatrix.load(mOrthoMatrix);
616
617 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700618 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
619 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700620
621 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700622 if (layer->isEmpty()) {
623 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
624 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700625 }
626
627 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700629
630#if DEBUG_LAYERS_AS_REGIONS
631 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
632 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000633 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700634
635 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700636 layer->deleteTexture();
637 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700638
639 delete layer;
640
641 return false;
642 }
643#endif
644
645 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800646 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700647 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700648 glClear(GL_COLOR_BUFFER_BIT);
649
650 dirtyClip();
651
652 // Change the ortho projection
653 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
654 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
655
656 return true;
657}
658
Romain Guy1c740bc2010-09-13 18:00:09 -0700659/**
660 * Read the documentation of createLayer() before doing anything in this method.
661 */
Romain Guy1d83e192010-08-17 11:37:00 -0700662void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
663 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000664 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700665 return;
666 }
667
Romain Guy5b3b3522010-10-27 18:57:51 -0700668 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700669
670 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700671 // Detach the texture from the FBO
672 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
673
Romain Guyeb993562010-10-05 18:14:38 -0700674 // Unbind current FBO and restore previous one
675 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
676 }
677
Romain Guy1d83e192010-08-17 11:37:00 -0700678 Layer* layer = current->layer;
679 const Rect& rect = layer->layer;
680
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700682 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700684 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700686 }
687
Romain Guy03750a02010-10-18 14:06:08 -0700688 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700689
Romain Guya1d3c912011-12-13 14:55:06 -0800690 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700691
Romain Guy5b3b3522010-10-27 18:57:51 -0700692 // When the layer is stored in an FBO, we can save a bit of fillrate by
693 // drawing only the dirty region
694 if (fboLayer) {
695 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 if (layer->getColorFilter()) {
697 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800698 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700700 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800701 resetColorFilter();
702 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 } else if (!rect.isEmpty()) {
704 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
705 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700706 }
Romain Guy8b55f372010-08-18 17:10:07 -0700707
Romain Guyeb993562010-10-05 18:14:38 -0700708 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800709 // Note: No need to use glDiscardFramebufferEXT() since we never
710 // create/compose layers that are not on screen with this
711 // code path
712 // See LayerRenderer::destroyLayer(Layer*)
713
Romain Guyeb993562010-10-05 18:14:38 -0700714 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
715 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700716 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700717 }
718
Romain Guy746b7402010-10-26 16:27:31 -0700719 dirtyClip();
720
Romain Guyeb993562010-10-05 18:14:38 -0700721 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700722 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700723 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700724 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700725 delete layer;
726 }
727}
728
Romain Guyaa6c24c2011-04-28 18:40:04 -0700729void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700730 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700731
Romain Guy302a9df2011-08-16 13:55:02 -0700732 mat4& transform = layer->getTransform();
733 if (!transform.isIdentity()) {
734 save(0);
735 mSnapshot->transform->multiply(transform);
736 }
737
Romain Guyaa6c24c2011-04-28 18:40:04 -0700738 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700740 setupDrawWithTexture();
741 } else {
742 setupDrawWithExternalTexture();
743 }
744 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700745 setupDrawColor(alpha, alpha, alpha, alpha);
746 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700747 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700748 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700749 setupDrawPureColorUniforms();
750 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
752 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700753 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700754 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700755 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700756 if (mSnapshot->transform->isPureTranslate() &&
757 layer->getWidth() == (uint32_t) rect.getWidth() &&
758 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700759 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
760 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
761
Romain Guyd21b6e12011-11-30 20:21:23 -0800762 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
764 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800765 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
767 }
768 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700769 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
770
771 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
772
773 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700774
775 if (!transform.isIdentity()) {
776 restore();
777 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700778}
779
Romain Guy5b3b3522010-10-27 18:57:51 -0700780void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700781 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700782 const Rect& texCoords = layer->texCoords;
783 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
784 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 float x = rect.left;
787 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700788 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700789 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700790 layer->getHeight() == (uint32_t) rect.getHeight();
791
792 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700793 // When we're swapping, the layer is already in screen coordinates
794 if (!swap) {
795 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
796 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
797 }
798
Romain Guyd21b6e12011-11-30 20:21:23 -0800799 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700800 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800801 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700802 }
803
804 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
805 layer->getTexture(), layer->getAlpha() / 255.0f,
806 layer->getMode(), layer->isBlend(),
807 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
808 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
Romain Guyaa6c24c2011-04-28 18:40:04 -0700810 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
811 } else {
812 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
813 drawTextureLayer(layer, rect);
814 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
815 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700816}
817
818void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
819#if RENDER_LAYERS_AS_REGIONS
820 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700821 layer->setRegionAsRect();
822
Romain Guy40667672011-03-18 14:34:03 -0700823 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700824
Romain Guy5b3b3522010-10-27 18:57:51 -0700825 layer->region.clear();
826 return;
827 }
828
Romain Guy8a3957d2011-09-07 17:55:15 -0700829 // TODO: See LayerRenderer.cpp::generateMesh() for important
830 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800831 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 size_t count;
833 const android::Rect* rects = layer->region.getArray(&count);
834
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 const float alpha = layer->getAlpha() / 255.0f;
836 const float texX = 1.0f / float(layer->getWidth());
837 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800838 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700839
840 TextureVertex* mesh = mCaches.getRegionMesh();
841 GLsizei numQuads = 0;
842
Romain Guy7230a742011-01-10 22:26:16 -0800843 setupDraw();
844 setupDrawWithTexture();
845 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800846 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700847 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800848 setupDrawProgram();
849 setupDrawDirtyRegionsDisabled();
850 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800851 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 setupDrawTexture(layer->getTexture());
853 if (mSnapshot->transform->isPureTranslate()) {
854 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
855 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
856
Romain Guyd21b6e12011-11-30 20:21:23 -0800857 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700858 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
859 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800860 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700861 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
862 }
Romain Guy15bc6432011-12-13 13:11:32 -0800863 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700864
865 for (size_t i = 0; i < count; i++) {
866 const android::Rect* r = &rects[i];
867
868 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800869 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700870 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800871 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700872
873 // TODO: Reject quads outside of the clip
874 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
875 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
876 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
877 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
878
879 numQuads++;
880
881 if (numQuads >= REGION_MESH_QUAD_COUNT) {
882 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
883 numQuads = 0;
884 mesh = mCaches.getRegionMesh();
885 }
886 }
887
888 if (numQuads > 0) {
889 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
890 }
891
Romain Guy7230a742011-01-10 22:26:16 -0800892 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700893
894#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800895 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700896#endif
897
898 layer->region.clear();
899 }
900#else
901 composeLayerRect(layer, rect);
902#endif
903}
904
Romain Guy3a3133d2011-02-01 22:59:58 -0800905void OpenGLRenderer::drawRegionRects(const Region& region) {
906#if DEBUG_LAYERS_AS_REGIONS
907 size_t count;
908 const android::Rect* rects = region.getArray(&count);
909
910 uint32_t colors[] = {
911 0x7fff0000, 0x7f00ff00,
912 0x7f0000ff, 0x7fff00ff,
913 };
914
915 int offset = 0;
916 int32_t top = rects[0].top;
917
918 for (size_t i = 0; i < count; i++) {
919 if (top != rects[i].top) {
920 offset ^= 0x2;
921 top = rects[i].top;
922 }
923
924 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
925 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
926 SkXfermode::kSrcOver_Mode);
927 }
928#endif
929}
930
Romain Guy5b3b3522010-10-27 18:57:51 -0700931void OpenGLRenderer::dirtyLayer(const float left, const float top,
932 const float right, const float bottom, const mat4 transform) {
933#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800934 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700935 Rect bounds(left, top, right, bottom);
936 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800937 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 }
939#endif
940}
941
942void OpenGLRenderer::dirtyLayer(const float left, const float top,
943 const float right, const float bottom) {
944#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800945 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700946 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800947 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800948 }
949#endif
950}
951
952void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
953#if RENDER_LAYERS_AS_REGIONS
954 if (bounds.intersect(*mSnapshot->clipRect)) {
955 bounds.snapToPixelBoundaries();
956 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
957 if (!dirty.isEmpty()) {
958 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700959 }
960 }
961#endif
962}
963
Romain Guy54be1cd2011-06-13 19:04:27 -0700964void OpenGLRenderer::clearLayerRegions() {
965 const size_t count = mLayers.size();
966 if (count == 0) return;
967
968 if (!mSnapshot->isIgnored()) {
969 // Doing several glScissor/glClear here can negatively impact
970 // GPUs with a tiler architecture, instead we draw quads with
971 // the Clear blending mode
972
973 // The list contains bounds that have already been clipped
974 // against their initial clip rect, and the current clip
975 // is likely different so we need to disable clipping here
976 glDisable(GL_SCISSOR_TEST);
977
978 Vertex mesh[count * 6];
979 Vertex* vertex = mesh;
980
981 for (uint32_t i = 0; i < count; i++) {
982 Rect* bounds = mLayers.itemAt(i);
983
984 Vertex::set(vertex++, bounds->left, bounds->bottom);
985 Vertex::set(vertex++, bounds->left, bounds->top);
986 Vertex::set(vertex++, bounds->right, bounds->top);
987 Vertex::set(vertex++, bounds->left, bounds->bottom);
988 Vertex::set(vertex++, bounds->right, bounds->top);
989 Vertex::set(vertex++, bounds->right, bounds->bottom);
990
991 delete bounds;
992 }
993
994 setupDraw(false);
995 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
996 setupDrawBlending(true, SkXfermode::kClear_Mode);
997 setupDrawProgram();
998 setupDrawPureColorUniforms();
999 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001000 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001001
Romain Guy54be1cd2011-06-13 19:04:27 -07001002 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1003
1004 glEnable(GL_SCISSOR_TEST);
1005 } else {
1006 for (uint32_t i = 0; i < count; i++) {
1007 delete mLayers.itemAt(i);
1008 }
1009 }
1010
1011 mLayers.clear();
1012}
1013
Romain Guybd6b79b2010-06-26 00:13:53 -07001014///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001015// Transforms
1016///////////////////////////////////////////////////////////////////////////////
1017
1018void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001019 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001020}
1021
1022void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001023 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001024}
1025
1026void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001027 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001028}
1029
Romain Guy807daf72011-01-18 11:19:19 -08001030void OpenGLRenderer::skew(float sx, float sy) {
1031 mSnapshot->transform->skew(sx, sy);
1032}
1033
Romain Guyf6a11b82010-06-23 17:47:49 -07001034void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001035 if (matrix) {
1036 mSnapshot->transform->load(*matrix);
1037 } else {
1038 mSnapshot->transform->loadIdentity();
1039 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001040}
1041
1042void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001043 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001044}
1045
1046void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001047 SkMatrix transform;
1048 mSnapshot->transform->copyTo(transform);
1049 transform.preConcat(*matrix);
1050 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001051}
1052
1053///////////////////////////////////////////////////////////////////////////////
1054// Clipping
1055///////////////////////////////////////////////////////////////////////////////
1056
Romain Guybb9524b2010-06-22 18:56:38 -07001057void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001058 Rect clip(*mSnapshot->clipRect);
1059 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001060
1061 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1062 clip.getWidth(), clip.getHeight());
1063
Romain Guy746b7402010-10-26 16:27:31 -07001064 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001065}
1066
1067const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001068 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001069}
1070
Romain Guyc7d53492010-06-25 13:41:57 -07001071bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001072 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001073 return true;
1074 }
1075
Romain Guy1d83e192010-08-17 11:37:00 -07001076 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001077 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001078 r.snapToPixelBoundaries();
1079
1080 Rect clipRect(*mSnapshot->clipRect);
1081 clipRect.snapToPixelBoundaries();
1082
1083 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001084}
1085
Romain Guy079ba2c2010-07-16 14:12:24 -07001086bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1087 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001088 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001089 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001090 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001091 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001092}
1093
Chet Haasea23eed82012-04-12 15:19:04 -07001094Rect* OpenGLRenderer::getClipRect() {
1095 return mSnapshot->clipRect;
1096}
1097
Romain Guyf6a11b82010-06-23 17:47:49 -07001098///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001099// Drawing commands
1100///////////////////////////////////////////////////////////////////////////////
1101
Romain Guy54be1cd2011-06-13 19:04:27 -07001102void OpenGLRenderer::setupDraw(bool clear) {
1103 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001104 if (mDirtyClip) {
1105 setScissorFromClip();
1106 }
1107 mDescription.reset();
1108 mSetShaderColor = false;
1109 mColorSet = false;
1110 mColorA = mColorR = mColorG = mColorB = 0.0f;
1111 mTextureUnit = 0;
1112 mTrackDirtyRegions = true;
1113}
1114
1115void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1116 mDescription.hasTexture = true;
1117 mDescription.hasAlpha8Texture = isAlpha8;
1118}
1119
Romain Guyaa6c24c2011-04-28 18:40:04 -07001120void OpenGLRenderer::setupDrawWithExternalTexture() {
1121 mDescription.hasExternalTexture = true;
1122}
1123
Romain Guy15bc6432011-12-13 13:11:32 -08001124void OpenGLRenderer::setupDrawNoTexture() {
1125 mCaches.disbaleTexCoordsVertexArray();
1126}
1127
Chet Haase5b0200b2011-04-13 17:58:08 -07001128void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001129 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001130}
1131
Romain Guyed6fcb02011-03-21 13:11:28 -07001132void OpenGLRenderer::setupDrawPoint(float pointSize) {
1133 mDescription.isPoint = true;
1134 mDescription.pointSize = pointSize;
1135}
1136
Romain Guy70ca14e2010-12-13 18:24:33 -08001137void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001138 setupDrawColor(color, (color >> 24) & 0xFF);
1139}
1140
1141void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1142 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001143 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001144 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1145 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001146 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001147 mColorR = a * ((color >> 16) & 0xFF);
1148 mColorG = a * ((color >> 8) & 0xFF);
1149 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001150 mColorSet = true;
1151 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1152}
1153
Romain Guy86568192010-12-14 15:55:39 -08001154void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1155 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001156 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1157 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001158 const float a = mColorA / 255.0f;
1159 mColorR = a * ((color >> 16) & 0xFF);
1160 mColorG = a * ((color >> 8) & 0xFF);
1161 mColorB = a * ((color ) & 0xFF);
1162 mColorSet = true;
1163 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1164}
1165
Romain Guy70ca14e2010-12-13 18:24:33 -08001166void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1167 mColorA = a;
1168 mColorR = r;
1169 mColorG = g;
1170 mColorB = b;
1171 mColorSet = true;
1172 mSetShaderColor = mDescription.setColor(r, g, b, a);
1173}
1174
Romain Guy86568192010-12-14 15:55:39 -08001175void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1176 mColorA = a;
1177 mColorR = r;
1178 mColorG = g;
1179 mColorB = b;
1180 mColorSet = true;
1181 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1182}
1183
Romain Guy70ca14e2010-12-13 18:24:33 -08001184void OpenGLRenderer::setupDrawShader() {
1185 if (mShader) {
1186 mShader->describe(mDescription, mCaches.extensions);
1187 }
1188}
1189
1190void OpenGLRenderer::setupDrawColorFilter() {
1191 if (mColorFilter) {
1192 mColorFilter->describe(mDescription, mCaches.extensions);
1193 }
1194}
1195
Romain Guyf09ef512011-05-27 11:43:46 -07001196void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1197 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1198 mColorA = 1.0f;
1199 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001200 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001201 }
1202}
1203
Romain Guy70ca14e2010-12-13 18:24:33 -08001204void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001205 // When the blending mode is kClear_Mode, we need to use a modulate color
1206 // argb=1,0,0,0
1207 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001208 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1209 mDescription, swapSrcDst);
1210}
1211
1212void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001213 // When the blending mode is kClear_Mode, we need to use a modulate color
1214 // argb=1,0,0,0
1215 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001216 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1217 mDescription, swapSrcDst);
1218}
1219
1220void OpenGLRenderer::setupDrawProgram() {
1221 useProgram(mCaches.programCache.get(mDescription));
1222}
1223
1224void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1225 mTrackDirtyRegions = false;
1226}
1227
1228void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1229 bool ignoreTransform) {
1230 mModelView.loadTranslate(left, top, 0.0f);
1231 if (!ignoreTransform) {
1232 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1233 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1234 } else {
1235 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1236 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1237 }
1238}
1239
Chet Haase8a5cc922011-04-26 07:28:09 -07001240void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1241 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001242}
1243
Romain Guy70ca14e2010-12-13 18:24:33 -08001244void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1245 bool ignoreTransform, bool ignoreModelView) {
1246 if (!ignoreModelView) {
1247 mModelView.loadTranslate(left, top, 0.0f);
1248 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001249 } else {
1250 mModelView.loadIdentity();
1251 }
Romain Guy86568192010-12-14 15:55:39 -08001252 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1253 if (!ignoreTransform) {
1254 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1255 if (mTrackDirtyRegions && dirty) {
1256 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1257 }
1258 } else {
1259 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1260 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1261 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001262}
1263
Romain Guyed6fcb02011-03-21 13:11:28 -07001264void OpenGLRenderer::setupDrawPointUniforms() {
1265 int slot = mCaches.currentProgram->getUniform("pointSize");
1266 glUniform1f(slot, mDescription.pointSize);
1267}
1268
Romain Guy70ca14e2010-12-13 18:24:33 -08001269void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001270 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001271 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1272 }
1273}
1274
Romain Guy86568192010-12-14 15:55:39 -08001275void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001276 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001277 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001278 }
1279}
1280
Romain Guy70ca14e2010-12-13 18:24:33 -08001281void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1282 if (mShader) {
1283 if (ignoreTransform) {
1284 mModelView.loadInverse(*mSnapshot->transform);
1285 }
1286 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1287 }
1288}
1289
Romain Guy8d0d4782010-12-14 20:13:35 -08001290void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1291 if (mShader) {
1292 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1293 }
1294}
1295
Romain Guy70ca14e2010-12-13 18:24:33 -08001296void OpenGLRenderer::setupDrawColorFilterUniforms() {
1297 if (mColorFilter) {
1298 mColorFilter->setupProgram(mCaches.currentProgram);
1299 }
1300}
1301
1302void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001303 bool force = mCaches.bindMeshBuffer();
1304 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001305 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001306}
1307
1308void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1309 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001310 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001311 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001312}
1313
Romain Guyaa6c24c2011-04-28 18:40:04 -07001314void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1315 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001316 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001317 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001318}
1319
Romain Guy8f0095c2011-05-02 17:24:22 -07001320void OpenGLRenderer::setupDrawTextureTransform() {
1321 mDescription.hasTextureTransform = true;
1322}
1323
1324void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001325 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1326 GL_FALSE, &transform.data[0]);
1327}
1328
Romain Guy70ca14e2010-12-13 18:24:33 -08001329void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001330 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001331 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001332 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001333 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001334 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001335 }
Romain Guyd71dd362011-12-12 19:03:35 -08001336
Romain Guyf3a910b42011-12-12 20:35:21 -08001337 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001338 if (mCaches.currentProgram->texCoords >= 0) {
1339 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1340 }
1341
1342 mCaches.unbindIndicesBuffer();
1343}
1344
1345void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1346 bool force = mCaches.unbindMeshBuffer();
1347 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1348 if (mCaches.currentProgram->texCoords >= 0) {
1349 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001350 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001351}
1352
Chet Haase5b0200b2011-04-13 17:58:08 -07001353void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001354 bool force = mCaches.unbindMeshBuffer();
1355 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1356 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001357 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001358}
1359
1360/**
1361 * 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 -07001362 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1363 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1364 * attributes (one per vertex) are values from zero to one that tells the fragment
1365 * shader where the fragment is in relation to the line width/length overall; these values are
1366 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1367 * region of the line.
1368 * Note that we only pass down the width values in this setup function. The length coordinates
1369 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001370 */
Chet Haase99585ad2011-05-02 15:00:16 -07001371void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001372 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001373 bool force = mCaches.unbindMeshBuffer();
1374 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1375 vertices, gAAVertexStride);
1376 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001377 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001378
Romain Guy7b631422012-04-04 11:38:54 -07001379 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001380 glEnableVertexAttribArray(widthSlot);
1381 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001382
Romain Guy7b631422012-04-04 11:38:54 -07001383 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001384 glEnableVertexAttribArray(lengthSlot);
1385 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001386
Chet Haase5b0200b2011-04-13 17:58:08 -07001387 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001388 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001389
Chet Haase99585ad2011-05-02 15:00:16 -07001390 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001391 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001392 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1393}
1394
1395void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1396 glDisableVertexAttribArray(widthSlot);
1397 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001398}
1399
Romain Guy70ca14e2010-12-13 18:24:33 -08001400void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001401}
1402
1403///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001404// Drawing
1405///////////////////////////////////////////////////////////////////////////////
1406
Chet Haase1271e2c2012-04-20 09:54:27 -07001407status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001408 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001409
Romain Guy0fe478e2010-11-08 12:08:41 -08001410 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1411 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001412 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001413 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001414 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001415
Romain Guy65549432012-03-26 16:45:05 -07001416 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001417}
1418
Chet Haaseed30fd82011-04-22 16:18:45 -07001419void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1420 if (displayList) {
1421 displayList->output(*this, level);
1422 }
1423}
1424
Romain Guya168d732011-03-18 16:50:13 -07001425void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1426 int alpha;
1427 SkXfermode::Mode mode;
1428 getAlphaAndMode(paint, &alpha, &mode);
1429
Romain Guya168d732011-03-18 16:50:13 -07001430 float x = left;
1431 float y = top;
1432
Romain Guye3c26852011-07-25 16:36:01 -07001433 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001434 bool ignoreTransform = false;
1435 if (mSnapshot->transform->isPureTranslate()) {
1436 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1437 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1438 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001439 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001440 } else {
1441 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001442 }
1443
1444 setupDraw();
1445 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001446 if (paint) {
1447 setupDrawAlpha8Color(paint->getColor(), alpha);
1448 }
Romain Guya168d732011-03-18 16:50:13 -07001449 setupDrawColorFilter();
1450 setupDrawShader();
1451 setupDrawBlending(true, mode);
1452 setupDrawProgram();
1453 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001454
Romain Guya168d732011-03-18 16:50:13 -07001455 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001456 texture->setWrap(GL_CLAMP_TO_EDGE);
1457 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001458
Romain Guya168d732011-03-18 16:50:13 -07001459 setupDrawPureColorUniforms();
1460 setupDrawColorFilterUniforms();
1461 setupDrawShaderUniforms();
1462 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1463
1464 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1465
1466 finishDrawTexture();
1467}
1468
Chet Haase5c13d892010-10-08 08:37:55 -07001469void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001470 const float right = left + bitmap->width();
1471 const float bottom = top + bitmap->height();
1472
1473 if (quickReject(left, top, right, bottom)) {
1474 return;
1475 }
1476
Romain Guya1d3c912011-12-13 14:55:06 -08001477 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001478 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001479 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001480 const AutoTexture autoCleanup(texture);
1481
Romain Guy211370f2012-02-01 16:10:55 -08001482 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001483 drawAlphaBitmap(texture, left, top, paint);
1484 } else {
1485 drawTextureRect(left, top, right, bottom, texture, paint);
1486 }
Romain Guyce0537b2010-06-29 21:05:21 -07001487}
1488
Chet Haase5c13d892010-10-08 08:37:55 -07001489void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001490 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1491 const mat4 transform(*matrix);
1492 transform.mapRect(r);
1493
Romain Guy6926c722010-07-12 20:20:03 -07001494 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1495 return;
1496 }
1497
Romain Guya1d3c912011-12-13 14:55:06 -08001498 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001499 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001500 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001501 const AutoTexture autoCleanup(texture);
1502
Romain Guy5b3b3522010-10-27 18:57:51 -07001503 // This could be done in a cheaper way, all we need is pass the matrix
1504 // to the vertex shader. The save/restore is a bit overkill.
1505 save(SkCanvas::kMatrix_SaveFlag);
1506 concatMatrix(matrix);
1507 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1508 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001509}
1510
Romain Guye651cc62012-05-14 19:44:40 -07001511void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
1512 const float right = left + bitmap->width();
1513 const float bottom = top + bitmap->height();
1514
1515 if (quickReject(left, top, right, bottom)) {
1516 return;
1517 }
1518
1519 mCaches.activeTexture(0);
1520 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1521 const AutoTexture autoCleanup(texture);
1522
1523 drawTextureRect(left, top, right, bottom, texture, paint);
1524}
1525
Romain Guy5a7b4662011-01-20 19:09:30 -08001526void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1527 float* vertices, int* colors, SkPaint* paint) {
1528 // TODO: Do a quickReject
1529 if (!vertices || mSnapshot->isIgnored()) {
1530 return;
1531 }
1532
Romain Guya1d3c912011-12-13 14:55:06 -08001533 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001534 Texture* texture = mCaches.textureCache.get(bitmap);
1535 if (!texture) return;
1536 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001537
Romain Guyd21b6e12011-11-30 20:21:23 -08001538 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1539 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001540
1541 int alpha;
1542 SkXfermode::Mode mode;
1543 getAlphaAndMode(paint, &alpha, &mode);
1544
Romain Guy5a7b4662011-01-20 19:09:30 -08001545 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001546
Romain Guyb18d2d02011-02-10 15:52:54 -08001547 float left = FLT_MAX;
1548 float top = FLT_MAX;
1549 float right = FLT_MIN;
1550 float bottom = FLT_MIN;
1551
1552#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001553 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001554#else
Romain Guy211370f2012-02-01 16:10:55 -08001555 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001556#endif
1557
Romain Guya566b7c2011-01-23 16:36:11 -08001558 // TODO: Support the colors array
1559 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001560 TextureVertex* vertex = mesh;
1561 for (int32_t y = 0; y < meshHeight; y++) {
1562 for (int32_t x = 0; x < meshWidth; x++) {
1563 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1564
1565 float u1 = float(x) / meshWidth;
1566 float u2 = float(x + 1) / meshWidth;
1567 float v1 = float(y) / meshHeight;
1568 float v2 = float(y + 1) / meshHeight;
1569
1570 int ax = i + (meshWidth + 1) * 2;
1571 int ay = ax + 1;
1572 int bx = i;
1573 int by = bx + 1;
1574 int cx = i + 2;
1575 int cy = cx + 1;
1576 int dx = i + (meshWidth + 1) * 2 + 2;
1577 int dy = dx + 1;
1578
1579 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1580 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1581 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1582
1583 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1584 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1585 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001586
1587#if RENDER_LAYERS_AS_REGIONS
1588 if (hasActiveLayer) {
1589 // TODO: This could be optimized to avoid unnecessary ops
1590 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1591 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1592 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1593 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1594 }
1595#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001596 }
1597 }
1598
Romain Guyb18d2d02011-02-10 15:52:54 -08001599#if RENDER_LAYERS_AS_REGIONS
1600 if (hasActiveLayer) {
1601 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1602 }
1603#endif
1604
Romain Guy5a7b4662011-01-20 19:09:30 -08001605 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1606 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001607 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001608}
1609
Romain Guy8ba548f2010-06-30 19:21:21 -07001610void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1611 float srcLeft, float srcTop, float srcRight, float srcBottom,
1612 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001613 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001614 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1615 return;
1616 }
1617
Romain Guya1d3c912011-12-13 14:55:06 -08001618 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001619 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001620 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001621 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001622
Romain Guy8ba548f2010-06-30 19:21:21 -07001623 const float width = texture->width;
1624 const float height = texture->height;
1625
Romain Guy68169722011-08-22 17:33:33 -07001626 const float u1 = fmax(0.0f, srcLeft / width);
1627 const float v1 = fmax(0.0f, srcTop / height);
1628 const float u2 = fmin(1.0f, srcRight / width);
1629 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001630
Romain Guy03750a02010-10-18 14:06:08 -07001631 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001632 resetDrawTextureTexCoords(u1, v1, u2, v2);
1633
Romain Guy03750a02010-10-18 14:06:08 -07001634 int alpha;
1635 SkXfermode::Mode mode;
1636 getAlphaAndMode(paint, &alpha, &mode);
1637
Romain Guyd21b6e12011-11-30 20:21:23 -08001638 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1639
Romain Guy211370f2012-02-01 16:10:55 -08001640 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001641 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1642 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1643
Romain Guyb5014982011-07-28 15:39:12 -07001644 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001645 // Enable linear filtering if the source rectangle is scaled
1646 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001647 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001648 }
Romain Guyb5014982011-07-28 15:39:12 -07001649
Romain Guyd21b6e12011-11-30 20:21:23 -08001650 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001651 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1652 texture->id, alpha / 255.0f, mode, texture->blend,
1653 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1654 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1655 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001656 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001657 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1658 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1659 GL_TRIANGLE_STRIP, gMeshCount);
1660 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001661
1662 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1663}
1664
Romain Guy4aa90572010-09-26 18:40:37 -07001665void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001666 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001667 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001668 if (quickReject(left, top, right, bottom)) {
1669 return;
1670 }
1671
Romain Guya1d3c912011-12-13 14:55:06 -08001672 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001673 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001674 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001675 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001676 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1677 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001678
1679 int alpha;
1680 SkXfermode::Mode mode;
1681 getAlphaAndMode(paint, &alpha, &mode);
1682
Romain Guy2728f962010-10-08 18:36:15 -07001683 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001684 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001685
Romain Guy211370f2012-02-01 16:10:55 -08001686 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001687 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001688#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001689 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001690 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001691 const float offsetX = left + mSnapshot->transform->getTranslateX();
1692 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001693 const size_t count = mesh->quads.size();
1694 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001695 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001696 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001697 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1698 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1699 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001700 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001701 dirtyLayer(left + bounds.left, top + bounds.top,
1702 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001703 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001704 }
1705 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001706#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001707
Romain Guy211370f2012-02-01 16:10:55 -08001708 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001709 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1710 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1711
1712 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1713 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1714 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1715 true, !mesh->hasEmptyQuads);
1716 } else {
1717 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1718 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1719 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1720 true, !mesh->hasEmptyQuads);
1721 }
Romain Guy054dc182010-10-15 17:55:25 -07001722 }
Romain Guyf7f93552010-07-08 19:17:03 -07001723}
1724
Chet Haase99ecdc42011-05-06 12:06:34 -07001725/**
Chet Haase858aa932011-05-12 09:06:00 -07001726 * This function uses a similar approach to that of AA lines in the drawLines() function.
1727 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1728 * shader to compute the translucency of the color, determined by whether a given pixel is
1729 * within that boundary region and how far into the region it is.
1730 */
1731void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001732 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001733 float inverseScaleX = 1.0f;
1734 float inverseScaleY = 1.0f;
1735 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001736 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001737 Matrix4 *mat = mSnapshot->transform;
1738 float m00 = mat->data[Matrix4::kScaleX];
1739 float m01 = mat->data[Matrix4::kSkewY];
1740 float m02 = mat->data[2];
1741 float m10 = mat->data[Matrix4::kSkewX];
1742 float m11 = mat->data[Matrix4::kScaleX];
1743 float m12 = mat->data[6];
1744 float scaleX = sqrt(m00 * m00 + m01 * m01);
1745 float scaleY = sqrt(m10 * m10 + m11 * m11);
1746 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1747 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1748 }
1749
1750 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001751 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001752 setupDrawAALine();
1753 setupDrawColor(color);
1754 setupDrawColorFilter();
1755 setupDrawShader();
1756 setupDrawBlending(true, mode);
1757 setupDrawProgram();
1758 setupDrawModelViewIdentity(true);
1759 setupDrawColorUniforms();
1760 setupDrawColorFilterUniforms();
1761 setupDrawShaderIdentityUniforms();
1762
1763 AAVertex rects[4];
1764 AAVertex* aaVertices = &rects[0];
1765 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1766 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1767
1768 float boundarySizeX = .5 * inverseScaleX;
1769 float boundarySizeY = .5 * inverseScaleY;
1770
1771 // Adjust the rect by the AA boundary padding
1772 left -= boundarySizeX;
1773 right += boundarySizeX;
1774 top -= boundarySizeY;
1775 bottom += boundarySizeY;
1776
1777 float width = right - left;
1778 float height = bottom - top;
1779
Romain Guy7b631422012-04-04 11:38:54 -07001780 int widthSlot;
1781 int lengthSlot;
1782
Chet Haase858aa932011-05-12 09:06:00 -07001783 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1784 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001785 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1786 boundaryWidthProportion, widthSlot, lengthSlot);
1787
Chet Haase858aa932011-05-12 09:06:00 -07001788 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1789 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1790 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001791 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001792
1793 if (!quickReject(left, top, right, bottom)) {
1794 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1795 AAVertex::set(aaVertices++, left, top, 1, 0);
1796 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1797 AAVertex::set(aaVertices++, right, top, 0, 0);
1798 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1799 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1800 }
Romain Guy7b631422012-04-04 11:38:54 -07001801
1802 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001803}
1804
1805/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001806 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1807 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1808 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1809 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1810 * of the line. Hairlines are more involved because we need to account for transform scaling
1811 * to end up with a one-pixel-wide line in screen space..
1812 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1813 * in combination with values that we calculate and pass down in this method. The basic approach
1814 * is that the quad we create contains both the core line area plus a bounding area in which
1815 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1816 * proportion of the width and the length of a given segment is represented by the boundary
1817 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1818 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1819 * on the inside). This ends up giving the result we want, with pixels that are completely
1820 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1821 * how far into the boundary region they are, which is determined by shader interpolation.
1822 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001823void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1824 if (mSnapshot->isIgnored()) return;
1825
1826 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001827 // We use half the stroke width here because we're going to position the quad
1828 // corner vertices half of the width away from the line endpoints
1829 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001830 // A stroke width of 0 has a special meaning in Skia:
1831 // it draws a line 1 px wide regardless of current transform
1832 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001833
Chet Haase99ecdc42011-05-06 12:06:34 -07001834 float inverseScaleX = 1.0f;
1835 float inverseScaleY = 1.0f;
1836 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001837
Chet Haase8a5cc922011-04-26 07:28:09 -07001838 int alpha;
1839 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001840
Chet Haase8a5cc922011-04-26 07:28:09 -07001841 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001842 int verticesCount = count;
1843 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001844 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001845 verticesCount += (count - 4);
1846 }
1847
1848 if (isHairLine || isAA) {
1849 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1850 // the line on the screen should always be one pixel wide regardless of scale. For
1851 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001852 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001853 Matrix4 *mat = mSnapshot->transform;
1854 float m00 = mat->data[Matrix4::kScaleX];
1855 float m01 = mat->data[Matrix4::kSkewY];
1856 float m02 = mat->data[2];
1857 float m10 = mat->data[Matrix4::kSkewX];
1858 float m11 = mat->data[Matrix4::kScaleX];
1859 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001860
Romain Guy211370f2012-02-01 16:10:55 -08001861 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1862 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001863
Chet Haase99ecdc42011-05-06 12:06:34 -07001864 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1865 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001866
Chet Haase99ecdc42011-05-06 12:06:34 -07001867 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1868 scaled = true;
1869 }
1870 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001871 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001872
1873 getAlphaAndMode(paint, &alpha, &mode);
1874 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001875 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001876 if (isAA) {
1877 setupDrawAALine();
1878 }
1879 setupDrawColor(paint->getColor(), alpha);
1880 setupDrawColorFilter();
1881 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001882 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001883 setupDrawProgram();
1884 setupDrawModelViewIdentity(true);
1885 setupDrawColorUniforms();
1886 setupDrawColorFilterUniforms();
1887 setupDrawShaderIdentityUniforms();
1888
1889 if (isHairLine) {
1890 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001891 halfStrokeWidth = isAA? 1 : .5;
1892 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001893 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001894 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 }
Romain Guy7b631422012-04-04 11:38:54 -07001896
1897 int widthSlot;
1898 int lengthSlot;
1899
Chet Haase5b0200b2011-04-13 17:58:08 -07001900 Vertex lines[verticesCount];
1901 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001902
Chet Haase99585ad2011-05-02 15:00:16 -07001903 AAVertex wLines[verticesCount];
1904 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001905
Romain Guy211370f2012-02-01 16:10:55 -08001906 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001907 setupDrawVertices(vertices);
1908 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001909 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1910 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001911 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001912 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1913 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001914 // We will need to calculate the actual width proportion on each segment for
1915 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1916 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001917 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1918 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001919 }
1920
Chet Haase99ecdc42011-05-06 12:06:34 -07001921 AAVertex* prevAAVertex = NULL;
1922 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001923
Chet Haase99585ad2011-05-02 15:00:16 -07001924 int boundaryLengthSlot = -1;
1925 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001926 int boundaryWidthSlot = -1;
1927 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001928
Chet Haase5b0200b2011-04-13 17:58:08 -07001929 for (int i = 0; i < count; i += 4) {
1930 // a = start point, b = end point
1931 vec2 a(points[i], points[i + 1]);
1932 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001933
Chet Haase99585ad2011-05-02 15:00:16 -07001934 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001935 float boundaryLengthProportion = 0;
1936 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001937
Chet Haase5b0200b2011-04-13 17:58:08 -07001938 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001939 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001940 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001941 if (isAA) {
1942 float wideningFactor;
1943 if (fabs(n.x) >= fabs(n.y)) {
1944 wideningFactor = fabs(1.0f / n.x);
1945 } else {
1946 wideningFactor = fabs(1.0f / n.y);
1947 }
1948 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001949 }
Romain Guy7b631422012-04-04 11:38:54 -07001950
Chet Haase99ecdc42011-05-06 12:06:34 -07001951 if (scaled) {
1952 n.x *= inverseScaleX;
1953 n.y *= inverseScaleY;
1954 }
1955 } else if (scaled) {
1956 // Extend n by .5 pixel on each side, post-transform
1957 vec2 extendedN = n.copyNormalized();
1958 extendedN /= 2;
1959 extendedN.x *= inverseScaleX;
1960 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001961
Chet Haase99ecdc42011-05-06 12:06:34 -07001962 float extendedNLength = extendedN.length();
1963 // We need to set this value on the shader prior to drawing
1964 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1965 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001966 }
Romain Guy7b631422012-04-04 11:38:54 -07001967
Chet Haase5b0200b2011-04-13 17:58:08 -07001968 float x = n.x;
1969 n.x = -n.y;
1970 n.y = x;
1971
Chet Haase99585ad2011-05-02 15:00:16 -07001972 // aa lines expand the endpoint vertices to encompass the AA boundary
1973 if (isAA) {
1974 vec2 abVector = (b - a);
1975 length = abVector.length();
1976 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001977
Chet Haase99ecdc42011-05-06 12:06:34 -07001978 if (scaled) {
1979 abVector.x *= inverseScaleX;
1980 abVector.y *= inverseScaleY;
1981 float abLength = abVector.length();
1982 boundaryLengthProportion = abLength / (length + abLength);
1983 } else {
1984 boundaryLengthProportion = .5 / (length + 1);
1985 }
Romain Guy7b631422012-04-04 11:38:54 -07001986
Chet Haase99ecdc42011-05-06 12:06:34 -07001987 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001988 a -= abVector;
1989 b += abVector;
1990 }
1991
Chet Haase5b0200b2011-04-13 17:58:08 -07001992 // Four corners of the rectangle defining a thick line
1993 vec2 p1 = a - n;
1994 vec2 p2 = a + n;
1995 vec2 p3 = b + n;
1996 vec2 p4 = b - n;
1997
Chet Haase99585ad2011-05-02 15:00:16 -07001998
Chet Haase5b0200b2011-04-13 17:58:08 -07001999 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
2000 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
2001 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2002 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2003
2004 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002005 if (!isAA) {
2006 if (prevVertex != NULL) {
2007 // Issue two repeat vertices to create degenerate triangles to bridge
2008 // between the previous line and the new one. This is necessary because
2009 // we are creating a single triangle_strip which will contain
2010 // potentially discontinuous line segments.
2011 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2012 Vertex::set(vertices++, p1.x, p1.y);
2013 generatedVerticesCount += 2;
2014 }
Romain Guy7b631422012-04-04 11:38:54 -07002015
Chet Haase5b0200b2011-04-13 17:58:08 -07002016 Vertex::set(vertices++, p1.x, p1.y);
2017 Vertex::set(vertices++, p2.x, p2.y);
2018 Vertex::set(vertices++, p4.x, p4.y);
2019 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002020
Chet Haase5b0200b2011-04-13 17:58:08 -07002021 prevVertex = vertices - 1;
2022 generatedVerticesCount += 4;
2023 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002024 if (!isHairLine && scaled) {
2025 // Must set width proportions per-segment for scaled non-hairlines to use the
2026 // correct AA boundary dimensions
2027 if (boundaryWidthSlot < 0) {
2028 boundaryWidthSlot =
2029 mCaches.currentProgram->getUniform("boundaryWidth");
2030 inverseBoundaryWidthSlot =
2031 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2032 }
Romain Guy7b631422012-04-04 11:38:54 -07002033
Chet Haase99ecdc42011-05-06 12:06:34 -07002034 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2035 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2036 }
Romain Guy7b631422012-04-04 11:38:54 -07002037
Chet Haase99585ad2011-05-02 15:00:16 -07002038 if (boundaryLengthSlot < 0) {
2039 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2040 inverseBoundaryLengthSlot =
2041 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2042 }
Romain Guy7b631422012-04-04 11:38:54 -07002043
Chet Haase99ecdc42011-05-06 12:06:34 -07002044 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2045 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002046
Chet Haase5b0200b2011-04-13 17:58:08 -07002047 if (prevAAVertex != NULL) {
2048 // Issue two repeat vertices to create degenerate triangles to bridge
2049 // between the previous line and the new one. This is necessary because
2050 // we are creating a single triangle_strip which will contain
2051 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002052 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2053 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2054 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002055 generatedVerticesCount += 2;
2056 }
Romain Guy7b631422012-04-04 11:38:54 -07002057
Chet Haase99585ad2011-05-02 15:00:16 -07002058 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2059 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2060 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2061 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002062
Chet Haase5b0200b2011-04-13 17:58:08 -07002063 prevAAVertex = aaVertices - 1;
2064 generatedVerticesCount += 4;
2065 }
Romain Guy7b631422012-04-04 11:38:54 -07002066
Chet Haase99585ad2011-05-02 15:00:16 -07002067 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2068 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2069 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002070 }
2071 }
Romain Guy7b631422012-04-04 11:38:54 -07002072
Chet Haase5b0200b2011-04-13 17:58:08 -07002073 if (generatedVerticesCount > 0) {
2074 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2075 }
Romain Guy7b631422012-04-04 11:38:54 -07002076
2077 if (isAA) {
2078 finishDrawAALine(widthSlot, lengthSlot);
2079 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002080}
2081
Romain Guyed6fcb02011-03-21 13:11:28 -07002082void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2083 if (mSnapshot->isIgnored()) return;
2084
2085 // TODO: The paint's cap style defines whether the points are square or circular
2086 // TODO: Handle AA for round points
2087
Chet Haase5b0200b2011-04-13 17:58:08 -07002088 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002089 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002090 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002091 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002092 if (isHairLine) {
2093 // Now that we know it's hairline, we can set the effective width, to be used later
2094 strokeWidth = 1.0f;
2095 }
2096 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002097 int alpha;
2098 SkXfermode::Mode mode;
2099 getAlphaAndMode(paint, &alpha, &mode);
2100
2101 int verticesCount = count >> 1;
2102 int generatedVerticesCount = 0;
2103
2104 TextureVertex pointsData[verticesCount];
2105 TextureVertex* vertex = &pointsData[0];
2106
2107 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002108 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002109 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002110 setupDrawColor(paint->getColor(), alpha);
2111 setupDrawColorFilter();
2112 setupDrawShader();
2113 setupDrawBlending(mode);
2114 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002115 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002116 setupDrawColorUniforms();
2117 setupDrawColorFilterUniforms();
2118 setupDrawPointUniforms();
2119 setupDrawShaderIdentityUniforms();
2120 setupDrawMesh(vertex);
2121
2122 for (int i = 0; i < count; i += 2) {
2123 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2124 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002125
Chet Haase8a5cc922011-04-26 07:28:09 -07002126 float left = points[i] - halfWidth;
2127 float right = points[i] + halfWidth;
2128 float top = points[i + 1] - halfWidth;
2129 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002130
Chet Haase8a5cc922011-04-26 07:28:09 -07002131 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002132 }
2133
2134 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2135}
2136
Romain Guy85bf02f2010-06-22 13:11:24 -07002137void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002138 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002139 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002140
Romain Guyae88e5e2010-10-22 17:49:18 -07002141 Rect& clip(*mSnapshot->clipRect);
2142 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002143
Romain Guy3d58c032010-07-14 16:34:53 -07002144 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002145}
Romain Guy9d5316e2010-06-24 19:30:36 -07002146
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002147void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002148 if (!texture) return;
2149 const AutoTexture autoCleanup(texture);
2150
2151 const float x = left + texture->left - texture->offset;
2152 const float y = top + texture->top - texture->offset;
2153
2154 drawPathTexture(texture, x, y, paint);
2155}
2156
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002157void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2158 float rx, float ry, SkPaint* paint) {
2159 if (mSnapshot->isIgnored()) return;
2160
Romain Guya1d3c912011-12-13 14:55:06 -08002161 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002162 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2163 right - left, bottom - top, rx, ry, paint);
2164 drawShape(left, top, texture, paint);
2165}
2166
Romain Guy01d58e42011-01-19 21:54:02 -08002167void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2168 if (mSnapshot->isIgnored()) return;
2169
Romain Guya1d3c912011-12-13 14:55:06 -08002170 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002171 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002172 drawShape(x - radius, y - radius, texture, paint);
2173}
Romain Guy01d58e42011-01-19 21:54:02 -08002174
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002175void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2176 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002177
Romain Guya1d3c912011-12-13 14:55:06 -08002178 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002179 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2180 drawShape(left, top, texture, paint);
2181}
2182
Romain Guy8b2f5262011-01-23 16:15:02 -08002183void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2184 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2185 if (mSnapshot->isIgnored()) return;
2186
2187 if (fabs(sweepAngle) >= 360.0f) {
2188 drawOval(left, top, right, bottom, paint);
2189 return;
2190 }
2191
Romain Guya1d3c912011-12-13 14:55:06 -08002192 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002193 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2194 startAngle, sweepAngle, useCenter, paint);
2195 drawShape(left, top, texture, paint);
2196}
2197
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002198void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2199 SkPaint* paint) {
2200 if (mSnapshot->isIgnored()) return;
2201
Romain Guya1d3c912011-12-13 14:55:06 -08002202 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002203 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2204 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002205}
2206
Chet Haase5c13d892010-10-08 08:37:55 -07002207void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002208 if (p->getStyle() != SkPaint::kFill_Style) {
2209 drawRectAsShape(left, top, right, bottom, p);
2210 return;
2211 }
2212
Romain Guy6926c722010-07-12 20:20:03 -07002213 if (quickReject(left, top, right, bottom)) {
2214 return;
2215 }
2216
Romain Guy026c5e162010-06-28 17:12:22 -07002217 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002218 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002219 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2220 if (!isMode) {
2221 // Assume SRC_OVER
2222 mode = SkXfermode::kSrcOver_Mode;
2223 }
2224 } else {
2225 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002226 }
2227
Romain Guy026c5e162010-06-28 17:12:22 -07002228 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002229 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002230 drawAARect(left, top, right, bottom, color, mode);
2231 } else {
2232 drawColorRect(left, top, right, bottom, color, mode);
2233 }
Romain Guyc7d53492010-06-25 13:41:57 -07002234}
Romain Guy9d5316e2010-06-24 19:30:36 -07002235
Romain Guyeb9a5362012-01-17 17:39:26 -08002236void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2237 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002238 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2239 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002240 return;
2241 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002242
Romain Guy671d6cf2012-01-18 12:39:17 -08002243 // NOTE: Skia does not support perspective transform on drawPosText yet
2244 if (!mSnapshot->transform->isSimple()) {
2245 return;
2246 }
2247
2248 float x = 0.0f;
2249 float y = 0.0f;
2250 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2251 if (pureTranslate) {
2252 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2253 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2254 }
2255
2256 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2257 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2258 paint->getTextSize());
2259
2260 int alpha;
2261 SkXfermode::Mode mode;
2262 getAlphaAndMode(paint, &alpha, &mode);
2263
2264 // Pick the appropriate texture filtering
2265 bool linearFilter = mSnapshot->transform->changesBounds();
2266 if (pureTranslate && !linearFilter) {
2267 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2268 }
2269
2270 mCaches.activeTexture(0);
2271 setupDraw();
2272 setupDrawDirtyRegionsDisabled();
2273 setupDrawWithTexture(true);
2274 setupDrawAlpha8Color(paint->getColor(), alpha);
2275 setupDrawColorFilter();
2276 setupDrawShader();
2277 setupDrawBlending(true, mode);
2278 setupDrawProgram();
2279 setupDrawModelView(x, y, x, y, pureTranslate, true);
2280 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2281 setupDrawPureColorUniforms();
2282 setupDrawColorFilterUniforms();
2283 setupDrawShaderUniforms(pureTranslate);
2284
2285 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2286 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2287
2288#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002289 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002290#else
Romain Guy211370f2012-02-01 16:10:55 -08002291 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002292#endif
2293
2294 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2295 positions, hasActiveLayer ? &bounds : NULL)) {
2296#if RENDER_LAYERS_AS_REGIONS
2297 if (hasActiveLayer) {
2298 if (!pureTranslate) {
2299 mSnapshot->transform->mapRect(bounds);
2300 }
2301 dirtyLayerUnchecked(bounds, getRegion());
2302 }
2303#endif
2304 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002305}
2306
Romain Guye8e62a42010-07-23 18:55:21 -07002307void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002308 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002309 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2310 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002311 return;
2312 }
2313
Chet Haasea1cff502012-02-21 13:43:44 -08002314 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002315 switch (paint->getTextAlign()) {
2316 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002317 x -= length / 2.0f;
2318 break;
2319 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002320 x -= length;
2321 break;
2322 default:
2323 break;
2324 }
2325
Romain Guycac5fd32011-12-01 20:08:50 -08002326 SkPaint::FontMetrics metrics;
2327 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002328 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002329 return;
2330 }
2331
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002332 const float oldX = x;
2333 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002334 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002335 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002336 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2337 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2338 }
2339
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002340#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002341 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002342#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002343
2344 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002345 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002346 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002347
Romain Guy86568192010-12-14 15:55:39 -08002348 int alpha;
2349 SkXfermode::Mode mode;
2350 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002351
Romain Guy211370f2012-02-01 16:10:55 -08002352 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002353 mCaches.activeTexture(0);
2354
Romain Guyb45c0c92010-08-26 20:35:23 -07002355 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002356 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2357 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002358 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002359
Romain Guy740bf2b2011-04-26 15:33:10 -07002360 const float sx = oldX - shadow->left + mShadowDx;
2361 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002362
Romain Guy86568192010-12-14 15:55:39 -08002363 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002364 int shadowColor = mShadowColor;
2365 if (mShader) {
2366 shadowColor = 0xffffffff;
2367 }
Romain Guy86568192010-12-14 15:55:39 -08002368
Romain Guy86568192010-12-14 15:55:39 -08002369 setupDraw();
2370 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002371 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2372 setupDrawColorFilter();
2373 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002374 setupDrawBlending(true, mode);
2375 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002376 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002377 setupDrawTexture(shadow->id);
2378 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002379 setupDrawColorFilterUniforms();
2380 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002381 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2382
Romain Guy0a417492010-08-16 20:26:20 -07002383 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002384 }
2385
Romain Guy6620c6d2010-12-06 18:07:02 -08002386 // Pick the appropriate texture filtering
2387 bool linearFilter = mSnapshot->transform->changesBounds();
2388 if (pureTranslate && !linearFilter) {
2389 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2390 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002391
Romain Guya1d3c912011-12-13 14:55:06 -08002392 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002393 setupDraw();
2394 setupDrawDirtyRegionsDisabled();
2395 setupDrawWithTexture(true);
2396 setupDrawAlpha8Color(paint->getColor(), alpha);
2397 setupDrawColorFilter();
2398 setupDrawShader();
2399 setupDrawBlending(true, mode);
2400 setupDrawProgram();
2401 setupDrawModelView(x, y, x, y, pureTranslate, true);
2402 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2403 setupDrawPureColorUniforms();
2404 setupDrawColorFilterUniforms();
2405 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002406
Romain Guy6620c6d2010-12-06 18:07:02 -08002407 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002408 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2409
2410#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002411 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002412#else
Romain Guy211370f2012-02-01 16:10:55 -08002413 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002414#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002415
Romain Guy6620c6d2010-12-06 18:07:02 -08002416 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002417 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002418#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002419 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002420 if (!pureTranslate) {
2421 mSnapshot->transform->mapRect(bounds);
2422 }
Romain Guyf219da52011-01-16 12:54:25 -08002423 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002424 }
2425#endif
2426 }
Romain Guy694b5192010-07-21 21:33:20 -07002427
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002428 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002429}
2430
Romain Guy325740f2012-02-24 16:48:34 -08002431void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2432 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002433 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2434 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2435 return;
2436 }
2437
Romain Guy03d58522012-02-24 17:54:07 -08002438 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2439 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2440 paint->getTextSize());
2441
2442 int alpha;
2443 SkXfermode::Mode mode;
2444 getAlphaAndMode(paint, &alpha, &mode);
2445
2446 mCaches.activeTexture(0);
2447 setupDraw();
2448 setupDrawDirtyRegionsDisabled();
2449 setupDrawWithTexture(true);
2450 setupDrawAlpha8Color(paint->getColor(), alpha);
2451 setupDrawColorFilter();
2452 setupDrawShader();
2453 setupDrawBlending(true, mode);
2454 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002455 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002456 setupDrawTexture(fontRenderer.getTexture(true));
2457 setupDrawPureColorUniforms();
2458 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002459 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002460
Romain Guy97771732012-02-28 18:17:02 -08002461 const Rect* clip = &mSnapshot->getLocalClip();
2462 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 -08002463
Romain Guy97771732012-02-28 18:17:02 -08002464#if RENDER_LAYERS_AS_REGIONS
2465 const bool hasActiveLayer = hasLayer();
2466#else
2467 const bool hasActiveLayer = false;
2468#endif
2469
2470 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2471 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2472#if RENDER_LAYERS_AS_REGIONS
2473 if (hasActiveLayer) {
2474 mSnapshot->transform->mapRect(bounds);
2475 dirtyLayerUnchecked(bounds, getRegion());
2476 }
2477#endif
2478 }
Romain Guy325740f2012-02-24 16:48:34 -08002479}
2480
Romain Guy7fbcc042010-08-04 15:40:07 -07002481void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002482 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002483
Romain Guya1d3c912011-12-13 14:55:06 -08002484 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002485
Romain Guy33f6beb2012-02-16 19:24:51 -08002486 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002487 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002488 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002489 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002490
Romain Guy8b55f372010-08-18 17:10:07 -07002491 const float x = texture->left - texture->offset;
2492 const float y = texture->top - texture->offset;
2493
Romain Guy01d58e42011-01-19 21:54:02 -08002494 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002495}
2496
Romain Guyada830f2011-01-13 12:13:20 -08002497void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2498 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002499 return;
2500 }
2501
Romain Guy2bf68f02012-03-02 13:37:47 -08002502 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2503 OpenGLRenderer* renderer = layer->renderer;
2504 Rect& dirty = layer->dirtyRect;
2505
2506 interrupt();
2507 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2508 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002509 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002510 renderer->finish();
2511 resume();
2512
2513 dirty.setEmpty();
2514 layer->deferredUpdateScheduled = false;
2515 layer->renderer = NULL;
2516 layer->displayList = NULL;
2517 }
2518
Romain Guya1d3c912011-12-13 14:55:06 -08002519 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002520
2521 int alpha;
2522 SkXfermode::Mode mode;
2523 getAlphaAndMode(paint, &alpha, &mode);
2524
Romain Guy9ace8f52011-07-07 20:50:11 -07002525 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002526
Romain Guyf219da52011-01-16 12:54:25 -08002527#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002528 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002529 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002530 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002531 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002532 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002533 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002534
Romain Guyc88e3572011-01-22 00:32:12 -08002535 setupDraw();
2536 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002537 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002538 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002539 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002540 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002541 setupDrawPureColorUniforms();
2542 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002543 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002544 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002545 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2546 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2547
Romain Guyd21b6e12011-11-30 20:21:23 -08002548 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002549 setupDrawModelViewTranslate(x, y,
2550 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2551 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002552 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002553 setupDrawModelViewTranslate(x, y,
2554 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2555 }
Romain Guyc88e3572011-01-22 00:32:12 -08002556 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002557
Romain Guyc88e3572011-01-22 00:32:12 -08002558 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2559 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002560
Romain Guyc88e3572011-01-22 00:32:12 -08002561 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002562
2563#if DEBUG_LAYERS_AS_REGIONS
2564 drawRegionRects(layer->region);
2565#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002566 }
Romain Guyf219da52011-01-16 12:54:25 -08002567 }
2568#else
Romain Guyada830f2011-01-13 12:13:20 -08002569 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2570 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002571#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002572}
2573
Romain Guy6926c722010-07-12 20:20:03 -07002574///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002575// Shaders
2576///////////////////////////////////////////////////////////////////////////////
2577
2578void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002579 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002580}
2581
Romain Guy06f96e22010-07-30 19:18:16 -07002582void OpenGLRenderer::setupShader(SkiaShader* shader) {
2583 mShader = shader;
2584 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002585 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002586 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002587}
2588
Romain Guyd27977d2010-07-14 19:18:51 -07002589///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002590// Color filters
2591///////////////////////////////////////////////////////////////////////////////
2592
2593void OpenGLRenderer::resetColorFilter() {
2594 mColorFilter = NULL;
2595}
2596
2597void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2598 mColorFilter = filter;
2599}
2600
2601///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002602// Drop shadow
2603///////////////////////////////////////////////////////////////////////////////
2604
2605void OpenGLRenderer::resetShadow() {
2606 mHasShadow = false;
2607}
2608
2609void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2610 mHasShadow = true;
2611 mShadowRadius = radius;
2612 mShadowDx = dx;
2613 mShadowDy = dy;
2614 mShadowColor = color;
2615}
2616
2617///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002618// Draw filters
2619///////////////////////////////////////////////////////////////////////////////
2620
2621void OpenGLRenderer::resetPaintFilter() {
2622 mHasDrawFilter = false;
2623}
2624
2625void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2626 mHasDrawFilter = true;
2627 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2628 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2629}
2630
2631SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002632 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002633
2634 uint32_t flags = paint->getFlags();
2635
2636 mFilteredPaint = *paint;
2637 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2638
2639 return &mFilteredPaint;
2640}
2641
2642///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002643// Drawing implementation
2644///////////////////////////////////////////////////////////////////////////////
2645
Romain Guy01d58e42011-01-19 21:54:02 -08002646void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2647 float x, float y, SkPaint* paint) {
2648 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2649 return;
2650 }
2651
2652 int alpha;
2653 SkXfermode::Mode mode;
2654 getAlphaAndMode(paint, &alpha, &mode);
2655
2656 setupDraw();
2657 setupDrawWithTexture(true);
2658 setupDrawAlpha8Color(paint->getColor(), alpha);
2659 setupDrawColorFilter();
2660 setupDrawShader();
2661 setupDrawBlending(true, mode);
2662 setupDrawProgram();
2663 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2664 setupDrawTexture(texture->id);
2665 setupDrawPureColorUniforms();
2666 setupDrawColorFilterUniforms();
2667 setupDrawShaderUniforms();
2668 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2669
2670 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2671
2672 finishDrawTexture();
2673}
2674
Romain Guyf607bdc2010-09-10 19:20:06 -07002675// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002676#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2677#define kStdUnderline_Offset (1.0f / 9.0f)
2678#define kStdUnderline_Thickness (1.0f / 18.0f)
2679
2680void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2681 float x, float y, SkPaint* paint) {
2682 // Handle underline and strike-through
2683 uint32_t flags = paint->getFlags();
2684 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002685 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002686 float underlineWidth = length;
2687 // If length is > 0.0f, we already measured the text for the text alignment
2688 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002689 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002690 }
2691
2692 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002693 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002694 case SkPaint::kCenter_Align:
2695 offsetX = underlineWidth * 0.5f;
2696 break;
2697 case SkPaint::kRight_Align:
2698 offsetX = underlineWidth;
2699 break;
2700 default:
2701 break;
2702 }
2703
Romain Guy211370f2012-02-01 16:10:55 -08002704 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002705 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002706 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002707
Romain Guye20ecbd2010-09-22 19:49:04 -07002708 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002709 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002710
Romain Guyf6834472011-01-23 13:32:12 -08002711 int linesCount = 0;
2712 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2713 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2714
2715 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002716 float points[pointsCount];
2717 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002718
2719 if (flags & SkPaint::kUnderlineText_Flag) {
2720 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002721 points[currentPoint++] = left;
2722 points[currentPoint++] = top;
2723 points[currentPoint++] = left + underlineWidth;
2724 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002725 }
2726
2727 if (flags & SkPaint::kStrikeThruText_Flag) {
2728 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002729 points[currentPoint++] = left;
2730 points[currentPoint++] = top;
2731 points[currentPoint++] = left + underlineWidth;
2732 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002733 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002734
Romain Guy726aeba2011-06-01 14:52:00 -07002735 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002736
Romain Guy726aeba2011-06-01 14:52:00 -07002737 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002738 }
2739 }
2740}
2741
Romain Guy026c5e162010-06-28 17:12:22 -07002742void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002743 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002744 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002745 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002746 color |= 0x00ffffff;
2747 }
2748
Romain Guy70ca14e2010-12-13 18:24:33 -08002749 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002750 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002751 setupDrawColor(color);
2752 setupDrawShader();
2753 setupDrawColorFilter();
2754 setupDrawBlending(mode);
2755 setupDrawProgram();
2756 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2757 setupDrawColorUniforms();
2758 setupDrawShaderUniforms(ignoreTransform);
2759 setupDrawColorFilterUniforms();
2760 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002761
Romain Guyc95c8d62010-09-17 15:31:32 -07002762 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2763}
2764
Romain Guy82ba8142010-07-09 13:25:56 -07002765void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002766 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002767 int alpha;
2768 SkXfermode::Mode mode;
2769 getAlphaAndMode(paint, &alpha, &mode);
2770
Romain Guyd21b6e12011-11-30 20:21:23 -08002771 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002772
Romain Guy211370f2012-02-01 16:10:55 -08002773 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002774 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2775 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2776
Romain Guyd21b6e12011-11-30 20:21:23 -08002777 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002778 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2779 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2780 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2781 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002782 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002783 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2784 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2785 GL_TRIANGLE_STRIP, gMeshCount);
2786 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002787}
2788
Romain Guybd6b79b2010-06-26 00:13:53 -07002789void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002790 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2791 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002792 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002793}
2794
2795void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002796 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002797 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002798 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002799
Romain Guy746b7402010-10-26 16:27:31 -07002800 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002801 setupDrawWithTexture();
2802 setupDrawColor(alpha, alpha, alpha, alpha);
2803 setupDrawColorFilter();
2804 setupDrawBlending(blend, mode, swapSrcDst);
2805 setupDrawProgram();
2806 if (!dirty) {
2807 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002808 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002809 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002810 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002811 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002812 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002813 }
Romain Guy86568192010-12-14 15:55:39 -08002814 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002815 setupDrawColorFilterUniforms();
2816 setupDrawTexture(texture);
2817 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002818
Romain Guy6820ac82010-09-15 18:11:50 -07002819 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002820
2821 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002822}
2823
Romain Guya5aed0d2010-09-09 14:42:43 -07002824void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002825 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002826 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002827
Romain Guy82ba8142010-07-09 13:25:56 -07002828 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002829 // These blend modes are not supported by OpenGL directly and have
2830 // to be implemented using shaders. Since the shader will perform
2831 // the blending, turn blending off here
2832 // If the blend mode cannot be implemented using shaders, fall
2833 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002834 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2835 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002836 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002837 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002838
Romain Guy82bc7a72012-01-03 14:13:39 -08002839 if (mCaches.blend) {
2840 glDisable(GL_BLEND);
2841 mCaches.blend = false;
2842 }
2843
2844 return;
2845 } else {
2846 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002847 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002848 }
2849
2850 if (!mCaches.blend) {
2851 glEnable(GL_BLEND);
2852 }
2853
2854 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2855 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2856
2857 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2858 glBlendFunc(sourceMode, destMode);
2859 mCaches.lastSrcMode = sourceMode;
2860 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002861 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002862 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002863 glDisable(GL_BLEND);
2864 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002865 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002866}
2867
Romain Guy889f8d12010-07-29 14:37:42 -07002868bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002869 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002870 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002871 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002872 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002873 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002874 }
Romain Guy6926c722010-07-12 20:20:03 -07002875 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002876}
2877
Romain Guy026c5e162010-06-28 17:12:22 -07002878void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002879 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002880 TextureVertex::setUV(v++, u1, v1);
2881 TextureVertex::setUV(v++, u2, v1);
2882 TextureVertex::setUV(v++, u1, v2);
2883 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002884}
2885
Chet Haase5c13d892010-10-08 08:37:55 -07002886void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002887 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002888 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002889
2890 // Skia draws using the color's alpha channel if < 255
2891 // Otherwise, it uses the paint's alpha
2892 int color = paint->getColor();
2893 *alpha = (color >> 24) & 0xFF;
2894 if (*alpha == 255) {
2895 *alpha = paint->getAlpha();
2896 }
2897 } else {
2898 *mode = SkXfermode::kSrcOver_Mode;
2899 *alpha = 255;
2900 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002901 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002902}
2903
Romain Guya5aed0d2010-09-09 14:42:43 -07002904SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002905 SkXfermode::Mode resultMode;
2906 if (!SkXfermode::AsMode(mode, &resultMode)) {
2907 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002908 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002909 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002910}
2911
Romain Guy9d5316e2010-06-24 19:30:36 -07002912}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002913}; // namespace android