blob: 90003917ee8d7f86e22346befa797f86921893c8 [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) {
Romain Guye0aa84b2012-04-03 19:30:26 -0700597 // Detach the texture from the FBO
598 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
599
Romain Guyeb993562010-10-05 18:14:38 -0700600 // Unbind current FBO and restore previous one
601 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
602 }
603
Romain Guy1d83e192010-08-17 11:37:00 -0700604 Layer* layer = current->layer;
605 const Rect& rect = layer->layer;
606
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700608 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700611 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700612 }
613
Romain Guy03750a02010-10-18 14:06:08 -0700614 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700615
Romain Guya1d3c912011-12-13 14:55:06 -0800616 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700617
Romain Guy5b3b3522010-10-27 18:57:51 -0700618 // When the layer is stored in an FBO, we can save a bit of fillrate by
619 // drawing only the dirty region
620 if (fboLayer) {
621 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700622 if (layer->getColorFilter()) {
623 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800624 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700625 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700626 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800627 resetColorFilter();
628 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700629 } else if (!rect.isEmpty()) {
630 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
631 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700632 }
Romain Guy8b55f372010-08-18 17:10:07 -0700633
Romain Guyeb993562010-10-05 18:14:38 -0700634 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800635 // Note: No need to use glDiscardFramebufferEXT() since we never
636 // create/compose layers that are not on screen with this
637 // code path
638 // See LayerRenderer::destroyLayer(Layer*)
639
Romain Guyeb993562010-10-05 18:14:38 -0700640 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
641 mCaches.fboCache.put(current->fbo);
Romain Guy5c88fc72012-04-02 17:43:05 -0700642 layer->setFbo(0);
Romain Guyeb993562010-10-05 18:14:38 -0700643 }
644
Romain Guy746b7402010-10-26 16:27:31 -0700645 dirtyClip();
646
Romain Guyeb993562010-10-05 18:14:38 -0700647 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700648 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700649 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700650 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700651 delete layer;
652 }
653}
654
Romain Guyaa6c24c2011-04-28 18:40:04 -0700655void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700656 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657
Romain Guy302a9df2011-08-16 13:55:02 -0700658 mat4& transform = layer->getTransform();
659 if (!transform.isIdentity()) {
660 save(0);
661 mSnapshot->transform->multiply(transform);
662 }
663
Romain Guyaa6c24c2011-04-28 18:40:04 -0700664 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700666 setupDrawWithTexture();
667 } else {
668 setupDrawWithExternalTexture();
669 }
670 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700671 setupDrawColor(alpha, alpha, alpha, alpha);
672 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700673 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700674 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700675 setupDrawPureColorUniforms();
676 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
678 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700679 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700680 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700681 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700682 if (mSnapshot->transform->isPureTranslate() &&
683 layer->getWidth() == (uint32_t) rect.getWidth() &&
684 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
686 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
687
Romain Guyd21b6e12011-11-30 20:21:23 -0800688 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
690 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800691 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
693 }
694 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700695 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
696
697 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
698
699 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700700
701 if (!transform.isIdentity()) {
702 restore();
703 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700704}
705
Romain Guy5b3b3522010-10-27 18:57:51 -0700706void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700708 const Rect& texCoords = layer->texCoords;
709 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
710 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700711
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 float x = rect.left;
713 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700714 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700715 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700716 layer->getHeight() == (uint32_t) rect.getHeight();
717
718 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700719 // When we're swapping, the layer is already in screen coordinates
720 if (!swap) {
721 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
722 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
723 }
724
Romain Guyd21b6e12011-11-30 20:21:23 -0800725 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700726 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800727 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700728 }
729
730 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
731 layer->getTexture(), layer->getAlpha() / 255.0f,
732 layer->getMode(), layer->isBlend(),
733 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
734 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700735
Romain Guyaa6c24c2011-04-28 18:40:04 -0700736 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
737 } else {
738 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
739 drawTextureLayer(layer, rect);
740 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
741 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700742}
743
744void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
745#if RENDER_LAYERS_AS_REGIONS
746 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700747 layer->setRegionAsRect();
748
Romain Guy40667672011-03-18 14:34:03 -0700749 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700750
Romain Guy5b3b3522010-10-27 18:57:51 -0700751 layer->region.clear();
752 return;
753 }
754
Romain Guy8a3957d2011-09-07 17:55:15 -0700755 // TODO: See LayerRenderer.cpp::generateMesh() for important
756 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -0800757 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700758 size_t count;
759 const android::Rect* rects = layer->region.getArray(&count);
760
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 const float alpha = layer->getAlpha() / 255.0f;
762 const float texX = 1.0f / float(layer->getWidth());
763 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800764 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700765
766 TextureVertex* mesh = mCaches.getRegionMesh();
767 GLsizei numQuads = 0;
768
Romain Guy7230a742011-01-10 22:26:16 -0800769 setupDraw();
770 setupDrawWithTexture();
771 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800772 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700773 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800774 setupDrawProgram();
775 setupDrawDirtyRegionsDisabled();
776 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800777 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700778 setupDrawTexture(layer->getTexture());
779 if (mSnapshot->transform->isPureTranslate()) {
780 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
781 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
782
Romain Guyd21b6e12011-11-30 20:21:23 -0800783 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
785 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800786 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
788 }
Romain Guy15bc6432011-12-13 13:11:32 -0800789 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700790
791 for (size_t i = 0; i < count; i++) {
792 const android::Rect* r = &rects[i];
793
794 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800795 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700796 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800797 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700798
799 // TODO: Reject quads outside of the clip
800 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
801 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
802 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
803 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
804
805 numQuads++;
806
807 if (numQuads >= REGION_MESH_QUAD_COUNT) {
808 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
809 numQuads = 0;
810 mesh = mCaches.getRegionMesh();
811 }
812 }
813
814 if (numQuads > 0) {
815 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
816 }
817
Romain Guy7230a742011-01-10 22:26:16 -0800818 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700819
820#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800821 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700822#endif
823
824 layer->region.clear();
825 }
826#else
827 composeLayerRect(layer, rect);
828#endif
829}
830
Romain Guy3a3133d2011-02-01 22:59:58 -0800831void OpenGLRenderer::drawRegionRects(const Region& region) {
832#if DEBUG_LAYERS_AS_REGIONS
833 size_t count;
834 const android::Rect* rects = region.getArray(&count);
835
836 uint32_t colors[] = {
837 0x7fff0000, 0x7f00ff00,
838 0x7f0000ff, 0x7fff00ff,
839 };
840
841 int offset = 0;
842 int32_t top = rects[0].top;
843
844 for (size_t i = 0; i < count; i++) {
845 if (top != rects[i].top) {
846 offset ^= 0x2;
847 top = rects[i].top;
848 }
849
850 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
851 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
852 SkXfermode::kSrcOver_Mode);
853 }
854#endif
855}
856
Romain Guy5b3b3522010-10-27 18:57:51 -0700857void OpenGLRenderer::dirtyLayer(const float left, const float top,
858 const float right, const float bottom, const mat4 transform) {
859#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800860 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700861 Rect bounds(left, top, right, bottom);
862 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800863 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 }
865#endif
866}
867
868void OpenGLRenderer::dirtyLayer(const float left, const float top,
869 const float right, const float bottom) {
870#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800871 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700872 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800873 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800874 }
875#endif
876}
877
878void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
879#if RENDER_LAYERS_AS_REGIONS
880 if (bounds.intersect(*mSnapshot->clipRect)) {
881 bounds.snapToPixelBoundaries();
882 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
883 if (!dirty.isEmpty()) {
884 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700885 }
886 }
887#endif
888}
889
Romain Guy54be1cd2011-06-13 19:04:27 -0700890void OpenGLRenderer::clearLayerRegions() {
891 const size_t count = mLayers.size();
892 if (count == 0) return;
893
894 if (!mSnapshot->isIgnored()) {
895 // Doing several glScissor/glClear here can negatively impact
896 // GPUs with a tiler architecture, instead we draw quads with
897 // the Clear blending mode
898
899 // The list contains bounds that have already been clipped
900 // against their initial clip rect, and the current clip
901 // is likely different so we need to disable clipping here
902 glDisable(GL_SCISSOR_TEST);
903
904 Vertex mesh[count * 6];
905 Vertex* vertex = mesh;
906
907 for (uint32_t i = 0; i < count; i++) {
908 Rect* bounds = mLayers.itemAt(i);
909
910 Vertex::set(vertex++, bounds->left, bounds->bottom);
911 Vertex::set(vertex++, bounds->left, bounds->top);
912 Vertex::set(vertex++, bounds->right, bounds->top);
913 Vertex::set(vertex++, bounds->left, bounds->bottom);
914 Vertex::set(vertex++, bounds->right, bounds->top);
915 Vertex::set(vertex++, bounds->right, bounds->bottom);
916
917 delete bounds;
918 }
919
920 setupDraw(false);
921 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
922 setupDrawBlending(true, SkXfermode::kClear_Mode);
923 setupDrawProgram();
924 setupDrawPureColorUniforms();
925 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800926 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700927
Romain Guy54be1cd2011-06-13 19:04:27 -0700928 glDrawArrays(GL_TRIANGLES, 0, count * 6);
929
930 glEnable(GL_SCISSOR_TEST);
931 } else {
932 for (uint32_t i = 0; i < count; i++) {
933 delete mLayers.itemAt(i);
934 }
935 }
936
937 mLayers.clear();
938}
939
Romain Guybd6b79b2010-06-26 00:13:53 -0700940///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700941// Transforms
942///////////////////////////////////////////////////////////////////////////////
943
944void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700945 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700946}
947
948void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700949 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700950}
951
952void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700953 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700954}
955
Romain Guy807daf72011-01-18 11:19:19 -0800956void OpenGLRenderer::skew(float sx, float sy) {
957 mSnapshot->transform->skew(sx, sy);
958}
959
Romain Guyf6a11b82010-06-23 17:47:49 -0700960void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700961 if (matrix) {
962 mSnapshot->transform->load(*matrix);
963 } else {
964 mSnapshot->transform->loadIdentity();
965 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700966}
967
968void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700969 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700970}
971
972void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700973 SkMatrix transform;
974 mSnapshot->transform->copyTo(transform);
975 transform.preConcat(*matrix);
976 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700977}
978
979///////////////////////////////////////////////////////////////////////////////
980// Clipping
981///////////////////////////////////////////////////////////////////////////////
982
Romain Guybb9524b2010-06-22 18:56:38 -0700983void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700984 Rect clip(*mSnapshot->clipRect);
985 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800986
987 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
988 clip.getWidth(), clip.getHeight());
989
Romain Guy746b7402010-10-26 16:27:31 -0700990 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700991}
992
993const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700994 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700995}
996
Romain Guyc7d53492010-06-25 13:41:57 -0700997bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800998 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700999 return true;
1000 }
1001
Romain Guy1d83e192010-08-17 11:37:00 -07001002 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -07001003 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001004 r.snapToPixelBoundaries();
1005
1006 Rect clipRect(*mSnapshot->clipRect);
1007 clipRect.snapToPixelBoundaries();
1008
1009 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001010}
1011
Romain Guy079ba2c2010-07-16 14:12:24 -07001012bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1013 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001014 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001015 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001016 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001017 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001018}
1019
Romain Guyf6a11b82010-06-23 17:47:49 -07001020///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001021// Drawing commands
1022///////////////////////////////////////////////////////////////////////////////
1023
Romain Guy54be1cd2011-06-13 19:04:27 -07001024void OpenGLRenderer::setupDraw(bool clear) {
1025 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001026 if (mDirtyClip) {
1027 setScissorFromClip();
1028 }
1029 mDescription.reset();
1030 mSetShaderColor = false;
1031 mColorSet = false;
1032 mColorA = mColorR = mColorG = mColorB = 0.0f;
1033 mTextureUnit = 0;
1034 mTrackDirtyRegions = true;
1035}
1036
1037void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1038 mDescription.hasTexture = true;
1039 mDescription.hasAlpha8Texture = isAlpha8;
1040}
1041
Romain Guyaa6c24c2011-04-28 18:40:04 -07001042void OpenGLRenderer::setupDrawWithExternalTexture() {
1043 mDescription.hasExternalTexture = true;
1044}
1045
Romain Guy15bc6432011-12-13 13:11:32 -08001046void OpenGLRenderer::setupDrawNoTexture() {
1047 mCaches.disbaleTexCoordsVertexArray();
1048}
1049
Chet Haase5b0200b2011-04-13 17:58:08 -07001050void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001051 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001052}
1053
Romain Guyed6fcb02011-03-21 13:11:28 -07001054void OpenGLRenderer::setupDrawPoint(float pointSize) {
1055 mDescription.isPoint = true;
1056 mDescription.pointSize = pointSize;
1057}
1058
Romain Guy70ca14e2010-12-13 18:24:33 -08001059void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001060 setupDrawColor(color, (color >> 24) & 0xFF);
1061}
1062
1063void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1064 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001065 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1066 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001067 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001068 mColorR = a * ((color >> 16) & 0xFF);
1069 mColorG = a * ((color >> 8) & 0xFF);
1070 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001071 mColorSet = true;
1072 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1073}
1074
Romain Guy86568192010-12-14 15:55:39 -08001075void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1076 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001077 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1078 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001079 const float a = mColorA / 255.0f;
1080 mColorR = a * ((color >> 16) & 0xFF);
1081 mColorG = a * ((color >> 8) & 0xFF);
1082 mColorB = a * ((color ) & 0xFF);
1083 mColorSet = true;
1084 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1085}
1086
Romain Guy70ca14e2010-12-13 18:24:33 -08001087void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1088 mColorA = a;
1089 mColorR = r;
1090 mColorG = g;
1091 mColorB = b;
1092 mColorSet = true;
1093 mSetShaderColor = mDescription.setColor(r, g, b, a);
1094}
1095
Romain Guy86568192010-12-14 15:55:39 -08001096void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1097 mColorA = a;
1098 mColorR = r;
1099 mColorG = g;
1100 mColorB = b;
1101 mColorSet = true;
1102 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1103}
1104
Romain Guy70ca14e2010-12-13 18:24:33 -08001105void OpenGLRenderer::setupDrawShader() {
1106 if (mShader) {
1107 mShader->describe(mDescription, mCaches.extensions);
1108 }
1109}
1110
1111void OpenGLRenderer::setupDrawColorFilter() {
1112 if (mColorFilter) {
1113 mColorFilter->describe(mDescription, mCaches.extensions);
1114 }
1115}
1116
Romain Guyf09ef512011-05-27 11:43:46 -07001117void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1118 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1119 mColorA = 1.0f;
1120 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001121 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001122 }
1123}
1124
Romain Guy70ca14e2010-12-13 18:24:33 -08001125void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001126 // When the blending mode is kClear_Mode, we need to use a modulate color
1127 // argb=1,0,0,0
1128 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001129 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1130 mDescription, swapSrcDst);
1131}
1132
1133void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001134 // When the blending mode is kClear_Mode, we need to use a modulate color
1135 // argb=1,0,0,0
1136 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001137 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1138 mDescription, swapSrcDst);
1139}
1140
1141void OpenGLRenderer::setupDrawProgram() {
1142 useProgram(mCaches.programCache.get(mDescription));
1143}
1144
1145void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1146 mTrackDirtyRegions = false;
1147}
1148
1149void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1150 bool ignoreTransform) {
1151 mModelView.loadTranslate(left, top, 0.0f);
1152 if (!ignoreTransform) {
1153 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1154 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1155 } else {
1156 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1157 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1158 }
1159}
1160
Chet Haase8a5cc922011-04-26 07:28:09 -07001161void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1162 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001163}
1164
Romain Guy70ca14e2010-12-13 18:24:33 -08001165void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1166 bool ignoreTransform, bool ignoreModelView) {
1167 if (!ignoreModelView) {
1168 mModelView.loadTranslate(left, top, 0.0f);
1169 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001170 } else {
1171 mModelView.loadIdentity();
1172 }
Romain Guy86568192010-12-14 15:55:39 -08001173 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1174 if (!ignoreTransform) {
1175 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1176 if (mTrackDirtyRegions && dirty) {
1177 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1178 }
1179 } else {
1180 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1181 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1182 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001183}
1184
Romain Guyed6fcb02011-03-21 13:11:28 -07001185void OpenGLRenderer::setupDrawPointUniforms() {
1186 int slot = mCaches.currentProgram->getUniform("pointSize");
1187 glUniform1f(slot, mDescription.pointSize);
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55fd2c92012-03-09 17:36:01 -08001191 if ((mColorSet && !mShader) || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001192 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1193 }
1194}
1195
Romain Guy86568192010-12-14 15:55:39 -08001196void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001197 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001198 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001199 }
1200}
1201
Romain Guy70ca14e2010-12-13 18:24:33 -08001202void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1203 if (mShader) {
1204 if (ignoreTransform) {
1205 mModelView.loadInverse(*mSnapshot->transform);
1206 }
1207 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1208 }
1209}
1210
Romain Guy8d0d4782010-12-14 20:13:35 -08001211void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1212 if (mShader) {
1213 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1214 }
1215}
1216
Romain Guy70ca14e2010-12-13 18:24:33 -08001217void OpenGLRenderer::setupDrawColorFilterUniforms() {
1218 if (mColorFilter) {
1219 mColorFilter->setupProgram(mCaches.currentProgram);
1220 }
1221}
1222
1223void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001224 bool force = mCaches.bindMeshBuffer();
1225 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001226 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001227}
1228
1229void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1230 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001231 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001232 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001233}
1234
Romain Guyaa6c24c2011-04-28 18:40:04 -07001235void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1236 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001237 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001238 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001239}
1240
Romain Guy8f0095c2011-05-02 17:24:22 -07001241void OpenGLRenderer::setupDrawTextureTransform() {
1242 mDescription.hasTextureTransform = true;
1243}
1244
1245void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001246 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1247 GL_FALSE, &transform.data[0]);
1248}
1249
Romain Guy70ca14e2010-12-13 18:24:33 -08001250void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001251 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001252 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001253 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001254 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001255 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001256 }
Romain Guyd71dd362011-12-12 19:03:35 -08001257
Romain Guyf3a910b42011-12-12 20:35:21 -08001258 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001259 if (mCaches.currentProgram->texCoords >= 0) {
1260 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1261 }
1262
1263 mCaches.unbindIndicesBuffer();
1264}
1265
1266void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1267 bool force = mCaches.unbindMeshBuffer();
1268 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1269 if (mCaches.currentProgram->texCoords >= 0) {
1270 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001271 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001272}
1273
Chet Haase5b0200b2011-04-13 17:58:08 -07001274void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001275 bool force = mCaches.unbindMeshBuffer();
1276 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1277 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001278 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001279}
1280
1281/**
1282 * 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 -07001283 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1284 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1285 * attributes (one per vertex) are values from zero to one that tells the fragment
1286 * shader where the fragment is in relation to the line width/length overall; these values are
1287 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1288 * region of the line.
1289 * Note that we only pass down the width values in this setup function. The length coordinates
1290 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001291 */
Chet Haase99585ad2011-05-02 15:00:16 -07001292void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001293 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001294 bool force = mCaches.unbindMeshBuffer();
1295 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1296 vertices, gAAVertexStride);
1297 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001298 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001299
Chet Haase99585ad2011-05-02 15:00:16 -07001300 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1301 glEnableVertexAttribArray(widthSlot);
1302 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001303
Chet Haase99585ad2011-05-02 15:00:16 -07001304 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1305 glEnableVertexAttribArray(lengthSlot);
1306 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001307
Chet Haase5b0200b2011-04-13 17:58:08 -07001308 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001309 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001310
Chet Haase99585ad2011-05-02 15:00:16 -07001311 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001312 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001313 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001314}
1315
Romain Guy70ca14e2010-12-13 18:24:33 -08001316void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001317}
1318
1319///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001320// Drawing
1321///////////////////////////////////////////////////////////////////////////////
1322
Romain Guy65549432012-03-26 16:45:05 -07001323status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -08001324 Rect& dirty, int32_t flags, uint32_t level) {
Chet Haaseb85967b2012-03-26 14:37:51 -07001325
1326 if (!USE_DISPLAY_LIST_PROPERTIES && quickReject(0, 0, width, height)) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001327 return false;
1328 }
1329
Romain Guy0fe478e2010-11-08 12:08:41 -08001330 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1331 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001332 if (displayList && displayList->isRenderable()) {
Chet Haasea1cff502012-02-21 13:43:44 -08001333 return displayList->replay(*this, width, height, dirty, flags, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001334 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001335
Romain Guy65549432012-03-26 16:45:05 -07001336 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001337}
1338
Chet Haaseed30fd82011-04-22 16:18:45 -07001339void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1340 if (displayList) {
1341 displayList->output(*this, level);
1342 }
1343}
1344
Romain Guya168d732011-03-18 16:50:13 -07001345void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1346 int alpha;
1347 SkXfermode::Mode mode;
1348 getAlphaAndMode(paint, &alpha, &mode);
1349
Romain Guya168d732011-03-18 16:50:13 -07001350 float x = left;
1351 float y = top;
1352
Romain Guye3c26852011-07-25 16:36:01 -07001353 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001354 bool ignoreTransform = false;
1355 if (mSnapshot->transform->isPureTranslate()) {
1356 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1357 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1358 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001359 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001360 } else {
1361 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001362 }
1363
1364 setupDraw();
1365 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001366 if (paint) {
1367 setupDrawAlpha8Color(paint->getColor(), alpha);
1368 }
Romain Guya168d732011-03-18 16:50:13 -07001369 setupDrawColorFilter();
1370 setupDrawShader();
1371 setupDrawBlending(true, mode);
1372 setupDrawProgram();
1373 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001374
Romain Guya168d732011-03-18 16:50:13 -07001375 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001376 texture->setWrap(GL_CLAMP_TO_EDGE);
1377 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001378
Romain Guya168d732011-03-18 16:50:13 -07001379 setupDrawPureColorUniforms();
1380 setupDrawColorFilterUniforms();
1381 setupDrawShaderUniforms();
1382 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1383
1384 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1385
1386 finishDrawTexture();
1387}
1388
Chet Haase5c13d892010-10-08 08:37:55 -07001389void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001390 const float right = left + bitmap->width();
1391 const float bottom = top + bitmap->height();
1392
1393 if (quickReject(left, top, right, bottom)) {
1394 return;
1395 }
1396
Romain Guya1d3c912011-12-13 14:55:06 -08001397 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001398 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001399 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001400 const AutoTexture autoCleanup(texture);
1401
Romain Guy211370f2012-02-01 16:10:55 -08001402 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001403 drawAlphaBitmap(texture, left, top, paint);
1404 } else {
1405 drawTextureRect(left, top, right, bottom, texture, paint);
1406 }
Romain Guyce0537b2010-06-29 21:05:21 -07001407}
1408
Chet Haase5c13d892010-10-08 08:37:55 -07001409void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001410 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1411 const mat4 transform(*matrix);
1412 transform.mapRect(r);
1413
Romain Guy6926c722010-07-12 20:20:03 -07001414 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1415 return;
1416 }
1417
Romain Guya1d3c912011-12-13 14:55:06 -08001418 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001419 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001420 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001421 const AutoTexture autoCleanup(texture);
1422
Romain Guy5b3b3522010-10-27 18:57:51 -07001423 // This could be done in a cheaper way, all we need is pass the matrix
1424 // to the vertex shader. The save/restore is a bit overkill.
1425 save(SkCanvas::kMatrix_SaveFlag);
1426 concatMatrix(matrix);
1427 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1428 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001429}
1430
Romain Guy5a7b4662011-01-20 19:09:30 -08001431void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1432 float* vertices, int* colors, SkPaint* paint) {
1433 // TODO: Do a quickReject
1434 if (!vertices || mSnapshot->isIgnored()) {
1435 return;
1436 }
1437
Romain Guya1d3c912011-12-13 14:55:06 -08001438 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001439 Texture* texture = mCaches.textureCache.get(bitmap);
1440 if (!texture) return;
1441 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001442
Romain Guyd21b6e12011-11-30 20:21:23 -08001443 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1444 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001445
1446 int alpha;
1447 SkXfermode::Mode mode;
1448 getAlphaAndMode(paint, &alpha, &mode);
1449
Romain Guy5a7b4662011-01-20 19:09:30 -08001450 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001451
Romain Guyb18d2d02011-02-10 15:52:54 -08001452 float left = FLT_MAX;
1453 float top = FLT_MAX;
1454 float right = FLT_MIN;
1455 float bottom = FLT_MIN;
1456
1457#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08001458 const bool hasActiveLayer = hasLayer();
Romain Guyb18d2d02011-02-10 15:52:54 -08001459#else
Romain Guy211370f2012-02-01 16:10:55 -08001460 const bool hasActiveLayer = false;
Romain Guyb18d2d02011-02-10 15:52:54 -08001461#endif
1462
Romain Guya566b7c2011-01-23 16:36:11 -08001463 // TODO: Support the colors array
1464 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001465 TextureVertex* vertex = mesh;
1466 for (int32_t y = 0; y < meshHeight; y++) {
1467 for (int32_t x = 0; x < meshWidth; x++) {
1468 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1469
1470 float u1 = float(x) / meshWidth;
1471 float u2 = float(x + 1) / meshWidth;
1472 float v1 = float(y) / meshHeight;
1473 float v2 = float(y + 1) / meshHeight;
1474
1475 int ax = i + (meshWidth + 1) * 2;
1476 int ay = ax + 1;
1477 int bx = i;
1478 int by = bx + 1;
1479 int cx = i + 2;
1480 int cy = cx + 1;
1481 int dx = i + (meshWidth + 1) * 2 + 2;
1482 int dy = dx + 1;
1483
1484 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1485 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1486 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1487
1488 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1489 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1490 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001491
1492#if RENDER_LAYERS_AS_REGIONS
1493 if (hasActiveLayer) {
1494 // TODO: This could be optimized to avoid unnecessary ops
1495 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1496 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1497 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1498 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1499 }
1500#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001501 }
1502 }
1503
Romain Guyb18d2d02011-02-10 15:52:54 -08001504#if RENDER_LAYERS_AS_REGIONS
1505 if (hasActiveLayer) {
1506 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1507 }
1508#endif
1509
Romain Guy5a7b4662011-01-20 19:09:30 -08001510 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1511 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001512 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001513}
1514
Romain Guy8ba548f2010-06-30 19:21:21 -07001515void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1516 float srcLeft, float srcTop, float srcRight, float srcBottom,
1517 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001518 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001519 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1520 return;
1521 }
1522
Romain Guya1d3c912011-12-13 14:55:06 -08001523 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001524 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001525 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001526 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001527
Romain Guy8ba548f2010-06-30 19:21:21 -07001528 const float width = texture->width;
1529 const float height = texture->height;
1530
Romain Guy68169722011-08-22 17:33:33 -07001531 const float u1 = fmax(0.0f, srcLeft / width);
1532 const float v1 = fmax(0.0f, srcTop / height);
1533 const float u2 = fmin(1.0f, srcRight / width);
1534 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001535
Romain Guy03750a02010-10-18 14:06:08 -07001536 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001537 resetDrawTextureTexCoords(u1, v1, u2, v2);
1538
Romain Guy03750a02010-10-18 14:06:08 -07001539 int alpha;
1540 SkXfermode::Mode mode;
1541 getAlphaAndMode(paint, &alpha, &mode);
1542
Romain Guyd21b6e12011-11-30 20:21:23 -08001543 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1544
Romain Guy211370f2012-02-01 16:10:55 -08001545 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001546 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1547 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1548
Romain Guyb5014982011-07-28 15:39:12 -07001549 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001550 // Enable linear filtering if the source rectangle is scaled
1551 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001552 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001553 }
Romain Guyb5014982011-07-28 15:39:12 -07001554
Romain Guyd21b6e12011-11-30 20:21:23 -08001555 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001556 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1557 texture->id, alpha / 255.0f, mode, texture->blend,
1558 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1559 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1560 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001561 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001562 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1563 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1564 GL_TRIANGLE_STRIP, gMeshCount);
1565 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001566
1567 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1568}
1569
Romain Guy4aa90572010-09-26 18:40:37 -07001570void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001571 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001572 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001573 if (quickReject(left, top, right, bottom)) {
1574 return;
1575 }
1576
Romain Guya1d3c912011-12-13 14:55:06 -08001577 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001578 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001579 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001580 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001581 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1582 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001583
1584 int alpha;
1585 SkXfermode::Mode mode;
1586 getAlphaAndMode(paint, &alpha, &mode);
1587
Romain Guy2728f962010-10-08 18:36:15 -07001588 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001589 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001590
Romain Guy211370f2012-02-01 16:10:55 -08001591 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001592 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001593#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001594 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001595 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001596 const float offsetX = left + mSnapshot->transform->getTranslateX();
1597 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001598 const size_t count = mesh->quads.size();
1599 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001600 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08001601 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001602 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1603 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1604 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001605 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001606 dirtyLayer(left + bounds.left, top + bounds.top,
1607 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001608 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001609 }
1610 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001611#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001612
Romain Guy211370f2012-02-01 16:10:55 -08001613 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001614 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1615 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1616
1617 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1618 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1619 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1620 true, !mesh->hasEmptyQuads);
1621 } else {
1622 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1623 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1624 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1625 true, !mesh->hasEmptyQuads);
1626 }
Romain Guy054dc182010-10-15 17:55:25 -07001627 }
Romain Guyf7f93552010-07-08 19:17:03 -07001628}
1629
Chet Haase99ecdc42011-05-06 12:06:34 -07001630/**
Chet Haase858aa932011-05-12 09:06:00 -07001631 * This function uses a similar approach to that of AA lines in the drawLines() function.
1632 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1633 * shader to compute the translucency of the color, determined by whether a given pixel is
1634 * within that boundary region and how far into the region it is.
1635 */
1636void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001637 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001638 float inverseScaleX = 1.0f;
1639 float inverseScaleY = 1.0f;
1640 // The quad that we use needs to account for scaling.
Romain Guy211370f2012-02-01 16:10:55 -08001641 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase858aa932011-05-12 09:06:00 -07001642 Matrix4 *mat = mSnapshot->transform;
1643 float m00 = mat->data[Matrix4::kScaleX];
1644 float m01 = mat->data[Matrix4::kSkewY];
1645 float m02 = mat->data[2];
1646 float m10 = mat->data[Matrix4::kSkewX];
1647 float m11 = mat->data[Matrix4::kScaleX];
1648 float m12 = mat->data[6];
1649 float scaleX = sqrt(m00 * m00 + m01 * m01);
1650 float scaleY = sqrt(m10 * m10 + m11 * m11);
1651 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1652 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1653 }
1654
1655 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001656 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001657 setupDrawAALine();
1658 setupDrawColor(color);
1659 setupDrawColorFilter();
1660 setupDrawShader();
1661 setupDrawBlending(true, mode);
1662 setupDrawProgram();
1663 setupDrawModelViewIdentity(true);
1664 setupDrawColorUniforms();
1665 setupDrawColorFilterUniforms();
1666 setupDrawShaderIdentityUniforms();
1667
1668 AAVertex rects[4];
1669 AAVertex* aaVertices = &rects[0];
1670 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1671 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1672
1673 float boundarySizeX = .5 * inverseScaleX;
1674 float boundarySizeY = .5 * inverseScaleY;
1675
1676 // Adjust the rect by the AA boundary padding
1677 left -= boundarySizeX;
1678 right += boundarySizeX;
1679 top -= boundarySizeY;
1680 bottom += boundarySizeY;
1681
1682 float width = right - left;
1683 float height = bottom - top;
1684
1685 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1686 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1687 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1688 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1689 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1690 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1691 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1692
1693 if (!quickReject(left, top, right, bottom)) {
1694 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1695 AAVertex::set(aaVertices++, left, top, 1, 0);
1696 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1697 AAVertex::set(aaVertices++, right, top, 0, 0);
1698 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1699 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1700 }
1701}
1702
1703/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001704 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1705 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1706 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1707 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1708 * of the line. Hairlines are more involved because we need to account for transform scaling
1709 * to end up with a one-pixel-wide line in screen space..
1710 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1711 * in combination with values that we calculate and pass down in this method. The basic approach
1712 * is that the quad we create contains both the core line area plus a bounding area in which
1713 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1714 * proportion of the width and the length of a given segment is represented by the boundary
1715 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1716 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1717 * on the inside). This ends up giving the result we want, with pixels that are completely
1718 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1719 * how far into the boundary region they are, which is determined by shader interpolation.
1720 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001721void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1722 if (mSnapshot->isIgnored()) return;
1723
1724 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001725 // We use half the stroke width here because we're going to position the quad
1726 // corner vertices half of the width away from the line endpoints
1727 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001728 // A stroke width of 0 has a special meaning in Skia:
1729 // it draws a line 1 px wide regardless of current transform
1730 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001731 float inverseScaleX = 1.0f;
1732 float inverseScaleY = 1.0f;
1733 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001734 int alpha;
1735 SkXfermode::Mode mode;
1736 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001737 int verticesCount = count;
1738 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001739 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001740 verticesCount += (count - 4);
1741 }
1742
1743 if (isHairLine || isAA) {
1744 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1745 // the line on the screen should always be one pixel wide regardless of scale. For
1746 // AA lines, we only want one pixel of translucent boundary around the quad.
Romain Guy211370f2012-02-01 16:10:55 -08001747 if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
Chet Haase99ecdc42011-05-06 12:06:34 -07001748 Matrix4 *mat = mSnapshot->transform;
1749 float m00 = mat->data[Matrix4::kScaleX];
1750 float m01 = mat->data[Matrix4::kSkewY];
1751 float m02 = mat->data[2];
1752 float m10 = mat->data[Matrix4::kSkewX];
1753 float m11 = mat->data[Matrix4::kScaleX];
1754 float m12 = mat->data[6];
Romain Guy211370f2012-02-01 16:10:55 -08001755 float scaleX = sqrtf(m00 * m00 + m01 * m01);
1756 float scaleY = sqrtf(m10 * m10 + m11 * m11);
Chet Haase99ecdc42011-05-06 12:06:34 -07001757 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1758 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1759 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1760 scaled = true;
1761 }
1762 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001763 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001764
1765 getAlphaAndMode(paint, &alpha, &mode);
1766 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001767 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001768 if (isAA) {
1769 setupDrawAALine();
1770 }
1771 setupDrawColor(paint->getColor(), alpha);
1772 setupDrawColorFilter();
1773 setupDrawShader();
Romain Guy211370f2012-02-01 16:10:55 -08001774 setupDrawBlending(isAA, mode);
Chet Haase8a5cc922011-04-26 07:28:09 -07001775 setupDrawProgram();
1776 setupDrawModelViewIdentity(true);
1777 setupDrawColorUniforms();
1778 setupDrawColorFilterUniforms();
1779 setupDrawShaderIdentityUniforms();
1780
1781 if (isHairLine) {
1782 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001783 halfStrokeWidth = isAA? 1 : .5;
1784 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001785 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001786 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001787 }
1788 Vertex lines[verticesCount];
1789 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001790 AAVertex wLines[verticesCount];
1791 AAVertex* aaVertices = &wLines[0];
Romain Guy211370f2012-02-01 16:10:55 -08001792 if (CC_UNLIKELY(!isAA)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001793 setupDrawVertices(vertices);
1794 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001795 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1796 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001798 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1799 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001800 // We will need to calculate the actual width proportion on each segment for
1801 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1802 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1803 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001804 }
1805
Chet Haase99ecdc42011-05-06 12:06:34 -07001806 AAVertex* prevAAVertex = NULL;
1807 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001808
Chet Haase99585ad2011-05-02 15:00:16 -07001809 int boundaryLengthSlot = -1;
1810 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001811 int boundaryWidthSlot = -1;
1812 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001813 for (int i = 0; i < count; i += 4) {
1814 // a = start point, b = end point
1815 vec2 a(points[i], points[i + 1]);
1816 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001817 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001818 float boundaryLengthProportion = 0;
1819 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001820
Chet Haase5b0200b2011-04-13 17:58:08 -07001821 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001822 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001823 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001824 if (isAA) {
1825 float wideningFactor;
1826 if (fabs(n.x) >= fabs(n.y)) {
1827 wideningFactor = fabs(1.0f / n.x);
1828 } else {
1829 wideningFactor = fabs(1.0f / n.y);
1830 }
1831 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001832 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001833 if (scaled) {
1834 n.x *= inverseScaleX;
1835 n.y *= inverseScaleY;
1836 }
1837 } else if (scaled) {
1838 // Extend n by .5 pixel on each side, post-transform
1839 vec2 extendedN = n.copyNormalized();
1840 extendedN /= 2;
1841 extendedN.x *= inverseScaleX;
1842 extendedN.y *= inverseScaleY;
1843 float extendedNLength = extendedN.length();
1844 // We need to set this value on the shader prior to drawing
1845 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1846 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001847 }
1848 float x = n.x;
1849 n.x = -n.y;
1850 n.y = x;
1851
Chet Haase99585ad2011-05-02 15:00:16 -07001852 // aa lines expand the endpoint vertices to encompass the AA boundary
1853 if (isAA) {
1854 vec2 abVector = (b - a);
1855 length = abVector.length();
1856 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001857 if (scaled) {
1858 abVector.x *= inverseScaleX;
1859 abVector.y *= inverseScaleY;
1860 float abLength = abVector.length();
1861 boundaryLengthProportion = abLength / (length + abLength);
1862 } else {
1863 boundaryLengthProportion = .5 / (length + 1);
1864 }
1865 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001866 a -= abVector;
1867 b += abVector;
1868 }
1869
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 // Four corners of the rectangle defining a thick line
1871 vec2 p1 = a - n;
1872 vec2 p2 = a + n;
1873 vec2 p3 = b + n;
1874 vec2 p4 = b - n;
1875
Chet Haase99585ad2011-05-02 15:00:16 -07001876
Chet Haase5b0200b2011-04-13 17:58:08 -07001877 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1878 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1879 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1880 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1881
1882 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001883 if (!isAA) {
1884 if (prevVertex != NULL) {
1885 // Issue two repeat vertices to create degenerate triangles to bridge
1886 // between the previous line and the new one. This is necessary because
1887 // we are creating a single triangle_strip which will contain
1888 // potentially discontinuous line segments.
1889 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1890 Vertex::set(vertices++, p1.x, p1.y);
1891 generatedVerticesCount += 2;
1892 }
1893 Vertex::set(vertices++, p1.x, p1.y);
1894 Vertex::set(vertices++, p2.x, p2.y);
1895 Vertex::set(vertices++, p4.x, p4.y);
1896 Vertex::set(vertices++, p3.x, p3.y);
1897 prevVertex = vertices - 1;
1898 generatedVerticesCount += 4;
1899 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001900 if (!isHairLine && scaled) {
1901 // Must set width proportions per-segment for scaled non-hairlines to use the
1902 // correct AA boundary dimensions
1903 if (boundaryWidthSlot < 0) {
1904 boundaryWidthSlot =
1905 mCaches.currentProgram->getUniform("boundaryWidth");
1906 inverseBoundaryWidthSlot =
1907 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1908 }
1909 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1910 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1911 }
Chet Haase99585ad2011-05-02 15:00:16 -07001912 if (boundaryLengthSlot < 0) {
1913 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1914 inverseBoundaryLengthSlot =
1915 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1916 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001917 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1918 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001919
Chet Haase5b0200b2011-04-13 17:58:08 -07001920 if (prevAAVertex != NULL) {
1921 // Issue two repeat vertices to create degenerate triangles to bridge
1922 // between the previous line and the new one. This is necessary because
1923 // we are creating a single triangle_strip which will contain
1924 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001925 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1926 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1927 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001928 generatedVerticesCount += 2;
1929 }
Chet Haase99585ad2011-05-02 15:00:16 -07001930 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1931 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1932 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1933 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001934 prevAAVertex = aaVertices - 1;
1935 generatedVerticesCount += 4;
1936 }
Chet Haase99585ad2011-05-02 15:00:16 -07001937 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1938 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1939 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 }
1941 }
1942 if (generatedVerticesCount > 0) {
1943 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1944 }
1945}
1946
Romain Guyed6fcb02011-03-21 13:11:28 -07001947void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1948 if (mSnapshot->isIgnored()) return;
1949
1950 // TODO: The paint's cap style defines whether the points are square or circular
1951 // TODO: Handle AA for round points
1952
Chet Haase5b0200b2011-04-13 17:58:08 -07001953 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001954 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001955 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001956 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001957 if (isHairLine) {
1958 // Now that we know it's hairline, we can set the effective width, to be used later
1959 strokeWidth = 1.0f;
1960 }
1961 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001962 int alpha;
1963 SkXfermode::Mode mode;
1964 getAlphaAndMode(paint, &alpha, &mode);
1965
1966 int verticesCount = count >> 1;
1967 int generatedVerticesCount = 0;
1968
1969 TextureVertex pointsData[verticesCount];
1970 TextureVertex* vertex = &pointsData[0];
1971
1972 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001973 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001974 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001975 setupDrawColor(paint->getColor(), alpha);
1976 setupDrawColorFilter();
1977 setupDrawShader();
1978 setupDrawBlending(mode);
1979 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001980 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001981 setupDrawColorUniforms();
1982 setupDrawColorFilterUniforms();
1983 setupDrawPointUniforms();
1984 setupDrawShaderIdentityUniforms();
1985 setupDrawMesh(vertex);
1986
1987 for (int i = 0; i < count; i += 2) {
1988 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1989 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001990 float left = points[i] - halfWidth;
1991 float right = points[i] + halfWidth;
1992 float top = points[i + 1] - halfWidth;
1993 float bottom = points [i + 1] + halfWidth;
1994 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001995 }
1996
1997 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1998}
1999
Romain Guy85bf02f2010-06-22 13:11:24 -07002000void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002001 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08002002 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002003
Romain Guyae88e5e2010-10-22 17:49:18 -07002004 Rect& clip(*mSnapshot->clipRect);
2005 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002006
Romain Guy3d58c032010-07-14 16:34:53 -07002007 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07002008}
Romain Guy9d5316e2010-06-24 19:30:36 -07002009
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002010void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002011 if (!texture) return;
2012 const AutoTexture autoCleanup(texture);
2013
2014 const float x = left + texture->left - texture->offset;
2015 const float y = top + texture->top - texture->offset;
2016
2017 drawPathTexture(texture, x, y, paint);
2018}
2019
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002020void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2021 float rx, float ry, SkPaint* paint) {
2022 if (mSnapshot->isIgnored()) return;
2023
Romain Guya1d3c912011-12-13 14:55:06 -08002024 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002025 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2026 right - left, bottom - top, rx, ry, paint);
2027 drawShape(left, top, texture, paint);
2028}
2029
Romain Guy01d58e42011-01-19 21:54:02 -08002030void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2031 if (mSnapshot->isIgnored()) return;
2032
Romain Guya1d3c912011-12-13 14:55:06 -08002033 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002034 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002035 drawShape(x - radius, y - radius, texture, paint);
2036}
Romain Guy01d58e42011-01-19 21:54:02 -08002037
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002038void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2039 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002040
Romain Guya1d3c912011-12-13 14:55:06 -08002041 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002042 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2043 drawShape(left, top, texture, paint);
2044}
2045
Romain Guy8b2f5262011-01-23 16:15:02 -08002046void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2047 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2048 if (mSnapshot->isIgnored()) return;
2049
2050 if (fabs(sweepAngle) >= 360.0f) {
2051 drawOval(left, top, right, bottom, paint);
2052 return;
2053 }
2054
Romain Guya1d3c912011-12-13 14:55:06 -08002055 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002056 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2057 startAngle, sweepAngle, useCenter, paint);
2058 drawShape(left, top, texture, paint);
2059}
2060
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002061void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2062 SkPaint* paint) {
2063 if (mSnapshot->isIgnored()) return;
2064
Romain Guya1d3c912011-12-13 14:55:06 -08002065 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002066 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2067 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002068}
2069
Chet Haase5c13d892010-10-08 08:37:55 -07002070void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002071 if (p->getStyle() != SkPaint::kFill_Style) {
2072 drawRectAsShape(left, top, right, bottom, p);
2073 return;
2074 }
2075
Romain Guy6926c722010-07-12 20:20:03 -07002076 if (quickReject(left, top, right, bottom)) {
2077 return;
2078 }
2079
Romain Guy026c5e162010-06-28 17:12:22 -07002080 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002081 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002082 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2083 if (!isMode) {
2084 // Assume SRC_OVER
2085 mode = SkXfermode::kSrcOver_Mode;
2086 }
2087 } else {
2088 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002089 }
2090
Romain Guy026c5e162010-06-28 17:12:22 -07002091 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002092 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002093 drawAARect(left, top, right, bottom, color, mode);
2094 } else {
2095 drawColorRect(left, top, right, bottom, color, mode);
2096 }
Romain Guyc7d53492010-06-25 13:41:57 -07002097}
Romain Guy9d5316e2010-06-24 19:30:36 -07002098
Romain Guyeb9a5362012-01-17 17:39:26 -08002099void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2100 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002101 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2102 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002103 return;
2104 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002105
Romain Guy671d6cf2012-01-18 12:39:17 -08002106 // NOTE: Skia does not support perspective transform on drawPosText yet
2107 if (!mSnapshot->transform->isSimple()) {
2108 return;
2109 }
2110
2111 float x = 0.0f;
2112 float y = 0.0f;
2113 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2114 if (pureTranslate) {
2115 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2116 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2117 }
2118
2119 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2120 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2121 paint->getTextSize());
2122
2123 int alpha;
2124 SkXfermode::Mode mode;
2125 getAlphaAndMode(paint, &alpha, &mode);
2126
2127 // Pick the appropriate texture filtering
2128 bool linearFilter = mSnapshot->transform->changesBounds();
2129 if (pureTranslate && !linearFilter) {
2130 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2131 }
2132
2133 mCaches.activeTexture(0);
2134 setupDraw();
2135 setupDrawDirtyRegionsDisabled();
2136 setupDrawWithTexture(true);
2137 setupDrawAlpha8Color(paint->getColor(), alpha);
2138 setupDrawColorFilter();
2139 setupDrawShader();
2140 setupDrawBlending(true, mode);
2141 setupDrawProgram();
2142 setupDrawModelView(x, y, x, y, pureTranslate, true);
2143 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2144 setupDrawPureColorUniforms();
2145 setupDrawColorFilterUniforms();
2146 setupDrawShaderUniforms(pureTranslate);
2147
2148 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2149 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2150
2151#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002152 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002153#else
Romain Guy211370f2012-02-01 16:10:55 -08002154 const bool hasActiveLayer = false;
Romain Guy671d6cf2012-01-18 12:39:17 -08002155#endif
2156
2157 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2158 positions, hasActiveLayer ? &bounds : NULL)) {
2159#if RENDER_LAYERS_AS_REGIONS
2160 if (hasActiveLayer) {
2161 if (!pureTranslate) {
2162 mSnapshot->transform->mapRect(bounds);
2163 }
2164 dirtyLayerUnchecked(bounds, getRegion());
2165 }
2166#endif
2167 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002168}
2169
Romain Guye8e62a42010-07-23 18:55:21 -07002170void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002171 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002172 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2173 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002174 return;
2175 }
2176
Chet Haasea1cff502012-02-21 13:43:44 -08002177 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002178 switch (paint->getTextAlign()) {
2179 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002180 x -= length / 2.0f;
2181 break;
2182 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002183 x -= length;
2184 break;
2185 default:
2186 break;
2187 }
2188
Romain Guycac5fd32011-12-01 20:08:50 -08002189 SkPaint::FontMetrics metrics;
2190 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002191 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Romain Guycac5fd32011-12-01 20:08:50 -08002192 return;
2193 }
2194
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002195 const float oldX = x;
2196 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002197 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guy211370f2012-02-01 16:10:55 -08002198 if (CC_LIKELY(pureTranslate)) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002199 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2200 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2201 }
2202
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002203#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002204 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002205#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002206
2207 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002208 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002209 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002210
Romain Guy86568192010-12-14 15:55:39 -08002211 int alpha;
2212 SkXfermode::Mode mode;
2213 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002214
Romain Guy211370f2012-02-01 16:10:55 -08002215 if (CC_UNLIKELY(mHasShadow)) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002216 mCaches.activeTexture(0);
2217
Romain Guyb45c0c92010-08-26 20:35:23 -07002218 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002219 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2220 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002221 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002222
Romain Guy740bf2b2011-04-26 15:33:10 -07002223 const float sx = oldX - shadow->left + mShadowDx;
2224 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002225
Romain Guy86568192010-12-14 15:55:39 -08002226 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002227 int shadowColor = mShadowColor;
2228 if (mShader) {
2229 shadowColor = 0xffffffff;
2230 }
Romain Guy86568192010-12-14 15:55:39 -08002231
Romain Guy86568192010-12-14 15:55:39 -08002232 setupDraw();
2233 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002234 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2235 setupDrawColorFilter();
2236 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002237 setupDrawBlending(true, mode);
2238 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002239 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002240 setupDrawTexture(shadow->id);
2241 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002242 setupDrawColorFilterUniforms();
2243 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002244 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2245
Romain Guy0a417492010-08-16 20:26:20 -07002246 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002247 }
2248
Romain Guy6620c6d2010-12-06 18:07:02 -08002249 // Pick the appropriate texture filtering
2250 bool linearFilter = mSnapshot->transform->changesBounds();
2251 if (pureTranslate && !linearFilter) {
2252 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2253 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002254
Romain Guya1d3c912011-12-13 14:55:06 -08002255 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002256 setupDraw();
2257 setupDrawDirtyRegionsDisabled();
2258 setupDrawWithTexture(true);
2259 setupDrawAlpha8Color(paint->getColor(), alpha);
2260 setupDrawColorFilter();
2261 setupDrawShader();
2262 setupDrawBlending(true, mode);
2263 setupDrawProgram();
2264 setupDrawModelView(x, y, x, y, pureTranslate, true);
2265 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2266 setupDrawPureColorUniforms();
2267 setupDrawColorFilterUniforms();
2268 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002269
Romain Guy6620c6d2010-12-06 18:07:02 -08002270 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002271 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2272
2273#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002274 const bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002275#else
Romain Guy211370f2012-02-01 16:10:55 -08002276 const bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002277#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002278
Romain Guy6620c6d2010-12-06 18:07:02 -08002279 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002280 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002281#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002282 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002283 if (!pureTranslate) {
2284 mSnapshot->transform->mapRect(bounds);
2285 }
Romain Guyf219da52011-01-16 12:54:25 -08002286 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002287 }
2288#endif
2289 }
Romain Guy694b5192010-07-21 21:33:20 -07002290
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002291 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002292}
2293
Romain Guy325740f2012-02-24 16:48:34 -08002294void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
2295 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy03d58522012-02-24 17:54:07 -08002296 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2297 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
2298 return;
2299 }
2300
Romain Guy03d58522012-02-24 17:54:07 -08002301 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2302 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2303 paint->getTextSize());
2304
2305 int alpha;
2306 SkXfermode::Mode mode;
2307 getAlphaAndMode(paint, &alpha, &mode);
2308
2309 mCaches.activeTexture(0);
2310 setupDraw();
2311 setupDrawDirtyRegionsDisabled();
2312 setupDrawWithTexture(true);
2313 setupDrawAlpha8Color(paint->getColor(), alpha);
2314 setupDrawColorFilter();
2315 setupDrawShader();
2316 setupDrawBlending(true, mode);
2317 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002318 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy03d58522012-02-24 17:54:07 -08002319 setupDrawTexture(fontRenderer.getTexture(true));
2320 setupDrawPureColorUniforms();
2321 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002322 setupDrawShaderUniforms(false);
Romain Guy03d58522012-02-24 17:54:07 -08002323
Romain Guy97771732012-02-28 18:17:02 -08002324 const Rect* clip = &mSnapshot->getLocalClip();
2325 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 -08002326
Romain Guy97771732012-02-28 18:17:02 -08002327#if RENDER_LAYERS_AS_REGIONS
2328 const bool hasActiveLayer = hasLayer();
2329#else
2330 const bool hasActiveLayer = false;
2331#endif
2332
2333 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2334 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
2335#if RENDER_LAYERS_AS_REGIONS
2336 if (hasActiveLayer) {
2337 mSnapshot->transform->mapRect(bounds);
2338 dirtyLayerUnchecked(bounds, getRegion());
2339 }
2340#endif
2341 }
Romain Guy325740f2012-02-24 16:48:34 -08002342}
2343
Romain Guy7fbcc042010-08-04 15:40:07 -07002344void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002345 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002346
Romain Guya1d3c912011-12-13 14:55:06 -08002347 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002348
Romain Guy33f6beb2012-02-16 19:24:51 -08002349 // TODO: Perform early clip test before we rasterize the path
Romain Guyfb8b7632010-08-23 21:05:08 -07002350 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002351 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002352 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002353
Romain Guy8b55f372010-08-18 17:10:07 -07002354 const float x = texture->left - texture->offset;
2355 const float y = texture->top - texture->offset;
2356
Romain Guy01d58e42011-01-19 21:54:02 -08002357 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002358}
2359
Romain Guyada830f2011-01-13 12:13:20 -08002360void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2361 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002362 return;
2363 }
2364
Romain Guy2bf68f02012-03-02 13:37:47 -08002365 if (layer->deferredUpdateScheduled && layer->renderer && layer->displayList) {
2366 OpenGLRenderer* renderer = layer->renderer;
2367 Rect& dirty = layer->dirtyRect;
2368
2369 interrupt();
2370 renderer->setViewport(layer->layer.getWidth(), layer->layer.getHeight());
2371 renderer->prepareDirty(dirty.left, dirty.top, dirty.right, dirty.bottom, !layer->isBlend());
2372 renderer->drawDisplayList(layer->displayList, layer->getWidth(), layer->getHeight(),
2373 dirty, DisplayList::kReplayFlag_ClipChildren);
2374 renderer->finish();
2375 resume();
2376
2377 dirty.setEmpty();
2378 layer->deferredUpdateScheduled = false;
2379 layer->renderer = NULL;
2380 layer->displayList = NULL;
2381 }
2382
Romain Guya1d3c912011-12-13 14:55:06 -08002383 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002384
2385 int alpha;
2386 SkXfermode::Mode mode;
2387 getAlphaAndMode(paint, &alpha, &mode);
2388
Romain Guy9ace8f52011-07-07 20:50:11 -07002389 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002390
Romain Guyf219da52011-01-16 12:54:25 -08002391#if RENDER_LAYERS_AS_REGIONS
Romain Guy211370f2012-02-01 16:10:55 -08002392 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002393 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002394 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002395 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002396 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002397 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002398
Romain Guyc88e3572011-01-22 00:32:12 -08002399 setupDraw();
2400 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002401 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002402 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002403 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002404 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002405 setupDrawPureColorUniforms();
2406 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002407 setupDrawTexture(layer->getTexture());
Romain Guy211370f2012-02-01 16:10:55 -08002408 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy9ace8f52011-07-07 20:50:11 -07002409 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2410 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2411
Romain Guyd21b6e12011-11-30 20:21:23 -08002412 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002413 setupDrawModelViewTranslate(x, y,
2414 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2415 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002416 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002417 setupDrawModelViewTranslate(x, y,
2418 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2419 }
Romain Guyc88e3572011-01-22 00:32:12 -08002420 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002421
Romain Guyc88e3572011-01-22 00:32:12 -08002422 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2423 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002424
Romain Guyc88e3572011-01-22 00:32:12 -08002425 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002426
2427#if DEBUG_LAYERS_AS_REGIONS
2428 drawRegionRects(layer->region);
2429#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002430 }
Romain Guyf219da52011-01-16 12:54:25 -08002431 }
2432#else
Romain Guyada830f2011-01-13 12:13:20 -08002433 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2434 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002435#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002436}
2437
Romain Guy6926c722010-07-12 20:20:03 -07002438///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002439// Shaders
2440///////////////////////////////////////////////////////////////////////////////
2441
2442void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002443 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002444}
2445
Romain Guy06f96e22010-07-30 19:18:16 -07002446void OpenGLRenderer::setupShader(SkiaShader* shader) {
2447 mShader = shader;
2448 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002449 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002450 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002451}
2452
Romain Guyd27977d2010-07-14 19:18:51 -07002453///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002454// Color filters
2455///////////////////////////////////////////////////////////////////////////////
2456
2457void OpenGLRenderer::resetColorFilter() {
2458 mColorFilter = NULL;
2459}
2460
2461void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2462 mColorFilter = filter;
2463}
2464
2465///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002466// Drop shadow
2467///////////////////////////////////////////////////////////////////////////////
2468
2469void OpenGLRenderer::resetShadow() {
2470 mHasShadow = false;
2471}
2472
2473void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2474 mHasShadow = true;
2475 mShadowRadius = radius;
2476 mShadowDx = dx;
2477 mShadowDy = dy;
2478 mShadowColor = color;
2479}
2480
2481///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002482// Draw filters
2483///////////////////////////////////////////////////////////////////////////////
2484
2485void OpenGLRenderer::resetPaintFilter() {
2486 mHasDrawFilter = false;
2487}
2488
2489void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2490 mHasDrawFilter = true;
2491 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2492 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2493}
2494
2495SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guydd7c8e4c2012-03-01 12:08:38 -08002496 if (CC_LIKELY(!mHasDrawFilter || !paint)) return paint;
Romain Guy5ff9df62012-01-23 17:09:05 -08002497
2498 uint32_t flags = paint->getFlags();
2499
2500 mFilteredPaint = *paint;
2501 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2502
2503 return &mFilteredPaint;
2504}
2505
2506///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002507// Drawing implementation
2508///////////////////////////////////////////////////////////////////////////////
2509
Romain Guy01d58e42011-01-19 21:54:02 -08002510void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2511 float x, float y, SkPaint* paint) {
2512 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2513 return;
2514 }
2515
2516 int alpha;
2517 SkXfermode::Mode mode;
2518 getAlphaAndMode(paint, &alpha, &mode);
2519
2520 setupDraw();
2521 setupDrawWithTexture(true);
2522 setupDrawAlpha8Color(paint->getColor(), alpha);
2523 setupDrawColorFilter();
2524 setupDrawShader();
2525 setupDrawBlending(true, mode);
2526 setupDrawProgram();
2527 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2528 setupDrawTexture(texture->id);
2529 setupDrawPureColorUniforms();
2530 setupDrawColorFilterUniforms();
2531 setupDrawShaderUniforms();
2532 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2533
2534 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2535
2536 finishDrawTexture();
2537}
2538
Romain Guyf607bdc2010-09-10 19:20:06 -07002539// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002540#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2541#define kStdUnderline_Offset (1.0f / 9.0f)
2542#define kStdUnderline_Thickness (1.0f / 18.0f)
2543
2544void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2545 float x, float y, SkPaint* paint) {
2546 // Handle underline and strike-through
2547 uint32_t flags = paint->getFlags();
2548 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002549 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002550 float underlineWidth = length;
2551 // If length is > 0.0f, we already measured the text for the text alignment
2552 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002553 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002554 }
2555
2556 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002557 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002558 case SkPaint::kCenter_Align:
2559 offsetX = underlineWidth * 0.5f;
2560 break;
2561 case SkPaint::kRight_Align:
2562 offsetX = underlineWidth;
2563 break;
2564 default:
2565 break;
2566 }
2567
Romain Guy211370f2012-02-01 16:10:55 -08002568 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002569 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002570 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002571
Romain Guye20ecbd2010-09-22 19:49:04 -07002572 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002573 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002574
Romain Guyf6834472011-01-23 13:32:12 -08002575 int linesCount = 0;
2576 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2577 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2578
2579 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002580 float points[pointsCount];
2581 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002582
2583 if (flags & SkPaint::kUnderlineText_Flag) {
2584 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002585 points[currentPoint++] = left;
2586 points[currentPoint++] = top;
2587 points[currentPoint++] = left + underlineWidth;
2588 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002589 }
2590
2591 if (flags & SkPaint::kStrikeThruText_Flag) {
2592 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002593 points[currentPoint++] = left;
2594 points[currentPoint++] = top;
2595 points[currentPoint++] = left + underlineWidth;
2596 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002597 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002598
Romain Guy726aeba2011-06-01 14:52:00 -07002599 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002600
Romain Guy726aeba2011-06-01 14:52:00 -07002601 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002602 }
2603 }
2604}
2605
Romain Guy026c5e162010-06-28 17:12:22 -07002606void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002607 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002608 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002609 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002610 color |= 0x00ffffff;
2611 }
2612
Romain Guy70ca14e2010-12-13 18:24:33 -08002613 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002614 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002615 setupDrawColor(color);
2616 setupDrawShader();
2617 setupDrawColorFilter();
2618 setupDrawBlending(mode);
2619 setupDrawProgram();
2620 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2621 setupDrawColorUniforms();
2622 setupDrawShaderUniforms(ignoreTransform);
2623 setupDrawColorFilterUniforms();
2624 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002625
Romain Guyc95c8d62010-09-17 15:31:32 -07002626 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2627}
2628
Romain Guy82ba8142010-07-09 13:25:56 -07002629void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002630 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002631 int alpha;
2632 SkXfermode::Mode mode;
2633 getAlphaAndMode(paint, &alpha, &mode);
2634
Romain Guyd21b6e12011-11-30 20:21:23 -08002635 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002636
Romain Guy211370f2012-02-01 16:10:55 -08002637 if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002638 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2639 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2640
Romain Guyd21b6e12011-11-30 20:21:23 -08002641 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002642 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2643 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2644 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2645 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002646 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002647 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2648 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2649 GL_TRIANGLE_STRIP, gMeshCount);
2650 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002651}
2652
Romain Guybd6b79b2010-06-26 00:13:53 -07002653void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002654 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2655 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002656 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002657}
2658
2659void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002660 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002661 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002662 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002663
Romain Guy746b7402010-10-26 16:27:31 -07002664 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002665 setupDrawWithTexture();
2666 setupDrawColor(alpha, alpha, alpha, alpha);
2667 setupDrawColorFilter();
2668 setupDrawBlending(blend, mode, swapSrcDst);
2669 setupDrawProgram();
2670 if (!dirty) {
2671 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002672 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002673 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002674 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002675 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002676 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002677 }
Romain Guy86568192010-12-14 15:55:39 -08002678 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002679 setupDrawColorFilterUniforms();
2680 setupDrawTexture(texture);
2681 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002682
Romain Guy6820ac82010-09-15 18:11:50 -07002683 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002684
2685 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002686}
2687
Romain Guya5aed0d2010-09-09 14:42:43 -07002688void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002689 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002690 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2691 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002692 // These blend modes are not supported by OpenGL directly and have
2693 // to be implemented using shaders. Since the shader will perform
2694 // the blending, turn blending off here
2695 // If the blend mode cannot be implemented using shaders, fall
2696 // back to the default SrcOver blend mode instead
Romain Guy211370f2012-02-01 16:10:55 -08002697 if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2698 if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002699 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002700 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002701
Romain Guy82bc7a72012-01-03 14:13:39 -08002702 if (mCaches.blend) {
2703 glDisable(GL_BLEND);
2704 mCaches.blend = false;
2705 }
2706
2707 return;
2708 } else {
2709 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002710 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002711 }
2712
2713 if (!mCaches.blend) {
2714 glEnable(GL_BLEND);
2715 }
2716
2717 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2718 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2719
2720 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2721 glBlendFunc(sourceMode, destMode);
2722 mCaches.lastSrcMode = sourceMode;
2723 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002724 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002725 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002726 glDisable(GL_BLEND);
2727 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002728 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002729}
2730
Romain Guy889f8d12010-07-29 14:37:42 -07002731bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002732 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002733 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002734 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002735 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002736 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002737 }
Romain Guy6926c722010-07-12 20:20:03 -07002738 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002739}
2740
Romain Guy026c5e162010-06-28 17:12:22 -07002741void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002742 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002743 TextureVertex::setUV(v++, u1, v1);
2744 TextureVertex::setUV(v++, u2, v1);
2745 TextureVertex::setUV(v++, u1, v2);
2746 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002747}
2748
Chet Haase5c13d892010-10-08 08:37:55 -07002749void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002750 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002751 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002752
2753 // Skia draws using the color's alpha channel if < 255
2754 // Otherwise, it uses the paint's alpha
2755 int color = paint->getColor();
2756 *alpha = (color >> 24) & 0xFF;
2757 if (*alpha == 255) {
2758 *alpha = paint->getAlpha();
2759 }
2760 } else {
2761 *mode = SkXfermode::kSrcOver_Mode;
2762 *alpha = 255;
2763 }
Romain Guy026c5e162010-06-28 17:12:22 -07002764}
2765
Romain Guya5aed0d2010-09-09 14:42:43 -07002766SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002767 SkXfermode::Mode resultMode;
2768 if (!SkXfermode::AsMode(mode, &resultMode)) {
2769 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002770 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002771 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002772}
2773
Romain Guy9d5316e2010-06-24 19:30:36 -07002774}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002775}; // namespace android