blob: a7c53cacca9ce9b13e20475a2f2cb42707e1ea7a [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080036#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070037
38namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070039namespace uirenderer {
40
41///////////////////////////////////////////////////////////////////////////////
42// Defines
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy759ea802010-09-16 20:49:46 -070045#define RAD_TO_DEG (180.0f / 3.14159265f)
46#define MIN_ANGLE 0.001f
47
Romain Guydbc26d22010-10-11 17:58:29 -070048// TODO: This should be set in properties
49#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
50
Romain Guyd21b6e12011-11-30 20:21:23 -080051#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
52
Romain Guy9d5316e2010-06-24 19:30:36 -070053///////////////////////////////////////////////////////////////////////////////
54// Globals
55///////////////////////////////////////////////////////////////////////////////
56
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070069 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
82 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
83 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070084};
Romain Guye4d01122010-06-16 18:44:05 -070085
Romain Guy87a76572010-09-13 18:11:21 -070086// This array contains the swapped version of each SkXfermode. For instance
87// this array's SrcOver blending mode is actually DstOver. You can refer to
88// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070089static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070090 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
92 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
93 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
94 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
96 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
97 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
100 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
103 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
104 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700105};
106
Romain Guyf6a11b82010-06-23 17:47:49 -0700107///////////////////////////////////////////////////////////////////////////////
108// Constructors/destructor
109///////////////////////////////////////////////////////////////////////////////
110
Romain Guyfb8b7632010-08-23 21:05:08 -0700111OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700112 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700113 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700114 mHasShadow = false;
Romain Guy5ff9df62012-01-23 17:09:05 -0800115 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700116
Romain Guyac670c02010-07-27 17:39:27 -0700117 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
118
Romain Guyae5575b2010-07-29 18:48:04 -0700119 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700120}
121
Romain Guy85bf02f2010-06-22 13:11:24 -0700122OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700123 // The context has already been destroyed at this point, do not call
124 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
Romain Guy13631f32012-01-30 17:41:55 -0800128// Debug
129///////////////////////////////////////////////////////////////////////////////
130
131void OpenGLRenderer::startMark(const char* name) const {
132 mCaches.startMark(0, name);
133}
134
135void OpenGLRenderer::endMark() const {
136 mCaches.endMark();
137}
138
139///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700140// Setup
141///////////////////////////////////////////////////////////////////////////////
142
Romain Guy530041d2012-01-25 18:56:29 -0800143uint32_t OpenGLRenderer::getStencilSize() {
144 return STENCIL_BUFFER_SIZE;
145}
146
Romain Guy85bf02f2010-06-22 13:11:24 -0700147void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700148 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700149
150 mWidth = width;
151 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700152
153 mFirstSnapshot->height = height;
154 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700155
Romain Guy3e263fa2011-12-12 16:47:48 -0800156 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800159
Romain Guy3e263fa2011-12-12 16:47:48 -0800160 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
165}
166
167void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800168 mCaches.clearGarbage();
169
Romain Guy8aef54f2010-09-01 15:13:49 -0700170 mSnapshot = new Snapshot(mFirstSnapshot,
171 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800172 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700173 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700174
Romain Guy39d252a2011-12-12 18:14:06 -0800175 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800176 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800177
Romain Guy7d7b5492011-01-24 16:33:45 -0800178 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800179 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800180
Romain Guy6b7bd242010-10-06 19:49:23 -0700181 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700182 glClear(GL_COLOR_BUFFER_BIT);
183 }
Romain Guybb9524b2010-06-22 18:56:38 -0700184}
185
Romain Guyb025b9c2010-09-16 14:16:48 -0700186void OpenGLRenderer::finish() {
187#if DEBUG_OPENGL
188 GLenum status = GL_NO_ERROR;
189 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000190 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800191 switch (status) {
192 case GL_OUT_OF_MEMORY:
Steve Block3762c312012-01-06 19:20:56 +0000193 ALOGE(" OpenGLRenderer is out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800194 break;
195 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700196 }
197#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800198#if DEBUG_MEMORY_USAGE
199 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800200#else
201 if (mCaches.getDebugLevel() & kDebugMemory) {
202 mCaches.dumpMemoryUsage();
203 }
Romain Guyc15008e2010-11-10 11:59:15 -0800204#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700205}
206
Romain Guy6c319ca2011-01-11 14:29:25 -0800207void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700208 if (mCaches.currentProgram) {
209 if (mCaches.currentProgram->isInUse()) {
210 mCaches.currentProgram->remove();
211 mCaches.currentProgram = NULL;
212 }
213 }
Romain Guy50c0f092010-10-19 11:42:22 -0700214 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800215 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800216 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800217 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700218}
219
Romain Guy6c319ca2011-01-11 14:29:25 -0800220void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800221 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
222
223 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800224 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
225
Romain Guyda8532c2010-08-31 11:50:35 -0700226 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800227 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700228 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700229
Romain Guya1d3c912011-12-13 14:55:06 -0800230 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800231 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700232
Romain Guy50c0f092010-10-19 11:42:22 -0700233 mCaches.blend = true;
234 glEnable(GL_BLEND);
235 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
236 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700237}
238
Romain Guy65549432012-03-26 16:45:05 -0700239status_t OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800240 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800241 if (mDirtyClip) {
242 setScissorFromClip();
243 }
Romain Guyd643bb52011-03-01 14:55:21 -0800244
Romain Guy80911b82011-03-16 15:30:12 -0700245 Rect clip(*mSnapshot->clipRect);
246 clip.snapToPixelBoundaries();
247
Romain Guyd643bb52011-03-01 14:55:21 -0800248#if RENDER_LAYERS_AS_REGIONS
249 // Since we don't know what the functor will draw, let's dirty
250 // tne entire clip region
251 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800252 dirtyLayerUnchecked(clip, getRegion());
253 }
254#endif
255
Romain Guy08aa2cb2011-03-17 11:06:57 -0700256 DrawGlInfo info;
257 info.clipLeft = clip.left;
258 info.clipTop = clip.top;
259 info.clipRight = clip.right;
260 info.clipBottom = clip.bottom;
261 info.isLayer = hasLayer();
262 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700263
Romain Guy08aa2cb2011-03-17 11:06:57 -0700264 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800265
266 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700267 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800268 dirty.unionWith(localDirty);
269 }
270
Chet Haasedaf98e92011-01-10 14:10:36 -0800271 resume();
Romain Guy65549432012-03-26 16:45:05 -0700272 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800273}
274
Romain Guyf6a11b82010-06-23 17:47:49 -0700275///////////////////////////////////////////////////////////////////////////////
276// State management
277///////////////////////////////////////////////////////////////////////////////
278
Romain Guybb9524b2010-06-22 18:56:38 -0700279int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700280 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700281}
282
283int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700284 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
287void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700288 if (mSaveCount > 1) {
289 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700290 }
Romain Guybb9524b2010-06-22 18:56:38 -0700291}
292
293void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700294 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700295
Romain Guy8fb95422010-08-17 18:38:51 -0700296 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700297 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700298 }
Romain Guybb9524b2010-06-22 18:56:38 -0700299}
300
Romain Guy8aef54f2010-09-01 15:13:49 -0700301int OpenGLRenderer::saveSnapshot(int flags) {
302 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700303 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700304}
305
306bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700307 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700308 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700309 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700310
Romain Guybd6b79b2010-06-26 00:13:53 -0700311 sp<Snapshot> current = mSnapshot;
312 sp<Snapshot> previous = mSnapshot->previous;
313
Romain Guyeb993562010-10-05 18:14:38 -0700314 if (restoreOrtho) {
315 Rect& r = previous->viewport;
316 glViewport(r.left, r.top, r.right, r.bottom);
317 mOrthoMatrix.load(current->orthoMatrix);
318 }
319
Romain Guy8b55f372010-08-18 17:10:07 -0700320 mSaveCount--;
321 mSnapshot = previous;
322
Romain Guy2542d192010-08-18 11:47:12 -0700323 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700324 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700325 }
Romain Guy2542d192010-08-18 11:47:12 -0700326
Romain Guy5ec99242010-11-03 16:19:08 -0700327 if (restoreLayer) {
328 composeLayer(current, previous);
329 }
330
Romain Guy2542d192010-08-18 11:47:12 -0700331 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700332}
333
Romain Guyf6a11b82010-06-23 17:47:49 -0700334///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700335// Layers
336///////////////////////////////////////////////////////////////////////////////
337
338int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700339 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700340 const GLuint previousFbo = mSnapshot->fbo;
341 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700342
Romain Guyaf636eb2010-12-09 17:47:21 -0800343 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700344 int alpha = 255;
345 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700346
Romain Guye45362c2010-11-03 19:58:32 -0700347 if (p) {
348 alpha = p->getAlpha();
349 if (!mCaches.extensions.hasFramebufferFetch()) {
350 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
351 if (!isMode) {
352 // Assume SRC_OVER
353 mode = SkXfermode::kSrcOver_Mode;
354 }
355 } else {
356 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700357 }
358 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700359 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700360 }
Romain Guyd55a8612010-06-28 17:42:46 -0700361
Romain Guydbc26d22010-10-11 17:58:29 -0700362 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
363 }
Romain Guyd55a8612010-06-28 17:42:46 -0700364
365 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700366}
367
368int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
369 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700370 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700371 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700372 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700373 SkPaint paint;
374 paint.setAlpha(alpha);
375 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700376 }
Romain Guyd55a8612010-06-28 17:42:46 -0700377}
Romain Guybd6b79b2010-06-26 00:13:53 -0700378
Romain Guy1c740bc2010-09-13 18:00:09 -0700379/**
380 * Layers are viewed by Skia are slightly different than layers in image editing
381 * programs (for instance.) When a layer is created, previously created layers
382 * and the frame buffer still receive every drawing command. For instance, if a
383 * layer is created and a shape intersecting the bounds of the layers and the
384 * framebuffer is draw, the shape will be drawn on both (unless the layer was
385 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
386 *
387 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
388 * texture. Unfortunately, this is inefficient as it requires every primitive to
389 * be drawn n + 1 times, where n is the number of active layers. In practice this
390 * means, for every primitive:
391 * - Switch active frame buffer
392 * - Change viewport, clip and projection matrix
393 * - Issue the drawing
394 *
395 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700396 * To avoid this, layers are implemented in a different way here, at least in the
397 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
398 * is set. When this flag is set we can redirect all drawing operations into a
399 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700400 *
401 * This implementation relies on the frame buffer being at least RGBA 8888. When
402 * a layer is created, only a texture is created, not an FBO. The content of the
403 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700404 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700405 * buffer and drawing continues as normal. This technique therefore treats the
406 * frame buffer as a scratch buffer for the layers.
407 *
408 * To compose the layers back onto the frame buffer, each layer texture
409 * (containing the original frame buffer data) is drawn as a simple quad over
410 * the frame buffer. The trick is that the quad is set as the composition
411 * destination in the blending equation, and the frame buffer becomes the source
412 * of the composition.
413 *
414 * Drawing layers with an alpha value requires an extra step before composition.
415 * An empty quad is drawn over the layer's region in the frame buffer. This quad
416 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
417 * quad is used to multiply the colors in the frame buffer. This is achieved by
418 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
419 * GL_ZERO, GL_SRC_ALPHA.
420 *
421 * Because glCopyTexImage2D() can be slow, an alternative implementation might
422 * be use to draw a single clipped layer. The implementation described above
423 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700424 *
425 * (1) The frame buffer is actually not cleared right away. To allow the GPU
426 * to potentially optimize series of calls to glCopyTexImage2D, the frame
427 * buffer is left untouched until the first drawing operation. Only when
428 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700429 */
Romain Guyd55a8612010-06-28 17:42:46 -0700430bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700431 float right, float bottom, int alpha, SkXfermode::Mode mode,
432 int flags, GLuint previousFbo) {
433 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700434 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700435
Romain Guyeb993562010-10-05 18:14:38 -0700436 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
437
Romain Guyf607bdc2010-09-10 19:20:06 -0700438 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700439 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700440 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700441 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700442
Romain Guyeb993562010-10-05 18:14:38 -0700443 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700444 if (bounds.intersect(*snapshot->clipRect)) {
445 // We cannot work with sub-pixels in this case
446 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700447
Romain Guyad37cd32011-03-15 11:12:25 -0700448 // When the layer is not an FBO, we may use glCopyTexImage so we
449 // need to make sure the layer does not extend outside the bounds
450 // of the framebuffer
451 if (!bounds.intersect(snapshot->previous->viewport)) {
452 bounds.setEmpty();
453 }
454 } else {
455 bounds.setEmpty();
456 }
Romain Guyeb993562010-10-05 18:14:38 -0700457 }
Romain Guybf434112010-09-16 14:40:17 -0700458
Romain Guy746b7402010-10-26 16:27:31 -0700459 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
460 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800461 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700462 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700463 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700464 }
465
466 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800467 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700468 return false;
469 }
Romain Guyf18fd992010-07-08 11:45:51 -0700470
Romain Guya1d3c912011-12-13 14:55:06 -0800471 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700472 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700473 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700474 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700475 }
476
Romain Guy9ace8f52011-07-07 20:50:11 -0700477 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700478 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700479 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
480 bounds.getWidth() / float(layer->getWidth()), 0.0f);
481 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700482
Romain Guy8fb95422010-08-17 18:38:51 -0700483 // Save the layer in the snapshot
484 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700485 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700486
Romain Guyeb993562010-10-05 18:14:38 -0700487 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700488 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700489 } else {
490 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700491 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800492 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700493 if (layer->isEmpty()) {
494 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
495 bounds.left, snapshot->height - bounds.bottom,
496 layer->getWidth(), layer->getHeight(), 0);
497 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800498 } else {
499 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
500 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
501 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700502
Romain Guy54be1cd2011-06-13 19:04:27 -0700503 // Enqueue the buffer coordinates to clear the corresponding region later
504 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700505 }
Romain Guyeb993562010-10-05 18:14:38 -0700506 }
Romain Guyf86ef572010-07-01 11:05:42 -0700507
Romain Guyd55a8612010-06-28 17:42:46 -0700508 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700509}
510
Romain Guy5b3b3522010-10-27 18:57:51 -0700511bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
512 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700513 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515#if RENDER_LAYERS_AS_REGIONS
516 snapshot->region = &snapshot->layer->region;
517 snapshot->flags |= Snapshot::kFlagFboTarget;
518#endif
519
520 Rect clip(bounds);
521 snapshot->transform->mapRect(clip);
522 clip.intersect(*snapshot->clipRect);
523 clip.snapToPixelBoundaries();
524 clip.intersect(snapshot->previous->viewport);
525
526 mat4 inverse;
527 inverse.loadInverse(*mSnapshot->transform);
528
529 inverse.mapRect(clip);
530 clip.snapToPixelBoundaries();
531 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700532 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700533
534 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700536 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700537 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
538 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
539 snapshot->height = bounds.getHeight();
540 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
541 snapshot->orthoMatrix.load(mOrthoMatrix);
542
543 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700544 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
545 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700546
547 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700548 if (layer->isEmpty()) {
549 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
550 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700551 }
552
553 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700554 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700555
556#if DEBUG_LAYERS_AS_REGIONS
557 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
558 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000559 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700560
561 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700562 layer->deleteTexture();
563 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700564
565 delete layer;
566
567 return false;
568 }
569#endif
570
571 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800572 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700573 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700574 glClear(GL_COLOR_BUFFER_BIT);
575
576 dirtyClip();
577
578 // Change the ortho projection
579 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
580 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
581
582 return true;
583}
584
Romain Guy1c740bc2010-09-13 18:00:09 -0700585/**
586 * Read the documentation of createLayer() before doing anything in this method.
587 */
Romain Guy1d83e192010-08-17 11:37:00 -0700588void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
589 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000590 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700591 return;
592 }
593
Romain Guy5b3b3522010-10-27 18:57:51 -0700594 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700595
596 if (fboLayer) {
597 // Unbind current FBO and restore previous one
598 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
599 }
600
Romain Guy1d83e192010-08-17 11:37:00 -0700601 Layer* layer = current->layer;
602 const Rect& rect = layer->layer;
603
Romain Guy9ace8f52011-07-07 20:50:11 -0700604 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700605 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700606 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700607 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700608 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700609 }
610
Romain Guy03750a02010-10-18 14:06:08 -0700611 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700612
Romain Guya1d3c912011-12-13 14:55:06 -0800613 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700614
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 // When the layer is stored in an FBO, we can save a bit of fillrate by
616 // drawing only the dirty region
617 if (fboLayer) {
618 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700619 if (layer->getColorFilter()) {
620 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800621 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700622 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700623 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800624 resetColorFilter();
625 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 } else if (!rect.isEmpty()) {
627 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
628 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700629 }
Romain Guy8b55f372010-08-18 17:10:07 -0700630
Romain Guyeb993562010-10-05 18:14:38 -0700631 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800632 // Note: No need to use glDiscardFramebufferEXT() since we never
633 // create/compose layers that are not on screen with this
634 // code path
635 // See LayerRenderer::destroyLayer(Layer*)
636
Romain Guyeb993562010-10-05 18:14:38 -0700637 // Detach the texture from the FBO
638 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
639 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
640 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
641
642 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
643 mCaches.fboCache.put(current->fbo);
644 }
645
Romain Guy746b7402010-10-26 16:27:31 -0700646 dirtyClip();
647
Romain Guyeb993562010-10-05 18:14:38 -0700648 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700649 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700650 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700652 delete layer;
653 }
654}
655
Romain Guyaa6c24c2011-04-28 18:40:04 -0700656void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700657 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658
Romain Guy302a9df2011-08-16 13:55:02 -0700659 mat4& transform = layer->getTransform();
660 if (!transform.isIdentity()) {
661 save(0);
662 mSnapshot->transform->multiply(transform);
663 }
664
Romain Guyaa6c24c2011-04-28 18:40:04 -0700665 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700666 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700667 setupDrawWithTexture();
668 } else {
669 setupDrawWithExternalTexture();
670 }
671 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700672 setupDrawColor(alpha, alpha, alpha, alpha);
673 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700675 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700676 setupDrawPureColorUniforms();
677 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700678 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
679 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700680 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700682 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700683 if (mSnapshot->transform->isPureTranslate() &&
684 layer->getWidth() == (uint32_t) rect.getWidth() &&
685 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700686 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
687 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
688
Romain Guyd21b6e12011-11-30 20:21:23 -0800689 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
691 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800692 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700693 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
694 }
695 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700696 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
697
698 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
699
700 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700701
702 if (!transform.isIdentity()) {
703 restore();
704 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700705}
706
Romain Guy5b3b3522010-10-27 18:57:51 -0700707void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700709 const Rect& texCoords = layer->texCoords;
710 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
711 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700712
Romain Guy9ace8f52011-07-07 20:50:11 -0700713 float x = rect.left;
714 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700715 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700716 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700717 layer->getHeight() == (uint32_t) rect.getHeight();
718
719 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700720 // When we're swapping, the layer is already in screen coordinates
721 if (!swap) {
722 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
723 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
724 }
725
Romain Guyd21b6e12011-11-30 20:21:23 -0800726 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700727 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800728 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700729 }
730
731 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
732 layer->getTexture(), layer->getAlpha() / 255.0f,
733 layer->getMode(), layer->isBlend(),
734 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
735 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700736
Romain Guyaa6c24c2011-04-28 18:40:04 -0700737 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
738 } else {
739 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
740 drawTextureLayer(layer, rect);
741 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
742 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700743}
744
745void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
746#if RENDER_LAYERS_AS_REGIONS
747 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700748 layer->setRegionAsRect();
749
Romain Guy40667672011-03-18 14:34:03 -0700750 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700751
Romain Guy5b3b3522010-10-27 18:57:51 -0700752 layer->region.clear();
753 return;
754 }
755
Romain Guy8a3957d2011-09-07 17:55:15 -0700756 // TODO: See LayerRenderer.cpp::generateMesh() for important
757 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800758 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700759 size_t count;
760 const android::Rect* rects = layer->region.getArray(&count);
761
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 const float alpha = layer->getAlpha() / 255.0f;
763 const float texX = 1.0f / float(layer->getWidth());
764 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800765 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700766
767 TextureVertex* mesh = mCaches.getRegionMesh();
768 GLsizei numQuads = 0;
769
Romain Guy7230a742011-01-10 22:26:16 -0800770 setupDraw();
771 setupDrawWithTexture();
772 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800773 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800775 setupDrawProgram();
776 setupDrawDirtyRegionsDisabled();
777 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800778 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 setupDrawTexture(layer->getTexture());
780 if (mSnapshot->transform->isPureTranslate()) {
781 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
782 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
783
Romain Guyd21b6e12011-11-30 20:21:23 -0800784 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
786 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800787 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
789 }
Romain Guy15bc6432011-12-13 13:11:32 -0800790 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700791
792 for (size_t i = 0; i < count; i++) {
793 const android::Rect* r = &rects[i];
794
795 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800796 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700797 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800798 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700799
800 // TODO: Reject quads outside of the clip
801 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
802 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
803 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
804 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
805
806 numQuads++;
807
808 if (numQuads >= REGION_MESH_QUAD_COUNT) {
809 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
810 numQuads = 0;
811 mesh = mCaches.getRegionMesh();
812 }
813 }
814
815 if (numQuads > 0) {
816 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
817 }
818
Romain Guy7230a742011-01-10 22:26:16 -0800819 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
821#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800822 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700823#endif
824
825 layer->region.clear();
826 }
827#else
828 composeLayerRect(layer, rect);
829#endif
830}
831
Romain Guy3a3133d2011-02-01 22:59:58 -0800832void OpenGLRenderer::drawRegionRects(const Region& region) {
833#if DEBUG_LAYERS_AS_REGIONS
834 size_t count;
835 const android::Rect* rects = region.getArray(&count);
836
837 uint32_t colors[] = {
838 0x7fff0000, 0x7f00ff00,
839 0x7f0000ff, 0x7fff00ff,
840 };
841
842 int offset = 0;
843 int32_t top = rects[0].top;
844
845 for (size_t i = 0; i < count; i++) {
846 if (top != rects[i].top) {
847 offset ^= 0x2;
848 top = rects[i].top;
849 }
850
851 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
852 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
853 SkXfermode::kSrcOver_Mode);
854 }
855#endif
856}
857
Romain Guy5b3b3522010-10-27 18:57:51 -0700858void OpenGLRenderer::dirtyLayer(const float left, const float top,
859 const float right, const float bottom, const mat4 transform) {
860#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800861 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700862 Rect bounds(left, top, right, bottom);
863 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800864 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 }
866#endif
867}
868
869void OpenGLRenderer::dirtyLayer(const float left, const float top,
870 const float right, const float bottom) {
871#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800872 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800874 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800875 }
876#endif
877}
878
879void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
880#if RENDER_LAYERS_AS_REGIONS
881 if (bounds.intersect(*mSnapshot->clipRect)) {
882 bounds.snapToPixelBoundaries();
883 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
884 if (!dirty.isEmpty()) {
885 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700886 }
887 }
888#endif
889}
890
Romain Guy54be1cd2011-06-13 19:04:27 -0700891void OpenGLRenderer::clearLayerRegions() {
892 const size_t count = mLayers.size();
893 if (count == 0) return;
894
895 if (!mSnapshot->isIgnored()) {
896 // Doing several glScissor/glClear here can negatively impact
897 // GPUs with a tiler architecture, instead we draw quads with
898 // the Clear blending mode
899
900 // The list contains bounds that have already been clipped
901 // against their initial clip rect, and the current clip
902 // is likely different so we need to disable clipping here
903 glDisable(GL_SCISSOR_TEST);
904
905 Vertex mesh[count * 6];
906 Vertex* vertex = mesh;
907
908 for (uint32_t i = 0; i < count; i++) {
909 Rect* bounds = mLayers.itemAt(i);
910
911 Vertex::set(vertex++, bounds->left, bounds->bottom);
912 Vertex::set(vertex++, bounds->left, bounds->top);
913 Vertex::set(vertex++, bounds->right, bounds->top);
914 Vertex::set(vertex++, bounds->left, bounds->bottom);
915 Vertex::set(vertex++, bounds->right, bounds->top);
916 Vertex::set(vertex++, bounds->right, bounds->bottom);
917
918 delete bounds;
919 }
920
921 setupDraw(false);
922 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
923 setupDrawBlending(true, SkXfermode::kClear_Mode);
924 setupDrawProgram();
925 setupDrawPureColorUniforms();
926 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800927 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700928
Romain Guy54be1cd2011-06-13 19:04:27 -0700929 glDrawArrays(GL_TRIANGLES, 0, count * 6);
930
931 glEnable(GL_SCISSOR_TEST);
932 } else {
933 for (uint32_t i = 0; i < count; i++) {
934 delete mLayers.itemAt(i);
935 }
936 }
937
938 mLayers.clear();
939}
940
Romain Guybd6b79b2010-06-26 00:13:53 -0700941///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700942// Transforms
943///////////////////////////////////////////////////////////////////////////////
944
945void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700946 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700947}
948
949void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700950 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700951}
952
953void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700954 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700955}
956
Romain Guy807daf72011-01-18 11:19:19 -0800957void OpenGLRenderer::skew(float sx, float sy) {
958 mSnapshot->transform->skew(sx, sy);
959}
960
Romain Guyf6a11b82010-06-23 17:47:49 -0700961void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700962 if (matrix) {
963 mSnapshot->transform->load(*matrix);
964 } else {
965 mSnapshot->transform->loadIdentity();
966 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700967}
968
969void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700970 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700971}
972
973void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700974 SkMatrix transform;
975 mSnapshot->transform->copyTo(transform);
976 transform.preConcat(*matrix);
977 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700978}
979
980///////////////////////////////////////////////////////////////////////////////
981// Clipping
982///////////////////////////////////////////////////////////////////////////////
983
Romain Guybb9524b2010-06-22 18:56:38 -0700984void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700985 Rect clip(*mSnapshot->clipRect);
986 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800987
988 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
989 clip.getWidth(), clip.getHeight());
990
Romain Guy746b7402010-10-26 16:27:31 -0700991 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700992}
993
994const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700995 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700996}
997
Romain Guyc7d53492010-06-25 13:41:57 -0700998bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800999 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -07001000 return true;
1001 }
1002
Romain Guy1d83e192010-08-17 11:37:00 -07001003 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001004 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001005 r.snapToPixelBoundaries();
1006
1007 Rect clipRect(*mSnapshot->clipRect);
1008 clipRect.snapToPixelBoundaries();
1009
1010 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001011}
1012
Romain Guy079ba2c2010-07-16 14:12:24 -07001013bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1014 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001015 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001016 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001017 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001018 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001019}
1020
Romain Guyf6a11b82010-06-23 17:47:49 -07001021///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001022// Drawing commands
1023///////////////////////////////////////////////////////////////////////////////
1024
Romain Guy54be1cd2011-06-13 19:04:27 -07001025void OpenGLRenderer::setupDraw(bool clear) {
1026 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001027 if (mDirtyClip) {
1028 setScissorFromClip();
1029 }
1030 mDescription.reset();
1031 mSetShaderColor = false;
1032 mColorSet = false;
1033 mColorA = mColorR = mColorG = mColorB = 0.0f;
1034 mTextureUnit = 0;
1035 mTrackDirtyRegions = true;
1036}
1037
1038void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1039 mDescription.hasTexture = true;
1040 mDescription.hasAlpha8Texture = isAlpha8;
1041}
1042
Romain Guyaa6c24c2011-04-28 18:40:04 -07001043void OpenGLRenderer::setupDrawWithExternalTexture() {
1044 mDescription.hasExternalTexture = true;
1045}
1046
Romain Guy15bc6432011-12-13 13:11:32 -08001047void OpenGLRenderer::setupDrawNoTexture() {
1048 mCaches.disbaleTexCoordsVertexArray();
1049}
1050
Chet Haase5b0200b2011-04-13 17:58:08 -07001051void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001052 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001053}
1054
Romain Guyed6fcb02011-03-21 13:11:28 -07001055void OpenGLRenderer::setupDrawPoint(float pointSize) {
1056 mDescription.isPoint = true;
1057 mDescription.pointSize = pointSize;
1058}
1059
Romain Guy70ca14e2010-12-13 18:24:33 -08001060void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001061 setupDrawColor(color, (color >> 24) & 0xFF);
1062}
1063
1064void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1065 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001066 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1067 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001068 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001069 mColorR = a * ((color >> 16) & 0xFF);
1070 mColorG = a * ((color >> 8) & 0xFF);
1071 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001072 mColorSet = true;
1073 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1074}
1075
Romain Guy86568192010-12-14 15:55:39 -08001076void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1077 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001078 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1079 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001080 const float a = mColorA / 255.0f;
1081 mColorR = a * ((color >> 16) & 0xFF);
1082 mColorG = a * ((color >> 8) & 0xFF);
1083 mColorB = a * ((color ) & 0xFF);
1084 mColorSet = true;
1085 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1086}
1087
Romain Guy70ca14e2010-12-13 18:24:33 -08001088void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1089 mColorA = a;
1090 mColorR = r;
1091 mColorG = g;
1092 mColorB = b;
1093 mColorSet = true;
1094 mSetShaderColor = mDescription.setColor(r, g, b, a);
1095}
1096
Romain Guy86568192010-12-14 15:55:39 -08001097void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1098 mColorA = a;
1099 mColorR = r;
1100 mColorG = g;
1101 mColorB = b;
1102 mColorSet = true;
1103 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1104}
1105
Romain Guy70ca14e2010-12-13 18:24:33 -08001106void OpenGLRenderer::setupDrawShader() {
1107 if (mShader) {
1108 mShader->describe(mDescription, mCaches.extensions);
1109 }
1110}
1111
1112void OpenGLRenderer::setupDrawColorFilter() {
1113 if (mColorFilter) {
1114 mColorFilter->describe(mDescription, mCaches.extensions);
1115 }
1116}
1117
Romain Guyf09ef512011-05-27 11:43:46 -07001118void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1119 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1120 mColorA = 1.0f;
1121 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001122 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001123 }
1124}
1125
Romain Guy70ca14e2010-12-13 18:24:33 -08001126void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001127 // When the blending mode is kClear_Mode, we need to use a modulate color
1128 // argb=1,0,0,0
1129 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001130 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1131 mDescription, swapSrcDst);
1132}
1133
1134void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001135 // When the blending mode is kClear_Mode, we need to use a modulate color
1136 // argb=1,0,0,0
1137 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001138 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1139 mDescription, swapSrcDst);
1140}
1141
1142void OpenGLRenderer::setupDrawProgram() {
1143 useProgram(mCaches.programCache.get(mDescription));
1144}
1145
1146void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1147 mTrackDirtyRegions = false;
1148}
1149
1150void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1151 bool ignoreTransform) {
1152 mModelView.loadTranslate(left, top, 0.0f);
1153 if (!ignoreTransform) {
1154 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1155 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1156 } else {
1157 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1158 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1159 }
1160}
1161
Chet Haase8a5cc922011-04-26 07:28:09 -07001162void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1163 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001164}
1165
Romain Guy70ca14e2010-12-13 18:24:33 -08001166void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1167 bool ignoreTransform, bool ignoreModelView) {
1168 if (!ignoreModelView) {
1169 mModelView.loadTranslate(left, top, 0.0f);
1170 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001171 } else {
1172 mModelView.loadIdentity();
1173 }
Romain Guy86568192010-12-14 15:55:39 -08001174 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1175 if (!ignoreTransform) {
1176 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1177 if (mTrackDirtyRegions && dirty) {
1178 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1179 }
1180 } else {
1181 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1182 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1183 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001184}
1185
Romain Guyed6fcb02011-03-21 13:11:28 -07001186void OpenGLRenderer::setupDrawPointUniforms() {
1187 int slot = mCaches.currentProgram->getUniform("pointSize");
1188 glUniform1f(slot, mDescription.pointSize);
1189}
1190
Romain Guy70ca14e2010-12-13 18:24:33 -08001191void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001192 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001193 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1194 }
1195}
1196
Romain Guy86568192010-12-14 15:55:39 -08001197void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001198 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001199 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001200 }
1201}
1202
Romain Guy70ca14e2010-12-13 18:24:33 -08001203void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1204 if (mShader) {
1205 if (ignoreTransform) {
1206 mModelView.loadInverse(*mSnapshot->transform);
1207 }
1208 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1209 }
1210}
1211
Romain Guy8d0d4782010-12-14 20:13:35 -08001212void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1213 if (mShader) {
1214 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1215 }
1216}
1217
Romain Guy70ca14e2010-12-13 18:24:33 -08001218void OpenGLRenderer::setupDrawColorFilterUniforms() {
1219 if (mColorFilter) {
1220 mColorFilter->setupProgram(mCaches.currentProgram);
1221 }
1222}
1223
1224void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001225 bool force = mCaches.bindMeshBuffer();
1226 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001227 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001228}
1229
1230void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1231 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001232 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001233 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001234}
1235
Romain Guyaa6c24c2011-04-28 18:40:04 -07001236void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1237 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001238 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001239 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001240}
1241
Romain Guy8f0095c2011-05-02 17:24:22 -07001242void OpenGLRenderer::setupDrawTextureTransform() {
1243 mDescription.hasTextureTransform = true;
1244}
1245
1246void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001247 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1248 GL_FALSE, &transform.data[0]);
1249}
1250
Romain Guy70ca14e2010-12-13 18:24:33 -08001251void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001252 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001253 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001254 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001255 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001256 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001257 }
Romain Guyd71dd362011-12-12 19:03:35 -08001258
Romain Guyf3a910b42011-12-12 20:35:21 -08001259 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001260 if (mCaches.currentProgram->texCoords >= 0) {
1261 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1262 }
1263
1264 mCaches.unbindIndicesBuffer();
1265}
1266
1267void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1268 bool force = mCaches.unbindMeshBuffer();
1269 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1270 if (mCaches.currentProgram->texCoords >= 0) {
1271 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001272 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001273}
1274
Chet Haase5b0200b2011-04-13 17:58:08 -07001275void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001276 bool force = mCaches.unbindMeshBuffer();
1277 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1278 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001279 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001280}
1281
1282/**
1283 * 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 -07001284 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1285 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1286 * attributes (one per vertex) are values from zero to one that tells the fragment
1287 * shader where the fragment is in relation to the line width/length overall; these values are
1288 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1289 * region of the line.
1290 * Note that we only pass down the width values in this setup function. The length coordinates
1291 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001292 */
Chet Haase99585ad2011-05-02 15:00:16 -07001293void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001294 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001295 bool force = mCaches.unbindMeshBuffer();
1296 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1297 vertices, gAAVertexStride);
1298 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001299 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001300
Chet Haase99585ad2011-05-02 15:00:16 -07001301 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1302 glEnableVertexAttribArray(widthSlot);
1303 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001304
Chet Haase99585ad2011-05-02 15:00:16 -07001305 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1306 glEnableVertexAttribArray(lengthSlot);
1307 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001308
Chet Haase5b0200b2011-04-13 17:58:08 -07001309 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001310 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001311
Chet Haase99585ad2011-05-02 15:00:16 -07001312 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001313 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001314 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001315}
1316
Romain Guy70ca14e2010-12-13 18:24:33 -08001317void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001318}
1319
1320///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001321// Drawing
1322///////////////////////////////////////////////////////////////////////////////
1323
Romain Guy65549432012-03-26 16:45:05 -07001324status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -08001325 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haasea1cff502012-02-21 13:43:44 -08001326 float top = 0;
1327 float left = 0;
1328 float right = width;
1329 float bottom = height;
1330 if (USE_DISPLAY_LIST_PROPERTIES) {
1331 Rect transformedRect;
1332 displayList->transformRect(left, top, right, bottom, transformedRect);
1333 left = transformedRect.left;
1334 top = transformedRect.top;
1335 right = transformedRect.right;
1336 bottom = transformedRect.bottom;
1337 }
1338 if (quickReject(left, top, right, bottom)) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001339 return false;
1340 }
1341
Romain Guy0fe478e2010-11-08 12:08:41 -08001342 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1343 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001344 if (displayList && displayList->isRenderable()) {
Chet Haasea1cff502012-02-21 13:43:44 -08001345 return displayList->replay(*this, width, height, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001346 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001347
Romain Guy65549432012-03-26 16:45:05 -07001348 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001349}
1350
Chet Haaseed30fd82011-04-22 16:18:45 -07001351void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1352 if (displayList) {
1353 displayList->output(*this, level);
1354 }
1355}
1356
Romain Guya168d732011-03-18 16:50:13 -07001357void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1358 int alpha;
1359 SkXfermode::Mode mode;
1360 getAlphaAndMode(paint, &alpha, &mode);
1361
Romain Guya168d732011-03-18 16:50:13 -07001362 float x = left;
1363 float y = top;
1364
Romain Guye3c26852011-07-25 16:36:01 -07001365 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001366 bool ignoreTransform = false;
1367 if (mSnapshot->transform->isPureTranslate()) {
1368 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1369 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1370 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001371 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001372 } else {
1373 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001374 }
1375
1376 setupDraw();
1377 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001378 if (paint) {
1379 setupDrawAlpha8Color(paint->getColor(), alpha);
1380 }
Romain Guya168d732011-03-18 16:50:13 -07001381 setupDrawColorFilter();
1382 setupDrawShader();
1383 setupDrawBlending(true, mode);
1384 setupDrawProgram();
1385 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001386
Romain Guya168d732011-03-18 16:50:13 -07001387 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001388 texture->setWrap(GL_CLAMP_TO_EDGE);
1389 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001390
Romain Guya168d732011-03-18 16:50:13 -07001391 setupDrawPureColorUniforms();
1392 setupDrawColorFilterUniforms();
1393 setupDrawShaderUniforms();
1394 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1395
1396 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1397
1398 finishDrawTexture();
1399}
1400
Chet Haase5c13d892010-10-08 08:37:55 -07001401void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001402 const float right = left + bitmap->width();
1403 const float bottom = top + bitmap->height();
1404
1405 if (quickReject(left, top, right, bottom)) {
1406 return;
1407 }
1408
Romain Guya1d3c912011-12-13 14:55:06 -08001409 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001410 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001411 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001412 const AutoTexture autoCleanup(texture);
1413
Romain Guy211370f2012-02-01 16:10:55 -08001414 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001415 drawAlphaBitmap(texture, left, top, paint);
1416 } else {
1417 drawTextureRect(left, top, right, bottom, texture, paint);
1418 }
Romain Guyce0537b2010-06-29 21:05:21 -07001419}
1420
Chet Haase5c13d892010-10-08 08:37:55 -07001421void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001422 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1423 const mat4 transform(*matrix);
1424 transform.mapRect(r);
1425
Romain Guy6926c722010-07-12 20:20:03 -07001426 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1427 return;
1428 }
1429
Romain Guya1d3c912011-12-13 14:55:06 -08001430 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001431 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001432 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001433 const AutoTexture autoCleanup(texture);
1434
Romain Guy5b3b3522010-10-27 18:57:51 -07001435 // This could be done in a cheaper way, all we need is pass the matrix
1436 // to the vertex shader. The save/restore is a bit overkill.
1437 save(SkCanvas::kMatrix_SaveFlag);
1438 concatMatrix(matrix);
1439 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1440 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001441}
1442
Romain Guy5a7b4662011-01-20 19:09:30 -08001443void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1444 float* vertices, int* colors, SkPaint* paint) {
1445 // TODO: Do a quickReject
1446 if (!vertices || mSnapshot->isIgnored()) {
1447 return;
1448 }
1449
Romain Guya1d3c912011-12-13 14:55:06 -08001450 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001451 Texture* texture = mCaches.textureCache.get(bitmap);
1452 if (!texture) return;
1453 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001454
Romain Guyd21b6e12011-11-30 20:21:23 -08001455 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1456 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001457
1458 int alpha;
1459 SkXfermode::Mode mode;
1460 getAlphaAndMode(paint, &alpha, &mode);
1461
Romain Guy5a7b4662011-01-20 19:09:30 -08001462 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001463
Romain Guyb18d2d02011-02-10 15:52:54 -08001464 float left = FLT_MAX;
1465 float top = FLT_MAX;
1466 float right = FLT_MIN;
1467 float bottom = FLT_MIN;
1468
1469#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001470 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001471#else
Romain Guy211370f2012-02-01 16:10:55 -08001472 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001473#endif
1474
Romain Guya566b7c2011-01-23 16:36:11 -08001475 // TODO: Support the colors array
1476 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001477 TextureVertex* vertex = mesh;
1478 for (int32_t y = 0; y < meshHeight; y++) {
1479 for (int32_t x = 0; x < meshWidth; x++) {
1480 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1481
1482 float u1 = float(x) / meshWidth;
1483 float u2 = float(x + 1) / meshWidth;
1484 float v1 = float(y) / meshHeight;
1485 float v2 = float(y + 1) / meshHeight;
1486
1487 int ax = i + (meshWidth + 1) * 2;
1488 int ay = ax + 1;
1489 int bx = i;
1490 int by = bx + 1;
1491 int cx = i + 2;
1492 int cy = cx + 1;
1493 int dx = i + (meshWidth + 1) * 2 + 2;
1494 int dy = dx + 1;
1495
1496 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1497 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1498 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1499
1500 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1501 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1502 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001503
1504#if RENDER_LAYERS_AS_REGIONS
1505 if (hasActiveLayer) {
1506 // TODO: This could be optimized to avoid unnecessary ops
1507 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1508 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1509 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1510 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1511 }
1512#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001513 }
1514 }
1515
Romain Guyb18d2d02011-02-10 15:52:54 -08001516#if RENDER_LAYERS_AS_REGIONS
1517 if (hasActiveLayer) {
1518 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1519 }
1520#endif
1521
Romain Guy5a7b4662011-01-20 19:09:30 -08001522 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1523 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001524 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001525}
1526
Romain Guy8ba548f2010-06-30 19:21:21 -07001527void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1528 float srcLeft, float srcTop, float srcRight, float srcBottom,
1529 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001530 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001531 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1532 return;
1533 }
1534
Romain Guya1d3c912011-12-13 14:55:06 -08001535 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001536 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001537 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001538 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001539
Romain Guy8ba548f2010-06-30 19:21:21 -07001540 const float width = texture->width;
1541 const float height = texture->height;
1542
Romain Guy68169722011-08-22 17:33:33 -07001543 const float u1 = fmax(0.0f, srcLeft / width);
1544 const float v1 = fmax(0.0f, srcTop / height);
1545 const float u2 = fmin(1.0f, srcRight / width);
1546 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001547
Romain Guy03750a02010-10-18 14:06:08 -07001548 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001549 resetDrawTextureTexCoords(u1, v1, u2, v2);
1550
Romain Guy03750a02010-10-18 14:06:08 -07001551 int alpha;
1552 SkXfermode::Mode mode;
1553 getAlphaAndMode(paint, &alpha, &mode);
1554
Romain Guyd21b6e12011-11-30 20:21:23 -08001555 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1556
Romain Guy211370f2012-02-01 16:10:55 -08001557 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001558 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1559 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1560
Romain Guyb5014982011-07-28 15:39:12 -07001561 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001562 // Enable linear filtering if the source rectangle is scaled
1563 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001564 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001565 }
Romain Guyb5014982011-07-28 15:39:12 -07001566
Romain Guyd21b6e12011-11-30 20:21:23 -08001567 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001568 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1569 texture->id, alpha / 255.0f, mode, texture->blend,
1570 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1571 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1572 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001573 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001574 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1575 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1576 GL_TRIANGLE_STRIP, gMeshCount);
1577 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001578
1579 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1580}
1581
Romain Guy4aa90572010-09-26 18:40:37 -07001582void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001583 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001584 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001585 if (quickReject(left, top, right, bottom)) {
1586 return;
1587 }
1588
Romain Guya1d3c912011-12-13 14:55:06 -08001589 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001590 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001591 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001592 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001593 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1594 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001595
1596 int alpha;
1597 SkXfermode::Mode mode;
1598 getAlphaAndMode(paint, &alpha, &mode);
1599
Romain Guy2728f962010-10-08 18:36:15 -07001600 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001601 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001602
Romain Guy211370f2012-02-01 16:10:55 -08001603 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001604 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001605#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001606 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001607 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001608 const float offsetX = left + mSnapshot->transform->getTranslateX();
1609 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001610 const size_t count = mesh->quads.size();
1611 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001612 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001613 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001614 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1615 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1616 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001617 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001618 dirtyLayer(left + bounds.left, top + bounds.top,
1619 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001620 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001621 }
1622 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001623#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001624
Romain Guy211370f2012-02-01 16:10:55 -08001625 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001626 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1627 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1628
1629 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1630 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1631 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1632 true, !mesh->hasEmptyQuads);
1633 } else {
1634 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1635 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1636 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1637 true, !mesh->hasEmptyQuads);
1638 }
Romain Guy054dc182010-10-15 17:55:25 -07001639 }
Romain Guyf7f93552010-07-08 19:17:03 -07001640}
1641
Chet Haase99ecdc42011-05-06 12:06:34 -07001642/**
Chet Haase858aa932011-05-12 09:06:00 -07001643 * This function uses a similar approach to that of AA lines in the drawLines() function.
1644 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1645 * shader to compute the translucency of the color, determined by whether a given pixel is
1646 * within that boundary region and how far into the region it is.
1647 */
1648void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001649 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001650 float inverseScaleX = 1.0f;
1651 float inverseScaleY = 1.0f;
1652 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001653 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001654 Matrix4 *mat = mSnapshot->transform;
1655 float m00 = mat->data[Matrix4::kScaleX];
1656 float m01 = mat->data[Matrix4::kSkewY];
1657 float m02 = mat->data[2];
1658 float m10 = mat->data[Matrix4::kSkewX];
1659 float m11 = mat->data[Matrix4::kScaleX];
1660 float m12 = mat->data[6];
1661 float scaleX = sqrt(m00 * m00 + m01 * m01);
1662 float scaleY = sqrt(m10 * m10 + m11 * m11);
1663 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1664 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1665 }
1666
1667 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001668 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001669 setupDrawAALine();
1670 setupDrawColor(color);
1671 setupDrawColorFilter();
1672 setupDrawShader();
1673 setupDrawBlending(true, mode);
1674 setupDrawProgram();
1675 setupDrawModelViewIdentity(true);
1676 setupDrawColorUniforms();
1677 setupDrawColorFilterUniforms();
1678 setupDrawShaderIdentityUniforms();
1679
1680 AAVertex rects[4];
1681 AAVertex* aaVertices = &rects[0];
1682 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1683 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1684
1685 float boundarySizeX = .5 * inverseScaleX;
1686 float boundarySizeY = .5 * inverseScaleY;
1687
1688 // Adjust the rect by the AA boundary padding
1689 left -= boundarySizeX;
1690 right += boundarySizeX;
1691 top -= boundarySizeY;
1692 bottom += boundarySizeY;
1693
1694 float width = right - left;
1695 float height = bottom - top;
1696
1697 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1698 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1699 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1700 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1701 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1702 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1703 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1704
1705 if (!quickReject(left, top, right, bottom)) {
1706 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1707 AAVertex::set(aaVertices++, left, top, 1, 0);
1708 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1709 AAVertex::set(aaVertices++, right, top, 0, 0);
1710 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1711 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1712 }
1713}
1714
1715/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001716 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1717 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1718 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1719 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1720 * of the line. Hairlines are more involved because we need to account for transform scaling
1721 * to end up with a one-pixel-wide line in screen space..
1722 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1723 * in combination with values that we calculate and pass down in this method. The basic approach
1724 * is that the quad we create contains both the core line area plus a bounding area in which
1725 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1726 * proportion of the width and the length of a given segment is represented by the boundary
1727 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1728 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1729 * on the inside). This ends up giving the result we want, with pixels that are completely
1730 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1731 * how far into the boundary region they are, which is determined by shader interpolation.
1732 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001733void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1734 if (mSnapshot->isIgnored()) return;
1735
1736 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001737 // We use half the stroke width here because we're going to position the quad
1738 // corner vertices half of the width away from the line endpoints
1739 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001740 // A stroke width of 0 has a special meaning in Skia:
1741 // it draws a line 1 px wide regardless of current transform
1742 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001743 float inverseScaleX = 1.0f;
1744 float inverseScaleY = 1.0f;
1745 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001746 int alpha;
1747 SkXfermode::Mode mode;
1748 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001749 int verticesCount = count;
1750 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001751 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001752 verticesCount += (count - 4);
1753 }
1754
1755 if (isHairLine || isAA) {
1756 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1757 // the line on the screen should always be one pixel wide regardless of scale. For
1758 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001759 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001760 Matrix4 *mat = mSnapshot->transform;
1761 float m00 = mat->data[Matrix4::kScaleX];
1762 float m01 = mat->data[Matrix4::kSkewY];
1763 float m02 = mat->data[2];
1764 float m10 = mat->data[Matrix4::kSkewX];
1765 float m11 = mat->data[Matrix4::kScaleX];
1766 float m12 = mat->data[6];
Romain Guy211370f2012-02-01 16:10:55 -08001767 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1768 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Chet Haase99ecdc42011-05-06 12:06:34 -07001769 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1770 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1771 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1772 scaled = true;
1773 }
1774 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001775 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001776
1777 getAlphaAndMode(paint, &alpha, &mode);
1778 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001779 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001780 if (isAA) {
1781 setupDrawAALine();
1782 }
1783 setupDrawColor(paint->getColor(), alpha);
1784 setupDrawColorFilter();
1785 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001786 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001787 setupDrawProgram();
1788 setupDrawModelViewIdentity(true);
1789 setupDrawColorUniforms();
1790 setupDrawColorFilterUniforms();
1791 setupDrawShaderIdentityUniforms();
1792
1793 if (isHairLine) {
1794 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001795 halfStrokeWidth = isAA? 1 : .5;
1796 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001797 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001798 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001799 }
1800 Vertex lines[verticesCount];
1801 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001802 AAVertex wLines[verticesCount];
1803 AAVertex* aaVertices = &wLines[0];
Romain Guy211370f2012-02-01 16:10:55 -08001804 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001805 setupDrawVertices(vertices);
1806 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001807 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1808 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001809 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001810 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1811 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001812 // We will need to calculate the actual width proportion on each segment for
1813 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1814 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1815 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001816 }
1817
Chet Haase99ecdc42011-05-06 12:06:34 -07001818 AAVertex* prevAAVertex = NULL;
1819 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001820
Chet Haase99585ad2011-05-02 15:00:16 -07001821 int boundaryLengthSlot = -1;
1822 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001823 int boundaryWidthSlot = -1;
1824 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001825 for (int i = 0; i < count; i += 4) {
1826 // a = start point, b = end point
1827 vec2 a(points[i], points[i + 1]);
1828 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001829 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001830 float boundaryLengthProportion = 0;
1831 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001832
Chet Haase5b0200b2011-04-13 17:58:08 -07001833 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001834 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001835 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001836 if (isAA) {
1837 float wideningFactor;
1838 if (fabs(n.x) >= fabs(n.y)) {
1839 wideningFactor = fabs(1.0f / n.x);
1840 } else {
1841 wideningFactor = fabs(1.0f / n.y);
1842 }
1843 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001844 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001845 if (scaled) {
1846 n.x *= inverseScaleX;
1847 n.y *= inverseScaleY;
1848 }
1849 } else if (scaled) {
1850 // Extend n by .5 pixel on each side, post-transform
1851 vec2 extendedN = n.copyNormalized();
1852 extendedN /= 2;
1853 extendedN.x *= inverseScaleX;
1854 extendedN.y *= inverseScaleY;
1855 float extendedNLength = extendedN.length();
1856 // We need to set this value on the shader prior to drawing
1857 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1858 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001859 }
1860 float x = n.x;
1861 n.x = -n.y;
1862 n.y = x;
1863
Chet Haase99585ad2011-05-02 15:00:16 -07001864 // aa lines expand the endpoint vertices to encompass the AA boundary
1865 if (isAA) {
1866 vec2 abVector = (b - a);
1867 length = abVector.length();
1868 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001869 if (scaled) {
1870 abVector.x *= inverseScaleX;
1871 abVector.y *= inverseScaleY;
1872 float abLength = abVector.length();
1873 boundaryLengthProportion = abLength / (length + abLength);
1874 } else {
1875 boundaryLengthProportion = .5 / (length + 1);
1876 }
1877 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001878 a -= abVector;
1879 b += abVector;
1880 }
1881
Chet Haase5b0200b2011-04-13 17:58:08 -07001882 // Four corners of the rectangle defining a thick line
1883 vec2 p1 = a - n;
1884 vec2 p2 = a + n;
1885 vec2 p3 = b + n;
1886 vec2 p4 = b - n;
1887
Chet Haase99585ad2011-05-02 15:00:16 -07001888
Chet Haase5b0200b2011-04-13 17:58:08 -07001889 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1890 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1891 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1892 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1893
1894 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 if (!isAA) {
1896 if (prevVertex != NULL) {
1897 // Issue two repeat vertices to create degenerate triangles to bridge
1898 // between the previous line and the new one. This is necessary because
1899 // we are creating a single triangle_strip which will contain
1900 // potentially discontinuous line segments.
1901 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1902 Vertex::set(vertices++, p1.x, p1.y);
1903 generatedVerticesCount += 2;
1904 }
1905 Vertex::set(vertices++, p1.x, p1.y);
1906 Vertex::set(vertices++, p2.x, p2.y);
1907 Vertex::set(vertices++, p4.x, p4.y);
1908 Vertex::set(vertices++, p3.x, p3.y);
1909 prevVertex = vertices - 1;
1910 generatedVerticesCount += 4;
1911 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001912 if (!isHairLine && scaled) {
1913 // Must set width proportions per-segment for scaled non-hairlines to use the
1914 // correct AA boundary dimensions
1915 if (boundaryWidthSlot < 0) {
1916 boundaryWidthSlot =
1917 mCaches.currentProgram->getUniform("boundaryWidth");
1918 inverseBoundaryWidthSlot =
1919 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1920 }
1921 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1922 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1923 }
Chet Haase99585ad2011-05-02 15:00:16 -07001924 if (boundaryLengthSlot < 0) {
1925 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1926 inverseBoundaryLengthSlot =
1927 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1928 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001929 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1930 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001931
Chet Haase5b0200b2011-04-13 17:58:08 -07001932 if (prevAAVertex != NULL) {
1933 // Issue two repeat vertices to create degenerate triangles to bridge
1934 // between the previous line and the new one. This is necessary because
1935 // we are creating a single triangle_strip which will contain
1936 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001937 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1938 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1939 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 generatedVerticesCount += 2;
1941 }
Chet Haase99585ad2011-05-02 15:00:16 -07001942 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1943 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1944 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1945 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001946 prevAAVertex = aaVertices - 1;
1947 generatedVerticesCount += 4;
1948 }
Chet Haase99585ad2011-05-02 15:00:16 -07001949 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1950 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1951 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001952 }
1953 }
1954 if (generatedVerticesCount > 0) {
1955 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1956 }
1957}
1958
Romain Guyed6fcb02011-03-21 13:11:28 -07001959void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1960 if (mSnapshot->isIgnored()) return;
1961
1962 // TODO: The paint's cap style defines whether the points are square or circular
1963 // TODO: Handle AA for round points
1964
Chet Haase5b0200b2011-04-13 17:58:08 -07001965 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001966 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001967 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001968 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001969 if (isHairLine) {
1970 // Now that we know it's hairline, we can set the effective width, to be used later
1971 strokeWidth = 1.0f;
1972 }
1973 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001974 int alpha;
1975 SkXfermode::Mode mode;
1976 getAlphaAndMode(paint, &alpha, &mode);
1977
1978 int verticesCount = count >> 1;
1979 int generatedVerticesCount = 0;
1980
1981 TextureVertex pointsData[verticesCount];
1982 TextureVertex* vertex = &pointsData[0];
1983
1984 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001985 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001986 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001987 setupDrawColor(paint->getColor(), alpha);
1988 setupDrawColorFilter();
1989 setupDrawShader();
1990 setupDrawBlending(mode);
1991 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001992 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001993 setupDrawColorUniforms();
1994 setupDrawColorFilterUniforms();
1995 setupDrawPointUniforms();
1996 setupDrawShaderIdentityUniforms();
1997 setupDrawMesh(vertex);
1998
1999 for (int i = 0; i < count; i += 2) {
2000 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2001 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07002002 float left = points[i] - halfWidth;
2003 float right = points[i] + halfWidth;
2004 float top = points[i + 1] - halfWidth;
2005 float bottom = points [i + 1] + halfWidth;
2006 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07002007 }
2008
2009 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
2010}
2011
Romain Guy85bf02f2010-06-22 13:11:24 -07002012void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002013 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002014 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002015
Romain Guyae88e5e2010-10-22 17:49:18 -07002016 Rect& clip(*mSnapshot->clipRect);
2017 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002018
Romain Guy3d58c032010-07-14 16:34:53 -07002019 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002020}
Romain Guy9d5316e2010-06-24 19:30:36 -07002021
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002022void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002023 if (!texture) return;
2024 const AutoTexture autoCleanup(texture);
2025
2026 const float x = left + texture->left - texture->offset;
2027 const float y = top + texture->top - texture->offset;
2028
2029 drawPathTexture(texture, x, y, paint);
2030}
2031
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002032void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2033 float rx, float ry, SkPaint* paint) {
2034 if (mSnapshot->isIgnored()) return;
2035
Romain Guya1d3c912011-12-13 14:55:06 -08002036 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002037 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2038 right - left, bottom - top, rx, ry, paint);
2039 drawShape(left, top, texture, paint);
2040}
2041
Romain Guy01d58e42011-01-19 21:54:02 -08002042void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2043 if (mSnapshot->isIgnored()) return;
2044
Romain Guya1d3c912011-12-13 14:55:06 -08002045 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002046 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002047 drawShape(x - radius, y - radius, texture, paint);
2048}
Romain Guy01d58e42011-01-19 21:54:02 -08002049
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002050void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2051 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002052
Romain Guya1d3c912011-12-13 14:55:06 -08002053 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002054 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2055 drawShape(left, top, texture, paint);
2056}
2057
Romain Guy8b2f5262011-01-23 16:15:02 -08002058void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2059 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2060 if (mSnapshot->isIgnored()) return;
2061
2062 if (fabs(sweepAngle) >= 360.0f) {
2063 drawOval(left, top, right, bottom, paint);
2064 return;
2065 }
2066
Romain Guya1d3c912011-12-13 14:55:06 -08002067 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002068 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2069 startAngle, sweepAngle, useCenter, paint);
2070 drawShape(left, top, texture, paint);
2071}
2072
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002073void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2074 SkPaint* paint) {
2075 if (mSnapshot->isIgnored()) return;
2076
Romain Guya1d3c912011-12-13 14:55:06 -08002077 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002078 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2079 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002080}
2081
Chet Haase5c13d892010-10-08 08:37:55 -07002082void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002083 if (p->getStyle() != SkPaint::kFill_Style) {
2084 drawRectAsShape(left, top, right, bottom, p);
2085 return;
2086 }
2087
Romain Guy6926c722010-07-12 20:20:03 -07002088 if (quickReject(left, top, right, bottom)) {
2089 return;
2090 }
2091
Romain Guy026c5e162010-06-28 17:12:22 -07002092 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002093 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002094 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2095 if (!isMode) {
2096 // Assume SRC_OVER
2097 mode = SkXfermode::kSrcOver_Mode;
2098 }
2099 } else {
2100 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002101 }
2102
Romain Guy026c5e162010-06-28 17:12:22 -07002103 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002104 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002105 drawAARect(left, top, right, bottom, color, mode);
2106 } else {
2107 drawColorRect(left, top, right, bottom, color, mode);
2108 }
Romain Guyc7d53492010-06-25 13:41:57 -07002109}
Romain Guy9d5316e2010-06-24 19:30:36 -07002110
Romain Guyeb9a5362012-01-17 17:39:26 -08002111void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2112 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002113 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2114 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002115 return;
2116 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002117
Romain Guy671d6cf2012-01-18 12:39:17 -08002118 // NOTE: Skia does not support perspective transform on drawPosText yet
2119 if (!mSnapshot->transform->isSimple()) {
2120 return;
2121 }
2122
2123 float x = 0.0f;
2124 float y = 0.0f;
2125 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2126 if (pureTranslate) {
2127 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2128 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2129 }
2130
2131 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2132 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2133 paint->getTextSize());
2134
2135 int alpha;
2136 SkXfermode::Mode mode;
2137 getAlphaAndMode(paint, &alpha, &mode);
2138
2139 // Pick the appropriate texture filtering
2140 bool linearFilter = mSnapshot->transform->changesBounds();
2141 if (pureTranslate && !linearFilter) {
2142 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2143 }
2144
2145 mCaches.activeTexture(0);
2146 setupDraw();
2147 setupDrawDirtyRegionsDisabled();
2148 setupDrawWithTexture(true);
2149 setupDrawAlpha8Color(paint->getColor(), alpha);
2150 setupDrawColorFilter();
2151 setupDrawShader();
2152 setupDrawBlending(true, mode);
2153 setupDrawProgram();
2154 setupDrawModelView(x, y, x, y, pureTranslate, true);
2155 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2156 setupDrawPureColorUniforms();
2157 setupDrawColorFilterUniforms();
2158 setupDrawShaderUniforms(pureTranslate);
2159
2160 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2161 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2162
2163#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002164 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002165#else
Romain Guy211370f2012-02-01 16:10:55 -08002166 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002167#endif
2168
2169 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2170 positions, hasActiveLayer ? &bounds : NULL)) {
2171#if RENDER_LAYERS_AS_REGIONS
2172 if (hasActiveLayer) {
2173 if (!pureTranslate) {
2174 mSnapshot->transform->mapRect(bounds);
2175 }
2176 dirtyLayerUnchecked(bounds, getRegion());
2177 }
2178#endif
2179 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002180}
2181
Romain Guye8e62a42010-07-23 18:55:21 -07002182void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002183 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002184 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2185 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002186 return;
2187 }
2188
Chet Haasea1cff502012-02-21 13:43:44 -08002189 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002190 switch (paint->getTextAlign()) {
2191 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002192 x -= length / 2.0f;
2193 break;
2194 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002195 x -= length;
2196 break;
2197 default:
2198 break;
2199 }
2200
Romain Guycac5fd32011-12-01 20:08:50 -08002201 SkPaint::FontMetrics metrics;
2202 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002203 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002204 return;
2205 }
2206
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002207 const float oldX = x;
2208 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002209 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002210 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002211 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2212 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2213 }
2214
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002215#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002216 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002217#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002218
2219 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002220 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002221 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002222
Romain Guy86568192010-12-14 15:55:39 -08002223 int alpha;
2224 SkXfermode::Mode mode;
2225 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002226
Romain Guy211370f2012-02-01 16:10:55 -08002227 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002228 mCaches.activeTexture(0);
2229
Romain Guyb45c0c92010-08-26 20:35:23 -07002230 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002231 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2232 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002233 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002234
Romain Guy740bf2b2011-04-26 15:33:10 -07002235 const float sx = oldX - shadow->left + mShadowDx;
2236 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002237
Romain Guy86568192010-12-14 15:55:39 -08002238 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002239 int shadowColor = mShadowColor;
2240 if (mShader) {
2241 shadowColor = 0xffffffff;
2242 }
Romain Guy86568192010-12-14 15:55:39 -08002243
Romain Guy86568192010-12-14 15:55:39 -08002244 setupDraw();
2245 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002246 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2247 setupDrawColorFilter();
2248 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002249 setupDrawBlending(true, mode);
2250 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002251 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002252 setupDrawTexture(shadow->id);
2253 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002254 setupDrawColorFilterUniforms();
2255 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002256 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2257
Romain Guy0a417492010-08-16 20:26:20 -07002258 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002259 }
2260
Romain Guy6620c6d2010-12-06 18:07:02 -08002261 // Pick the appropriate texture filtering
2262 bool linearFilter = mSnapshot->transform->changesBounds();
2263 if (pureTranslate && !linearFilter) {
2264 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2265 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002266
Romain Guya1d3c912011-12-13 14:55:06 -08002267 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002268 setupDraw();
2269 setupDrawDirtyRegionsDisabled();
2270 setupDrawWithTexture(true);
2271 setupDrawAlpha8Color(paint->getColor(), alpha);
2272 setupDrawColorFilter();
2273 setupDrawShader();
2274 setupDrawBlending(true, mode);
2275 setupDrawProgram();
2276 setupDrawModelView(x, y, x, y, pureTranslate, true);
2277 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2278 setupDrawPureColorUniforms();
2279 setupDrawColorFilterUniforms();
2280 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002281
Romain Guy6620c6d2010-12-06 18:07:02 -08002282 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002283 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2284
2285#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002286 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002287#else
Romain Guy211370f2012-02-01 16:10:55 -08002288 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002289#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002290
Romain Guy6620c6d2010-12-06 18:07:02 -08002291 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002292 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002293#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002294 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002295 if (!pureTranslate) {
2296 mSnapshot->transform->mapRect(bounds);
2297 }
Romain Guyf219da52011-01-16 12:54:25 -08002298 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002299 }
2300#endif
2301 }
Romain Guy694b5192010-07-21 21:33:20 -07002302
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002303 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002304}
2305
Romain Guy325740f2012-02-24 16:48:34 -08002306void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2307 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002308 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2309 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2310 return;
2311 }
2312
Romain Guy03d58522012-02-24 17:54:07 -08002313 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2314 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2315 paint->getTextSize());
2316
2317 int alpha;
2318 SkXfermode::Mode mode;
2319 getAlphaAndMode(paint, &alpha, &mode);
2320
2321 mCaches.activeTexture(0);
2322 setupDraw();
2323 setupDrawDirtyRegionsDisabled();
2324 setupDrawWithTexture(true);
2325 setupDrawAlpha8Color(paint->getColor(), alpha);
2326 setupDrawColorFilter();
2327 setupDrawShader();
2328 setupDrawBlending(true, mode);
2329 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002330 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002331 setupDrawTexture(fontRenderer.getTexture(true));
2332 setupDrawPureColorUniforms();
2333 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002334 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002335
Romain Guy97771732012-02-28 18:17:02 -08002336 const Rect* clip = &mSnapshot->getLocalClip();
2337 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 -08002338
Romain Guy97771732012-02-28 18:17:02 -08002339#if RENDER_LAYERS_AS_REGIONS
2340 const bool hasActiveLayer = hasLayer();
2341#else
2342 const bool hasActiveLayer = false;
2343#endif
2344
2345 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2346 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2347#if RENDER_LAYERS_AS_REGIONS
2348 if (hasActiveLayer) {
2349 mSnapshot->transform->mapRect(bounds);
2350 dirtyLayerUnchecked(bounds, getRegion());
2351 }
2352#endif
2353 }
Romain Guy325740f2012-02-24 16:48:34 -08002354}
2355
Romain Guy7fbcc042010-08-04 15:40:07 -07002356void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002357 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002358
Romain Guya1d3c912011-12-13 14:55:06 -08002359 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002360
Romain Guy33f6beb2012-02-16 19:24:51 -08002361 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002362 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002363 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002364 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002365
Romain Guy8b55f372010-08-18 17:10:07 -07002366 const float x = texture->left - texture->offset;
2367 const float y = texture->top - texture->offset;
2368
Romain Guy01d58e42011-01-19 21:54:02 -08002369 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002370}
2371
Romain Guyada830f2011-01-13 12:13:20 -08002372void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2373 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002374 return;
2375 }
2376
Romain Guy2bf68f02012-03-02 13:37:47 -08002377 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2378 OpenGLRenderer* renderer = layer->renderer;
2379 Rect& dirty = layer->dirtyRect;
2380
2381 interrupt();
2382 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2383 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
2384 renderer->drawDisplayList(layer->displayList, layer->getWidth(), layer->getHeight(),
2385 dirty, DisplayList::kReplayFlag_ClipChildren);
2386 renderer->finish();
2387 resume();
2388
2389 dirty.setEmpty();
2390 layer->deferredUpdateScheduled = false;
2391 layer->renderer = NULL;
2392 layer->displayList = NULL;
2393 }
2394
Romain Guya1d3c912011-12-13 14:55:06 -08002395 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002396
2397 int alpha;
2398 SkXfermode::Mode mode;
2399 getAlphaAndMode(paint, &alpha, &mode);
2400
Romain Guy9ace8f52011-07-07 20:50:11 -07002401 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002402
Romain Guyf219da52011-01-16 12:54:25 -08002403#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002404 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002405 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002406 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002407 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002408 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002409 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002410
Romain Guyc88e3572011-01-22 00:32:12 -08002411 setupDraw();
2412 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002413 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002414 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002415 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002416 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002417 setupDrawPureColorUniforms();
2418 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002419 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002420 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002421 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2422 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2423
Romain Guyd21b6e12011-11-30 20:21:23 -08002424 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002425 setupDrawModelViewTranslate(x, y,
2426 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2427 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002428 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002429 setupDrawModelViewTranslate(x, y,
2430 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2431 }
Romain Guyc88e3572011-01-22 00:32:12 -08002432 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002433
Romain Guyc88e3572011-01-22 00:32:12 -08002434 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2435 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002436
Romain Guyc88e3572011-01-22 00:32:12 -08002437 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002438
2439#if DEBUG_LAYERS_AS_REGIONS
2440 drawRegionRects(layer->region);
2441#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002442 }
Romain Guyf219da52011-01-16 12:54:25 -08002443 }
2444#else
Romain Guyada830f2011-01-13 12:13:20 -08002445 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2446 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002447#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002448}
2449
Romain Guy6926c722010-07-12 20:20:03 -07002450///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002451// Shaders
2452///////////////////////////////////////////////////////////////////////////////
2453
2454void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002455 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002456}
2457
Romain Guy06f96e22010-07-30 19:18:16 -07002458void OpenGLRenderer::setupShader(SkiaShader* shader) {
2459 mShader = shader;
2460 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002461 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002462 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002463}
2464
Romain Guyd27977d2010-07-14 19:18:51 -07002465///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002466// Color filters
2467///////////////////////////////////////////////////////////////////////////////
2468
2469void OpenGLRenderer::resetColorFilter() {
2470 mColorFilter = NULL;
2471}
2472
2473void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2474 mColorFilter = filter;
2475}
2476
2477///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002478// Drop shadow
2479///////////////////////////////////////////////////////////////////////////////
2480
2481void OpenGLRenderer::resetShadow() {
2482 mHasShadow = false;
2483}
2484
2485void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2486 mHasShadow = true;
2487 mShadowRadius = radius;
2488 mShadowDx = dx;
2489 mShadowDy = dy;
2490 mShadowColor = color;
2491}
2492
2493///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002494// Draw filters
2495///////////////////////////////////////////////////////////////////////////////
2496
2497void OpenGLRenderer::resetPaintFilter() {
2498 mHasDrawFilter = false;
2499}
2500
2501void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2502 mHasDrawFilter = true;
2503 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2504 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2505}
2506
2507SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002508 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002509
2510 uint32_t flags = paint->getFlags();
2511
2512 mFilteredPaint = *paint;
2513 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2514
2515 return &mFilteredPaint;
2516}
2517
2518///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002519// Drawing implementation
2520///////////////////////////////////////////////////////////////////////////////
2521
Romain Guy01d58e42011-01-19 21:54:02 -08002522void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2523 float x, float y, SkPaint* paint) {
2524 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2525 return;
2526 }
2527
2528 int alpha;
2529 SkXfermode::Mode mode;
2530 getAlphaAndMode(paint, &alpha, &mode);
2531
2532 setupDraw();
2533 setupDrawWithTexture(true);
2534 setupDrawAlpha8Color(paint->getColor(), alpha);
2535 setupDrawColorFilter();
2536 setupDrawShader();
2537 setupDrawBlending(true, mode);
2538 setupDrawProgram();
2539 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2540 setupDrawTexture(texture->id);
2541 setupDrawPureColorUniforms();
2542 setupDrawColorFilterUniforms();
2543 setupDrawShaderUniforms();
2544 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2545
2546 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2547
2548 finishDrawTexture();
2549}
2550
Romain Guyf607bdc2010-09-10 19:20:06 -07002551// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002552#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2553#define kStdUnderline_Offset (1.0f / 9.0f)
2554#define kStdUnderline_Thickness (1.0f / 18.0f)
2555
2556void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2557 float x, float y, SkPaint* paint) {
2558 // Handle underline and strike-through
2559 uint32_t flags = paint->getFlags();
2560 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002561 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002562 float underlineWidth = length;
2563 // If length is > 0.0f, we already measured the text for the text alignment
2564 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002565 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002566 }
2567
2568 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002569 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002570 case SkPaint::kCenter_Align:
2571 offsetX = underlineWidth * 0.5f;
2572 break;
2573 case SkPaint::kRight_Align:
2574 offsetX = underlineWidth;
2575 break;
2576 default:
2577 break;
2578 }
2579
Romain Guy211370f2012-02-01 16:10:55 -08002580 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002581 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002582 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002583
Romain Guye20ecbd2010-09-22 19:49:04 -07002584 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002585 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002586
Romain Guyf6834472011-01-23 13:32:12 -08002587 int linesCount = 0;
2588 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2589 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2590
2591 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002592 float points[pointsCount];
2593 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002594
2595 if (flags & SkPaint::kUnderlineText_Flag) {
2596 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002597 points[currentPoint++] = left;
2598 points[currentPoint++] = top;
2599 points[currentPoint++] = left + underlineWidth;
2600 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002601 }
2602
2603 if (flags & SkPaint::kStrikeThruText_Flag) {
2604 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002605 points[currentPoint++] = left;
2606 points[currentPoint++] = top;
2607 points[currentPoint++] = left + underlineWidth;
2608 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002609 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002610
Romain Guy726aeba2011-06-01 14:52:00 -07002611 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002612
Romain Guy726aeba2011-06-01 14:52:00 -07002613 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002614 }
2615 }
2616}
2617
Romain Guy026c5e162010-06-28 17:12:22 -07002618void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002619 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002620 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002621 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002622 color |= 0x00ffffff;
2623 }
2624
Romain Guy70ca14e2010-12-13 18:24:33 -08002625 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002626 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002627 setupDrawColor(color);
2628 setupDrawShader();
2629 setupDrawColorFilter();
2630 setupDrawBlending(mode);
2631 setupDrawProgram();
2632 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2633 setupDrawColorUniforms();
2634 setupDrawShaderUniforms(ignoreTransform);
2635 setupDrawColorFilterUniforms();
2636 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002637
Romain Guyc95c8d62010-09-17 15:31:32 -07002638 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2639}
2640
Romain Guy82ba8142010-07-09 13:25:56 -07002641void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002642 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002643 int alpha;
2644 SkXfermode::Mode mode;
2645 getAlphaAndMode(paint, &alpha, &mode);
2646
Romain Guyd21b6e12011-11-30 20:21:23 -08002647 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002648
Romain Guy211370f2012-02-01 16:10:55 -08002649 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002650 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2651 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2652
Romain Guyd21b6e12011-11-30 20:21:23 -08002653 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002654 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2655 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2656 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2657 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002658 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002659 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2660 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2661 GL_TRIANGLE_STRIP, gMeshCount);
2662 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002663}
2664
Romain Guybd6b79b2010-06-26 00:13:53 -07002665void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002666 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2667 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002668 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002669}
2670
2671void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002672 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002673 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002674 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002675
Romain Guy746b7402010-10-26 16:27:31 -07002676 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002677 setupDrawWithTexture();
2678 setupDrawColor(alpha, alpha, alpha, alpha);
2679 setupDrawColorFilter();
2680 setupDrawBlending(blend, mode, swapSrcDst);
2681 setupDrawProgram();
2682 if (!dirty) {
2683 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002684 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002685 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002686 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002687 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002688 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002689 }
Romain Guy86568192010-12-14 15:55:39 -08002690 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002691 setupDrawColorFilterUniforms();
2692 setupDrawTexture(texture);
2693 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002694
Romain Guy6820ac82010-09-15 18:11:50 -07002695 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002696
2697 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002698}
2699
Romain Guya5aed0d2010-09-09 14:42:43 -07002700void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002701 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002702 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2703 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002704 // These blend modes are not supported by OpenGL directly and have
2705 // to be implemented using shaders. Since the shader will perform
2706 // the blending, turn blending off here
2707 // If the blend mode cannot be implemented using shaders, fall
2708 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002709 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2710 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002711 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002712 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002713
Romain Guy82bc7a72012-01-03 14:13:39 -08002714 if (mCaches.blend) {
2715 glDisable(GL_BLEND);
2716 mCaches.blend = false;
2717 }
2718
2719 return;
2720 } else {
2721 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002722 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002723 }
2724
2725 if (!mCaches.blend) {
2726 glEnable(GL_BLEND);
2727 }
2728
2729 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2730 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2731
2732 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2733 glBlendFunc(sourceMode, destMode);
2734 mCaches.lastSrcMode = sourceMode;
2735 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002736 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002737 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002738 glDisable(GL_BLEND);
2739 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002740 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002741}
2742
Romain Guy889f8d12010-07-29 14:37:42 -07002743bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002744 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002745 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002746 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002747 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002748 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002749 }
Romain Guy6926c722010-07-12 20:20:03 -07002750 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002751}
2752
Romain Guy026c5e162010-06-28 17:12:22 -07002753void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002754 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002755 TextureVertex::setUV(v++, u1, v1);
2756 TextureVertex::setUV(v++, u2, v1);
2757 TextureVertex::setUV(v++, u1, v2);
2758 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002759}
2760
Chet Haase5c13d892010-10-08 08:37:55 -07002761void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002762 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002763 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002764
2765 // Skia draws using the color's alpha channel if < 255
2766 // Otherwise, it uses the paint's alpha
2767 int color = paint->getColor();
2768 *alpha = (color >> 24) & 0xFF;
2769 if (*alpha == 255) {
2770 *alpha = paint->getAlpha();
2771 }
2772 } else {
2773 *mode = SkXfermode::kSrcOver_Mode;
2774 *alpha = 255;
2775 }
Romain Guy026c5e162010-06-28 17:12:22 -07002776}
2777
Romain Guya5aed0d2010-09-09 14:42:43 -07002778SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002779 SkXfermode::Mode resultMode;
2780 if (!SkXfermode::AsMode(mode, &resultMode)) {
2781 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002782 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002783 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002784}
2785
Romain Guy9d5316e2010-06-24 19:30:36 -07002786}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002787}; // namespace android