blob: 50f5d57c425818a9788637358623eca16a9bb485 [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();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700173 mFunctors.clear();
Romain Guyfe48f652010-11-11 15:36:56 -0800174
Romain Guy8aef54f2010-09-01 15:13:49 -0700175 mSnapshot = new Snapshot(mFirstSnapshot,
176 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800177 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700178 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700179
Romain Guy39d252a2011-12-12 18:14:06 -0800180 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800181 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800182
Romain Guy7d7b5492011-01-24 16:33:45 -0800183 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800184 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800185
Romain Guy6b7bd242010-10-06 19:49:23 -0700186 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700187 glClear(GL_COLOR_BUFFER_BIT);
188 }
Romain Guybb9524b2010-06-22 18:56:38 -0700189}
190
Romain Guyb025b9c2010-09-16 14:16:48 -0700191void OpenGLRenderer::finish() {
192#if DEBUG_OPENGL
193 GLenum status = GL_NO_ERROR;
194 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000195 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800196 switch (status) {
Romain Guya44a63a2012-04-26 14:05:02 -0700197 case GL_INVALID_ENUM:
198 ALOGE(" GL_INVALID_ENUM");
199 break;
200 case GL_INVALID_VALUE:
201 ALOGE(" GL_INVALID_VALUE");
202 break;
203 case GL_INVALID_OPERATION:
204 ALOGE(" GL_INVALID_OPERATION");
205 break;
Romain Guya07105b2011-01-10 21:14:18 -0800206 case GL_OUT_OF_MEMORY:
Romain Guya44a63a2012-04-26 14:05:02 -0700207 ALOGE(" Out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800208 break;
209 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700210 }
211#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800212#if DEBUG_MEMORY_USAGE
213 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800214#else
215 if (mCaches.getDebugLevel() & kDebugMemory) {
216 mCaches.dumpMemoryUsage();
217 }
Romain Guyc15008e2010-11-10 11:59:15 -0800218#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700219}
220
Romain Guy6c319ca2011-01-11 14:29:25 -0800221void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700222 if (mCaches.currentProgram) {
223 if (mCaches.currentProgram->isInUse()) {
224 mCaches.currentProgram->remove();
225 mCaches.currentProgram = NULL;
226 }
227 }
Romain Guy50c0f092010-10-19 11:42:22 -0700228 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800229 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800230 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800231 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700232}
233
Romain Guy6c319ca2011-01-11 14:29:25 -0800234void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800235 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
236
237 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800238 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
239
Romain Guyda8532c2010-08-31 11:50:35 -0700240 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800241 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700242 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700243
Romain Guya1d3c912011-12-13 14:55:06 -0800244 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800245 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700246
Romain Guy50c0f092010-10-19 11:42:22 -0700247 mCaches.blend = true;
248 glEnable(GL_BLEND);
249 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
250 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700251}
252
Romain Guyba6be8a2012-04-23 18:22:09 -0700253void OpenGLRenderer::detachFunctor(Functor* functor) {
254 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();
Romain Guyf90f8172011-01-25 22:53:24 -0800306 if (mDirtyClip) {
307 setScissorFromClip();
308 }
Romain Guyd643bb52011-03-01 14:55:21 -0800309
Romain Guy80911b82011-03-16 15:30:12 -0700310 Rect clip(*mSnapshot->clipRect);
311 clip.snapToPixelBoundaries();
312
Romain Guyd643bb52011-03-01 14:55:21 -0800313#if RENDER_LAYERS_AS_REGIONS
314 // Since we don't know what the functor will draw, let's dirty
315 // tne entire clip region
316 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800317 dirtyLayerUnchecked(clip, getRegion());
318 }
319#endif
320
Romain Guy08aa2cb2011-03-17 11:06:57 -0700321 DrawGlInfo info;
322 info.clipLeft = clip.left;
323 info.clipTop = clip.top;
324 info.clipRight = clip.right;
325 info.clipBottom = clip.bottom;
326 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700327 info.width = getSnapshot()->viewport.getWidth();
328 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700329 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700330
Romain Guy8f3b8e32012-03-27 16:33:45 -0700331 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800332
Romain Guy8f3b8e32012-03-27 16:33:45 -0700333 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700334 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800335 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700336
Chris Craik65924a32012-04-05 17:52:11 -0700337 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700338 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700339 }
Romain Guycabfcc12011-03-07 18:06:46 -0800340 }
341
Chet Haasedaf98e92011-01-10 14:10:36 -0800342 resume();
Romain Guy65549432012-03-26 16:45:05 -0700343 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800344}
345
Romain Guyf6a11b82010-06-23 17:47:49 -0700346///////////////////////////////////////////////////////////////////////////////
347// State management
348///////////////////////////////////////////////////////////////////////////////
349
Romain Guybb9524b2010-06-22 18:56:38 -0700350int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700351 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700352}
353
354int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700355 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700356}
357
358void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700359 if (mSaveCount > 1) {
360 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700361 }
Romain Guybb9524b2010-06-22 18:56:38 -0700362}
363
364void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700365 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700366
Romain Guy8fb95422010-08-17 18:38:51 -0700367 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700368 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 }
Romain Guybb9524b2010-06-22 18:56:38 -0700370}
371
Romain Guy8aef54f2010-09-01 15:13:49 -0700372int OpenGLRenderer::saveSnapshot(int flags) {
373 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700374 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700375}
376
377bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700379 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700380 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700381
Romain Guybd6b79b2010-06-26 00:13:53 -0700382 sp<Snapshot> current = mSnapshot;
383 sp<Snapshot> previous = mSnapshot->previous;
384
Romain Guyeb993562010-10-05 18:14:38 -0700385 if (restoreOrtho) {
386 Rect& r = previous->viewport;
387 glViewport(r.left, r.top, r.right, r.bottom);
388 mOrthoMatrix.load(current->orthoMatrix);
389 }
390
Romain Guy8b55f372010-08-18 17:10:07 -0700391 mSaveCount--;
392 mSnapshot = previous;
393
Romain Guy2542d192010-08-18 11:47:12 -0700394 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700395 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700396 }
Romain Guy2542d192010-08-18 11:47:12 -0700397
Romain Guy5ec99242010-11-03 16:19:08 -0700398 if (restoreLayer) {
399 composeLayer(current, previous);
400 }
401
Romain Guy2542d192010-08-18 11:47:12 -0700402 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700403}
404
Romain Guyf6a11b82010-06-23 17:47:49 -0700405///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700406// Layers
407///////////////////////////////////////////////////////////////////////////////
408
409int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700410 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700411 const GLuint previousFbo = mSnapshot->fbo;
412 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700413
Romain Guyaf636eb2010-12-09 17:47:21 -0800414 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700415 int alpha = 255;
416 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700417
Romain Guye45362c2010-11-03 19:58:32 -0700418 if (p) {
419 alpha = p->getAlpha();
420 if (!mCaches.extensions.hasFramebufferFetch()) {
421 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
422 if (!isMode) {
423 // Assume SRC_OVER
424 mode = SkXfermode::kSrcOver_Mode;
425 }
426 } else {
427 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700428 }
429 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700430 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700431 }
Romain Guyd55a8612010-06-28 17:42:46 -0700432
Romain Guydbc26d22010-10-11 17:58:29 -0700433 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
434 }
Romain Guyd55a8612010-06-28 17:42:46 -0700435
436 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700437}
438
439int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
440 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700441 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700442 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700443 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700444 SkPaint paint;
445 paint.setAlpha(alpha);
446 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700447 }
Romain Guyd55a8612010-06-28 17:42:46 -0700448}
Romain Guybd6b79b2010-06-26 00:13:53 -0700449
Romain Guy1c740bc2010-09-13 18:00:09 -0700450/**
451 * Layers are viewed by Skia are slightly different than layers in image editing
452 * programs (for instance.) When a layer is created, previously created layers
453 * and the frame buffer still receive every drawing command. For instance, if a
454 * layer is created and a shape intersecting the bounds of the layers and the
455 * framebuffer is draw, the shape will be drawn on both (unless the layer was
456 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
457 *
458 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
459 * texture. Unfortunately, this is inefficient as it requires every primitive to
460 * be drawn n + 1 times, where n is the number of active layers. In practice this
461 * means, for every primitive:
462 * - Switch active frame buffer
463 * - Change viewport, clip and projection matrix
464 * - Issue the drawing
465 *
466 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700467 * To avoid this, layers are implemented in a different way here, at least in the
468 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
469 * is set. When this flag is set we can redirect all drawing operations into a
470 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700471 *
472 * This implementation relies on the frame buffer being at least RGBA 8888. When
473 * a layer is created, only a texture is created, not an FBO. The content of the
474 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700475 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700476 * buffer and drawing continues as normal. This technique therefore treats the
477 * frame buffer as a scratch buffer for the layers.
478 *
479 * To compose the layers back onto the frame buffer, each layer texture
480 * (containing the original frame buffer data) is drawn as a simple quad over
481 * the frame buffer. The trick is that the quad is set as the composition
482 * destination in the blending equation, and the frame buffer becomes the source
483 * of the composition.
484 *
485 * Drawing layers with an alpha value requires an extra step before composition.
486 * An empty quad is drawn over the layer's region in the frame buffer. This quad
487 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
488 * quad is used to multiply the colors in the frame buffer. This is achieved by
489 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
490 * GL_ZERO, GL_SRC_ALPHA.
491 *
492 * Because glCopyTexImage2D() can be slow, an alternative implementation might
493 * be use to draw a single clipped layer. The implementation described above
494 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700495 *
496 * (1) The frame buffer is actually not cleared right away. To allow the GPU
497 * to potentially optimize series of calls to glCopyTexImage2D, the frame
498 * buffer is left untouched until the first drawing operation. Only when
499 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700500 */
Romain Guyd55a8612010-06-28 17:42:46 -0700501bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700502 float right, float bottom, int alpha, SkXfermode::Mode mode,
503 int flags, GLuint previousFbo) {
504 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700505 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700506
Romain Guyeb993562010-10-05 18:14:38 -0700507 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
508
Romain Guyf607bdc2010-09-10 19:20:06 -0700509 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700510 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700511 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700512 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700513
Romain Guyeb993562010-10-05 18:14:38 -0700514 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700515 if (bounds.intersect(*snapshot->clipRect)) {
516 // We cannot work with sub-pixels in this case
517 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700518
Romain Guyad37cd32011-03-15 11:12:25 -0700519 // When the layer is not an FBO, we may use glCopyTexImage so we
520 // need to make sure the layer does not extend outside the bounds
521 // of the framebuffer
522 if (!bounds.intersect(snapshot->previous->viewport)) {
523 bounds.setEmpty();
524 }
525 } else {
526 bounds.setEmpty();
527 }
Romain Guyeb993562010-10-05 18:14:38 -0700528 }
Romain Guybf434112010-09-16 14:40:17 -0700529
Romain Guy746b7402010-10-26 16:27:31 -0700530 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
531 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800532 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700533 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700534 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700535 }
536
537 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800538 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700539 return false;
540 }
Romain Guyf18fd992010-07-08 11:45:51 -0700541
Romain Guya1d3c912011-12-13 14:55:06 -0800542 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700543 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700544 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700545 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700546 }
547
Romain Guy9ace8f52011-07-07 20:50:11 -0700548 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700549 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700550 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
551 bounds.getWidth() / float(layer->getWidth()), 0.0f);
552 layer->setColorFilter(mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700553 layer->setBlend(true);
Romain Guydda570202010-07-06 11:39:32 -0700554
Romain Guy8fb95422010-08-17 18:38:51 -0700555 // Save the layer in the snapshot
556 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700557 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700558
Romain Guyeb993562010-10-05 18:14:38 -0700559 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700560 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700561 } else {
562 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700563 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800564 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700565 if (layer->isEmpty()) {
566 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
567 bounds.left, snapshot->height - bounds.bottom,
568 layer->getWidth(), layer->getHeight(), 0);
569 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800570 } else {
571 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
572 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
573 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700574
Romain Guy54be1cd2011-06-13 19:04:27 -0700575 // Enqueue the buffer coordinates to clear the corresponding region later
576 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700577 }
Romain Guyeb993562010-10-05 18:14:38 -0700578 }
Romain Guyf86ef572010-07-01 11:05:42 -0700579
Romain Guyd55a8612010-06-28 17:42:46 -0700580 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700581}
582
Romain Guy5b3b3522010-10-27 18:57:51 -0700583bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
584 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700586
587#if RENDER_LAYERS_AS_REGIONS
588 snapshot->region = &snapshot->layer->region;
589 snapshot->flags |= Snapshot::kFlagFboTarget;
590#endif
591
592 Rect clip(bounds);
593 snapshot->transform->mapRect(clip);
594 clip.intersect(*snapshot->clipRect);
595 clip.snapToPixelBoundaries();
596 clip.intersect(snapshot->previous->viewport);
597
598 mat4 inverse;
599 inverse.loadInverse(*mSnapshot->transform);
600
601 inverse.mapRect(clip);
602 clip.snapToPixelBoundaries();
603 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700604 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700605
606 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
610 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
611 snapshot->height = bounds.getHeight();
612 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
613 snapshot->orthoMatrix.load(mOrthoMatrix);
614
615 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700616 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
617 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700618
619 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700620 if (layer->isEmpty()) {
621 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
622 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700623 }
624
625 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700627
628#if DEBUG_LAYERS_AS_REGIONS
629 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
630 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000631 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700632
633 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 layer->deleteTexture();
635 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700636
637 delete layer;
638
639 return false;
640 }
641#endif
642
643 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800644 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700645 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700646 glClear(GL_COLOR_BUFFER_BIT);
647
648 dirtyClip();
649
650 // Change the ortho projection
651 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
652 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
653
654 return true;
655}
656
Romain Guy1c740bc2010-09-13 18:00:09 -0700657/**
658 * Read the documentation of createLayer() before doing anything in this method.
659 */
Romain Guy1d83e192010-08-17 11:37:00 -0700660void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
661 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000662 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700663 return;
664 }
665
Romain Guy5b3b3522010-10-27 18:57:51 -0700666 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700667
668 if (fboLayer) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700669 // Detach the texture from the FBO
670 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
671
Romain Guyeb993562010-10-05 18:14:38 -0700672 // Unbind current FBO and restore previous one
673 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
674 }
675
Romain Guy1d83e192010-08-17 11:37:00 -0700676 Layer* layer = current->layer;
677 const Rect& rect = layer->layer;
678
Romain Guy9ace8f52011-07-07 20:50:11 -0700679 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700680 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700682 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700683 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700684 }
685
Romain Guy03750a02010-10-18 14:06:08 -0700686 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700687
Romain Guya1d3c912011-12-13 14:55:06 -0800688 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700689
Romain Guy5b3b3522010-10-27 18:57:51 -0700690 // When the layer is stored in an FBO, we can save a bit of fillrate by
691 // drawing only the dirty region
692 if (fboLayer) {
693 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 if (layer->getColorFilter()) {
695 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800696 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700697 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700698 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800699 resetColorFilter();
700 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700701 } else if (!rect.isEmpty()) {
702 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
703 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700704 }
Romain Guy8b55f372010-08-18 17:10:07 -0700705
Romain Guyeb993562010-10-05 18:14:38 -0700706 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800707 // Note: No need to use glDiscardFramebufferEXT() since we never
708 // create/compose layers that are not on screen with this
709 // code path
710 // See LayerRenderer::destroyLayer(Layer*)
711
Romain Guyeb993562010-10-05 18:14:38 -0700712 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
713 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700714 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700715 }
716
Romain Guy746b7402010-10-26 16:27:31 -0700717 dirtyClip();
718
Romain Guyeb993562010-10-05 18:14:38 -0700719 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700720 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700721 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700722 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700723 delete layer;
724 }
725}
726
Romain Guyaa6c24c2011-04-28 18:40:04 -0700727void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700729
Romain Guy302a9df2011-08-16 13:55:02 -0700730 mat4& transform = layer->getTransform();
731 if (!transform.isIdentity()) {
732 save(0);
733 mSnapshot->transform->multiply(transform);
734 }
735
Romain Guyaa6c24c2011-04-28 18:40:04 -0700736 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700737 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700738 setupDrawWithTexture();
739 } else {
740 setupDrawWithExternalTexture();
741 }
742 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700743 setupDrawColor(alpha, alpha, alpha, alpha);
744 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700746 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700747 setupDrawPureColorUniforms();
748 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
750 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700751 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700752 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700753 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700754 if (mSnapshot->transform->isPureTranslate() &&
755 layer->getWidth() == (uint32_t) rect.getWidth() &&
756 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
758 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
759
Romain Guyd21b6e12011-11-30 20:21:23 -0800760 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
762 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800763 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
765 }
766 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700767 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
768
769 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
770
771 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700772
773 if (!transform.isIdentity()) {
774 restore();
775 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700776}
777
Romain Guy5b3b3522010-10-27 18:57:51 -0700778void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700780 const Rect& texCoords = layer->texCoords;
781 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
782 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700783
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 float x = rect.left;
785 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700786 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700787 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700788 layer->getHeight() == (uint32_t) rect.getHeight();
789
790 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 // When we're swapping, the layer is already in screen coordinates
792 if (!swap) {
793 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
794 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
795 }
796
Romain Guyd21b6e12011-11-30 20:21:23 -0800797 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700798 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800799 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700800 }
801
802 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
803 layer->getTexture(), layer->getAlpha() / 255.0f,
804 layer->getMode(), layer->isBlend(),
805 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
806 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
Romain Guyaa6c24c2011-04-28 18:40:04 -0700808 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
809 } else {
810 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
811 drawTextureLayer(layer, rect);
812 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
813 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700814}
815
816void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
817#if RENDER_LAYERS_AS_REGIONS
818 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700819 layer->setRegionAsRect();
820
Romain Guy40667672011-03-18 14:34:03 -0700821 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700822
Romain Guy5b3b3522010-10-27 18:57:51 -0700823 layer->region.clear();
824 return;
825 }
826
Romain Guy8a3957d2011-09-07 17:55:15 -0700827 // TODO: See LayerRenderer.cpp::generateMesh() for important
828 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800829 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700830 size_t count;
831 const android::Rect* rects = layer->region.getArray(&count);
832
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 const float alpha = layer->getAlpha() / 255.0f;
834 const float texX = 1.0f / float(layer->getWidth());
835 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800836 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700837
838 TextureVertex* mesh = mCaches.getRegionMesh();
839 GLsizei numQuads = 0;
840
Romain Guy7230a742011-01-10 22:26:16 -0800841 setupDraw();
842 setupDrawWithTexture();
843 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800844 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700845 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800846 setupDrawProgram();
847 setupDrawDirtyRegionsDisabled();
848 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800849 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700850 setupDrawTexture(layer->getTexture());
851 if (mSnapshot->transform->isPureTranslate()) {
852 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
853 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
854
Romain Guyd21b6e12011-11-30 20:21:23 -0800855 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
857 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800858 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
860 }
Romain Guy15bc6432011-12-13 13:11:32 -0800861 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700862
863 for (size_t i = 0; i < count; i++) {
864 const android::Rect* r = &rects[i];
865
866 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800867 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700868 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800869 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700870
871 // TODO: Reject quads outside of the clip
872 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
873 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
874 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
875 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
876
877 numQuads++;
878
879 if (numQuads >= REGION_MESH_QUAD_COUNT) {
880 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
881 numQuads = 0;
882 mesh = mCaches.getRegionMesh();
883 }
884 }
885
886 if (numQuads > 0) {
887 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
888 }
889
Romain Guy7230a742011-01-10 22:26:16 -0800890 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700891
892#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800893 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700894#endif
895
896 layer->region.clear();
897 }
898#else
899 composeLayerRect(layer, rect);
900#endif
901}
902
Romain Guy3a3133d2011-02-01 22:59:58 -0800903void OpenGLRenderer::drawRegionRects(const Region& region) {
904#if DEBUG_LAYERS_AS_REGIONS
905 size_t count;
906 const android::Rect* rects = region.getArray(&count);
907
908 uint32_t colors[] = {
909 0x7fff0000, 0x7f00ff00,
910 0x7f0000ff, 0x7fff00ff,
911 };
912
913 int offset = 0;
914 int32_t top = rects[0].top;
915
916 for (size_t i = 0; i < count; i++) {
917 if (top != rects[i].top) {
918 offset ^= 0x2;
919 top = rects[i].top;
920 }
921
922 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
923 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
924 SkXfermode::kSrcOver_Mode);
925 }
926#endif
927}
928
Romain Guy5b3b3522010-10-27 18:57:51 -0700929void OpenGLRenderer::dirtyLayer(const float left, const float top,
930 const float right, const float bottom, const mat4 transform) {
931#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800932 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700933 Rect bounds(left, top, right, bottom);
934 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800935 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700936 }
937#endif
938}
939
940void OpenGLRenderer::dirtyLayer(const float left, const float top,
941 const float right, const float bottom) {
942#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800943 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700944 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800945 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800946 }
947#endif
948}
949
950void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
951#if RENDER_LAYERS_AS_REGIONS
952 if (bounds.intersect(*mSnapshot->clipRect)) {
953 bounds.snapToPixelBoundaries();
954 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
955 if (!dirty.isEmpty()) {
956 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700957 }
958 }
959#endif
960}
961
Romain Guy54be1cd2011-06-13 19:04:27 -0700962void OpenGLRenderer::clearLayerRegions() {
963 const size_t count = mLayers.size();
964 if (count == 0) return;
965
966 if (!mSnapshot->isIgnored()) {
967 // Doing several glScissor/glClear here can negatively impact
968 // GPUs with a tiler architecture, instead we draw quads with
969 // the Clear blending mode
970
971 // The list contains bounds that have already been clipped
972 // against their initial clip rect, and the current clip
973 // is likely different so we need to disable clipping here
974 glDisable(GL_SCISSOR_TEST);
975
976 Vertex mesh[count * 6];
977 Vertex* vertex = mesh;
978
979 for (uint32_t i = 0; i < count; i++) {
980 Rect* bounds = mLayers.itemAt(i);
981
982 Vertex::set(vertex++, bounds->left, bounds->bottom);
983 Vertex::set(vertex++, bounds->left, bounds->top);
984 Vertex::set(vertex++, bounds->right, bounds->top);
985 Vertex::set(vertex++, bounds->left, bounds->bottom);
986 Vertex::set(vertex++, bounds->right, bounds->top);
987 Vertex::set(vertex++, bounds->right, bounds->bottom);
988
989 delete bounds;
990 }
991
992 setupDraw(false);
993 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
994 setupDrawBlending(true, SkXfermode::kClear_Mode);
995 setupDrawProgram();
996 setupDrawPureColorUniforms();
997 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800998 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700999
Romain Guy54be1cd2011-06-13 19:04:27 -07001000 glDrawArrays(GL_TRIANGLES, 0, count * 6);
1001
1002 glEnable(GL_SCISSOR_TEST);
1003 } else {
1004 for (uint32_t i = 0; i < count; i++) {
1005 delete mLayers.itemAt(i);
1006 }
1007 }
1008
1009 mLayers.clear();
1010}
1011
Romain Guybd6b79b2010-06-26 00:13:53 -07001012///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001013// Transforms
1014///////////////////////////////////////////////////////////////////////////////
1015
1016void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001018}
1019
1020void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001021 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001022}
1023
1024void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001025 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001026}
1027
Romain Guy807daf72011-01-18 11:19:19 -08001028void OpenGLRenderer::skew(float sx, float sy) {
1029 mSnapshot->transform->skew(sx, sy);
1030}
1031
Romain Guyf6a11b82010-06-23 17:47:49 -07001032void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001033 if (matrix) {
1034 mSnapshot->transform->load(*matrix);
1035 } else {
1036 mSnapshot->transform->loadIdentity();
1037 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001038}
1039
1040void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -07001041 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001042}
1043
1044void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001045 SkMatrix transform;
1046 mSnapshot->transform->copyTo(transform);
1047 transform.preConcat(*matrix);
1048 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001049}
1050
1051///////////////////////////////////////////////////////////////////////////////
1052// Clipping
1053///////////////////////////////////////////////////////////////////////////////
1054
Romain Guybb9524b2010-06-22 18:56:38 -07001055void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001056 Rect clip(*mSnapshot->clipRect);
1057 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001058
1059 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1060 clip.getWidth(), clip.getHeight());
1061
Romain Guy746b7402010-10-26 16:27:31 -07001062 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -07001063}
1064
1065const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001066 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001067}
1068
Romain Guyc7d53492010-06-25 13:41:57 -07001069bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001070 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001071 return true;
1072 }
1073
Romain Guy1d83e192010-08-17 11:37:00 -07001074 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001075 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001076 r.snapToPixelBoundaries();
1077
1078 Rect clipRect(*mSnapshot->clipRect);
1079 clipRect.snapToPixelBoundaries();
1080
1081 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001082}
1083
Romain Guy079ba2c2010-07-16 14:12:24 -07001084bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1085 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001086 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001087 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001088 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001089 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001090}
1091
Chet Haasea23eed82012-04-12 15:19:04 -07001092Rect* OpenGLRenderer::getClipRect() {
1093 return mSnapshot->clipRect;
1094}
1095
Romain Guyf6a11b82010-06-23 17:47:49 -07001096///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001097// Drawing commands
1098///////////////////////////////////////////////////////////////////////////////
1099
Romain Guy54be1cd2011-06-13 19:04:27 -07001100void OpenGLRenderer::setupDraw(bool clear) {
1101 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001102 if (mDirtyClip) {
1103 setScissorFromClip();
1104 }
1105 mDescription.reset();
1106 mSetShaderColor = false;
1107 mColorSet = false;
1108 mColorA = mColorR = mColorG = mColorB = 0.0f;
1109 mTextureUnit = 0;
1110 mTrackDirtyRegions = true;
1111}
1112
1113void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1114 mDescription.hasTexture = true;
1115 mDescription.hasAlpha8Texture = isAlpha8;
1116}
1117
Romain Guyaa6c24c2011-04-28 18:40:04 -07001118void OpenGLRenderer::setupDrawWithExternalTexture() {
1119 mDescription.hasExternalTexture = true;
1120}
1121
Romain Guy15bc6432011-12-13 13:11:32 -08001122void OpenGLRenderer::setupDrawNoTexture() {
1123 mCaches.disbaleTexCoordsVertexArray();
1124}
1125
Chet Haase5b0200b2011-04-13 17:58:08 -07001126void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001127 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001128}
1129
Romain Guyed6fcb02011-03-21 13:11:28 -07001130void OpenGLRenderer::setupDrawPoint(float pointSize) {
1131 mDescription.isPoint = true;
1132 mDescription.pointSize = pointSize;
1133}
1134
Romain Guy70ca14e2010-12-13 18:24:33 -08001135void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001136 setupDrawColor(color, (color >> 24) & 0xFF);
1137}
1138
1139void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1140 mColorA = alpha / 255.0f;
Chet Haasedb8c9a62012-03-21 18:54:18 -07001141 mColorA *= mSnapshot->alpha;
Chet Haase6cfdf452011-04-22 16:42:10 -07001142 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1143 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001144 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001145 mColorR = a * ((color >> 16) & 0xFF);
1146 mColorG = a * ((color >> 8) & 0xFF);
1147 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001148 mColorSet = true;
1149 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1150}
1151
Romain Guy86568192010-12-14 15:55:39 -08001152void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1153 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001154 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1155 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001156 const float a = mColorA / 255.0f;
1157 mColorR = a * ((color >> 16) & 0xFF);
1158 mColorG = a * ((color >> 8) & 0xFF);
1159 mColorB = a * ((color ) & 0xFF);
1160 mColorSet = true;
1161 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1162}
1163
Romain Guy70ca14e2010-12-13 18:24:33 -08001164void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1165 mColorA = a;
1166 mColorR = r;
1167 mColorG = g;
1168 mColorB = b;
1169 mColorSet = true;
1170 mSetShaderColor = mDescription.setColor(r, g, b, a);
1171}
1172
Romain Guy86568192010-12-14 15:55:39 -08001173void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1174 mColorA = a;
1175 mColorR = r;
1176 mColorG = g;
1177 mColorB = b;
1178 mColorSet = true;
1179 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1180}
1181
Romain Guy70ca14e2010-12-13 18:24:33 -08001182void OpenGLRenderer::setupDrawShader() {
1183 if (mShader) {
1184 mShader->describe(mDescription, mCaches.extensions);
1185 }
1186}
1187
1188void OpenGLRenderer::setupDrawColorFilter() {
1189 if (mColorFilter) {
1190 mColorFilter->describe(mDescription, mCaches.extensions);
1191 }
1192}
1193
Romain Guyf09ef512011-05-27 11:43:46 -07001194void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1195 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1196 mColorA = 1.0f;
1197 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001198 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001199 }
1200}
1201
Romain Guy70ca14e2010-12-13 18:24:33 -08001202void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001203 // When the blending mode is kClear_Mode, we need to use a modulate color
1204 // argb=1,0,0,0
1205 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001206 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1207 mDescription, swapSrcDst);
1208}
1209
1210void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001211 // When the blending mode is kClear_Mode, we need to use a modulate color
1212 // argb=1,0,0,0
1213 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001214 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1215 mDescription, swapSrcDst);
1216}
1217
1218void OpenGLRenderer::setupDrawProgram() {
1219 useProgram(mCaches.programCache.get(mDescription));
1220}
1221
1222void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1223 mTrackDirtyRegions = false;
1224}
1225
1226void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1227 bool ignoreTransform) {
1228 mModelView.loadTranslate(left, top, 0.0f);
1229 if (!ignoreTransform) {
1230 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1231 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1232 } else {
1233 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1234 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1235 }
1236}
1237
Chet Haase8a5cc922011-04-26 07:28:09 -07001238void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1239 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001240}
1241
Romain Guy70ca14e2010-12-13 18:24:33 -08001242void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1243 bool ignoreTransform, bool ignoreModelView) {
1244 if (!ignoreModelView) {
1245 mModelView.loadTranslate(left, top, 0.0f);
1246 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001247 } else {
1248 mModelView.loadIdentity();
1249 }
Romain Guy86568192010-12-14 15:55:39 -08001250 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1251 if (!ignoreTransform) {
1252 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1253 if (mTrackDirtyRegions && dirty) {
1254 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1255 }
1256 } else {
1257 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1258 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1259 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001260}
1261
Romain Guyed6fcb02011-03-21 13:11:28 -07001262void OpenGLRenderer::setupDrawPointUniforms() {
1263 int slot = mCaches.currentProgram->getUniform("pointSize");
1264 glUniform1f(slot, mDescription.pointSize);
1265}
1266
Romain Guy70ca14e2010-12-13 18:24:33 -08001267void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001268 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001269 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1270 }
1271}
1272
Romain Guy86568192010-12-14 15:55:39 -08001273void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001274 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001275 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001276 }
1277}
1278
Romain Guy70ca14e2010-12-13 18:24:33 -08001279void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1280 if (mShader) {
1281 if (ignoreTransform) {
1282 mModelView.loadInverse(*mSnapshot->transform);
1283 }
1284 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1285 }
1286}
1287
Romain Guy8d0d4782010-12-14 20:13:35 -08001288void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1289 if (mShader) {
1290 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1291 }
1292}
1293
Romain Guy70ca14e2010-12-13 18:24:33 -08001294void OpenGLRenderer::setupDrawColorFilterUniforms() {
1295 if (mColorFilter) {
1296 mColorFilter->setupProgram(mCaches.currentProgram);
1297 }
1298}
1299
1300void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001301 bool force = mCaches.bindMeshBuffer();
1302 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001303 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001304}
1305
1306void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1307 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001308 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001309 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001310}
1311
Romain Guyaa6c24c2011-04-28 18:40:04 -07001312void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1313 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001314 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001315 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001316}
1317
Romain Guy8f0095c2011-05-02 17:24:22 -07001318void OpenGLRenderer::setupDrawTextureTransform() {
1319 mDescription.hasTextureTransform = true;
1320}
1321
1322void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001323 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1324 GL_FALSE, &transform.data[0]);
1325}
1326
Romain Guy70ca14e2010-12-13 18:24:33 -08001327void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001328 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001329 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001330 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001331 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001332 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001333 }
Romain Guyd71dd362011-12-12 19:03:35 -08001334
Romain Guyf3a910b42011-12-12 20:35:21 -08001335 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001336 if (mCaches.currentProgram->texCoords >= 0) {
1337 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1338 }
1339
1340 mCaches.unbindIndicesBuffer();
1341}
1342
1343void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1344 bool force = mCaches.unbindMeshBuffer();
1345 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1346 if (mCaches.currentProgram->texCoords >= 0) {
1347 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001348 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001349}
1350
Chet Haase5b0200b2011-04-13 17:58:08 -07001351void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001352 bool force = mCaches.unbindMeshBuffer();
1353 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1354 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001355 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001356}
1357
1358/**
1359 * 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 -07001360 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1361 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1362 * attributes (one per vertex) are values from zero to one that tells the fragment
1363 * shader where the fragment is in relation to the line width/length overall; these values are
1364 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1365 * region of the line.
1366 * Note that we only pass down the width values in this setup function. The length coordinates
1367 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001368 */
Chet Haase99585ad2011-05-02 15:00:16 -07001369void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Romain Guy7b631422012-04-04 11:38:54 -07001370 GLvoid* lengthCoords, float boundaryWidthProportion, int& widthSlot, int& lengthSlot) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001371 bool force = mCaches.unbindMeshBuffer();
1372 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1373 vertices, gAAVertexStride);
1374 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001375 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001376
Romain Guy7b631422012-04-04 11:38:54 -07001377 widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
Chet Haase99585ad2011-05-02 15:00:16 -07001378 glEnableVertexAttribArray(widthSlot);
1379 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001380
Romain Guy7b631422012-04-04 11:38:54 -07001381 lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
Chet Haase99585ad2011-05-02 15:00:16 -07001382 glEnableVertexAttribArray(lengthSlot);
1383 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001384
Chet Haase5b0200b2011-04-13 17:58:08 -07001385 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001386 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001387
Chet Haase99585ad2011-05-02 15:00:16 -07001388 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001389 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Romain Guy7b631422012-04-04 11:38:54 -07001390 glUniform1f(inverseBoundaryWidthSlot, 1.0f / boundaryWidthProportion);
1391}
1392
1393void OpenGLRenderer::finishDrawAALine(const int widthSlot, const int lengthSlot) {
1394 glDisableVertexAttribArray(widthSlot);
1395 glDisableVertexAttribArray(lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001396}
1397
Romain Guy70ca14e2010-12-13 18:24:33 -08001398void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001399}
1400
1401///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001402// Drawing
1403///////////////////////////////////////////////////////////////////////////////
1404
Chet Haase1271e2c2012-04-20 09:54:27 -07001405status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList,
Romain Guy33f6beb2012-02-16 19:24:51 -08001406 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001407
Romain Guy0fe478e2010-11-08 12:08:41 -08001408 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1409 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001410 if (displayList && displayList->isRenderable()) {
Chet Haase1271e2c2012-04-20 09:54:27 -07001411 return displayList->replay(*this, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001412 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001413
Romain Guy65549432012-03-26 16:45:05 -07001414 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001415}
1416
Chet Haaseed30fd82011-04-22 16:18:45 -07001417void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1418 if (displayList) {
1419 displayList->output(*this, level);
1420 }
1421}
1422
Romain Guya168d732011-03-18 16:50:13 -07001423void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1424 int alpha;
1425 SkXfermode::Mode mode;
1426 getAlphaAndMode(paint, &alpha, &mode);
1427
Romain Guya168d732011-03-18 16:50:13 -07001428 float x = left;
1429 float y = top;
1430
Romain Guye3c26852011-07-25 16:36:01 -07001431 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001432 bool ignoreTransform = false;
1433 if (mSnapshot->transform->isPureTranslate()) {
1434 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1435 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1436 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001437 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001438 } else {
1439 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001440 }
1441
1442 setupDraw();
1443 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001444 if (paint) {
1445 setupDrawAlpha8Color(paint->getColor(), alpha);
1446 }
Romain Guya168d732011-03-18 16:50:13 -07001447 setupDrawColorFilter();
1448 setupDrawShader();
1449 setupDrawBlending(true, mode);
1450 setupDrawProgram();
1451 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001452
Romain Guya168d732011-03-18 16:50:13 -07001453 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001454 texture->setWrap(GL_CLAMP_TO_EDGE);
1455 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001456
Romain Guya168d732011-03-18 16:50:13 -07001457 setupDrawPureColorUniforms();
1458 setupDrawColorFilterUniforms();
1459 setupDrawShaderUniforms();
1460 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1461
1462 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1463
1464 finishDrawTexture();
1465}
1466
Chet Haase5c13d892010-10-08 08:37:55 -07001467void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001468 const float right = left + bitmap->width();
1469 const float bottom = top + bitmap->height();
1470
1471 if (quickReject(left, top, right, bottom)) {
1472 return;
1473 }
1474
Romain Guya1d3c912011-12-13 14:55:06 -08001475 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001476 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001477 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001478 const AutoTexture autoCleanup(texture);
1479
Romain Guy211370f2012-02-01 16:10:55 -08001480 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001481 drawAlphaBitmap(texture, left, top, paint);
1482 } else {
1483 drawTextureRect(left, top, right, bottom, texture, paint);
1484 }
Romain Guyce0537b2010-06-29 21:05:21 -07001485}
1486
Chet Haase5c13d892010-10-08 08:37:55 -07001487void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001488 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1489 const mat4 transform(*matrix);
1490 transform.mapRect(r);
1491
Romain Guy6926c722010-07-12 20:20:03 -07001492 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1493 return;
1494 }
1495
Romain Guya1d3c912011-12-13 14:55:06 -08001496 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001497 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001498 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001499 const AutoTexture autoCleanup(texture);
1500
Romain Guy5b3b3522010-10-27 18:57:51 -07001501 // This could be done in a cheaper way, all we need is pass the matrix
1502 // to the vertex shader. The save/restore is a bit overkill.
1503 save(SkCanvas::kMatrix_SaveFlag);
1504 concatMatrix(matrix);
1505 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1506 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001507}
1508
Romain Guye651cc62012-05-14 19:44:40 -07001509void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
1510 const float right = left + bitmap->width();
1511 const float bottom = top + bitmap->height();
1512
1513 if (quickReject(left, top, right, bottom)) {
1514 return;
1515 }
1516
1517 mCaches.activeTexture(0);
1518 Texture* texture = mCaches.textureCache.getTransient(bitmap);
1519 const AutoTexture autoCleanup(texture);
1520
1521 drawTextureRect(left, top, right, bottom, texture, paint);
1522}
1523
Romain Guy5a7b4662011-01-20 19:09:30 -08001524void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1525 float* vertices, int* colors, SkPaint* paint) {
1526 // TODO: Do a quickReject
1527 if (!vertices || mSnapshot->isIgnored()) {
1528 return;
1529 }
1530
Romain Guya1d3c912011-12-13 14:55:06 -08001531 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001532 Texture* texture = mCaches.textureCache.get(bitmap);
1533 if (!texture) return;
1534 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001535
Romain Guyd21b6e12011-11-30 20:21:23 -08001536 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1537 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001538
1539 int alpha;
1540 SkXfermode::Mode mode;
1541 getAlphaAndMode(paint, &alpha, &mode);
1542
Romain Guy5a7b4662011-01-20 19:09:30 -08001543 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001544
Romain Guyb18d2d02011-02-10 15:52:54 -08001545 float left = FLT_MAX;
1546 float top = FLT_MAX;
1547 float right = FLT_MIN;
1548 float bottom = FLT_MIN;
1549
1550#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001551 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001552#else
Romain Guy211370f2012-02-01 16:10:55 -08001553 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001554#endif
1555
Romain Guya566b7c2011-01-23 16:36:11 -08001556 // TODO: Support the colors array
1557 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001558 TextureVertex* vertex = mesh;
1559 for (int32_t y = 0; y < meshHeight; y++) {
1560 for (int32_t x = 0; x < meshWidth; x++) {
1561 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1562
1563 float u1 = float(x) / meshWidth;
1564 float u2 = float(x + 1) / meshWidth;
1565 float v1 = float(y) / meshHeight;
1566 float v2 = float(y + 1) / meshHeight;
1567
1568 int ax = i + (meshWidth + 1) * 2;
1569 int ay = ax + 1;
1570 int bx = i;
1571 int by = bx + 1;
1572 int cx = i + 2;
1573 int cy = cx + 1;
1574 int dx = i + (meshWidth + 1) * 2 + 2;
1575 int dy = dx + 1;
1576
1577 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1578 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1579 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1580
1581 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1582 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1583 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001584
1585#if RENDER_LAYERS_AS_REGIONS
1586 if (hasActiveLayer) {
1587 // TODO: This could be optimized to avoid unnecessary ops
1588 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1589 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1590 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1591 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1592 }
1593#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001594 }
1595 }
1596
Romain Guyb18d2d02011-02-10 15:52:54 -08001597#if RENDER_LAYERS_AS_REGIONS
1598 if (hasActiveLayer) {
1599 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1600 }
1601#endif
1602
Romain Guy5a7b4662011-01-20 19:09:30 -08001603 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1604 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001605 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001606}
1607
Romain Guy8ba548f2010-06-30 19:21:21 -07001608void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1609 float srcLeft, float srcTop, float srcRight, float srcBottom,
1610 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001611 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001612 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1613 return;
1614 }
1615
Romain Guya1d3c912011-12-13 14:55:06 -08001616 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001617 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001618 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001619 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001620
Romain Guy8ba548f2010-06-30 19:21:21 -07001621 const float width = texture->width;
1622 const float height = texture->height;
1623
Romain Guy68169722011-08-22 17:33:33 -07001624 const float u1 = fmax(0.0f, srcLeft / width);
1625 const float v1 = fmax(0.0f, srcTop / height);
1626 const float u2 = fmin(1.0f, srcRight / width);
1627 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001628
Romain Guy03750a02010-10-18 14:06:08 -07001629 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001630 resetDrawTextureTexCoords(u1, v1, u2, v2);
1631
Romain Guy03750a02010-10-18 14:06:08 -07001632 int alpha;
1633 SkXfermode::Mode mode;
1634 getAlphaAndMode(paint, &alpha, &mode);
1635
Romain Guyd21b6e12011-11-30 20:21:23 -08001636 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1637
Romain Guy211370f2012-02-01 16:10:55 -08001638 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001639 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1640 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1641
Romain Guyb5014982011-07-28 15:39:12 -07001642 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001643 // Enable linear filtering if the source rectangle is scaled
1644 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001645 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001646 }
Romain Guyb5014982011-07-28 15:39:12 -07001647
Romain Guyd21b6e12011-11-30 20:21:23 -08001648 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001649 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1650 texture->id, alpha / 255.0f, mode, texture->blend,
1651 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1652 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1653 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001654 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001655 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1656 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1657 GL_TRIANGLE_STRIP, gMeshCount);
1658 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001659
1660 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1661}
1662
Romain Guy4aa90572010-09-26 18:40:37 -07001663void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001664 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001665 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001666 if (quickReject(left, top, right, bottom)) {
1667 return;
1668 }
1669
Romain Guya1d3c912011-12-13 14:55:06 -08001670 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001671 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001672 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001673 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001674 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1675 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001676
1677 int alpha;
1678 SkXfermode::Mode mode;
1679 getAlphaAndMode(paint, &alpha, &mode);
1680
Romain Guy2728f962010-10-08 18:36:15 -07001681 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001682 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001683
Romain Guy211370f2012-02-01 16:10:55 -08001684 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001685 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001686#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001687 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001688 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001689 const float offsetX = left + mSnapshot->transform->getTranslateX();
1690 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001691 const size_t count = mesh->quads.size();
1692 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001693 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001694 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001695 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1696 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1697 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001698 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001699 dirtyLayer(left + bounds.left, top + bounds.top,
1700 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001701 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001702 }
1703 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001704#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001705
Romain Guy211370f2012-02-01 16:10:55 -08001706 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001707 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1708 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1709
1710 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1711 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1712 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1713 true, !mesh->hasEmptyQuads);
1714 } else {
1715 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1716 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1717 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1718 true, !mesh->hasEmptyQuads);
1719 }
Romain Guy054dc182010-10-15 17:55:25 -07001720 }
Romain Guyf7f93552010-07-08 19:17:03 -07001721}
1722
Chet Haase99ecdc42011-05-06 12:06:34 -07001723/**
Chet Haase858aa932011-05-12 09:06:00 -07001724 * This function uses a similar approach to that of AA lines in the drawLines() function.
1725 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1726 * shader to compute the translucency of the color, determined by whether a given pixel is
1727 * within that boundary region and how far into the region it is.
1728 */
1729void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001730 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001731 float inverseScaleX = 1.0f;
1732 float inverseScaleY = 1.0f;
1733 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001734 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001735 Matrix4 *mat = mSnapshot->transform;
1736 float m00 = mat->data[Matrix4::kScaleX];
1737 float m01 = mat->data[Matrix4::kSkewY];
1738 float m02 = mat->data[2];
1739 float m10 = mat->data[Matrix4::kSkewX];
1740 float m11 = mat->data[Matrix4::kScaleX];
1741 float m12 = mat->data[6];
1742 float scaleX = sqrt(m00 * m00 + m01 * m01);
1743 float scaleY = sqrt(m10 * m10 + m11 * m11);
1744 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1745 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1746 }
1747
1748 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001749 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001750 setupDrawAALine();
1751 setupDrawColor(color);
1752 setupDrawColorFilter();
1753 setupDrawShader();
1754 setupDrawBlending(true, mode);
1755 setupDrawProgram();
1756 setupDrawModelViewIdentity(true);
1757 setupDrawColorUniforms();
1758 setupDrawColorFilterUniforms();
1759 setupDrawShaderIdentityUniforms();
1760
1761 AAVertex rects[4];
1762 AAVertex* aaVertices = &rects[0];
1763 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1764 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1765
1766 float boundarySizeX = .5 * inverseScaleX;
1767 float boundarySizeY = .5 * inverseScaleY;
1768
1769 // Adjust the rect by the AA boundary padding
1770 left -= boundarySizeX;
1771 right += boundarySizeX;
1772 top -= boundarySizeY;
1773 bottom += boundarySizeY;
1774
1775 float width = right - left;
1776 float height = bottom - top;
1777
Romain Guy7b631422012-04-04 11:38:54 -07001778 int widthSlot;
1779 int lengthSlot;
1780
Chet Haase858aa932011-05-12 09:06:00 -07001781 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1782 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001783 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1784 boundaryWidthProportion, widthSlot, lengthSlot);
1785
Chet Haase858aa932011-05-12 09:06:00 -07001786 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1787 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1788 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
Romain Guy7b631422012-04-04 11:38:54 -07001789 glUniform1f(inverseBoundaryLengthSlot, (1.0f / boundaryHeightProportion));
Chet Haase858aa932011-05-12 09:06:00 -07001790
1791 if (!quickReject(left, top, right, bottom)) {
1792 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1793 AAVertex::set(aaVertices++, left, top, 1, 0);
1794 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1795 AAVertex::set(aaVertices++, right, top, 0, 0);
1796 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1797 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1798 }
Romain Guy7b631422012-04-04 11:38:54 -07001799
1800 finishDrawAALine(widthSlot, lengthSlot);
Chet Haase858aa932011-05-12 09:06:00 -07001801}
1802
1803/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001804 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1805 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1806 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1807 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1808 * of the line. Hairlines are more involved because we need to account for transform scaling
1809 * to end up with a one-pixel-wide line in screen space..
1810 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1811 * in combination with values that we calculate and pass down in this method. The basic approach
1812 * is that the quad we create contains both the core line area plus a bounding area in which
1813 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1814 * proportion of the width and the length of a given segment is represented by the boundary
1815 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1816 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1817 * on the inside). This ends up giving the result we want, with pixels that are completely
1818 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1819 * how far into the boundary region they are, which is determined by shader interpolation.
1820 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001821void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1822 if (mSnapshot->isIgnored()) return;
1823
1824 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001825 // We use half the stroke width here because we're going to position the quad
1826 // corner vertices half of the width away from the line endpoints
1827 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001828 // A stroke width of 0 has a special meaning in Skia:
1829 // it draws a line 1 px wide regardless of current transform
1830 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Romain Guy7b631422012-04-04 11:38:54 -07001831
Chet Haase99ecdc42011-05-06 12:06:34 -07001832 float inverseScaleX = 1.0f;
1833 float inverseScaleY = 1.0f;
1834 bool scaled = false;
Romain Guy7b631422012-04-04 11:38:54 -07001835
Chet Haase8a5cc922011-04-26 07:28:09 -07001836 int alpha;
1837 SkXfermode::Mode mode;
Romain Guy7b631422012-04-04 11:38:54 -07001838
Chet Haase8a5cc922011-04-26 07:28:09 -07001839 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001840 int verticesCount = count;
1841 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001842 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001843 verticesCount += (count - 4);
1844 }
1845
1846 if (isHairLine || isAA) {
1847 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1848 // the line on the screen should always be one pixel wide regardless of scale. For
1849 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001850 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001851 Matrix4 *mat = mSnapshot->transform;
1852 float m00 = mat->data[Matrix4::kScaleX];
1853 float m01 = mat->data[Matrix4::kSkewY];
1854 float m02 = mat->data[2];
1855 float m10 = mat->data[Matrix4::kSkewX];
1856 float m11 = mat->data[Matrix4::kScaleX];
1857 float m12 = mat->data[6];
Romain Guy7b631422012-04-04 11:38:54 -07001858
Romain Guy211370f2012-02-01 16:10:55 -08001859 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1860 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Romain Guy7b631422012-04-04 11:38:54 -07001861
Chet Haase99ecdc42011-05-06 12:06:34 -07001862 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1863 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
Romain Guy7b631422012-04-04 11:38:54 -07001864
Chet Haase99ecdc42011-05-06 12:06:34 -07001865 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1866 scaled = true;
1867 }
1868 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001869 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001870
1871 getAlphaAndMode(paint, &alpha, &mode);
1872 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001873 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001874 if (isAA) {
1875 setupDrawAALine();
1876 }
1877 setupDrawColor(paint->getColor(), alpha);
1878 setupDrawColorFilter();
1879 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001880 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001881 setupDrawProgram();
1882 setupDrawModelViewIdentity(true);
1883 setupDrawColorUniforms();
1884 setupDrawColorFilterUniforms();
1885 setupDrawShaderIdentityUniforms();
1886
1887 if (isHairLine) {
1888 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001889 halfStrokeWidth = isAA? 1 : .5;
1890 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001891 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001892 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001893 }
Romain Guy7b631422012-04-04 11:38:54 -07001894
1895 int widthSlot;
1896 int lengthSlot;
1897
Chet Haase5b0200b2011-04-13 17:58:08 -07001898 Vertex lines[verticesCount];
1899 Vertex* vertices = &lines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001900
Chet Haase99585ad2011-05-02 15:00:16 -07001901 AAVertex wLines[verticesCount];
1902 AAVertex* aaVertices = &wLines[0];
Romain Guy7b631422012-04-04 11:38:54 -07001903
Romain Guy211370f2012-02-01 16:10:55 -08001904 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001905 setupDrawVertices(vertices);
1906 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001907 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1908 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001909 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001910 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1911 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001912 // We will need to calculate the actual width proportion on each segment for
1913 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1914 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
Romain Guy7b631422012-04-04 11:38:54 -07001915 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords,
1916 boundaryWidthProportion, widthSlot, lengthSlot);
Chet Haase5b0200b2011-04-13 17:58:08 -07001917 }
1918
Chet Haase99ecdc42011-05-06 12:06:34 -07001919 AAVertex* prevAAVertex = NULL;
1920 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001921
Chet Haase99585ad2011-05-02 15:00:16 -07001922 int boundaryLengthSlot = -1;
1923 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001924 int boundaryWidthSlot = -1;
1925 int inverseBoundaryWidthSlot = -1;
Romain Guy7b631422012-04-04 11:38:54 -07001926
Chet Haase5b0200b2011-04-13 17:58:08 -07001927 for (int i = 0; i < count; i += 4) {
1928 // a = start point, b = end point
1929 vec2 a(points[i], points[i + 1]);
1930 vec2 b(points[i + 2], points[i + 3]);
Romain Guy7b631422012-04-04 11:38:54 -07001931
Chet Haase99585ad2011-05-02 15:00:16 -07001932 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001933 float boundaryLengthProportion = 0;
1934 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001935
Chet Haase5b0200b2011-04-13 17:58:08 -07001936 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001937 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001938 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 if (isAA) {
1940 float wideningFactor;
1941 if (fabs(n.x) >= fabs(n.y)) {
1942 wideningFactor = fabs(1.0f / n.x);
1943 } else {
1944 wideningFactor = fabs(1.0f / n.y);
1945 }
1946 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001947 }
Romain Guy7b631422012-04-04 11:38:54 -07001948
Chet Haase99ecdc42011-05-06 12:06:34 -07001949 if (scaled) {
1950 n.x *= inverseScaleX;
1951 n.y *= inverseScaleY;
1952 }
1953 } else if (scaled) {
1954 // Extend n by .5 pixel on each side, post-transform
1955 vec2 extendedN = n.copyNormalized();
1956 extendedN /= 2;
1957 extendedN.x *= inverseScaleX;
1958 extendedN.y *= inverseScaleY;
Romain Guy7b631422012-04-04 11:38:54 -07001959
Chet Haase99ecdc42011-05-06 12:06:34 -07001960 float extendedNLength = extendedN.length();
1961 // We need to set this value on the shader prior to drawing
1962 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1963 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001964 }
Romain Guy7b631422012-04-04 11:38:54 -07001965
Chet Haase5b0200b2011-04-13 17:58:08 -07001966 float x = n.x;
1967 n.x = -n.y;
1968 n.y = x;
1969
Chet Haase99585ad2011-05-02 15:00:16 -07001970 // aa lines expand the endpoint vertices to encompass the AA boundary
1971 if (isAA) {
1972 vec2 abVector = (b - a);
1973 length = abVector.length();
1974 abVector.normalize();
Romain Guy7b631422012-04-04 11:38:54 -07001975
Chet Haase99ecdc42011-05-06 12:06:34 -07001976 if (scaled) {
1977 abVector.x *= inverseScaleX;
1978 abVector.y *= inverseScaleY;
1979 float abLength = abVector.length();
1980 boundaryLengthProportion = abLength / (length + abLength);
1981 } else {
1982 boundaryLengthProportion = .5 / (length + 1);
1983 }
Romain Guy7b631422012-04-04 11:38:54 -07001984
Chet Haase99ecdc42011-05-06 12:06:34 -07001985 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001986 a -= abVector;
1987 b += abVector;
1988 }
1989
Chet Haase5b0200b2011-04-13 17:58:08 -07001990 // Four corners of the rectangle defining a thick line
1991 vec2 p1 = a - n;
1992 vec2 p2 = a + n;
1993 vec2 p3 = b + n;
1994 vec2 p4 = b - n;
1995
Chet Haase99585ad2011-05-02 15:00:16 -07001996
Chet Haase5b0200b2011-04-13 17:58:08 -07001997 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1998 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1999 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
2000 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
2001
2002 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07002003 if (!isAA) {
2004 if (prevVertex != NULL) {
2005 // Issue two repeat vertices to create degenerate triangles to bridge
2006 // between the previous line and the new one. This is necessary because
2007 // we are creating a single triangle_strip which will contain
2008 // potentially discontinuous line segments.
2009 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
2010 Vertex::set(vertices++, p1.x, p1.y);
2011 generatedVerticesCount += 2;
2012 }
Romain Guy7b631422012-04-04 11:38:54 -07002013
Chet Haase5b0200b2011-04-13 17:58:08 -07002014 Vertex::set(vertices++, p1.x, p1.y);
2015 Vertex::set(vertices++, p2.x, p2.y);
2016 Vertex::set(vertices++, p4.x, p4.y);
2017 Vertex::set(vertices++, p3.x, p3.y);
Romain Guy7b631422012-04-04 11:38:54 -07002018
Chet Haase5b0200b2011-04-13 17:58:08 -07002019 prevVertex = vertices - 1;
2020 generatedVerticesCount += 4;
2021 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07002022 if (!isHairLine && scaled) {
2023 // Must set width proportions per-segment for scaled non-hairlines to use the
2024 // correct AA boundary dimensions
2025 if (boundaryWidthSlot < 0) {
2026 boundaryWidthSlot =
2027 mCaches.currentProgram->getUniform("boundaryWidth");
2028 inverseBoundaryWidthSlot =
2029 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
2030 }
Romain Guy7b631422012-04-04 11:38:54 -07002031
Chet Haase99ecdc42011-05-06 12:06:34 -07002032 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
2033 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
2034 }
Romain Guy7b631422012-04-04 11:38:54 -07002035
Chet Haase99585ad2011-05-02 15:00:16 -07002036 if (boundaryLengthSlot < 0) {
2037 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
2038 inverseBoundaryLengthSlot =
2039 mCaches.currentProgram->getUniform("inverseBoundaryLength");
2040 }
Romain Guy7b631422012-04-04 11:38:54 -07002041
Chet Haase99ecdc42011-05-06 12:06:34 -07002042 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
2043 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07002044
Chet Haase5b0200b2011-04-13 17:58:08 -07002045 if (prevAAVertex != NULL) {
2046 // Issue two repeat vertices to create degenerate triangles to bridge
2047 // between the previous line and the new one. This is necessary because
2048 // we are creating a single triangle_strip which will contain
2049 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07002050 AAVertex::set(aaVertices++,prevAAVertex->position[0],
2051 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
2052 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07002053 generatedVerticesCount += 2;
2054 }
Romain Guy7b631422012-04-04 11:38:54 -07002055
Chet Haase99585ad2011-05-02 15:00:16 -07002056 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
2057 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
2058 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
2059 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Romain Guy7b631422012-04-04 11:38:54 -07002060
Chet Haase5b0200b2011-04-13 17:58:08 -07002061 prevAAVertex = aaVertices - 1;
2062 generatedVerticesCount += 4;
2063 }
Romain Guy7b631422012-04-04 11:38:54 -07002064
Chet Haase99585ad2011-05-02 15:00:16 -07002065 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
2066 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
2067 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07002068 }
2069 }
Romain Guy7b631422012-04-04 11:38:54 -07002070
Chet Haase5b0200b2011-04-13 17:58:08 -07002071 if (generatedVerticesCount > 0) {
2072 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
2073 }
Romain Guy7b631422012-04-04 11:38:54 -07002074
2075 if (isAA) {
2076 finishDrawAALine(widthSlot, lengthSlot);
2077 }
Chet Haase5b0200b2011-04-13 17:58:08 -07002078}
2079
Romain Guyed6fcb02011-03-21 13:11:28 -07002080void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2081 if (mSnapshot->isIgnored()) return;
2082
2083 // TODO: The paint's cap style defines whether the points are square or circular
2084 // TODO: Handle AA for round points
2085
Chet Haase5b0200b2011-04-13 17:58:08 -07002086 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002087 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002088 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002089 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002090 if (isHairLine) {
2091 // Now that we know it's hairline, we can set the effective width, to be used later
2092 strokeWidth = 1.0f;
2093 }
2094 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07002095 int alpha;
2096 SkXfermode::Mode mode;
2097 getAlphaAndMode(paint, &alpha, &mode);
2098
2099 int verticesCount = count >> 1;
2100 int generatedVerticesCount = 0;
2101
2102 TextureVertex pointsData[verticesCount];
2103 TextureVertex* vertex = &pointsData[0];
2104
2105 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002106 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002107 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002108 setupDrawColor(paint->getColor(), alpha);
2109 setupDrawColorFilter();
2110 setupDrawShader();
2111 setupDrawBlending(mode);
2112 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002113 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002114 setupDrawColorUniforms();
2115 setupDrawColorFilterUniforms();
2116 setupDrawPointUniforms();
2117 setupDrawShaderIdentityUniforms();
2118 setupDrawMesh(vertex);
2119
2120 for (int i = 0; i < count; i += 2) {
2121 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2122 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002123
Chet Haase8a5cc922011-04-26 07:28:09 -07002124 float left = points[i] - halfWidth;
2125 float right = points[i] + halfWidth;
2126 float top = points[i + 1] - halfWidth;
2127 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002128
Chet Haase8a5cc922011-04-26 07:28:09 -07002129 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002130 }
2131
2132 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2133}
2134
Romain Guy85bf02f2010-06-22 13:11:24 -07002135void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002136 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002137 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002138
Romain Guyae88e5e2010-10-22 17:49:18 -07002139 Rect& clip(*mSnapshot->clipRect);
2140 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002141
Romain Guy3d58c032010-07-14 16:34:53 -07002142 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002143}
Romain Guy9d5316e2010-06-24 19:30:36 -07002144
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002145void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002146 if (!texture) return;
2147 const AutoTexture autoCleanup(texture);
2148
2149 const float x = left + texture->left - texture->offset;
2150 const float y = top + texture->top - texture->offset;
2151
2152 drawPathTexture(texture, x, y, paint);
2153}
2154
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002155void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2156 float rx, float ry, SkPaint* paint) {
2157 if (mSnapshot->isIgnored()) return;
2158
Romain Guya1d3c912011-12-13 14:55:06 -08002159 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002160 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2161 right - left, bottom - top, rx, ry, paint);
2162 drawShape(left, top, texture, paint);
2163}
2164
Romain Guy01d58e42011-01-19 21:54:02 -08002165void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2166 if (mSnapshot->isIgnored()) return;
2167
Romain Guya1d3c912011-12-13 14:55:06 -08002168 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002169 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002170 drawShape(x - radius, y - radius, texture, paint);
2171}
Romain Guy01d58e42011-01-19 21:54:02 -08002172
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002173void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2174 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002175
Romain Guya1d3c912011-12-13 14:55:06 -08002176 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002177 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2178 drawShape(left, top, texture, paint);
2179}
2180
Romain Guy8b2f5262011-01-23 16:15:02 -08002181void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2182 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2183 if (mSnapshot->isIgnored()) return;
2184
2185 if (fabs(sweepAngle) >= 360.0f) {
2186 drawOval(left, top, right, bottom, paint);
2187 return;
2188 }
2189
Romain Guya1d3c912011-12-13 14:55:06 -08002190 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002191 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2192 startAngle, sweepAngle, useCenter, paint);
2193 drawShape(left, top, texture, paint);
2194}
2195
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002196void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2197 SkPaint* paint) {
2198 if (mSnapshot->isIgnored()) return;
2199
Romain Guya1d3c912011-12-13 14:55:06 -08002200 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002201 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2202 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002203}
2204
Chet Haase5c13d892010-10-08 08:37:55 -07002205void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002206 if (p->getStyle() != SkPaint::kFill_Style) {
2207 drawRectAsShape(left, top, right, bottom, p);
2208 return;
2209 }
2210
Romain Guy6926c722010-07-12 20:20:03 -07002211 if (quickReject(left, top, right, bottom)) {
2212 return;
2213 }
2214
Romain Guy026c5e162010-06-28 17:12:22 -07002215 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002216 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002217 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2218 if (!isMode) {
2219 // Assume SRC_OVER
2220 mode = SkXfermode::kSrcOver_Mode;
2221 }
2222 } else {
2223 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002224 }
2225
Romain Guy026c5e162010-06-28 17:12:22 -07002226 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002227 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002228 drawAARect(left, top, right, bottom, color, mode);
2229 } else {
2230 drawColorRect(left, top, right, bottom, color, mode);
2231 }
Romain Guyc7d53492010-06-25 13:41:57 -07002232}
Romain Guy9d5316e2010-06-24 19:30:36 -07002233
Romain Guyeb9a5362012-01-17 17:39:26 -08002234void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2235 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002236 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2237 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002238 return;
2239 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002240
Romain Guy671d6cf2012-01-18 12:39:17 -08002241 // NOTE: Skia does not support perspective transform on drawPosText yet
2242 if (!mSnapshot->transform->isSimple()) {
2243 return;
2244 }
2245
2246 float x = 0.0f;
2247 float y = 0.0f;
2248 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2249 if (pureTranslate) {
2250 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2251 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2252 }
2253
2254 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2255 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2256 paint->getTextSize());
2257
2258 int alpha;
2259 SkXfermode::Mode mode;
2260 getAlphaAndMode(paint, &alpha, &mode);
2261
2262 // Pick the appropriate texture filtering
2263 bool linearFilter = mSnapshot->transform->changesBounds();
2264 if (pureTranslate && !linearFilter) {
2265 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2266 }
2267
2268 mCaches.activeTexture(0);
2269 setupDraw();
2270 setupDrawDirtyRegionsDisabled();
2271 setupDrawWithTexture(true);
2272 setupDrawAlpha8Color(paint->getColor(), alpha);
2273 setupDrawColorFilter();
2274 setupDrawShader();
2275 setupDrawBlending(true, mode);
2276 setupDrawProgram();
2277 setupDrawModelView(x, y, x, y, pureTranslate, true);
2278 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2279 setupDrawPureColorUniforms();
2280 setupDrawColorFilterUniforms();
2281 setupDrawShaderUniforms(pureTranslate);
2282
2283 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2284 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2285
2286#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002287 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002288#else
Romain Guy211370f2012-02-01 16:10:55 -08002289 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002290#endif
2291
2292 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2293 positions, hasActiveLayer ? &bounds : NULL)) {
2294#if RENDER_LAYERS_AS_REGIONS
2295 if (hasActiveLayer) {
2296 if (!pureTranslate) {
2297 mSnapshot->transform->mapRect(bounds);
2298 }
2299 dirtyLayerUnchecked(bounds, getRegion());
2300 }
2301#endif
2302 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002303}
2304
Romain Guye8e62a42010-07-23 18:55:21 -07002305void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002306 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002307 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2308 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002309 return;
2310 }
2311
Chet Haasea1cff502012-02-21 13:43:44 -08002312 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002313 switch (paint->getTextAlign()) {
2314 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002315 x -= length / 2.0f;
2316 break;
2317 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002318 x -= length;
2319 break;
2320 default:
2321 break;
2322 }
2323
Romain Guycac5fd32011-12-01 20:08:50 -08002324 SkPaint::FontMetrics metrics;
2325 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002326 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002327 return;
2328 }
2329
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002330 const float oldX = x;
2331 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002332 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002333 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002334 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2335 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2336 }
2337
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002338#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002339 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002340#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002341
2342 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002343 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002344 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002345
Romain Guy86568192010-12-14 15:55:39 -08002346 int alpha;
2347 SkXfermode::Mode mode;
2348 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002349
Romain Guy211370f2012-02-01 16:10:55 -08002350 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002351 mCaches.activeTexture(0);
2352
Romain Guyb45c0c92010-08-26 20:35:23 -07002353 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002354 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2355 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002356 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002357
Romain Guy740bf2b2011-04-26 15:33:10 -07002358 const float sx = oldX - shadow->left + mShadowDx;
2359 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002360
Romain Guy86568192010-12-14 15:55:39 -08002361 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002362 int shadowColor = mShadowColor;
2363 if (mShader) {
2364 shadowColor = 0xffffffff;
2365 }
Romain Guy86568192010-12-14 15:55:39 -08002366
Romain Guy86568192010-12-14 15:55:39 -08002367 setupDraw();
2368 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002369 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2370 setupDrawColorFilter();
2371 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002372 setupDrawBlending(true, mode);
2373 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002374 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002375 setupDrawTexture(shadow->id);
2376 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002377 setupDrawColorFilterUniforms();
2378 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002379 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2380
Romain Guy0a417492010-08-16 20:26:20 -07002381 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002382 }
2383
Romain Guy6620c6d2010-12-06 18:07:02 -08002384 // Pick the appropriate texture filtering
2385 bool linearFilter = mSnapshot->transform->changesBounds();
2386 if (pureTranslate && !linearFilter) {
2387 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2388 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002389
Romain Guya1d3c912011-12-13 14:55:06 -08002390 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002391 setupDraw();
2392 setupDrawDirtyRegionsDisabled();
2393 setupDrawWithTexture(true);
2394 setupDrawAlpha8Color(paint->getColor(), alpha);
2395 setupDrawColorFilter();
2396 setupDrawShader();
2397 setupDrawBlending(true, mode);
2398 setupDrawProgram();
2399 setupDrawModelView(x, y, x, y, pureTranslate, true);
2400 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2401 setupDrawPureColorUniforms();
2402 setupDrawColorFilterUniforms();
2403 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002404
Romain Guy6620c6d2010-12-06 18:07:02 -08002405 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002406 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2407
2408#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002409 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002410#else
Romain Guy211370f2012-02-01 16:10:55 -08002411 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002412#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002413
Romain Guy6620c6d2010-12-06 18:07:02 -08002414 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002415 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002416#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002417 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002418 if (!pureTranslate) {
2419 mSnapshot->transform->mapRect(bounds);
2420 }
Romain Guyf219da52011-01-16 12:54:25 -08002421 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002422 }
2423#endif
2424 }
Romain Guy694b5192010-07-21 21:33:20 -07002425
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002426 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002427}
2428
Romain Guy325740f2012-02-24 16:48:34 -08002429void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2430 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002431 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2432 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2433 return;
2434 }
2435
Romain Guy03d58522012-02-24 17:54:07 -08002436 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2437 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2438 paint->getTextSize());
2439
2440 int alpha;
2441 SkXfermode::Mode mode;
2442 getAlphaAndMode(paint, &alpha, &mode);
2443
2444 mCaches.activeTexture(0);
2445 setupDraw();
2446 setupDrawDirtyRegionsDisabled();
2447 setupDrawWithTexture(true);
2448 setupDrawAlpha8Color(paint->getColor(), alpha);
2449 setupDrawColorFilter();
2450 setupDrawShader();
2451 setupDrawBlending(true, mode);
2452 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002453 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002454 setupDrawTexture(fontRenderer.getTexture(true));
2455 setupDrawPureColorUniforms();
2456 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002457 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002458
Romain Guy97771732012-02-28 18:17:02 -08002459 const Rect* clip = &mSnapshot->getLocalClip();
2460 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 -08002461
Romain Guy97771732012-02-28 18:17:02 -08002462#if RENDER_LAYERS_AS_REGIONS
2463 const bool hasActiveLayer = hasLayer();
2464#else
2465 const bool hasActiveLayer = false;
2466#endif
2467
2468 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2469 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2470#if RENDER_LAYERS_AS_REGIONS
2471 if (hasActiveLayer) {
2472 mSnapshot->transform->mapRect(bounds);
2473 dirtyLayerUnchecked(bounds, getRegion());
2474 }
2475#endif
2476 }
Romain Guy325740f2012-02-24 16:48:34 -08002477}
2478
Romain Guy7fbcc042010-08-04 15:40:07 -07002479void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002480 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002481
Romain Guya1d3c912011-12-13 14:55:06 -08002482 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002483
Romain Guy33f6beb2012-02-16 19:24:51 -08002484 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002485 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002486 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002487 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002488
Romain Guy8b55f372010-08-18 17:10:07 -07002489 const float x = texture->left - texture->offset;
2490 const float y = texture->top - texture->offset;
2491
Romain Guy01d58e42011-01-19 21:54:02 -08002492 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002493}
2494
Romain Guyada830f2011-01-13 12:13:20 -08002495void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2496 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002497 return;
2498 }
2499
Romain Guy2bf68f02012-03-02 13:37:47 -08002500 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2501 OpenGLRenderer* renderer = layer->renderer;
2502 Rect& dirty = layer->dirtyRect;
2503
2504 interrupt();
2505 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2506 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
Chet Haase1271e2c2012-04-20 09:54:27 -07002507 renderer->drawDisplayList(layer->displayList, dirty, DisplayList::kReplayFlag_ClipChildren);
Romain Guy2bf68f02012-03-02 13:37:47 -08002508 renderer->finish();
2509 resume();
2510
2511 dirty.setEmpty();
2512 layer->deferredUpdateScheduled = false;
2513 layer->renderer = NULL;
2514 layer->displayList = NULL;
2515 }
2516
Romain Guya1d3c912011-12-13 14:55:06 -08002517 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002518
2519 int alpha;
2520 SkXfermode::Mode mode;
2521 getAlphaAndMode(paint, &alpha, &mode);
2522
Romain Guy9ace8f52011-07-07 20:50:11 -07002523 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002524
Romain Guyf219da52011-01-16 12:54:25 -08002525#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002526 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002527 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002528 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002529 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002530 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002531 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002532
Romain Guyc88e3572011-01-22 00:32:12 -08002533 setupDraw();
2534 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002535 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002536 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002537 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002538 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002539 setupDrawPureColorUniforms();
2540 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002541 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002542 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002543 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2544 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2545
Romain Guyd21b6e12011-11-30 20:21:23 -08002546 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002547 setupDrawModelViewTranslate(x, y,
2548 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2549 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002550 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002551 setupDrawModelViewTranslate(x, y,
2552 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2553 }
Romain Guyc88e3572011-01-22 00:32:12 -08002554 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002555
Romain Guyc88e3572011-01-22 00:32:12 -08002556 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2557 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002558
Romain Guyc88e3572011-01-22 00:32:12 -08002559 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002560
2561#if DEBUG_LAYERS_AS_REGIONS
2562 drawRegionRects(layer->region);
2563#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002564 }
Romain Guyf219da52011-01-16 12:54:25 -08002565 }
2566#else
Romain Guyada830f2011-01-13 12:13:20 -08002567 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2568 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002569#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002570}
2571
Romain Guy6926c722010-07-12 20:20:03 -07002572///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002573// Shaders
2574///////////////////////////////////////////////////////////////////////////////
2575
2576void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002577 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002578}
2579
Romain Guy06f96e22010-07-30 19:18:16 -07002580void OpenGLRenderer::setupShader(SkiaShader* shader) {
2581 mShader = shader;
2582 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002583 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002584 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002585}
2586
Romain Guyd27977d2010-07-14 19:18:51 -07002587///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002588// Color filters
2589///////////////////////////////////////////////////////////////////////////////
2590
2591void OpenGLRenderer::resetColorFilter() {
2592 mColorFilter = NULL;
2593}
2594
2595void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2596 mColorFilter = filter;
2597}
2598
2599///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002600// Drop shadow
2601///////////////////////////////////////////////////////////////////////////////
2602
2603void OpenGLRenderer::resetShadow() {
2604 mHasShadow = false;
2605}
2606
2607void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2608 mHasShadow = true;
2609 mShadowRadius = radius;
2610 mShadowDx = dx;
2611 mShadowDy = dy;
2612 mShadowColor = color;
2613}
2614
2615///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002616// Draw filters
2617///////////////////////////////////////////////////////////////////////////////
2618
2619void OpenGLRenderer::resetPaintFilter() {
2620 mHasDrawFilter = false;
2621}
2622
2623void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2624 mHasDrawFilter = true;
2625 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2626 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2627}
2628
2629SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002630 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002631
2632 uint32_t flags = paint->getFlags();
2633
2634 mFilteredPaint = *paint;
2635 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2636
2637 return &mFilteredPaint;
2638}
2639
2640///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002641// Drawing implementation
2642///////////////////////////////////////////////////////////////////////////////
2643
Romain Guy01d58e42011-01-19 21:54:02 -08002644void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2645 float x, float y, SkPaint* paint) {
2646 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2647 return;
2648 }
2649
2650 int alpha;
2651 SkXfermode::Mode mode;
2652 getAlphaAndMode(paint, &alpha, &mode);
2653
2654 setupDraw();
2655 setupDrawWithTexture(true);
2656 setupDrawAlpha8Color(paint->getColor(), alpha);
2657 setupDrawColorFilter();
2658 setupDrawShader();
2659 setupDrawBlending(true, mode);
2660 setupDrawProgram();
2661 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2662 setupDrawTexture(texture->id);
2663 setupDrawPureColorUniforms();
2664 setupDrawColorFilterUniforms();
2665 setupDrawShaderUniforms();
2666 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2667
2668 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2669
2670 finishDrawTexture();
2671}
2672
Romain Guyf607bdc2010-09-10 19:20:06 -07002673// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002674#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2675#define kStdUnderline_Offset (1.0f / 9.0f)
2676#define kStdUnderline_Thickness (1.0f / 18.0f)
2677
2678void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2679 float x, float y, SkPaint* paint) {
2680 // Handle underline and strike-through
2681 uint32_t flags = paint->getFlags();
2682 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002683 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002684 float underlineWidth = length;
2685 // If length is > 0.0f, we already measured the text for the text alignment
2686 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002687 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002688 }
2689
2690 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002691 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002692 case SkPaint::kCenter_Align:
2693 offsetX = underlineWidth * 0.5f;
2694 break;
2695 case SkPaint::kRight_Align:
2696 offsetX = underlineWidth;
2697 break;
2698 default:
2699 break;
2700 }
2701
Romain Guy211370f2012-02-01 16:10:55 -08002702 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002703 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002704 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002705
Romain Guye20ecbd2010-09-22 19:49:04 -07002706 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002707 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002708
Romain Guyf6834472011-01-23 13:32:12 -08002709 int linesCount = 0;
2710 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2711 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2712
2713 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002714 float points[pointsCount];
2715 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002716
2717 if (flags & SkPaint::kUnderlineText_Flag) {
2718 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002719 points[currentPoint++] = left;
2720 points[currentPoint++] = top;
2721 points[currentPoint++] = left + underlineWidth;
2722 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002723 }
2724
2725 if (flags & SkPaint::kStrikeThruText_Flag) {
2726 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002727 points[currentPoint++] = left;
2728 points[currentPoint++] = top;
2729 points[currentPoint++] = left + underlineWidth;
2730 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002731 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002732
Romain Guy726aeba2011-06-01 14:52:00 -07002733 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002734
Romain Guy726aeba2011-06-01 14:52:00 -07002735 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002736 }
2737 }
2738}
2739
Romain Guy026c5e162010-06-28 17:12:22 -07002740void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002741 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002742 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002743 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002744 color |= 0x00ffffff;
2745 }
2746
Romain Guy70ca14e2010-12-13 18:24:33 -08002747 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002748 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002749 setupDrawColor(color);
2750 setupDrawShader();
2751 setupDrawColorFilter();
2752 setupDrawBlending(mode);
2753 setupDrawProgram();
2754 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2755 setupDrawColorUniforms();
2756 setupDrawShaderUniforms(ignoreTransform);
2757 setupDrawColorFilterUniforms();
2758 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002759
Romain Guyc95c8d62010-09-17 15:31:32 -07002760 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2761}
2762
Romain Guy82ba8142010-07-09 13:25:56 -07002763void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002764 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002765 int alpha;
2766 SkXfermode::Mode mode;
2767 getAlphaAndMode(paint, &alpha, &mode);
2768
Romain Guyd21b6e12011-11-30 20:21:23 -08002769 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002770
Romain Guy211370f2012-02-01 16:10:55 -08002771 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002772 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2773 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2774
Romain Guyd21b6e12011-11-30 20:21:23 -08002775 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002776 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2777 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2778 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2779 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002780 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002781 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2782 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2783 GL_TRIANGLE_STRIP, gMeshCount);
2784 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002785}
2786
Romain Guybd6b79b2010-06-26 00:13:53 -07002787void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002788 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2789 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002790 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002791}
2792
2793void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002794 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002795 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002796 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002797
Romain Guy746b7402010-10-26 16:27:31 -07002798 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002799 setupDrawWithTexture();
2800 setupDrawColor(alpha, alpha, alpha, alpha);
2801 setupDrawColorFilter();
2802 setupDrawBlending(blend, mode, swapSrcDst);
2803 setupDrawProgram();
2804 if (!dirty) {
2805 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002806 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002807 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002808 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002809 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002810 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002811 }
Romain Guy86568192010-12-14 15:55:39 -08002812 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002813 setupDrawColorFilterUniforms();
2814 setupDrawTexture(texture);
2815 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002816
Romain Guy6820ac82010-09-15 18:11:50 -07002817 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002818
2819 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002820}
2821
Romain Guya5aed0d2010-09-09 14:42:43 -07002822void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002823 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002824 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07002825
Romain Guy82ba8142010-07-09 13:25:56 -07002826 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002827 // These blend modes are not supported by OpenGL directly and have
2828 // to be implemented using shaders. Since the shader will perform
2829 // the blending, turn blending off here
2830 // If the blend mode cannot be implemented using shaders, fall
2831 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002832 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2833 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002834 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002835 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002836
Romain Guy82bc7a72012-01-03 14:13:39 -08002837 if (mCaches.blend) {
2838 glDisable(GL_BLEND);
2839 mCaches.blend = false;
2840 }
2841
2842 return;
2843 } else {
2844 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002845 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002846 }
2847
2848 if (!mCaches.blend) {
2849 glEnable(GL_BLEND);
2850 }
2851
2852 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2853 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2854
2855 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2856 glBlendFunc(sourceMode, destMode);
2857 mCaches.lastSrcMode = sourceMode;
2858 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002859 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002860 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002861 glDisable(GL_BLEND);
2862 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002863 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002864}
2865
Romain Guy889f8d12010-07-29 14:37:42 -07002866bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002867 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002868 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002869 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002870 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002871 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002872 }
Romain Guy6926c722010-07-12 20:20:03 -07002873 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002874}
2875
Romain Guy026c5e162010-06-28 17:12:22 -07002876void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002877 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002878 TextureVertex::setUV(v++, u1, v1);
2879 TextureVertex::setUV(v++, u2, v1);
2880 TextureVertex::setUV(v++, u1, v2);
2881 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002882}
2883
Chet Haase5c13d892010-10-08 08:37:55 -07002884void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002885 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002886 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002887
2888 // Skia draws using the color's alpha channel if < 255
2889 // Otherwise, it uses the paint's alpha
2890 int color = paint->getColor();
2891 *alpha = (color >> 24) & 0xFF;
2892 if (*alpha == 255) {
2893 *alpha = paint->getAlpha();
2894 }
2895 } else {
2896 *mode = SkXfermode::kSrcOver_Mode;
2897 *alpha = 255;
2898 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07002899 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07002900}
2901
Romain Guya5aed0d2010-09-09 14:42:43 -07002902SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002903 SkXfermode::Mode resultMode;
2904 if (!SkXfermode::AsMode(mode, &resultMode)) {
2905 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002906 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002907 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002908}
2909
Romain Guy9d5316e2010-06-24 19:30:36 -07002910}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002911}; // namespace android