blob: e7eaa50beb5dfef500d687f09db6a6cdcc1d99a3 [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 Guyda8532c2010-08-31 11:50:35 -0700202}
203
Romain Guy6c319ca2011-01-11 14:29:25 -0800204void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800205 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
206
207 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700208
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() {
1204 mCaches.bindMeshBuffer();
1205 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1206 gMeshStride, 0);
1207}
1208
1209void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1210 bindTexture(texture);
1211 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1212
1213 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1214 glEnableVertexAttribArray(mTexCoordsSlot);
1215}
1216
Romain Guyaa6c24c2011-04-28 18:40:04 -07001217void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1218 bindExternalTexture(texture);
1219 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1220
1221 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1222 glEnableVertexAttribArray(mTexCoordsSlot);
1223}
1224
Romain Guy8f0095c2011-05-02 17:24:22 -07001225void OpenGLRenderer::setupDrawTextureTransform() {
1226 mDescription.hasTextureTransform = true;
1227}
1228
1229void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001230 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1231 GL_FALSE, &transform.data[0]);
1232}
1233
Romain Guy70ca14e2010-12-13 18:24:33 -08001234void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1235 if (!vertices) {
1236 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1237 } else {
1238 mCaches.unbindMeshBuffer();
1239 }
1240 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1241 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001242 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001243 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1244 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001245}
1246
Chet Haase5b0200b2011-04-13 17:58:08 -07001247void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1248 mCaches.unbindMeshBuffer();
1249 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1250 gVertexStride, vertices);
1251}
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) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001266 mCaches.unbindMeshBuffer();
1267 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001268 gAAVertexStride, vertices);
1269 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1270 glEnableVertexAttribArray(widthSlot);
1271 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1272 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1273 glEnableVertexAttribArray(lengthSlot);
1274 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001275 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001276 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001277 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001278 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001279 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001280}
1281
Romain Guy70ca14e2010-12-13 18:24:33 -08001282void OpenGLRenderer::finishDrawTexture() {
1283 glDisableVertexAttribArray(mTexCoordsSlot);
1284}
1285
1286///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001287// Drawing
1288///////////////////////////////////////////////////////////////////////////////
1289
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001290bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1291 Rect& dirty, uint32_t level) {
1292 if (quickReject(0.0f, 0.0f, width, height)) {
1293 return false;
1294 }
1295
Romain Guy0fe478e2010-11-08 12:08:41 -08001296 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1297 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001298 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001299 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001300 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001301
Chet Haasedaf98e92011-01-10 14:10:36 -08001302 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001303}
1304
Chet Haaseed30fd82011-04-22 16:18:45 -07001305void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1306 if (displayList) {
1307 displayList->output(*this, level);
1308 }
1309}
1310
Romain Guya168d732011-03-18 16:50:13 -07001311void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1312 int alpha;
1313 SkXfermode::Mode mode;
1314 getAlphaAndMode(paint, &alpha, &mode);
1315
Romain Guya168d732011-03-18 16:50:13 -07001316 float x = left;
1317 float y = top;
1318
Romain Guye3c26852011-07-25 16:36:01 -07001319 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001320 bool ignoreTransform = false;
1321 if (mSnapshot->transform->isPureTranslate()) {
1322 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1323 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1324 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001325 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001326 } else {
1327 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001328 }
1329
1330 setupDraw();
1331 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001332 if (paint) {
1333 setupDrawAlpha8Color(paint->getColor(), alpha);
1334 }
Romain Guya168d732011-03-18 16:50:13 -07001335 setupDrawColorFilter();
1336 setupDrawShader();
1337 setupDrawBlending(true, mode);
1338 setupDrawProgram();
1339 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001340
Romain Guya168d732011-03-18 16:50:13 -07001341 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001342 texture->setWrap(GL_CLAMP_TO_EDGE);
1343 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001344
Romain Guya168d732011-03-18 16:50:13 -07001345 setupDrawPureColorUniforms();
1346 setupDrawColorFilterUniforms();
1347 setupDrawShaderUniforms();
1348 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1349
1350 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1351
1352 finishDrawTexture();
1353}
1354
Chet Haase5c13d892010-10-08 08:37:55 -07001355void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001356 const float right = left + bitmap->width();
1357 const float bottom = top + bitmap->height();
1358
1359 if (quickReject(left, top, right, bottom)) {
1360 return;
1361 }
1362
Romain Guy86568192010-12-14 15:55:39 -08001363 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001364 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001365 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001366 const AutoTexture autoCleanup(texture);
1367
Romain Guya168d732011-03-18 16:50:13 -07001368 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1369 drawAlphaBitmap(texture, left, top, paint);
1370 } else {
1371 drawTextureRect(left, top, right, bottom, texture, paint);
1372 }
Romain Guyce0537b2010-06-29 21:05:21 -07001373}
1374
Chet Haase5c13d892010-10-08 08:37:55 -07001375void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001376 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1377 const mat4 transform(*matrix);
1378 transform.mapRect(r);
1379
Romain Guy6926c722010-07-12 20:20:03 -07001380 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1381 return;
1382 }
1383
Romain Guy86568192010-12-14 15:55:39 -08001384 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001385 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001386 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001387 const AutoTexture autoCleanup(texture);
1388
Romain Guy5b3b3522010-10-27 18:57:51 -07001389 // This could be done in a cheaper way, all we need is pass the matrix
1390 // to the vertex shader. The save/restore is a bit overkill.
1391 save(SkCanvas::kMatrix_SaveFlag);
1392 concatMatrix(matrix);
1393 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1394 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001395}
1396
Romain Guy5a7b4662011-01-20 19:09:30 -08001397void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1398 float* vertices, int* colors, SkPaint* paint) {
1399 // TODO: Do a quickReject
1400 if (!vertices || mSnapshot->isIgnored()) {
1401 return;
1402 }
1403
1404 glActiveTexture(gTextureUnits[0]);
1405 Texture* texture = mCaches.textureCache.get(bitmap);
1406 if (!texture) return;
1407 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001408
Romain Guyd21b6e12011-11-30 20:21:23 -08001409 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1410 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001411
1412 int alpha;
1413 SkXfermode::Mode mode;
1414 getAlphaAndMode(paint, &alpha, &mode);
1415
Romain Guy5a7b4662011-01-20 19:09:30 -08001416 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001417
Romain Guyb18d2d02011-02-10 15:52:54 -08001418 float left = FLT_MAX;
1419 float top = FLT_MAX;
1420 float right = FLT_MIN;
1421 float bottom = FLT_MIN;
1422
1423#if RENDER_LAYERS_AS_REGIONS
1424 bool hasActiveLayer = hasLayer();
1425#else
1426 bool hasActiveLayer = false;
1427#endif
1428
Romain Guya566b7c2011-01-23 16:36:11 -08001429 // TODO: Support the colors array
1430 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001431 TextureVertex* vertex = mesh;
1432 for (int32_t y = 0; y < meshHeight; y++) {
1433 for (int32_t x = 0; x < meshWidth; x++) {
1434 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1435
1436 float u1 = float(x) / meshWidth;
1437 float u2 = float(x + 1) / meshWidth;
1438 float v1 = float(y) / meshHeight;
1439 float v2 = float(y + 1) / meshHeight;
1440
1441 int ax = i + (meshWidth + 1) * 2;
1442 int ay = ax + 1;
1443 int bx = i;
1444 int by = bx + 1;
1445 int cx = i + 2;
1446 int cy = cx + 1;
1447 int dx = i + (meshWidth + 1) * 2 + 2;
1448 int dy = dx + 1;
1449
1450 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1451 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1452 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1453
1454 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1455 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1456 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001457
1458#if RENDER_LAYERS_AS_REGIONS
1459 if (hasActiveLayer) {
1460 // TODO: This could be optimized to avoid unnecessary ops
1461 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1462 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1463 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1464 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1465 }
1466#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001467 }
1468 }
1469
Romain Guyb18d2d02011-02-10 15:52:54 -08001470#if RENDER_LAYERS_AS_REGIONS
1471 if (hasActiveLayer) {
1472 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1473 }
1474#endif
1475
Romain Guy5a7b4662011-01-20 19:09:30 -08001476 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1477 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001478 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001479}
1480
Romain Guy8ba548f2010-06-30 19:21:21 -07001481void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1482 float srcLeft, float srcTop, float srcRight, float srcBottom,
1483 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001484 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001485 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1486 return;
1487 }
1488
Romain Guy746b7402010-10-26 16:27:31 -07001489 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001490 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001491 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001492 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001493
Romain Guy8ba548f2010-06-30 19:21:21 -07001494 const float width = texture->width;
1495 const float height = texture->height;
1496
Romain Guy68169722011-08-22 17:33:33 -07001497 const float u1 = fmax(0.0f, srcLeft / width);
1498 const float v1 = fmax(0.0f, srcTop / height);
1499 const float u2 = fmin(1.0f, srcRight / width);
1500 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001501
Romain Guy03750a02010-10-18 14:06:08 -07001502 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001503 resetDrawTextureTexCoords(u1, v1, u2, v2);
1504
Romain Guy03750a02010-10-18 14:06:08 -07001505 int alpha;
1506 SkXfermode::Mode mode;
1507 getAlphaAndMode(paint, &alpha, &mode);
1508
Romain Guyd21b6e12011-11-30 20:21:23 -08001509 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1510
Romain Guy6620c6d2010-12-06 18:07:02 -08001511 if (mSnapshot->transform->isPureTranslate()) {
1512 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1513 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1514
Romain Guyb5014982011-07-28 15:39:12 -07001515 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001516 // Enable linear filtering if the source rectangle is scaled
1517 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001518 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001519 }
Romain Guyb5014982011-07-28 15:39:12 -07001520
Romain Guyd21b6e12011-11-30 20:21:23 -08001521 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001522 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1523 texture->id, alpha / 255.0f, mode, texture->blend,
1524 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1525 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1526 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001527 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001528 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1529 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1530 GL_TRIANGLE_STRIP, gMeshCount);
1531 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001532
1533 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1534}
1535
Romain Guy4aa90572010-09-26 18:40:37 -07001536void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001537 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001538 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001539 if (quickReject(left, top, right, bottom)) {
1540 return;
1541 }
1542
Romain Guy746b7402010-10-26 16:27:31 -07001543 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001544 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001545 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001546 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001547 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1548 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001549
1550 int alpha;
1551 SkXfermode::Mode mode;
1552 getAlphaAndMode(paint, &alpha, &mode);
1553
Romain Guy2728f962010-10-08 18:36:15 -07001554 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001555 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001556
Romain Guya5ef39a2010-12-03 16:48:20 -08001557 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001558 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001559#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001560 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001561 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001562 const float offsetX = left + mSnapshot->transform->getTranslateX();
1563 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001564 const size_t count = mesh->quads.size();
1565 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001566 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001567 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001568 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1569 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1570 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001571 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001572 dirtyLayer(left + bounds.left, top + bounds.top,
1573 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001574 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001575 }
1576 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001577#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001578
Romain Guy6620c6d2010-12-06 18:07:02 -08001579 if (pureTranslate) {
1580 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1581 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1582
1583 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1584 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1585 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1586 true, !mesh->hasEmptyQuads);
1587 } else {
1588 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1589 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1590 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1591 true, !mesh->hasEmptyQuads);
1592 }
Romain Guy054dc182010-10-15 17:55:25 -07001593 }
Romain Guyf7f93552010-07-08 19:17:03 -07001594}
1595
Chet Haase99ecdc42011-05-06 12:06:34 -07001596/**
Chet Haase858aa932011-05-12 09:06:00 -07001597 * This function uses a similar approach to that of AA lines in the drawLines() function.
1598 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1599 * shader to compute the translucency of the color, determined by whether a given pixel is
1600 * within that boundary region and how far into the region it is.
1601 */
1602void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001603 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001604 float inverseScaleX = 1.0f;
1605 float inverseScaleY = 1.0f;
1606 // The quad that we use needs to account for scaling.
1607 if (!mSnapshot->transform->isPureTranslate()) {
1608 Matrix4 *mat = mSnapshot->transform;
1609 float m00 = mat->data[Matrix4::kScaleX];
1610 float m01 = mat->data[Matrix4::kSkewY];
1611 float m02 = mat->data[2];
1612 float m10 = mat->data[Matrix4::kSkewX];
1613 float m11 = mat->data[Matrix4::kScaleX];
1614 float m12 = mat->data[6];
1615 float scaleX = sqrt(m00 * m00 + m01 * m01);
1616 float scaleY = sqrt(m10 * m10 + m11 * m11);
1617 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1618 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1619 }
1620
1621 setupDraw();
1622 setupDrawAALine();
1623 setupDrawColor(color);
1624 setupDrawColorFilter();
1625 setupDrawShader();
1626 setupDrawBlending(true, mode);
1627 setupDrawProgram();
1628 setupDrawModelViewIdentity(true);
1629 setupDrawColorUniforms();
1630 setupDrawColorFilterUniforms();
1631 setupDrawShaderIdentityUniforms();
1632
1633 AAVertex rects[4];
1634 AAVertex* aaVertices = &rects[0];
1635 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1636 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1637
1638 float boundarySizeX = .5 * inverseScaleX;
1639 float boundarySizeY = .5 * inverseScaleY;
1640
1641 // Adjust the rect by the AA boundary padding
1642 left -= boundarySizeX;
1643 right += boundarySizeX;
1644 top -= boundarySizeY;
1645 bottom += boundarySizeY;
1646
1647 float width = right - left;
1648 float height = bottom - top;
1649
1650 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1651 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1652 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1653 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1654 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1655 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1656 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1657
1658 if (!quickReject(left, top, right, bottom)) {
1659 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1660 AAVertex::set(aaVertices++, left, top, 1, 0);
1661 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1662 AAVertex::set(aaVertices++, right, top, 0, 0);
1663 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1664 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1665 }
1666}
1667
1668/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001669 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1670 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1671 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1672 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1673 * of the line. Hairlines are more involved because we need to account for transform scaling
1674 * to end up with a one-pixel-wide line in screen space..
1675 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1676 * in combination with values that we calculate and pass down in this method. The basic approach
1677 * is that the quad we create contains both the core line area plus a bounding area in which
1678 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1679 * proportion of the width and the length of a given segment is represented by the boundary
1680 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1681 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1682 * on the inside). This ends up giving the result we want, with pixels that are completely
1683 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1684 * how far into the boundary region they are, which is determined by shader interpolation.
1685 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001686void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1687 if (mSnapshot->isIgnored()) return;
1688
1689 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001690 // We use half the stroke width here because we're going to position the quad
1691 // corner vertices half of the width away from the line endpoints
1692 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001693 // A stroke width of 0 has a special meaning in Skia:
1694 // it draws a line 1 px wide regardless of current transform
1695 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001696 float inverseScaleX = 1.0f;
1697 float inverseScaleY = 1.0f;
1698 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001699 int alpha;
1700 SkXfermode::Mode mode;
1701 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001702 int verticesCount = count;
1703 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001704 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001705 verticesCount += (count - 4);
1706 }
1707
1708 if (isHairLine || isAA) {
1709 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1710 // the line on the screen should always be one pixel wide regardless of scale. For
1711 // AA lines, we only want one pixel of translucent boundary around the quad.
1712 if (!mSnapshot->transform->isPureTranslate()) {
1713 Matrix4 *mat = mSnapshot->transform;
1714 float m00 = mat->data[Matrix4::kScaleX];
1715 float m01 = mat->data[Matrix4::kSkewY];
1716 float m02 = mat->data[2];
1717 float m10 = mat->data[Matrix4::kSkewX];
1718 float m11 = mat->data[Matrix4::kScaleX];
1719 float m12 = mat->data[6];
1720 float scaleX = sqrt(m00*m00 + m01*m01);
1721 float scaleY = sqrt(m10*m10 + m11*m11);
1722 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1723 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1724 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1725 scaled = true;
1726 }
1727 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001728 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001729
1730 getAlphaAndMode(paint, &alpha, &mode);
1731 setupDraw();
1732 if (isAA) {
1733 setupDrawAALine();
1734 }
1735 setupDrawColor(paint->getColor(), alpha);
1736 setupDrawColorFilter();
1737 setupDrawShader();
1738 if (isAA) {
1739 setupDrawBlending(true, mode);
1740 } else {
1741 setupDrawBlending(mode);
1742 }
1743 setupDrawProgram();
1744 setupDrawModelViewIdentity(true);
1745 setupDrawColorUniforms();
1746 setupDrawColorFilterUniforms();
1747 setupDrawShaderIdentityUniforms();
1748
1749 if (isHairLine) {
1750 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001751 halfStrokeWidth = isAA? 1 : .5;
1752 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001753 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001754 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001755 }
1756 Vertex lines[verticesCount];
1757 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001758 AAVertex wLines[verticesCount];
1759 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001760 if (!isAA) {
1761 setupDrawVertices(vertices);
1762 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001763 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1764 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001765 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001766 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1767 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001768 // We will need to calculate the actual width proportion on each segment for
1769 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1770 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1771 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001772 }
1773
Chet Haase99ecdc42011-05-06 12:06:34 -07001774 AAVertex* prevAAVertex = NULL;
1775 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001776
Chet Haase99585ad2011-05-02 15:00:16 -07001777 int boundaryLengthSlot = -1;
1778 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 int boundaryWidthSlot = -1;
1780 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001781 for (int i = 0; i < count; i += 4) {
1782 // a = start point, b = end point
1783 vec2 a(points[i], points[i + 1]);
1784 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001785 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001786 float boundaryLengthProportion = 0;
1787 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001788
Chet Haase5b0200b2011-04-13 17:58:08 -07001789 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001790 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001791 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001792 if (isAA) {
1793 float wideningFactor;
1794 if (fabs(n.x) >= fabs(n.y)) {
1795 wideningFactor = fabs(1.0f / n.x);
1796 } else {
1797 wideningFactor = fabs(1.0f / n.y);
1798 }
1799 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001800 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001801 if (scaled) {
1802 n.x *= inverseScaleX;
1803 n.y *= inverseScaleY;
1804 }
1805 } else if (scaled) {
1806 // Extend n by .5 pixel on each side, post-transform
1807 vec2 extendedN = n.copyNormalized();
1808 extendedN /= 2;
1809 extendedN.x *= inverseScaleX;
1810 extendedN.y *= inverseScaleY;
1811 float extendedNLength = extendedN.length();
1812 // We need to set this value on the shader prior to drawing
1813 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1814 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001815 }
1816 float x = n.x;
1817 n.x = -n.y;
1818 n.y = x;
1819
Chet Haase99585ad2011-05-02 15:00:16 -07001820 // aa lines expand the endpoint vertices to encompass the AA boundary
1821 if (isAA) {
1822 vec2 abVector = (b - a);
1823 length = abVector.length();
1824 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001825 if (scaled) {
1826 abVector.x *= inverseScaleX;
1827 abVector.y *= inverseScaleY;
1828 float abLength = abVector.length();
1829 boundaryLengthProportion = abLength / (length + abLength);
1830 } else {
1831 boundaryLengthProportion = .5 / (length + 1);
1832 }
1833 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001834 a -= abVector;
1835 b += abVector;
1836 }
1837
Chet Haase5b0200b2011-04-13 17:58:08 -07001838 // Four corners of the rectangle defining a thick line
1839 vec2 p1 = a - n;
1840 vec2 p2 = a + n;
1841 vec2 p3 = b + n;
1842 vec2 p4 = b - n;
1843
Chet Haase99585ad2011-05-02 15:00:16 -07001844
Chet Haase5b0200b2011-04-13 17:58:08 -07001845 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1846 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1847 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1848 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1849
1850 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001851 if (!isAA) {
1852 if (prevVertex != NULL) {
1853 // Issue two repeat vertices to create degenerate triangles to bridge
1854 // between the previous line and the new one. This is necessary because
1855 // we are creating a single triangle_strip which will contain
1856 // potentially discontinuous line segments.
1857 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1858 Vertex::set(vertices++, p1.x, p1.y);
1859 generatedVerticesCount += 2;
1860 }
1861 Vertex::set(vertices++, p1.x, p1.y);
1862 Vertex::set(vertices++, p2.x, p2.y);
1863 Vertex::set(vertices++, p4.x, p4.y);
1864 Vertex::set(vertices++, p3.x, p3.y);
1865 prevVertex = vertices - 1;
1866 generatedVerticesCount += 4;
1867 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001868 if (!isHairLine && scaled) {
1869 // Must set width proportions per-segment for scaled non-hairlines to use the
1870 // correct AA boundary dimensions
1871 if (boundaryWidthSlot < 0) {
1872 boundaryWidthSlot =
1873 mCaches.currentProgram->getUniform("boundaryWidth");
1874 inverseBoundaryWidthSlot =
1875 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1876 }
1877 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1878 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1879 }
Chet Haase99585ad2011-05-02 15:00:16 -07001880 if (boundaryLengthSlot < 0) {
1881 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1882 inverseBoundaryLengthSlot =
1883 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1884 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001885 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1886 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001887
Chet Haase5b0200b2011-04-13 17:58:08 -07001888 if (prevAAVertex != NULL) {
1889 // Issue two repeat vertices to create degenerate triangles to bridge
1890 // between the previous line and the new one. This is necessary because
1891 // we are creating a single triangle_strip which will contain
1892 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001893 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1894 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1895 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001896 generatedVerticesCount += 2;
1897 }
Chet Haase99585ad2011-05-02 15:00:16 -07001898 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1899 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1900 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1901 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001902 prevAAVertex = aaVertices - 1;
1903 generatedVerticesCount += 4;
1904 }
Chet Haase99585ad2011-05-02 15:00:16 -07001905 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1906 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1907 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001908 }
1909 }
1910 if (generatedVerticesCount > 0) {
1911 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1912 }
1913}
1914
Romain Guyed6fcb02011-03-21 13:11:28 -07001915void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1916 if (mSnapshot->isIgnored()) return;
1917
1918 // TODO: The paint's cap style defines whether the points are square or circular
1919 // TODO: Handle AA for round points
1920
Chet Haase5b0200b2011-04-13 17:58:08 -07001921 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001922 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001923 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001924 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001925 if (isHairLine) {
1926 // Now that we know it's hairline, we can set the effective width, to be used later
1927 strokeWidth = 1.0f;
1928 }
1929 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001930 int alpha;
1931 SkXfermode::Mode mode;
1932 getAlphaAndMode(paint, &alpha, &mode);
1933
1934 int verticesCount = count >> 1;
1935 int generatedVerticesCount = 0;
1936
1937 TextureVertex pointsData[verticesCount];
1938 TextureVertex* vertex = &pointsData[0];
1939
1940 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001941 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001942 setupDrawColor(paint->getColor(), alpha);
1943 setupDrawColorFilter();
1944 setupDrawShader();
1945 setupDrawBlending(mode);
1946 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001947 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001948 setupDrawColorUniforms();
1949 setupDrawColorFilterUniforms();
1950 setupDrawPointUniforms();
1951 setupDrawShaderIdentityUniforms();
1952 setupDrawMesh(vertex);
1953
1954 for (int i = 0; i < count; i += 2) {
1955 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1956 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001957 float left = points[i] - halfWidth;
1958 float right = points[i] + halfWidth;
1959 float top = points[i + 1] - halfWidth;
1960 float bottom = points [i + 1] + halfWidth;
1961 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001962 }
1963
1964 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1965}
1966
Romain Guy85bf02f2010-06-22 13:11:24 -07001967void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001968 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001969 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001970
Romain Guyae88e5e2010-10-22 17:49:18 -07001971 Rect& clip(*mSnapshot->clipRect);
1972 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001973
Romain Guy3d58c032010-07-14 16:34:53 -07001974 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001975}
Romain Guy9d5316e2010-06-24 19:30:36 -07001976
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001977void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001978 if (!texture) return;
1979 const AutoTexture autoCleanup(texture);
1980
1981 const float x = left + texture->left - texture->offset;
1982 const float y = top + texture->top - texture->offset;
1983
1984 drawPathTexture(texture, x, y, paint);
1985}
1986
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001987void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1988 float rx, float ry, SkPaint* paint) {
1989 if (mSnapshot->isIgnored()) return;
1990
1991 glActiveTexture(gTextureUnits[0]);
1992 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1993 right - left, bottom - top, rx, ry, paint);
1994 drawShape(left, top, texture, paint);
1995}
1996
Romain Guy01d58e42011-01-19 21:54:02 -08001997void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1998 if (mSnapshot->isIgnored()) return;
1999
2000 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002001 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002002 drawShape(x - radius, y - radius, texture, paint);
2003}
Romain Guy01d58e42011-01-19 21:54:02 -08002004
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002005void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2006 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002007
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002008 glActiveTexture(gTextureUnits[0]);
2009 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2010 drawShape(left, top, texture, paint);
2011}
2012
Romain Guy8b2f5262011-01-23 16:15:02 -08002013void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2014 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2015 if (mSnapshot->isIgnored()) return;
2016
2017 if (fabs(sweepAngle) >= 360.0f) {
2018 drawOval(left, top, right, bottom, paint);
2019 return;
2020 }
2021
2022 glActiveTexture(gTextureUnits[0]);
2023 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2024 startAngle, sweepAngle, useCenter, paint);
2025 drawShape(left, top, texture, paint);
2026}
2027
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002028void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2029 SkPaint* paint) {
2030 if (mSnapshot->isIgnored()) return;
2031
2032 glActiveTexture(gTextureUnits[0]);
2033 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2034 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002035}
2036
Chet Haase5c13d892010-10-08 08:37:55 -07002037void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002038 if (p->getStyle() != SkPaint::kFill_Style) {
2039 drawRectAsShape(left, top, right, bottom, p);
2040 return;
2041 }
2042
Romain Guy6926c722010-07-12 20:20:03 -07002043 if (quickReject(left, top, right, bottom)) {
2044 return;
2045 }
2046
Romain Guy026c5e162010-06-28 17:12:22 -07002047 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002048 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002049 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2050 if (!isMode) {
2051 // Assume SRC_OVER
2052 mode = SkXfermode::kSrcOver_Mode;
2053 }
2054 } else {
2055 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002056 }
2057
Romain Guy026c5e162010-06-28 17:12:22 -07002058 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002059 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002060 drawAARect(left, top, right, bottom, color, mode);
2061 } else {
2062 drawColorRect(left, top, right, bottom, color, mode);
2063 }
Romain Guyc7d53492010-06-25 13:41:57 -07002064}
Romain Guy9d5316e2010-06-24 19:30:36 -07002065
Romain Guye8e62a42010-07-23 18:55:21 -07002066void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002067 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002068 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002069 return;
2070 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002071 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002072
Romain Guy8f9a9f62011-12-05 11:53:26 -08002073 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002074
Romain Guye8e62a42010-07-23 18:55:21 -07002075 switch (paint->getTextAlign()) {
2076 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002077 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002078 x -= length / 2.0f;
2079 break;
2080 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002081 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002082 x -= length;
2083 break;
2084 default:
2085 break;
2086 }
2087
Romain Guycac5fd32011-12-01 20:08:50 -08002088 SkPaint::FontMetrics metrics;
2089 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002090 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002091 if (quickReject(x, y + metrics.fTop,
2092 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2093 return;
2094 }
2095
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002096 const float oldX = x;
2097 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002098 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2099 if (pureTranslate) {
2100 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2101 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2102 }
2103
Romain Guyb45c0c92010-08-26 20:35:23 -07002104 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002105#if DEBUG_GLYPHS
2106 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2107#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002108 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002109 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002110
Romain Guy86568192010-12-14 15:55:39 -08002111 int alpha;
2112 SkXfermode::Mode mode;
2113 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002114
Romain Guy1e45aae2010-08-13 19:39:53 -07002115 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002116 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002117 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2118 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002119 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002120
Romain Guy740bf2b2011-04-26 15:33:10 -07002121 const float sx = oldX - shadow->left + mShadowDx;
2122 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002123
Romain Guy86568192010-12-14 15:55:39 -08002124 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002125 int shadowColor = mShadowColor;
2126 if (mShader) {
2127 shadowColor = 0xffffffff;
2128 }
Romain Guy86568192010-12-14 15:55:39 -08002129
2130 glActiveTexture(gTextureUnits[0]);
2131 setupDraw();
2132 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002133 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2134 setupDrawColorFilter();
2135 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002136 setupDrawBlending(true, mode);
2137 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002138 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002139 setupDrawTexture(shadow->id);
2140 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002141 setupDrawColorFilterUniforms();
2142 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002143 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2144
Romain Guy0a417492010-08-16 20:26:20 -07002145 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002146
Romain Guy86568192010-12-14 15:55:39 -08002147 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002148 }
2149
Romain Guyb146b122010-12-15 17:06:45 -08002150 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2151 return;
2152 }
2153
Romain Guy6620c6d2010-12-06 18:07:02 -08002154 // Pick the appropriate texture filtering
2155 bool linearFilter = mSnapshot->transform->changesBounds();
2156 if (pureTranslate && !linearFilter) {
2157 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2158 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002159
Romain Guy86568192010-12-14 15:55:39 -08002160 glActiveTexture(gTextureUnits[0]);
2161 setupDraw();
2162 setupDrawDirtyRegionsDisabled();
2163 setupDrawWithTexture(true);
2164 setupDrawAlpha8Color(paint->getColor(), alpha);
2165 setupDrawColorFilter();
2166 setupDrawShader();
2167 setupDrawBlending(true, mode);
2168 setupDrawProgram();
2169 setupDrawModelView(x, y, x, y, pureTranslate, true);
2170 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2171 setupDrawPureColorUniforms();
2172 setupDrawColorFilterUniforms();
2173 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002174
Romain Guy6620c6d2010-12-06 18:07:02 -08002175 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002176 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2177
2178#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002179 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002180#else
Romain Guyf219da52011-01-16 12:54:25 -08002181 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002182#endif
Romain Guy03750a02010-10-18 14:06:08 -07002183 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002184
2185 // Tell font renderer the locations of position and texture coord
2186 // attributes so it can bind its data properly
2187 int positionSlot = mCaches.currentProgram->position;
2188 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002189 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002190 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002191#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002192 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002193 if (!pureTranslate) {
2194 mSnapshot->transform->mapRect(bounds);
2195 }
Romain Guyf219da52011-01-16 12:54:25 -08002196 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002197 }
2198#endif
2199 }
Romain Guy694b5192010-07-21 21:33:20 -07002200
2201 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002202 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002203
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002204 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002205}
2206
Romain Guy7fbcc042010-08-04 15:40:07 -07002207void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002208 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002209
Romain Guy01d58e42011-01-19 21:54:02 -08002210 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002211
Romain Guyfb8b7632010-08-23 21:05:08 -07002212 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002213 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002214 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002215
Romain Guy8b55f372010-08-18 17:10:07 -07002216 const float x = texture->left - texture->offset;
2217 const float y = texture->top - texture->offset;
2218
Romain Guy01d58e42011-01-19 21:54:02 -08002219 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002220}
2221
Romain Guyada830f2011-01-13 12:13:20 -08002222void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2223 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002224 return;
2225 }
2226
2227 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002228
2229 int alpha;
2230 SkXfermode::Mode mode;
2231 getAlphaAndMode(paint, &alpha, &mode);
2232
Romain Guy9ace8f52011-07-07 20:50:11 -07002233 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002234
Romain Guyf219da52011-01-16 12:54:25 -08002235#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002236 if (!layer->region.isEmpty()) {
2237 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002238 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002239 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002240 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002241 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002242
Romain Guyc88e3572011-01-22 00:32:12 -08002243 setupDraw();
2244 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002245 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002246 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002247 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002248 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002249 setupDrawPureColorUniforms();
2250 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002251 setupDrawTexture(layer->getTexture());
2252 if (mSnapshot->transform->isPureTranslate()) {
2253 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2254 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2255
Romain Guyd21b6e12011-11-30 20:21:23 -08002256 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002257 setupDrawModelViewTranslate(x, y,
2258 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2259 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002260 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002261 setupDrawModelViewTranslate(x, y,
2262 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2263 }
Romain Guyc88e3572011-01-22 00:32:12 -08002264 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002265
Romain Guyc88e3572011-01-22 00:32:12 -08002266 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2267 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002268
Romain Guyc88e3572011-01-22 00:32:12 -08002269 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002270
2271#if DEBUG_LAYERS_AS_REGIONS
2272 drawRegionRects(layer->region);
2273#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002274 }
Romain Guyf219da52011-01-16 12:54:25 -08002275 }
2276#else
Romain Guyada830f2011-01-13 12:13:20 -08002277 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2278 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002279#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002280}
2281
Romain Guy6926c722010-07-12 20:20:03 -07002282///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002283// Shaders
2284///////////////////////////////////////////////////////////////////////////////
2285
2286void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002287 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002288}
2289
Romain Guy06f96e22010-07-30 19:18:16 -07002290void OpenGLRenderer::setupShader(SkiaShader* shader) {
2291 mShader = shader;
2292 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002293 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002294 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002295}
2296
Romain Guyd27977d2010-07-14 19:18:51 -07002297///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002298// Color filters
2299///////////////////////////////////////////////////////////////////////////////
2300
2301void OpenGLRenderer::resetColorFilter() {
2302 mColorFilter = NULL;
2303}
2304
2305void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2306 mColorFilter = filter;
2307}
2308
2309///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002310// Drop shadow
2311///////////////////////////////////////////////////////////////////////////////
2312
2313void OpenGLRenderer::resetShadow() {
2314 mHasShadow = false;
2315}
2316
2317void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2318 mHasShadow = true;
2319 mShadowRadius = radius;
2320 mShadowDx = dx;
2321 mShadowDy = dy;
2322 mShadowColor = color;
2323}
2324
2325///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002326// Drawing implementation
2327///////////////////////////////////////////////////////////////////////////////
2328
Romain Guy01d58e42011-01-19 21:54:02 -08002329void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2330 float x, float y, SkPaint* paint) {
2331 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2332 return;
2333 }
2334
2335 int alpha;
2336 SkXfermode::Mode mode;
2337 getAlphaAndMode(paint, &alpha, &mode);
2338
2339 setupDraw();
2340 setupDrawWithTexture(true);
2341 setupDrawAlpha8Color(paint->getColor(), alpha);
2342 setupDrawColorFilter();
2343 setupDrawShader();
2344 setupDrawBlending(true, mode);
2345 setupDrawProgram();
2346 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2347 setupDrawTexture(texture->id);
2348 setupDrawPureColorUniforms();
2349 setupDrawColorFilterUniforms();
2350 setupDrawShaderUniforms();
2351 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2352
2353 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2354
2355 finishDrawTexture();
2356}
2357
Romain Guyf607bdc2010-09-10 19:20:06 -07002358// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002359#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2360#define kStdUnderline_Offset (1.0f / 9.0f)
2361#define kStdUnderline_Thickness (1.0f / 18.0f)
2362
2363void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2364 float x, float y, SkPaint* paint) {
2365 // Handle underline and strike-through
2366 uint32_t flags = paint->getFlags();
2367 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002368 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002369 float underlineWidth = length;
2370 // If length is > 0.0f, we already measured the text for the text alignment
2371 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002372 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002373 }
2374
2375 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002376 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002377 case SkPaint::kCenter_Align:
2378 offsetX = underlineWidth * 0.5f;
2379 break;
2380 case SkPaint::kRight_Align:
2381 offsetX = underlineWidth;
2382 break;
2383 default:
2384 break;
2385 }
2386
2387 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002388 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002389 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002390
Romain Guye20ecbd2010-09-22 19:49:04 -07002391 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002392 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002393
Romain Guyf6834472011-01-23 13:32:12 -08002394 int linesCount = 0;
2395 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2396 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2397
2398 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002399 float points[pointsCount];
2400 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002401
2402 if (flags & SkPaint::kUnderlineText_Flag) {
2403 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002404 points[currentPoint++] = left;
2405 points[currentPoint++] = top;
2406 points[currentPoint++] = left + underlineWidth;
2407 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002408 }
2409
2410 if (flags & SkPaint::kStrikeThruText_Flag) {
2411 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002412 points[currentPoint++] = left;
2413 points[currentPoint++] = top;
2414 points[currentPoint++] = left + underlineWidth;
2415 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002416 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002417
Romain Guy726aeba2011-06-01 14:52:00 -07002418 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002419
Romain Guy726aeba2011-06-01 14:52:00 -07002420 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002421 }
2422 }
2423}
2424
Romain Guy026c5e162010-06-28 17:12:22 -07002425void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002426 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002427 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002428 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002429 color |= 0x00ffffff;
2430 }
2431
Romain Guy70ca14e2010-12-13 18:24:33 -08002432 setupDraw();
2433 setupDrawColor(color);
2434 setupDrawShader();
2435 setupDrawColorFilter();
2436 setupDrawBlending(mode);
2437 setupDrawProgram();
2438 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2439 setupDrawColorUniforms();
2440 setupDrawShaderUniforms(ignoreTransform);
2441 setupDrawColorFilterUniforms();
2442 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002443
Romain Guyc95c8d62010-09-17 15:31:32 -07002444 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2445}
2446
Romain Guy82ba8142010-07-09 13:25:56 -07002447void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002448 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002449 int alpha;
2450 SkXfermode::Mode mode;
2451 getAlphaAndMode(paint, &alpha, &mode);
2452
Romain Guyd21b6e12011-11-30 20:21:23 -08002453 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002454
Romain Guy6620c6d2010-12-06 18:07:02 -08002455 if (mSnapshot->transform->isPureTranslate()) {
2456 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2457 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2458
Romain Guyd21b6e12011-11-30 20:21:23 -08002459 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002460 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2461 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2462 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2463 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002464 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002465 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2466 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2467 GL_TRIANGLE_STRIP, gMeshCount);
2468 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002469}
2470
Romain Guybd6b79b2010-06-26 00:13:53 -07002471void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002472 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2473 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002474 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002475}
2476
2477void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002478 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002479 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002480 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002481
Romain Guy746b7402010-10-26 16:27:31 -07002482 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002483 setupDrawWithTexture();
2484 setupDrawColor(alpha, alpha, alpha, alpha);
2485 setupDrawColorFilter();
2486 setupDrawBlending(blend, mode, swapSrcDst);
2487 setupDrawProgram();
2488 if (!dirty) {
2489 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002490 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002491 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002492 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002493 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002494 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002495 }
Romain Guy86568192010-12-14 15:55:39 -08002496 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002497 setupDrawColorFilterUniforms();
2498 setupDrawTexture(texture);
2499 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002500
Romain Guy6820ac82010-09-15 18:11:50 -07002501 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002502
2503 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002504}
2505
Romain Guya5aed0d2010-09-09 14:42:43 -07002506void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002507 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002508 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2509 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002510 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002511 if (!mCaches.blend) {
2512 glEnable(GL_BLEND);
2513 }
Romain Guy82ba8142010-07-09 13:25:56 -07002514
Romain Guyf607bdc2010-09-10 19:20:06 -07002515 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2516 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002517
Romain Guya5aed0d2010-09-09 14:42:43 -07002518 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2519 glBlendFunc(sourceMode, destMode);
2520 mCaches.lastSrcMode = sourceMode;
2521 mCaches.lastDstMode = destMode;
2522 }
2523 } else {
2524 // These blend modes are not supported by OpenGL directly and have
2525 // to be implemented using shaders. Since the shader will perform
2526 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002527 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002528 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002529 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002530 }
2531
2532 if (mCaches.blend) {
2533 glDisable(GL_BLEND);
2534 }
2535 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002536 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002537 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002538 glDisable(GL_BLEND);
2539 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002540 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002541}
2542
Romain Guy889f8d12010-07-29 14:37:42 -07002543bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002544 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002545 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002546 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002547 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002548 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002549 }
Romain Guy6926c722010-07-12 20:20:03 -07002550 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002551}
2552
Romain Guy026c5e162010-06-28 17:12:22 -07002553void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002554 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002555 TextureVertex::setUV(v++, u1, v1);
2556 TextureVertex::setUV(v++, u2, v1);
2557 TextureVertex::setUV(v++, u1, v2);
2558 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002559}
2560
Chet Haase5c13d892010-10-08 08:37:55 -07002561void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002562 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002563 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002564
2565 // Skia draws using the color's alpha channel if < 255
2566 // Otherwise, it uses the paint's alpha
2567 int color = paint->getColor();
2568 *alpha = (color >> 24) & 0xFF;
2569 if (*alpha == 255) {
2570 *alpha = paint->getAlpha();
2571 }
2572 } else {
2573 *mode = SkXfermode::kSrcOver_Mode;
2574 *alpha = 255;
2575 }
Romain Guy026c5e162010-06-28 17:12:22 -07002576}
2577
Romain Guya5aed0d2010-09-09 14:42:43 -07002578SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002579 SkXfermode::Mode resultMode;
2580 if (!SkXfermode::AsMode(mode, &resultMode)) {
2581 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002582 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002583 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002584}
2585
Romain Guy9d5316e2010-06-24 19:30:36 -07002586}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002587}; // namespace android