blob: fcac053be110acd9425912bd02db9894d5c9dcaa [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 Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080034#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080035#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070036
37namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044#define RAD_TO_DEG (180.0f / 3.14159265f)
45#define MIN_ANGLE 0.001f
46
Romain Guydbc26d22010-10-11 17:58:29 -070047// TODO: This should be set in properties
48#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guy889f8d12010-07-29 14:37:42 -0700106static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -0800107 GL_TEXTURE0,
108 GL_TEXTURE1,
109 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700110};
111
Romain Guyf6a11b82010-06-23 17:47:49 -0700112///////////////////////////////////////////////////////////////////////////////
113// Constructors/destructor
114///////////////////////////////////////////////////////////////////////////////
115
Romain Guyfb8b7632010-08-23 21:05:08 -0700116OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700117 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700118 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700119 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700120
Romain Guyac670c02010-07-27 17:39:27 -0700121 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
122
Romain Guyae5575b2010-07-29 18:48:04 -0700123 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700127 // The context has already been destroyed at this point, do not call
128 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guyf6a11b82010-06-23 17:47:49 -0700131///////////////////////////////////////////////////////////////////////////////
132// Setup
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guy85bf02f2010-06-22 13:11:24 -0700135void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700136 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700137
138 mWidth = width;
139 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700140
141 mFirstSnapshot->height = height;
142 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700143
Romain Guy3e263fa2011-12-12 16:47:48 -0800144 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800145 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800146 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy3e263fa2011-12-12 16:47:48 -0800147 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700148}
149
Romain Guy6b7bd242010-10-06 19:49:23 -0700150void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800151 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
152}
153
154void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800155 mCaches.clearGarbage();
156
Romain Guy8aef54f2010-09-01 15:13:49 -0700157 mSnapshot = new Snapshot(mFirstSnapshot,
158 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800159 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700160 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700161
Romain Guy39d252a2011-12-12 18:14:06 -0800162 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800163 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800164
Romain Guy7d7b5492011-01-24 16:33:45 -0800165 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800166 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800167
Romain Guy6b7bd242010-10-06 19:49:23 -0700168 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700169 glClear(GL_COLOR_BUFFER_BIT);
170 }
Romain Guybb9524b2010-06-22 18:56:38 -0700171}
172
Romain Guyb025b9c2010-09-16 14:16:48 -0700173void OpenGLRenderer::finish() {
174#if DEBUG_OPENGL
175 GLenum status = GL_NO_ERROR;
176 while ((status = glGetError()) != GL_NO_ERROR) {
177 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800178 switch (status) {
179 case GL_OUT_OF_MEMORY:
180 LOGE(" OpenGLRenderer is out of memory!");
181 break;
182 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700183 }
184#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800185#if DEBUG_MEMORY_USAGE
186 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800187#else
188 if (mCaches.getDebugLevel() & kDebugMemory) {
189 mCaches.dumpMemoryUsage();
190 }
Romain Guyc15008e2010-11-10 11:59:15 -0800191#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700192}
193
Romain Guy6c319ca2011-01-11 14:29:25 -0800194void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700195 if (mCaches.currentProgram) {
196 if (mCaches.currentProgram->isInUse()) {
197 mCaches.currentProgram->remove();
198 mCaches.currentProgram = NULL;
199 }
200 }
Romain Guy50c0f092010-10-19 11:42:22 -0700201 mCaches.unbindMeshBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800202 mCaches.resetVertexPointers();
Romain Guyda8532c2010-08-31 11:50:35 -0700203}
204
Romain Guy6c319ca2011-01-11 14:29:25 -0800205void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800206 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
207
208 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800209 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
210
Romain Guyda8532c2010-08-31 11:50:35 -0700211 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700212 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700213
Chet Haase08837c22011-11-28 11:53:21 -0800214 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy03750a02010-10-18 14:06:08 -0700215 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700216
Romain Guy50c0f092010-10-19 11:42:22 -0700217 mCaches.blend = true;
218 glEnable(GL_BLEND);
219 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
220 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700221}
222
Romain Guycabfcc12011-03-07 18:06:46 -0800223bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800224 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800225 if (mDirtyClip) {
226 setScissorFromClip();
227 }
Romain Guyd643bb52011-03-01 14:55:21 -0800228
Romain Guy80911b82011-03-16 15:30:12 -0700229 Rect clip(*mSnapshot->clipRect);
230 clip.snapToPixelBoundaries();
231
Romain Guyd643bb52011-03-01 14:55:21 -0800232#if RENDER_LAYERS_AS_REGIONS
233 // Since we don't know what the functor will draw, let's dirty
234 // tne entire clip region
235 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800236 dirtyLayerUnchecked(clip, getRegion());
237 }
238#endif
239
Romain Guy08aa2cb2011-03-17 11:06:57 -0700240 DrawGlInfo info;
241 info.clipLeft = clip.left;
242 info.clipTop = clip.top;
243 info.clipRight = clip.right;
244 info.clipBottom = clip.bottom;
245 info.isLayer = hasLayer();
246 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700247
Romain Guy08aa2cb2011-03-17 11:06:57 -0700248 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800249
250 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700251 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800252 dirty.unionWith(localDirty);
253 }
254
Chet Haasedaf98e92011-01-10 14:10:36 -0800255 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800256 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800257}
258
Romain Guyf6a11b82010-06-23 17:47:49 -0700259///////////////////////////////////////////////////////////////////////////////
260// State management
261///////////////////////////////////////////////////////////////////////////////
262
Romain Guybb9524b2010-06-22 18:56:38 -0700263int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700264 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700265}
266
267int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700268 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700269}
270
271void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700272 if (mSaveCount > 1) {
273 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
277void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700278 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700279
Romain Guy8fb95422010-08-17 18:38:51 -0700280 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700281 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700282 }
Romain Guybb9524b2010-06-22 18:56:38 -0700283}
284
Romain Guy8aef54f2010-09-01 15:13:49 -0700285int OpenGLRenderer::saveSnapshot(int flags) {
286 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700287 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700288}
289
290bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700291 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700292 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700293 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700294
Romain Guybd6b79b2010-06-26 00:13:53 -0700295 sp<Snapshot> current = mSnapshot;
296 sp<Snapshot> previous = mSnapshot->previous;
297
Romain Guyeb993562010-10-05 18:14:38 -0700298 if (restoreOrtho) {
299 Rect& r = previous->viewport;
300 glViewport(r.left, r.top, r.right, r.bottom);
301 mOrthoMatrix.load(current->orthoMatrix);
302 }
303
Romain Guy8b55f372010-08-18 17:10:07 -0700304 mSaveCount--;
305 mSnapshot = previous;
306
Romain Guy2542d192010-08-18 11:47:12 -0700307 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700308 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700309 }
Romain Guy2542d192010-08-18 11:47:12 -0700310
Romain Guy5ec99242010-11-03 16:19:08 -0700311 if (restoreLayer) {
312 composeLayer(current, previous);
313 }
314
Romain Guy2542d192010-08-18 11:47:12 -0700315 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700316}
317
Romain Guyf6a11b82010-06-23 17:47:49 -0700318///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700319// Layers
320///////////////////////////////////////////////////////////////////////////////
321
322int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700323 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700324 const GLuint previousFbo = mSnapshot->fbo;
325 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700326
Romain Guyaf636eb2010-12-09 17:47:21 -0800327 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700328 int alpha = 255;
329 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700330
Romain Guye45362c2010-11-03 19:58:32 -0700331 if (p) {
332 alpha = p->getAlpha();
333 if (!mCaches.extensions.hasFramebufferFetch()) {
334 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
335 if (!isMode) {
336 // Assume SRC_OVER
337 mode = SkXfermode::kSrcOver_Mode;
338 }
339 } else {
340 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700341 }
342 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700343 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700344 }
Romain Guyd55a8612010-06-28 17:42:46 -0700345
Romain Guydbc26d22010-10-11 17:58:29 -0700346 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
347 }
Romain Guyd55a8612010-06-28 17:42:46 -0700348
349 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700350}
351
352int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
353 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700354 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700355 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700356 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 SkPaint paint;
358 paint.setAlpha(alpha);
359 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700360 }
Romain Guyd55a8612010-06-28 17:42:46 -0700361}
Romain Guybd6b79b2010-06-26 00:13:53 -0700362
Romain Guy1c740bc2010-09-13 18:00:09 -0700363/**
364 * Layers are viewed by Skia are slightly different than layers in image editing
365 * programs (for instance.) When a layer is created, previously created layers
366 * and the frame buffer still receive every drawing command. For instance, if a
367 * layer is created and a shape intersecting the bounds of the layers and the
368 * framebuffer is draw, the shape will be drawn on both (unless the layer was
369 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
370 *
371 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
372 * texture. Unfortunately, this is inefficient as it requires every primitive to
373 * be drawn n + 1 times, where n is the number of active layers. In practice this
374 * means, for every primitive:
375 * - Switch active frame buffer
376 * - Change viewport, clip and projection matrix
377 * - Issue the drawing
378 *
379 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700380 * To avoid this, layers are implemented in a different way here, at least in the
381 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
382 * is set. When this flag is set we can redirect all drawing operations into a
383 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700384 *
385 * This implementation relies on the frame buffer being at least RGBA 8888. When
386 * a layer is created, only a texture is created, not an FBO. The content of the
387 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700388 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700389 * buffer and drawing continues as normal. This technique therefore treats the
390 * frame buffer as a scratch buffer for the layers.
391 *
392 * To compose the layers back onto the frame buffer, each layer texture
393 * (containing the original frame buffer data) is drawn as a simple quad over
394 * the frame buffer. The trick is that the quad is set as the composition
395 * destination in the blending equation, and the frame buffer becomes the source
396 * of the composition.
397 *
398 * Drawing layers with an alpha value requires an extra step before composition.
399 * An empty quad is drawn over the layer's region in the frame buffer. This quad
400 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
401 * quad is used to multiply the colors in the frame buffer. This is achieved by
402 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
403 * GL_ZERO, GL_SRC_ALPHA.
404 *
405 * Because glCopyTexImage2D() can be slow, an alternative implementation might
406 * be use to draw a single clipped layer. The implementation described above
407 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700408 *
409 * (1) The frame buffer is actually not cleared right away. To allow the GPU
410 * to potentially optimize series of calls to glCopyTexImage2D, the frame
411 * buffer is left untouched until the first drawing operation. Only when
412 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700413 */
Romain Guyd55a8612010-06-28 17:42:46 -0700414bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700415 float right, float bottom, int alpha, SkXfermode::Mode mode,
416 int flags, GLuint previousFbo) {
417 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700418 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700419
Romain Guyeb993562010-10-05 18:14:38 -0700420 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
421
Romain Guyf607bdc2010-09-10 19:20:06 -0700422 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700423 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700424 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700425 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700426
Romain Guyeb993562010-10-05 18:14:38 -0700427 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700428 if (bounds.intersect(*snapshot->clipRect)) {
429 // We cannot work with sub-pixels in this case
430 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700431
Romain Guyad37cd32011-03-15 11:12:25 -0700432 // When the layer is not an FBO, we may use glCopyTexImage so we
433 // need to make sure the layer does not extend outside the bounds
434 // of the framebuffer
435 if (!bounds.intersect(snapshot->previous->viewport)) {
436 bounds.setEmpty();
437 }
438 } else {
439 bounds.setEmpty();
440 }
Romain Guyeb993562010-10-05 18:14:38 -0700441 }
Romain Guybf434112010-09-16 14:40:17 -0700442
Romain Guy746b7402010-10-26 16:27:31 -0700443 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
444 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800445 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700446 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700447 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700448 }
449
450 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800451 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700452 return false;
453 }
Romain Guyf18fd992010-07-08 11:45:51 -0700454
Romain Guy5b3b3522010-10-27 18:57:51 -0700455 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700456 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700457 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700458 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700459 }
460
Romain Guy9ace8f52011-07-07 20:50:11 -0700461 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700462 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700463 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
464 bounds.getWidth() / float(layer->getWidth()), 0.0f);
465 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700466
Romain Guy8fb95422010-08-17 18:38:51 -0700467 // Save the layer in the snapshot
468 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700469 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700470
Romain Guyeb993562010-10-05 18:14:38 -0700471 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700472 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700473 } else {
474 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700475 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800476 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700477 if (layer->isEmpty()) {
478 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
479 bounds.left, snapshot->height - bounds.bottom,
480 layer->getWidth(), layer->getHeight(), 0);
481 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800482 } else {
483 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
484 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
485 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700486
Romain Guy54be1cd2011-06-13 19:04:27 -0700487 // Enqueue the buffer coordinates to clear the corresponding region later
488 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700489 }
Romain Guyeb993562010-10-05 18:14:38 -0700490 }
Romain Guyf86ef572010-07-01 11:05:42 -0700491
Romain Guyd55a8612010-06-28 17:42:46 -0700492 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700493}
494
Romain Guy5b3b3522010-10-27 18:57:51 -0700495bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
496 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700497 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700498
499#if RENDER_LAYERS_AS_REGIONS
500 snapshot->region = &snapshot->layer->region;
501 snapshot->flags |= Snapshot::kFlagFboTarget;
502#endif
503
504 Rect clip(bounds);
505 snapshot->transform->mapRect(clip);
506 clip.intersect(*snapshot->clipRect);
507 clip.snapToPixelBoundaries();
508 clip.intersect(snapshot->previous->viewport);
509
510 mat4 inverse;
511 inverse.loadInverse(*mSnapshot->transform);
512
513 inverse.mapRect(clip);
514 clip.snapToPixelBoundaries();
515 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700516 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700517
518 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700519 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700520 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700521 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
522 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
523 snapshot->height = bounds.getHeight();
524 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
525 snapshot->orthoMatrix.load(mOrthoMatrix);
526
527 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700528 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
529 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700530
531 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700532 if (layer->isEmpty()) {
533 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
534 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700535 }
536
537 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700538 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700539
540#if DEBUG_LAYERS_AS_REGIONS
541 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
542 if (status != GL_FRAMEBUFFER_COMPLETE) {
543 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
544
545 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700546 layer->deleteTexture();
547 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700548
549 delete layer;
550
551 return false;
552 }
553#endif
554
555 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
556 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
557 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700558 glClear(GL_COLOR_BUFFER_BIT);
559
560 dirtyClip();
561
562 // Change the ortho projection
563 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
564 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
565
566 return true;
567}
568
Romain Guy1c740bc2010-09-13 18:00:09 -0700569/**
570 * Read the documentation of createLayer() before doing anything in this method.
571 */
Romain Guy1d83e192010-08-17 11:37:00 -0700572void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
573 if (!current->layer) {
574 LOGE("Attempting to compose a layer that does not exist");
575 return;
576 }
577
Romain Guy5b3b3522010-10-27 18:57:51 -0700578 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700579
580 if (fboLayer) {
581 // Unbind current FBO and restore previous one
582 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
583 }
584
Romain Guy1d83e192010-08-17 11:37:00 -0700585 Layer* layer = current->layer;
586 const Rect& rect = layer->layer;
587
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700589 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700591 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700593 }
594
Romain Guy03750a02010-10-18 14:06:08 -0700595 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700596
Romain Guy746b7402010-10-26 16:27:31 -0700597 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700598
Romain Guy5b3b3522010-10-27 18:57:51 -0700599 // When the layer is stored in an FBO, we can save a bit of fillrate by
600 // drawing only the dirty region
601 if (fboLayer) {
602 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700603 if (layer->getColorFilter()) {
604 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800605 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700606 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800608 resetColorFilter();
609 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700610 } else if (!rect.isEmpty()) {
611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
612 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700613 }
Romain Guy8b55f372010-08-18 17:10:07 -0700614
Romain Guyeb993562010-10-05 18:14:38 -0700615 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800616 // Note: No need to use glDiscardFramebufferEXT() since we never
617 // create/compose layers that are not on screen with this
618 // code path
619 // See LayerRenderer::destroyLayer(Layer*)
620
Romain Guyeb993562010-10-05 18:14:38 -0700621 // Detach the texture from the FBO
622 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
623 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
624 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
625
626 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
627 mCaches.fboCache.put(current->fbo);
628 }
629
Romain Guy746b7402010-10-26 16:27:31 -0700630 dirtyClip();
631
Romain Guyeb993562010-10-05 18:14:38 -0700632 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700633 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700634 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700635 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700636 delete layer;
637 }
638}
639
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700641 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700642
Romain Guy302a9df2011-08-16 13:55:02 -0700643 mat4& transform = layer->getTransform();
644 if (!transform.isIdentity()) {
645 save(0);
646 mSnapshot->transform->multiply(transform);
647 }
648
Romain Guyaa6c24c2011-04-28 18:40:04 -0700649 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700650 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700651 setupDrawWithTexture();
652 } else {
653 setupDrawWithExternalTexture();
654 }
655 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700656 setupDrawColor(alpha, alpha, alpha, alpha);
657 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700658 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700659 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700660 setupDrawPureColorUniforms();
661 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700662 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
663 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700664 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700666 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700667 if (mSnapshot->transform->isPureTranslate() &&
668 layer->getWidth() == (uint32_t) rect.getWidth() &&
669 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700670 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
671 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
672
Romain Guyd21b6e12011-11-30 20:21:23 -0800673 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
675 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800676 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
678 }
679 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700680 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
681
682 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
683
684 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700685
686 if (!transform.isIdentity()) {
687 restore();
688 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700689}
690
Romain Guy5b3b3522010-10-27 18:57:51 -0700691void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700693 const Rect& texCoords = layer->texCoords;
694 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
695 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700696
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 float x = rect.left;
698 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700699 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700700 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700701 layer->getHeight() == (uint32_t) rect.getHeight();
702
703 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700704 // When we're swapping, the layer is already in screen coordinates
705 if (!swap) {
706 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
707 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
708 }
709
Romain Guyd21b6e12011-11-30 20:21:23 -0800710 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800712 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700713 }
714
715 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
716 layer->getTexture(), layer->getAlpha() / 255.0f,
717 layer->getMode(), layer->isBlend(),
718 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
719 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700720
Romain Guyaa6c24c2011-04-28 18:40:04 -0700721 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
722 } else {
723 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
724 drawTextureLayer(layer, rect);
725 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
726 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700727}
728
729void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
730#if RENDER_LAYERS_AS_REGIONS
731 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700732 layer->setRegionAsRect();
733
Romain Guy40667672011-03-18 14:34:03 -0700734 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700735
Romain Guy5b3b3522010-10-27 18:57:51 -0700736 layer->region.clear();
737 return;
738 }
739
Romain Guy8a3957d2011-09-07 17:55:15 -0700740 // TODO: See LayerRenderer.cpp::generateMesh() for important
741 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700742 if (!layer->region.isEmpty()) {
743 size_t count;
744 const android::Rect* rects = layer->region.getArray(&count);
745
Romain Guy9ace8f52011-07-07 20:50:11 -0700746 const float alpha = layer->getAlpha() / 255.0f;
747 const float texX = 1.0f / float(layer->getWidth());
748 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800749 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700750
751 TextureVertex* mesh = mCaches.getRegionMesh();
752 GLsizei numQuads = 0;
753
Romain Guy7230a742011-01-10 22:26:16 -0800754 setupDraw();
755 setupDrawWithTexture();
756 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800757 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800759 setupDrawProgram();
760 setupDrawDirtyRegionsDisabled();
761 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800762 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 setupDrawTexture(layer->getTexture());
764 if (mSnapshot->transform->isPureTranslate()) {
765 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
766 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
767
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
770 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800771 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
773 }
Romain Guy7230a742011-01-10 22:26:16 -0800774 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700775
776 for (size_t i = 0; i < count; i++) {
777 const android::Rect* r = &rects[i];
778
779 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800780 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700781 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800782 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700783
784 // TODO: Reject quads outside of the clip
785 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
786 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
787 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
788 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
789
790 numQuads++;
791
792 if (numQuads >= REGION_MESH_QUAD_COUNT) {
793 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
794 numQuads = 0;
795 mesh = mCaches.getRegionMesh();
796 }
797 }
798
799 if (numQuads > 0) {
800 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
801 }
802
803 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800804 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700805
806#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800807 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700808#endif
809
810 layer->region.clear();
811 }
812#else
813 composeLayerRect(layer, rect);
814#endif
815}
816
Romain Guy3a3133d2011-02-01 22:59:58 -0800817void OpenGLRenderer::drawRegionRects(const Region& region) {
818#if DEBUG_LAYERS_AS_REGIONS
819 size_t count;
820 const android::Rect* rects = region.getArray(&count);
821
822 uint32_t colors[] = {
823 0x7fff0000, 0x7f00ff00,
824 0x7f0000ff, 0x7fff00ff,
825 };
826
827 int offset = 0;
828 int32_t top = rects[0].top;
829
830 for (size_t i = 0; i < count; i++) {
831 if (top != rects[i].top) {
832 offset ^= 0x2;
833 top = rects[i].top;
834 }
835
836 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
837 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
838 SkXfermode::kSrcOver_Mode);
839 }
840#endif
841}
842
Romain Guy5b3b3522010-10-27 18:57:51 -0700843void OpenGLRenderer::dirtyLayer(const float left, const float top,
844 const float right, const float bottom, const mat4 transform) {
845#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800846 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 Rect bounds(left, top, right, bottom);
848 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800849 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 }
851#endif
852}
853
854void OpenGLRenderer::dirtyLayer(const float left, const float top,
855 const float right, const float bottom) {
856#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800857 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700858 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800859 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800860 }
861#endif
862}
863
864void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
865#if RENDER_LAYERS_AS_REGIONS
866 if (bounds.intersect(*mSnapshot->clipRect)) {
867 bounds.snapToPixelBoundaries();
868 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
869 if (!dirty.isEmpty()) {
870 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700871 }
872 }
873#endif
874}
875
Romain Guy54be1cd2011-06-13 19:04:27 -0700876void OpenGLRenderer::clearLayerRegions() {
877 const size_t count = mLayers.size();
878 if (count == 0) return;
879
880 if (!mSnapshot->isIgnored()) {
881 // Doing several glScissor/glClear here can negatively impact
882 // GPUs with a tiler architecture, instead we draw quads with
883 // the Clear blending mode
884
885 // The list contains bounds that have already been clipped
886 // against their initial clip rect, and the current clip
887 // is likely different so we need to disable clipping here
888 glDisable(GL_SCISSOR_TEST);
889
890 Vertex mesh[count * 6];
891 Vertex* vertex = mesh;
892
893 for (uint32_t i = 0; i < count; i++) {
894 Rect* bounds = mLayers.itemAt(i);
895
896 Vertex::set(vertex++, bounds->left, bounds->bottom);
897 Vertex::set(vertex++, bounds->left, bounds->top);
898 Vertex::set(vertex++, bounds->right, bounds->top);
899 Vertex::set(vertex++, bounds->left, bounds->bottom);
900 Vertex::set(vertex++, bounds->right, bounds->top);
901 Vertex::set(vertex++, bounds->right, bounds->bottom);
902
903 delete bounds;
904 }
905
906 setupDraw(false);
907 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
908 setupDrawBlending(true, SkXfermode::kClear_Mode);
909 setupDrawProgram();
910 setupDrawPureColorUniforms();
911 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800912 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700913
Romain Guy54be1cd2011-06-13 19:04:27 -0700914 glDrawArrays(GL_TRIANGLES, 0, count * 6);
915
916 glEnable(GL_SCISSOR_TEST);
917 } else {
918 for (uint32_t i = 0; i < count; i++) {
919 delete mLayers.itemAt(i);
920 }
921 }
922
923 mLayers.clear();
924}
925
Romain Guybd6b79b2010-06-26 00:13:53 -0700926///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700927// Transforms
928///////////////////////////////////////////////////////////////////////////////
929
930void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700931 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700932}
933
934void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700935 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700936}
937
938void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700939 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700940}
941
Romain Guy807daf72011-01-18 11:19:19 -0800942void OpenGLRenderer::skew(float sx, float sy) {
943 mSnapshot->transform->skew(sx, sy);
944}
945
Romain Guyf6a11b82010-06-23 17:47:49 -0700946void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700947 if (matrix) {
948 mSnapshot->transform->load(*matrix);
949 } else {
950 mSnapshot->transform->loadIdentity();
951 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700952}
953
954void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700955 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700956}
957
958void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700959 SkMatrix transform;
960 mSnapshot->transform->copyTo(transform);
961 transform.preConcat(*matrix);
962 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700963}
964
965///////////////////////////////////////////////////////////////////////////////
966// Clipping
967///////////////////////////////////////////////////////////////////////////////
968
Romain Guybb9524b2010-06-22 18:56:38 -0700969void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700970 Rect clip(*mSnapshot->clipRect);
971 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700972 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700973 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700974}
975
976const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700977 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700978}
979
Romain Guyc7d53492010-06-25 13:41:57 -0700980bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800981 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700982 return true;
983 }
984
Romain Guy1d83e192010-08-17 11:37:00 -0700985 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700986 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700987 r.snapToPixelBoundaries();
988
989 Rect clipRect(*mSnapshot->clipRect);
990 clipRect.snapToPixelBoundaries();
991
992 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700993}
994
Romain Guy079ba2c2010-07-16 14:12:24 -0700995bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
996 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700997 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700998 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700999 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001000 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001001}
1002
Romain Guyf6a11b82010-06-23 17:47:49 -07001003///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001004// Drawing commands
1005///////////////////////////////////////////////////////////////////////////////
1006
Romain Guy54be1cd2011-06-13 19:04:27 -07001007void OpenGLRenderer::setupDraw(bool clear) {
1008 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001009 if (mDirtyClip) {
1010 setScissorFromClip();
1011 }
1012 mDescription.reset();
1013 mSetShaderColor = false;
1014 mColorSet = false;
1015 mColorA = mColorR = mColorG = mColorB = 0.0f;
1016 mTextureUnit = 0;
1017 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001018 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001019}
1020
1021void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1022 mDescription.hasTexture = true;
1023 mDescription.hasAlpha8Texture = isAlpha8;
1024}
1025
Romain Guyaa6c24c2011-04-28 18:40:04 -07001026void OpenGLRenderer::setupDrawWithExternalTexture() {
1027 mDescription.hasExternalTexture = true;
1028}
1029
Chet Haase5b0200b2011-04-13 17:58:08 -07001030void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001031 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001032}
1033
Romain Guyed6fcb02011-03-21 13:11:28 -07001034void OpenGLRenderer::setupDrawPoint(float pointSize) {
1035 mDescription.isPoint = true;
1036 mDescription.pointSize = pointSize;
1037}
1038
Romain Guy70ca14e2010-12-13 18:24:33 -08001039void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001040 setupDrawColor(color, (color >> 24) & 0xFF);
1041}
1042
1043void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1044 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001045 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1046 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001047 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001048 mColorR = a * ((color >> 16) & 0xFF);
1049 mColorG = a * ((color >> 8) & 0xFF);
1050 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001051 mColorSet = true;
1052 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1053}
1054
Romain Guy86568192010-12-14 15:55:39 -08001055void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1056 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001057 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1058 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001059 const float a = mColorA / 255.0f;
1060 mColorR = a * ((color >> 16) & 0xFF);
1061 mColorG = a * ((color >> 8) & 0xFF);
1062 mColorB = a * ((color ) & 0xFF);
1063 mColorSet = true;
1064 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1065}
1066
Romain Guy70ca14e2010-12-13 18:24:33 -08001067void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1068 mColorA = a;
1069 mColorR = r;
1070 mColorG = g;
1071 mColorB = b;
1072 mColorSet = true;
1073 mSetShaderColor = mDescription.setColor(r, g, b, a);
1074}
1075
Romain Guy86568192010-12-14 15:55:39 -08001076void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1077 mColorA = a;
1078 mColorR = r;
1079 mColorG = g;
1080 mColorB = b;
1081 mColorSet = true;
1082 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1083}
1084
Romain Guy70ca14e2010-12-13 18:24:33 -08001085void OpenGLRenderer::setupDrawShader() {
1086 if (mShader) {
1087 mShader->describe(mDescription, mCaches.extensions);
1088 }
1089}
1090
1091void OpenGLRenderer::setupDrawColorFilter() {
1092 if (mColorFilter) {
1093 mColorFilter->describe(mDescription, mCaches.extensions);
1094 }
1095}
1096
Romain Guyf09ef512011-05-27 11:43:46 -07001097void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1098 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1099 mColorA = 1.0f;
1100 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001101 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001102 }
1103}
1104
Romain Guy70ca14e2010-12-13 18:24:33 -08001105void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001106 // When the blending mode is kClear_Mode, we need to use a modulate color
1107 // argb=1,0,0,0
1108 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001109 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1110 mDescription, swapSrcDst);
1111}
1112
1113void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001114 // When the blending mode is kClear_Mode, we need to use a modulate color
1115 // argb=1,0,0,0
1116 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001117 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1118 mDescription, swapSrcDst);
1119}
1120
1121void OpenGLRenderer::setupDrawProgram() {
1122 useProgram(mCaches.programCache.get(mDescription));
1123}
1124
1125void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1126 mTrackDirtyRegions = false;
1127}
1128
1129void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1130 bool ignoreTransform) {
1131 mModelView.loadTranslate(left, top, 0.0f);
1132 if (!ignoreTransform) {
1133 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1134 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1135 } else {
1136 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1137 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1138 }
1139}
1140
Chet Haase8a5cc922011-04-26 07:28:09 -07001141void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1142 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001143}
1144
Romain Guy70ca14e2010-12-13 18:24:33 -08001145void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1146 bool ignoreTransform, bool ignoreModelView) {
1147 if (!ignoreModelView) {
1148 mModelView.loadTranslate(left, top, 0.0f);
1149 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001150 } else {
1151 mModelView.loadIdentity();
1152 }
Romain Guy86568192010-12-14 15:55:39 -08001153 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1154 if (!ignoreTransform) {
1155 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1156 if (mTrackDirtyRegions && dirty) {
1157 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1158 }
1159 } else {
1160 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1161 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1162 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001163}
1164
Romain Guyed6fcb02011-03-21 13:11:28 -07001165void OpenGLRenderer::setupDrawPointUniforms() {
1166 int slot = mCaches.currentProgram->getUniform("pointSize");
1167 glUniform1f(slot, mDescription.pointSize);
1168}
1169
Romain Guy70ca14e2010-12-13 18:24:33 -08001170void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001171 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001172 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1173 }
1174}
1175
Romain Guy86568192010-12-14 15:55:39 -08001176void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001177 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001178 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001179 }
1180}
1181
Romain Guy70ca14e2010-12-13 18:24:33 -08001182void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1183 if (mShader) {
1184 if (ignoreTransform) {
1185 mModelView.loadInverse(*mSnapshot->transform);
1186 }
1187 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1188 }
1189}
1190
Romain Guy8d0d4782010-12-14 20:13:35 -08001191void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1192 if (mShader) {
1193 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1194 }
1195}
1196
Romain Guy70ca14e2010-12-13 18:24:33 -08001197void OpenGLRenderer::setupDrawColorFilterUniforms() {
1198 if (mColorFilter) {
1199 mColorFilter->setupProgram(mCaches.currentProgram);
1200 }
1201}
1202
1203void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001204 bool force = mCaches.bindMeshBuffer();
1205 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy70ca14e2010-12-13 18:24:33 -08001206}
1207
1208void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1209 bindTexture(texture);
1210 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1211
Romain Guyf3a910b42011-12-12 20:35:21 -08001212 mTexCoordsSlot = mCaches.currentProgram->texCoords;
Romain Guy70ca14e2010-12-13 18:24:33 -08001213 glEnableVertexAttribArray(mTexCoordsSlot);
1214}
1215
Romain Guyaa6c24c2011-04-28 18:40:04 -07001216void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1217 bindExternalTexture(texture);
1218 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1219
Romain Guyf3a910b42011-12-12 20:35:21 -08001220 mTexCoordsSlot = mCaches.currentProgram->texCoords;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001221 glEnableVertexAttribArray(mTexCoordsSlot);
1222}
1223
Romain Guy8f0095c2011-05-02 17:24:22 -07001224void OpenGLRenderer::setupDrawTextureTransform() {
1225 mDescription.hasTextureTransform = true;
1226}
1227
1228void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001229 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1230 GL_FALSE, &transform.data[0]);
1231}
1232
Romain Guy70ca14e2010-12-13 18:24:33 -08001233void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001234 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001235 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001236 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001237 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001238 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001239 }
Romain Guyd71dd362011-12-12 19:03:35 -08001240
Romain Guyf3a910b42011-12-12 20:35:21 -08001241 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
David Licf289572011-02-25 12:05:44 -08001242 if (mTexCoordsSlot >= 0) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001243 mCaches.bindTexCoordsVertexPointer(force, mTexCoordsSlot, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001244 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001245}
1246
Chet Haase5b0200b2011-04-13 17:58:08 -07001247void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001248 bool force = mCaches.unbindMeshBuffer();
1249 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1250 vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001251}
1252
1253/**
1254 * 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 -07001255 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1256 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1257 * attributes (one per vertex) are values from zero to one that tells the fragment
1258 * shader where the fragment is in relation to the line width/length overall; these values are
1259 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1260 * region of the line.
1261 * Note that we only pass down the width values in this setup function. The length coordinates
1262 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001263 */
Chet Haase99585ad2011-05-02 15:00:16 -07001264void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001265 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001266 bool force = mCaches.unbindMeshBuffer();
1267 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1268 vertices, gAAVertexStride);
1269 mCaches.resetTexCoordsVertexPointer();
Romain Guyd71dd362011-12-12 19:03:35 -08001270
Chet Haase99585ad2011-05-02 15:00:16 -07001271 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1272 glEnableVertexAttribArray(widthSlot);
1273 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001274
Chet Haase99585ad2011-05-02 15:00:16 -07001275 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1276 glEnableVertexAttribArray(lengthSlot);
1277 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001278
Chet Haase5b0200b2011-04-13 17:58:08 -07001279 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001280 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001281
Chet Haase99585ad2011-05-02 15:00:16 -07001282 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001283 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001284 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001285}
1286
Romain Guy70ca14e2010-12-13 18:24:33 -08001287void OpenGLRenderer::finishDrawTexture() {
1288 glDisableVertexAttribArray(mTexCoordsSlot);
1289}
1290
1291///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001292// Drawing
1293///////////////////////////////////////////////////////////////////////////////
1294
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001295bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1296 Rect& dirty, uint32_t level) {
1297 if (quickReject(0.0f, 0.0f, width, height)) {
1298 return false;
1299 }
1300
Romain Guy0fe478e2010-11-08 12:08:41 -08001301 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1302 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001303 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001304 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001305 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001306
Chet Haasedaf98e92011-01-10 14:10:36 -08001307 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001308}
1309
Chet Haaseed30fd82011-04-22 16:18:45 -07001310void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1311 if (displayList) {
1312 displayList->output(*this, level);
1313 }
1314}
1315
Romain Guya168d732011-03-18 16:50:13 -07001316void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1317 int alpha;
1318 SkXfermode::Mode mode;
1319 getAlphaAndMode(paint, &alpha, &mode);
1320
Romain Guya168d732011-03-18 16:50:13 -07001321 float x = left;
1322 float y = top;
1323
Romain Guye3c26852011-07-25 16:36:01 -07001324 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001325 bool ignoreTransform = false;
1326 if (mSnapshot->transform->isPureTranslate()) {
1327 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1328 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1329 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001330 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001331 } else {
1332 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001333 }
1334
1335 setupDraw();
1336 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001337 if (paint) {
1338 setupDrawAlpha8Color(paint->getColor(), alpha);
1339 }
Romain Guya168d732011-03-18 16:50:13 -07001340 setupDrawColorFilter();
1341 setupDrawShader();
1342 setupDrawBlending(true, mode);
1343 setupDrawProgram();
1344 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001345
Romain Guya168d732011-03-18 16:50:13 -07001346 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001347 texture->setWrap(GL_CLAMP_TO_EDGE);
1348 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001349
Romain Guya168d732011-03-18 16:50:13 -07001350 setupDrawPureColorUniforms();
1351 setupDrawColorFilterUniforms();
1352 setupDrawShaderUniforms();
1353 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1354
1355 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1356
1357 finishDrawTexture();
1358}
1359
Chet Haase5c13d892010-10-08 08:37:55 -07001360void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001361 const float right = left + bitmap->width();
1362 const float bottom = top + bitmap->height();
1363
1364 if (quickReject(left, top, right, bottom)) {
1365 return;
1366 }
1367
Romain Guy86568192010-12-14 15:55:39 -08001368 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001369 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001370 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001371 const AutoTexture autoCleanup(texture);
1372
Romain Guya168d732011-03-18 16:50:13 -07001373 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1374 drawAlphaBitmap(texture, left, top, paint);
1375 } else {
1376 drawTextureRect(left, top, right, bottom, texture, paint);
1377 }
Romain Guyce0537b2010-06-29 21:05:21 -07001378}
1379
Chet Haase5c13d892010-10-08 08:37:55 -07001380void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001381 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1382 const mat4 transform(*matrix);
1383 transform.mapRect(r);
1384
Romain Guy6926c722010-07-12 20:20:03 -07001385 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1386 return;
1387 }
1388
Romain Guy86568192010-12-14 15:55:39 -08001389 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001390 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001391 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001392 const AutoTexture autoCleanup(texture);
1393
Romain Guy5b3b3522010-10-27 18:57:51 -07001394 // This could be done in a cheaper way, all we need is pass the matrix
1395 // to the vertex shader. The save/restore is a bit overkill.
1396 save(SkCanvas::kMatrix_SaveFlag);
1397 concatMatrix(matrix);
1398 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1399 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001400}
1401
Romain Guy5a7b4662011-01-20 19:09:30 -08001402void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1403 float* vertices, int* colors, SkPaint* paint) {
1404 // TODO: Do a quickReject
1405 if (!vertices || mSnapshot->isIgnored()) {
1406 return;
1407 }
1408
1409 glActiveTexture(gTextureUnits[0]);
1410 Texture* texture = mCaches.textureCache.get(bitmap);
1411 if (!texture) return;
1412 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001413
Romain Guyd21b6e12011-11-30 20:21:23 -08001414 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1415 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001416
1417 int alpha;
1418 SkXfermode::Mode mode;
1419 getAlphaAndMode(paint, &alpha, &mode);
1420
Romain Guy5a7b4662011-01-20 19:09:30 -08001421 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001422
Romain Guyb18d2d02011-02-10 15:52:54 -08001423 float left = FLT_MAX;
1424 float top = FLT_MAX;
1425 float right = FLT_MIN;
1426 float bottom = FLT_MIN;
1427
1428#if RENDER_LAYERS_AS_REGIONS
1429 bool hasActiveLayer = hasLayer();
1430#else
1431 bool hasActiveLayer = false;
1432#endif
1433
Romain Guya566b7c2011-01-23 16:36:11 -08001434 // TODO: Support the colors array
1435 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001436 TextureVertex* vertex = mesh;
1437 for (int32_t y = 0; y < meshHeight; y++) {
1438 for (int32_t x = 0; x < meshWidth; x++) {
1439 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1440
1441 float u1 = float(x) / meshWidth;
1442 float u2 = float(x + 1) / meshWidth;
1443 float v1 = float(y) / meshHeight;
1444 float v2 = float(y + 1) / meshHeight;
1445
1446 int ax = i + (meshWidth + 1) * 2;
1447 int ay = ax + 1;
1448 int bx = i;
1449 int by = bx + 1;
1450 int cx = i + 2;
1451 int cy = cx + 1;
1452 int dx = i + (meshWidth + 1) * 2 + 2;
1453 int dy = dx + 1;
1454
1455 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1456 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1457 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1458
1459 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1460 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1461 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001462
1463#if RENDER_LAYERS_AS_REGIONS
1464 if (hasActiveLayer) {
1465 // TODO: This could be optimized to avoid unnecessary ops
1466 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1467 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1468 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1469 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1470 }
1471#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001472 }
1473 }
1474
Romain Guyb18d2d02011-02-10 15:52:54 -08001475#if RENDER_LAYERS_AS_REGIONS
1476 if (hasActiveLayer) {
1477 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1478 }
1479#endif
1480
Romain Guy5a7b4662011-01-20 19:09:30 -08001481 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1482 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001483 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001484}
1485
Romain Guy8ba548f2010-06-30 19:21:21 -07001486void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1487 float srcLeft, float srcTop, float srcRight, float srcBottom,
1488 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001489 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001490 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1491 return;
1492 }
1493
Romain Guy746b7402010-10-26 16:27:31 -07001494 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001495 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001496 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001497 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001498
Romain Guy8ba548f2010-06-30 19:21:21 -07001499 const float width = texture->width;
1500 const float height = texture->height;
1501
Romain Guy68169722011-08-22 17:33:33 -07001502 const float u1 = fmax(0.0f, srcLeft / width);
1503 const float v1 = fmax(0.0f, srcTop / height);
1504 const float u2 = fmin(1.0f, srcRight / width);
1505 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001506
Romain Guy03750a02010-10-18 14:06:08 -07001507 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001508 resetDrawTextureTexCoords(u1, v1, u2, v2);
1509
Romain Guy03750a02010-10-18 14:06:08 -07001510 int alpha;
1511 SkXfermode::Mode mode;
1512 getAlphaAndMode(paint, &alpha, &mode);
1513
Romain Guyd21b6e12011-11-30 20:21:23 -08001514 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1515
Romain Guy6620c6d2010-12-06 18:07:02 -08001516 if (mSnapshot->transform->isPureTranslate()) {
1517 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1518 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1519
Romain Guyb5014982011-07-28 15:39:12 -07001520 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001521 // Enable linear filtering if the source rectangle is scaled
1522 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001523 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001524 }
Romain Guyb5014982011-07-28 15:39:12 -07001525
Romain Guyd21b6e12011-11-30 20:21:23 -08001526 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001527 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1528 texture->id, alpha / 255.0f, mode, texture->blend,
1529 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1530 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1531 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001532 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001533 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1534 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1535 GL_TRIANGLE_STRIP, gMeshCount);
1536 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001537
1538 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1539}
1540
Romain Guy4aa90572010-09-26 18:40:37 -07001541void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001542 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001543 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001544 if (quickReject(left, top, right, bottom)) {
1545 return;
1546 }
1547
Romain Guy746b7402010-10-26 16:27:31 -07001548 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001549 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001550 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001551 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001552 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1553 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001554
1555 int alpha;
1556 SkXfermode::Mode mode;
1557 getAlphaAndMode(paint, &alpha, &mode);
1558
Romain Guy2728f962010-10-08 18:36:15 -07001559 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001560 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001561
Romain Guya5ef39a2010-12-03 16:48:20 -08001562 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001563 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001564#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001565 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001566 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001567 const float offsetX = left + mSnapshot->transform->getTranslateX();
1568 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001569 const size_t count = mesh->quads.size();
1570 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001571 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001572 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001573 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1574 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1575 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001576 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001577 dirtyLayer(left + bounds.left, top + bounds.top,
1578 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001579 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001580 }
1581 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001582#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001583
Romain Guy6620c6d2010-12-06 18:07:02 -08001584 if (pureTranslate) {
1585 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1586 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1587
1588 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1589 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1590 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1591 true, !mesh->hasEmptyQuads);
1592 } else {
1593 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1594 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1595 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1596 true, !mesh->hasEmptyQuads);
1597 }
Romain Guy054dc182010-10-15 17:55:25 -07001598 }
Romain Guyf7f93552010-07-08 19:17:03 -07001599}
1600
Chet Haase99ecdc42011-05-06 12:06:34 -07001601/**
Chet Haase858aa932011-05-12 09:06:00 -07001602 * This function uses a similar approach to that of AA lines in the drawLines() function.
1603 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1604 * shader to compute the translucency of the color, determined by whether a given pixel is
1605 * within that boundary region and how far into the region it is.
1606 */
1607void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001608 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001609 float inverseScaleX = 1.0f;
1610 float inverseScaleY = 1.0f;
1611 // The quad that we use needs to account for scaling.
1612 if (!mSnapshot->transform->isPureTranslate()) {
1613 Matrix4 *mat = mSnapshot->transform;
1614 float m00 = mat->data[Matrix4::kScaleX];
1615 float m01 = mat->data[Matrix4::kSkewY];
1616 float m02 = mat->data[2];
1617 float m10 = mat->data[Matrix4::kSkewX];
1618 float m11 = mat->data[Matrix4::kScaleX];
1619 float m12 = mat->data[6];
1620 float scaleX = sqrt(m00 * m00 + m01 * m01);
1621 float scaleY = sqrt(m10 * m10 + m11 * m11);
1622 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1623 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1624 }
1625
1626 setupDraw();
1627 setupDrawAALine();
1628 setupDrawColor(color);
1629 setupDrawColorFilter();
1630 setupDrawShader();
1631 setupDrawBlending(true, mode);
1632 setupDrawProgram();
1633 setupDrawModelViewIdentity(true);
1634 setupDrawColorUniforms();
1635 setupDrawColorFilterUniforms();
1636 setupDrawShaderIdentityUniforms();
1637
1638 AAVertex rects[4];
1639 AAVertex* aaVertices = &rects[0];
1640 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1641 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1642
1643 float boundarySizeX = .5 * inverseScaleX;
1644 float boundarySizeY = .5 * inverseScaleY;
1645
1646 // Adjust the rect by the AA boundary padding
1647 left -= boundarySizeX;
1648 right += boundarySizeX;
1649 top -= boundarySizeY;
1650 bottom += boundarySizeY;
1651
1652 float width = right - left;
1653 float height = bottom - top;
1654
1655 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1656 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1657 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1658 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1659 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1660 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1661 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1662
1663 if (!quickReject(left, top, right, bottom)) {
1664 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1665 AAVertex::set(aaVertices++, left, top, 1, 0);
1666 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1667 AAVertex::set(aaVertices++, right, top, 0, 0);
1668 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1669 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1670 }
1671}
1672
1673/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001674 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1675 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1676 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1677 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1678 * of the line. Hairlines are more involved because we need to account for transform scaling
1679 * to end up with a one-pixel-wide line in screen space..
1680 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1681 * in combination with values that we calculate and pass down in this method. The basic approach
1682 * is that the quad we create contains both the core line area plus a bounding area in which
1683 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1684 * proportion of the width and the length of a given segment is represented by the boundary
1685 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1686 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1687 * on the inside). This ends up giving the result we want, with pixels that are completely
1688 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1689 * how far into the boundary region they are, which is determined by shader interpolation.
1690 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001691void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1692 if (mSnapshot->isIgnored()) return;
1693
1694 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001695 // We use half the stroke width here because we're going to position the quad
1696 // corner vertices half of the width away from the line endpoints
1697 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001698 // A stroke width of 0 has a special meaning in Skia:
1699 // it draws a line 1 px wide regardless of current transform
1700 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001701 float inverseScaleX = 1.0f;
1702 float inverseScaleY = 1.0f;
1703 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001704 int alpha;
1705 SkXfermode::Mode mode;
1706 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001707 int verticesCount = count;
1708 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001709 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001710 verticesCount += (count - 4);
1711 }
1712
1713 if (isHairLine || isAA) {
1714 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1715 // the line on the screen should always be one pixel wide regardless of scale. For
1716 // AA lines, we only want one pixel of translucent boundary around the quad.
1717 if (!mSnapshot->transform->isPureTranslate()) {
1718 Matrix4 *mat = mSnapshot->transform;
1719 float m00 = mat->data[Matrix4::kScaleX];
1720 float m01 = mat->data[Matrix4::kSkewY];
1721 float m02 = mat->data[2];
1722 float m10 = mat->data[Matrix4::kSkewX];
1723 float m11 = mat->data[Matrix4::kScaleX];
1724 float m12 = mat->data[6];
1725 float scaleX = sqrt(m00*m00 + m01*m01);
1726 float scaleY = sqrt(m10*m10 + m11*m11);
1727 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1728 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1729 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1730 scaled = true;
1731 }
1732 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001733 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001734
1735 getAlphaAndMode(paint, &alpha, &mode);
1736 setupDraw();
1737 if (isAA) {
1738 setupDrawAALine();
1739 }
1740 setupDrawColor(paint->getColor(), alpha);
1741 setupDrawColorFilter();
1742 setupDrawShader();
1743 if (isAA) {
1744 setupDrawBlending(true, mode);
1745 } else {
1746 setupDrawBlending(mode);
1747 }
1748 setupDrawProgram();
1749 setupDrawModelViewIdentity(true);
1750 setupDrawColorUniforms();
1751 setupDrawColorFilterUniforms();
1752 setupDrawShaderIdentityUniforms();
1753
1754 if (isHairLine) {
1755 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001756 halfStrokeWidth = isAA? 1 : .5;
1757 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001758 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001759 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001760 }
1761 Vertex lines[verticesCount];
1762 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001763 AAVertex wLines[verticesCount];
1764 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001765 if (!isAA) {
1766 setupDrawVertices(vertices);
1767 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001768 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1769 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001770 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001771 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1772 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001773 // We will need to calculate the actual width proportion on each segment for
1774 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1775 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1776 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001777 }
1778
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 AAVertex* prevAAVertex = NULL;
1780 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001781
Chet Haase99585ad2011-05-02 15:00:16 -07001782 int boundaryLengthSlot = -1;
1783 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001784 int boundaryWidthSlot = -1;
1785 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001786 for (int i = 0; i < count; i += 4) {
1787 // a = start point, b = end point
1788 vec2 a(points[i], points[i + 1]);
1789 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001790 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001791 float boundaryLengthProportion = 0;
1792 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001793
Chet Haase5b0200b2011-04-13 17:58:08 -07001794 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001795 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001796 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001797 if (isAA) {
1798 float wideningFactor;
1799 if (fabs(n.x) >= fabs(n.y)) {
1800 wideningFactor = fabs(1.0f / n.x);
1801 } else {
1802 wideningFactor = fabs(1.0f / n.y);
1803 }
1804 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001805 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001806 if (scaled) {
1807 n.x *= inverseScaleX;
1808 n.y *= inverseScaleY;
1809 }
1810 } else if (scaled) {
1811 // Extend n by .5 pixel on each side, post-transform
1812 vec2 extendedN = n.copyNormalized();
1813 extendedN /= 2;
1814 extendedN.x *= inverseScaleX;
1815 extendedN.y *= inverseScaleY;
1816 float extendedNLength = extendedN.length();
1817 // We need to set this value on the shader prior to drawing
1818 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1819 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001820 }
1821 float x = n.x;
1822 n.x = -n.y;
1823 n.y = x;
1824
Chet Haase99585ad2011-05-02 15:00:16 -07001825 // aa lines expand the endpoint vertices to encompass the AA boundary
1826 if (isAA) {
1827 vec2 abVector = (b - a);
1828 length = abVector.length();
1829 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001830 if (scaled) {
1831 abVector.x *= inverseScaleX;
1832 abVector.y *= inverseScaleY;
1833 float abLength = abVector.length();
1834 boundaryLengthProportion = abLength / (length + abLength);
1835 } else {
1836 boundaryLengthProportion = .5 / (length + 1);
1837 }
1838 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001839 a -= abVector;
1840 b += abVector;
1841 }
1842
Chet Haase5b0200b2011-04-13 17:58:08 -07001843 // Four corners of the rectangle defining a thick line
1844 vec2 p1 = a - n;
1845 vec2 p2 = a + n;
1846 vec2 p3 = b + n;
1847 vec2 p4 = b - n;
1848
Chet Haase99585ad2011-05-02 15:00:16 -07001849
Chet Haase5b0200b2011-04-13 17:58:08 -07001850 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1851 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1852 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1853 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1854
1855 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001856 if (!isAA) {
1857 if (prevVertex != NULL) {
1858 // Issue two repeat vertices to create degenerate triangles to bridge
1859 // between the previous line and the new one. This is necessary because
1860 // we are creating a single triangle_strip which will contain
1861 // potentially discontinuous line segments.
1862 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1863 Vertex::set(vertices++, p1.x, p1.y);
1864 generatedVerticesCount += 2;
1865 }
1866 Vertex::set(vertices++, p1.x, p1.y);
1867 Vertex::set(vertices++, p2.x, p2.y);
1868 Vertex::set(vertices++, p4.x, p4.y);
1869 Vertex::set(vertices++, p3.x, p3.y);
1870 prevVertex = vertices - 1;
1871 generatedVerticesCount += 4;
1872 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001873 if (!isHairLine && scaled) {
1874 // Must set width proportions per-segment for scaled non-hairlines to use the
1875 // correct AA boundary dimensions
1876 if (boundaryWidthSlot < 0) {
1877 boundaryWidthSlot =
1878 mCaches.currentProgram->getUniform("boundaryWidth");
1879 inverseBoundaryWidthSlot =
1880 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1881 }
1882 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1883 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1884 }
Chet Haase99585ad2011-05-02 15:00:16 -07001885 if (boundaryLengthSlot < 0) {
1886 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1887 inverseBoundaryLengthSlot =
1888 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1889 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001890 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1891 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001892
Chet Haase5b0200b2011-04-13 17:58:08 -07001893 if (prevAAVertex != NULL) {
1894 // Issue two repeat vertices to create degenerate triangles to bridge
1895 // between the previous line and the new one. This is necessary because
1896 // we are creating a single triangle_strip which will contain
1897 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001898 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1899 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1900 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 generatedVerticesCount += 2;
1902 }
Chet Haase99585ad2011-05-02 15:00:16 -07001903 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1904 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1905 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1906 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001907 prevAAVertex = aaVertices - 1;
1908 generatedVerticesCount += 4;
1909 }
Chet Haase99585ad2011-05-02 15:00:16 -07001910 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1911 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1912 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 }
1914 }
1915 if (generatedVerticesCount > 0) {
1916 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1917 }
1918}
1919
Romain Guyed6fcb02011-03-21 13:11:28 -07001920void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1921 if (mSnapshot->isIgnored()) return;
1922
1923 // TODO: The paint's cap style defines whether the points are square or circular
1924 // TODO: Handle AA for round points
1925
Chet Haase5b0200b2011-04-13 17:58:08 -07001926 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001927 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001928 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001929 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001930 if (isHairLine) {
1931 // Now that we know it's hairline, we can set the effective width, to be used later
1932 strokeWidth = 1.0f;
1933 }
1934 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001935 int alpha;
1936 SkXfermode::Mode mode;
1937 getAlphaAndMode(paint, &alpha, &mode);
1938
1939 int verticesCount = count >> 1;
1940 int generatedVerticesCount = 0;
1941
1942 TextureVertex pointsData[verticesCount];
1943 TextureVertex* vertex = &pointsData[0];
1944
1945 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001947 setupDrawColor(paint->getColor(), alpha);
1948 setupDrawColorFilter();
1949 setupDrawShader();
1950 setupDrawBlending(mode);
1951 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001952 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001953 setupDrawColorUniforms();
1954 setupDrawColorFilterUniforms();
1955 setupDrawPointUniforms();
1956 setupDrawShaderIdentityUniforms();
1957 setupDrawMesh(vertex);
1958
1959 for (int i = 0; i < count; i += 2) {
1960 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1961 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001962 float left = points[i] - halfWidth;
1963 float right = points[i] + halfWidth;
1964 float top = points[i + 1] - halfWidth;
1965 float bottom = points [i + 1] + halfWidth;
1966 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001967 }
1968
1969 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1970}
1971
Romain Guy85bf02f2010-06-22 13:11:24 -07001972void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001973 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001974 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001975
Romain Guyae88e5e2010-10-22 17:49:18 -07001976 Rect& clip(*mSnapshot->clipRect);
1977 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001978
Romain Guy3d58c032010-07-14 16:34:53 -07001979 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001980}
Romain Guy9d5316e2010-06-24 19:30:36 -07001981
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001982void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001983 if (!texture) return;
1984 const AutoTexture autoCleanup(texture);
1985
1986 const float x = left + texture->left - texture->offset;
1987 const float y = top + texture->top - texture->offset;
1988
1989 drawPathTexture(texture, x, y, paint);
1990}
1991
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001992void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1993 float rx, float ry, SkPaint* paint) {
1994 if (mSnapshot->isIgnored()) return;
1995
1996 glActiveTexture(gTextureUnits[0]);
1997 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1998 right - left, bottom - top, rx, ry, paint);
1999 drawShape(left, top, texture, paint);
2000}
2001
Romain Guy01d58e42011-01-19 21:54:02 -08002002void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2003 if (mSnapshot->isIgnored()) return;
2004
2005 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002006 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002007 drawShape(x - radius, y - radius, texture, paint);
2008}
Romain Guy01d58e42011-01-19 21:54:02 -08002009
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002010void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2011 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002012
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002013 glActiveTexture(gTextureUnits[0]);
2014 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2015 drawShape(left, top, texture, paint);
2016}
2017
Romain Guy8b2f5262011-01-23 16:15:02 -08002018void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2019 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2020 if (mSnapshot->isIgnored()) return;
2021
2022 if (fabs(sweepAngle) >= 360.0f) {
2023 drawOval(left, top, right, bottom, paint);
2024 return;
2025 }
2026
2027 glActiveTexture(gTextureUnits[0]);
2028 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2029 startAngle, sweepAngle, useCenter, paint);
2030 drawShape(left, top, texture, paint);
2031}
2032
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002033void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2034 SkPaint* paint) {
2035 if (mSnapshot->isIgnored()) return;
2036
2037 glActiveTexture(gTextureUnits[0]);
2038 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2039 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002040}
2041
Chet Haase5c13d892010-10-08 08:37:55 -07002042void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002043 if (p->getStyle() != SkPaint::kFill_Style) {
2044 drawRectAsShape(left, top, right, bottom, p);
2045 return;
2046 }
2047
Romain Guy6926c722010-07-12 20:20:03 -07002048 if (quickReject(left, top, right, bottom)) {
2049 return;
2050 }
2051
Romain Guy026c5e162010-06-28 17:12:22 -07002052 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002053 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002054 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2055 if (!isMode) {
2056 // Assume SRC_OVER
2057 mode = SkXfermode::kSrcOver_Mode;
2058 }
2059 } else {
2060 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002061 }
2062
Romain Guy026c5e162010-06-28 17:12:22 -07002063 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002064 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002065 drawAARect(left, top, right, bottom, color, mode);
2066 } else {
2067 drawColorRect(left, top, right, bottom, color, mode);
2068 }
Romain Guyc7d53492010-06-25 13:41:57 -07002069}
Romain Guy9d5316e2010-06-24 19:30:36 -07002070
Romain Guye8e62a42010-07-23 18:55:21 -07002071void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002072 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002073 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002074 return;
2075 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002076 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002077
Romain Guy8f9a9f62011-12-05 11:53:26 -08002078 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002079
Romain Guye8e62a42010-07-23 18:55:21 -07002080 switch (paint->getTextAlign()) {
2081 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002082 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002083 x -= length / 2.0f;
2084 break;
2085 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002086 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002087 x -= length;
2088 break;
2089 default:
2090 break;
2091 }
2092
Romain Guycac5fd32011-12-01 20:08:50 -08002093 SkPaint::FontMetrics metrics;
2094 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002095 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002096 if (quickReject(x, y + metrics.fTop,
2097 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2098 return;
2099 }
2100
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002101 const float oldX = x;
2102 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002103 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2104 if (pureTranslate) {
2105 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2106 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2107 }
2108
Romain Guyb45c0c92010-08-26 20:35:23 -07002109 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002110#if DEBUG_GLYPHS
2111 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2112#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002113 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002114 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002115
Romain Guy86568192010-12-14 15:55:39 -08002116 int alpha;
2117 SkXfermode::Mode mode;
2118 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002119
Romain Guy1e45aae2010-08-13 19:39:53 -07002120 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002121 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002122 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2123 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002124 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002125
Romain Guy740bf2b2011-04-26 15:33:10 -07002126 const float sx = oldX - shadow->left + mShadowDx;
2127 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002128
Romain Guy86568192010-12-14 15:55:39 -08002129 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002130 int shadowColor = mShadowColor;
2131 if (mShader) {
2132 shadowColor = 0xffffffff;
2133 }
Romain Guy86568192010-12-14 15:55:39 -08002134
2135 glActiveTexture(gTextureUnits[0]);
2136 setupDraw();
2137 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002138 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2139 setupDrawColorFilter();
2140 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002141 setupDrawBlending(true, mode);
2142 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002143 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002144 setupDrawTexture(shadow->id);
2145 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002146 setupDrawColorFilterUniforms();
2147 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002148 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2149
Romain Guy0a417492010-08-16 20:26:20 -07002150 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002151
Romain Guy86568192010-12-14 15:55:39 -08002152 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002153 }
2154
Romain Guyb146b122010-12-15 17:06:45 -08002155 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2156 return;
2157 }
2158
Romain Guy6620c6d2010-12-06 18:07:02 -08002159 // Pick the appropriate texture filtering
2160 bool linearFilter = mSnapshot->transform->changesBounds();
2161 if (pureTranslate && !linearFilter) {
2162 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2163 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002164
Romain Guy86568192010-12-14 15:55:39 -08002165 glActiveTexture(gTextureUnits[0]);
2166 setupDraw();
2167 setupDrawDirtyRegionsDisabled();
2168 setupDrawWithTexture(true);
2169 setupDrawAlpha8Color(paint->getColor(), alpha);
2170 setupDrawColorFilter();
2171 setupDrawShader();
2172 setupDrawBlending(true, mode);
2173 setupDrawProgram();
2174 setupDrawModelView(x, y, x, y, pureTranslate, true);
2175 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2176 setupDrawPureColorUniforms();
2177 setupDrawColorFilterUniforms();
2178 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002179
Romain Guy6620c6d2010-12-06 18:07:02 -08002180 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002181 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2182
2183#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002184 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002185#else
Romain Guyf219da52011-01-16 12:54:25 -08002186 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002187#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002188
Romain Guyf3a910b42011-12-12 20:35:21 -08002189 float* buffer = fontRenderer.getMeshBuffer();
2190 int offset = fontRenderer.getMeshTexCoordsOffset();
2191
2192 bool force = mCaches.unbindMeshBuffer();
2193 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, buffer);
2194 mCaches.bindTexCoordsVertexPointer(force, mTexCoordsSlot, buffer + offset);
2195
Romain Guy6620c6d2010-12-06 18:07:02 -08002196 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002197 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002198#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002199 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002200 if (!pureTranslate) {
2201 mSnapshot->transform->mapRect(bounds);
2202 }
Romain Guyf219da52011-01-16 12:54:25 -08002203 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002204 }
2205#endif
2206 }
Romain Guy694b5192010-07-21 21:33:20 -07002207
2208 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf3a910b42011-12-12 20:35:21 -08002209 glDisableVertexAttribArray(mCaches.currentProgram->texCoords);
Romain Guya674ab72010-08-10 17:26:42 -07002210
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002211 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002212}
2213
Romain Guy7fbcc042010-08-04 15:40:07 -07002214void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002215 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002216
Romain Guy01d58e42011-01-19 21:54:02 -08002217 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002218
Romain Guyfb8b7632010-08-23 21:05:08 -07002219 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002220 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002221 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002222
Romain Guy8b55f372010-08-18 17:10:07 -07002223 const float x = texture->left - texture->offset;
2224 const float y = texture->top - texture->offset;
2225
Romain Guy01d58e42011-01-19 21:54:02 -08002226 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002227}
2228
Romain Guyada830f2011-01-13 12:13:20 -08002229void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2230 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002231 return;
2232 }
2233
2234 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002235
2236 int alpha;
2237 SkXfermode::Mode mode;
2238 getAlphaAndMode(paint, &alpha, &mode);
2239
Romain Guy9ace8f52011-07-07 20:50:11 -07002240 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002241
Romain Guyf219da52011-01-16 12:54:25 -08002242#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002243 if (!layer->region.isEmpty()) {
2244 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002245 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002246 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002247 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002248 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002249
Romain Guyc88e3572011-01-22 00:32:12 -08002250 setupDraw();
2251 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002252 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002253 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002254 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002256 setupDrawPureColorUniforms();
2257 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002258 setupDrawTexture(layer->getTexture());
2259 if (mSnapshot->transform->isPureTranslate()) {
2260 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2261 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2262
Romain Guyd21b6e12011-11-30 20:21:23 -08002263 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002264 setupDrawModelViewTranslate(x, y,
2265 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2266 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002267 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002268 setupDrawModelViewTranslate(x, y,
2269 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2270 }
Romain Guyc88e3572011-01-22 00:32:12 -08002271 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002272
Romain Guyc88e3572011-01-22 00:32:12 -08002273 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2274 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002275
Romain Guyc88e3572011-01-22 00:32:12 -08002276 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002277
2278#if DEBUG_LAYERS_AS_REGIONS
2279 drawRegionRects(layer->region);
2280#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002281 }
Romain Guyf219da52011-01-16 12:54:25 -08002282 }
2283#else
Romain Guyada830f2011-01-13 12:13:20 -08002284 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2285 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002286#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002287}
2288
Romain Guy6926c722010-07-12 20:20:03 -07002289///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002290// Shaders
2291///////////////////////////////////////////////////////////////////////////////
2292
2293void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002294 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002295}
2296
Romain Guy06f96e22010-07-30 19:18:16 -07002297void OpenGLRenderer::setupShader(SkiaShader* shader) {
2298 mShader = shader;
2299 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002300 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002301 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002302}
2303
Romain Guyd27977d2010-07-14 19:18:51 -07002304///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002305// Color filters
2306///////////////////////////////////////////////////////////////////////////////
2307
2308void OpenGLRenderer::resetColorFilter() {
2309 mColorFilter = NULL;
2310}
2311
2312void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2313 mColorFilter = filter;
2314}
2315
2316///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002317// Drop shadow
2318///////////////////////////////////////////////////////////////////////////////
2319
2320void OpenGLRenderer::resetShadow() {
2321 mHasShadow = false;
2322}
2323
2324void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2325 mHasShadow = true;
2326 mShadowRadius = radius;
2327 mShadowDx = dx;
2328 mShadowDy = dy;
2329 mShadowColor = color;
2330}
2331
2332///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002333// Drawing implementation
2334///////////////////////////////////////////////////////////////////////////////
2335
Romain Guy01d58e42011-01-19 21:54:02 -08002336void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2337 float x, float y, SkPaint* paint) {
2338 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2339 return;
2340 }
2341
2342 int alpha;
2343 SkXfermode::Mode mode;
2344 getAlphaAndMode(paint, &alpha, &mode);
2345
2346 setupDraw();
2347 setupDrawWithTexture(true);
2348 setupDrawAlpha8Color(paint->getColor(), alpha);
2349 setupDrawColorFilter();
2350 setupDrawShader();
2351 setupDrawBlending(true, mode);
2352 setupDrawProgram();
2353 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2354 setupDrawTexture(texture->id);
2355 setupDrawPureColorUniforms();
2356 setupDrawColorFilterUniforms();
2357 setupDrawShaderUniforms();
2358 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2359
2360 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2361
2362 finishDrawTexture();
2363}
2364
Romain Guyf607bdc2010-09-10 19:20:06 -07002365// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002366#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2367#define kStdUnderline_Offset (1.0f / 9.0f)
2368#define kStdUnderline_Thickness (1.0f / 18.0f)
2369
2370void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2371 float x, float y, SkPaint* paint) {
2372 // Handle underline and strike-through
2373 uint32_t flags = paint->getFlags();
2374 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002375 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002376 float underlineWidth = length;
2377 // If length is > 0.0f, we already measured the text for the text alignment
2378 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002379 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002380 }
2381
2382 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002383 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002384 case SkPaint::kCenter_Align:
2385 offsetX = underlineWidth * 0.5f;
2386 break;
2387 case SkPaint::kRight_Align:
2388 offsetX = underlineWidth;
2389 break;
2390 default:
2391 break;
2392 }
2393
2394 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002395 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002396 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002397
Romain Guye20ecbd2010-09-22 19:49:04 -07002398 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002399 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002400
Romain Guyf6834472011-01-23 13:32:12 -08002401 int linesCount = 0;
2402 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2403 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2404
2405 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002406 float points[pointsCount];
2407 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002408
2409 if (flags & SkPaint::kUnderlineText_Flag) {
2410 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002411 points[currentPoint++] = left;
2412 points[currentPoint++] = top;
2413 points[currentPoint++] = left + underlineWidth;
2414 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002415 }
2416
2417 if (flags & SkPaint::kStrikeThruText_Flag) {
2418 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002419 points[currentPoint++] = left;
2420 points[currentPoint++] = top;
2421 points[currentPoint++] = left + underlineWidth;
2422 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002423 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002424
Romain Guy726aeba2011-06-01 14:52:00 -07002425 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002426
Romain Guy726aeba2011-06-01 14:52:00 -07002427 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002428 }
2429 }
2430}
2431
Romain Guy026c5e162010-06-28 17:12:22 -07002432void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002433 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002434 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002435 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002436 color |= 0x00ffffff;
2437 }
2438
Romain Guy70ca14e2010-12-13 18:24:33 -08002439 setupDraw();
2440 setupDrawColor(color);
2441 setupDrawShader();
2442 setupDrawColorFilter();
2443 setupDrawBlending(mode);
2444 setupDrawProgram();
2445 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2446 setupDrawColorUniforms();
2447 setupDrawShaderUniforms(ignoreTransform);
2448 setupDrawColorFilterUniforms();
2449 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002450
Romain Guyc95c8d62010-09-17 15:31:32 -07002451 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2452}
2453
Romain Guy82ba8142010-07-09 13:25:56 -07002454void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002455 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002456 int alpha;
2457 SkXfermode::Mode mode;
2458 getAlphaAndMode(paint, &alpha, &mode);
2459
Romain Guyd21b6e12011-11-30 20:21:23 -08002460 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002461
Romain Guy6620c6d2010-12-06 18:07:02 -08002462 if (mSnapshot->transform->isPureTranslate()) {
2463 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2464 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2465
Romain Guyd21b6e12011-11-30 20:21:23 -08002466 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002467 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2468 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2469 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2470 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002471 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002472 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2473 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2474 GL_TRIANGLE_STRIP, gMeshCount);
2475 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002476}
2477
Romain Guybd6b79b2010-06-26 00:13:53 -07002478void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002479 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2480 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002481 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002482}
2483
2484void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002485 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002486 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002487 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002488
Romain Guy746b7402010-10-26 16:27:31 -07002489 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490 setupDrawWithTexture();
2491 setupDrawColor(alpha, alpha, alpha, alpha);
2492 setupDrawColorFilter();
2493 setupDrawBlending(blend, mode, swapSrcDst);
2494 setupDrawProgram();
2495 if (!dirty) {
2496 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002497 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002498 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002499 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002500 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002501 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002502 }
Romain Guy86568192010-12-14 15:55:39 -08002503 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002504 setupDrawColorFilterUniforms();
2505 setupDrawTexture(texture);
2506 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002507
Romain Guy6820ac82010-09-15 18:11:50 -07002508 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002509
2510 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002511}
2512
Romain Guya5aed0d2010-09-09 14:42:43 -07002513void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002514 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002515 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2516 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002517 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002518 if (!mCaches.blend) {
2519 glEnable(GL_BLEND);
2520 }
Romain Guy82ba8142010-07-09 13:25:56 -07002521
Romain Guyf607bdc2010-09-10 19:20:06 -07002522 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2523 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002524
Romain Guya5aed0d2010-09-09 14:42:43 -07002525 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2526 glBlendFunc(sourceMode, destMode);
2527 mCaches.lastSrcMode = sourceMode;
2528 mCaches.lastDstMode = destMode;
2529 }
2530 } else {
2531 // These blend modes are not supported by OpenGL directly and have
2532 // to be implemented using shaders. Since the shader will perform
2533 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002534 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002535 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002536 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002537 }
2538
2539 if (mCaches.blend) {
2540 glDisable(GL_BLEND);
2541 }
2542 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002543 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002544 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002545 glDisable(GL_BLEND);
2546 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002547 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002548}
2549
Romain Guy889f8d12010-07-29 14:37:42 -07002550bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002551 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002552 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002553 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002554 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002555 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002556 }
Romain Guy6926c722010-07-12 20:20:03 -07002557 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002558}
2559
Romain Guy026c5e162010-06-28 17:12:22 -07002560void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002561 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002562 TextureVertex::setUV(v++, u1, v1);
2563 TextureVertex::setUV(v++, u2, v1);
2564 TextureVertex::setUV(v++, u1, v2);
2565 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002566}
2567
Chet Haase5c13d892010-10-08 08:37:55 -07002568void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002569 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002570 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002571
2572 // Skia draws using the color's alpha channel if < 255
2573 // Otherwise, it uses the paint's alpha
2574 int color = paint->getColor();
2575 *alpha = (color >> 24) & 0xFF;
2576 if (*alpha == 255) {
2577 *alpha = paint->getAlpha();
2578 }
2579 } else {
2580 *mode = SkXfermode::kSrcOver_Mode;
2581 *alpha = 255;
2582 }
Romain Guy026c5e162010-06-28 17:12:22 -07002583}
2584
Romain Guya5aed0d2010-09-09 14:42:43 -07002585SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002586 SkXfermode::Mode resultMode;
2587 if (!SkXfermode::AsMode(mode, &resultMode)) {
2588 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002589 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002590 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002591}
2592
Romain Guy9d5316e2010-06-24 19:30:36 -07002593}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002594}; // namespace android