blob: fa17aad1b8b6d80ed4128dfe7a6d7f170b93e093 [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
144 mDirtyClip = false;
Romain Guy3e263fa2011-12-12 16:47:48 -0800145
146 glDisable(GL_DITHER);
147 glViewport(0, 0, width, height);
148
149 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
150
151 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700152}
153
Romain Guy6b7bd242010-10-06 19:49:23 -0700154void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800155 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
156}
157
158void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800159 mCaches.clearGarbage();
160
Romain Guy8aef54f2010-09-01 15:13:49 -0700161 mSnapshot = new Snapshot(mFirstSnapshot,
162 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800163 mSnapshot->fbo = getTargetFbo();
164
Romain Guy8fb95422010-08-17 18:38:51 -0700165 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700166
Romain Guy7d7b5492011-01-24 16:33:45 -0800167 glEnable(GL_SCISSOR_TEST);
168 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
169 mSnapshot->setClip(left, top, right, bottom);
170
Romain Guy6b7bd242010-10-06 19:49:23 -0700171 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700172 glClear(GL_COLOR_BUFFER_BIT);
173 }
Romain Guybb9524b2010-06-22 18:56:38 -0700174}
175
Romain Guyb025b9c2010-09-16 14:16:48 -0700176void OpenGLRenderer::finish() {
177#if DEBUG_OPENGL
178 GLenum status = GL_NO_ERROR;
179 while ((status = glGetError()) != GL_NO_ERROR) {
180 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800181 switch (status) {
182 case GL_OUT_OF_MEMORY:
183 LOGE(" OpenGLRenderer is out of memory!");
184 break;
185 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700186 }
187#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800188#if DEBUG_MEMORY_USAGE
189 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800190#else
191 if (mCaches.getDebugLevel() & kDebugMemory) {
192 mCaches.dumpMemoryUsage();
193 }
Romain Guyc15008e2010-11-10 11:59:15 -0800194#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700195}
196
Romain Guy6c319ca2011-01-11 14:29:25 -0800197void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700198 if (mCaches.currentProgram) {
199 if (mCaches.currentProgram->isInUse()) {
200 mCaches.currentProgram->remove();
201 mCaches.currentProgram = NULL;
202 }
203 }
Romain Guy50c0f092010-10-19 11:42:22 -0700204 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700205}
206
Romain Guy6c319ca2011-01-11 14:29:25 -0800207void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800208 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
209
210 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700211
Romain Guy3e263fa2011-12-12 16:47:48 -0800212 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
213
Romain Guyda8532c2010-08-31 11:50:35 -0700214 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700215 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700216
Romain Guyf607bdc2010-09-10 19:20:06 -0700217 glDisable(GL_DITHER);
218
Chet Haase08837c22011-11-28 11:53:21 -0800219 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy03750a02010-10-18 14:06:08 -0700220 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700221
Romain Guy3e263fa2011-12-12 16:47:48 -0800222 glEnableVertexAttribArray(Program::kBindingPosition);
223
Romain Guy50c0f092010-10-19 11:42:22 -0700224 mCaches.blend = true;
225 glEnable(GL_BLEND);
226 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
227 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700228}
229
Romain Guycabfcc12011-03-07 18:06:46 -0800230bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800231 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800232 if (mDirtyClip) {
233 setScissorFromClip();
234 }
Romain Guyd643bb52011-03-01 14:55:21 -0800235
Romain Guy80911b82011-03-16 15:30:12 -0700236 Rect clip(*mSnapshot->clipRect);
237 clip.snapToPixelBoundaries();
238
Romain Guyd643bb52011-03-01 14:55:21 -0800239#if RENDER_LAYERS_AS_REGIONS
240 // Since we don't know what the functor will draw, let's dirty
241 // tne entire clip region
242 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800243 dirtyLayerUnchecked(clip, getRegion());
244 }
245#endif
246
Romain Guy08aa2cb2011-03-17 11:06:57 -0700247 DrawGlInfo info;
248 info.clipLeft = clip.left;
249 info.clipTop = clip.top;
250 info.clipRight = clip.right;
251 info.clipBottom = clip.bottom;
252 info.isLayer = hasLayer();
253 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700254
Romain Guy08aa2cb2011-03-17 11:06:57 -0700255 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800256
257 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700258 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800259 dirty.unionWith(localDirty);
260 }
261
Chet Haasedaf98e92011-01-10 14:10:36 -0800262 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800263 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800264}
265
Romain Guyf6a11b82010-06-23 17:47:49 -0700266///////////////////////////////////////////////////////////////////////////////
267// State management
268///////////////////////////////////////////////////////////////////////////////
269
Romain Guybb9524b2010-06-22 18:56:38 -0700270int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700271 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700272}
273
274int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700275 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700276}
277
278void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700279 if (mSaveCount > 1) {
280 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700281 }
Romain Guybb9524b2010-06-22 18:56:38 -0700282}
283
284void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700285 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700286
Romain Guy8fb95422010-08-17 18:38:51 -0700287 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700288 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700289 }
Romain Guybb9524b2010-06-22 18:56:38 -0700290}
291
Romain Guy8aef54f2010-09-01 15:13:49 -0700292int OpenGLRenderer::saveSnapshot(int flags) {
293 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700294 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700295}
296
297bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700298 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700299 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700300 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700301
Romain Guybd6b79b2010-06-26 00:13:53 -0700302 sp<Snapshot> current = mSnapshot;
303 sp<Snapshot> previous = mSnapshot->previous;
304
Romain Guyeb993562010-10-05 18:14:38 -0700305 if (restoreOrtho) {
306 Rect& r = previous->viewport;
307 glViewport(r.left, r.top, r.right, r.bottom);
308 mOrthoMatrix.load(current->orthoMatrix);
309 }
310
Romain Guy8b55f372010-08-18 17:10:07 -0700311 mSaveCount--;
312 mSnapshot = previous;
313
Romain Guy2542d192010-08-18 11:47:12 -0700314 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700315 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700316 }
Romain Guy2542d192010-08-18 11:47:12 -0700317
Romain Guy5ec99242010-11-03 16:19:08 -0700318 if (restoreLayer) {
319 composeLayer(current, previous);
320 }
321
Romain Guy2542d192010-08-18 11:47:12 -0700322 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700323}
324
Romain Guyf6a11b82010-06-23 17:47:49 -0700325///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700326// Layers
327///////////////////////////////////////////////////////////////////////////////
328
329int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700330 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700331 const GLuint previousFbo = mSnapshot->fbo;
332 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700333
Romain Guyaf636eb2010-12-09 17:47:21 -0800334 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700335 int alpha = 255;
336 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700337
Romain Guye45362c2010-11-03 19:58:32 -0700338 if (p) {
339 alpha = p->getAlpha();
340 if (!mCaches.extensions.hasFramebufferFetch()) {
341 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
342 if (!isMode) {
343 // Assume SRC_OVER
344 mode = SkXfermode::kSrcOver_Mode;
345 }
346 } else {
347 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700348 }
349 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700350 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700351 }
Romain Guyd55a8612010-06-28 17:42:46 -0700352
Romain Guydbc26d22010-10-11 17:58:29 -0700353 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
354 }
Romain Guyd55a8612010-06-28 17:42:46 -0700355
356 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700357}
358
359int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
360 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700361 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700362 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700363 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700364 SkPaint paint;
365 paint.setAlpha(alpha);
366 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700367 }
Romain Guyd55a8612010-06-28 17:42:46 -0700368}
Romain Guybd6b79b2010-06-26 00:13:53 -0700369
Romain Guy1c740bc2010-09-13 18:00:09 -0700370/**
371 * Layers are viewed by Skia are slightly different than layers in image editing
372 * programs (for instance.) When a layer is created, previously created layers
373 * and the frame buffer still receive every drawing command. For instance, if a
374 * layer is created and a shape intersecting the bounds of the layers and the
375 * framebuffer is draw, the shape will be drawn on both (unless the layer was
376 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
377 *
378 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
379 * texture. Unfortunately, this is inefficient as it requires every primitive to
380 * be drawn n + 1 times, where n is the number of active layers. In practice this
381 * means, for every primitive:
382 * - Switch active frame buffer
383 * - Change viewport, clip and projection matrix
384 * - Issue the drawing
385 *
386 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700387 * To avoid this, layers are implemented in a different way here, at least in the
388 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
389 * is set. When this flag is set we can redirect all drawing operations into a
390 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700391 *
392 * This implementation relies on the frame buffer being at least RGBA 8888. When
393 * a layer is created, only a texture is created, not an FBO. The content of the
394 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700395 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700396 * buffer and drawing continues as normal. This technique therefore treats the
397 * frame buffer as a scratch buffer for the layers.
398 *
399 * To compose the layers back onto the frame buffer, each layer texture
400 * (containing the original frame buffer data) is drawn as a simple quad over
401 * the frame buffer. The trick is that the quad is set as the composition
402 * destination in the blending equation, and the frame buffer becomes the source
403 * of the composition.
404 *
405 * Drawing layers with an alpha value requires an extra step before composition.
406 * An empty quad is drawn over the layer's region in the frame buffer. This quad
407 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
408 * quad is used to multiply the colors in the frame buffer. This is achieved by
409 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
410 * GL_ZERO, GL_SRC_ALPHA.
411 *
412 * Because glCopyTexImage2D() can be slow, an alternative implementation might
413 * be use to draw a single clipped layer. The implementation described above
414 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700415 *
416 * (1) The frame buffer is actually not cleared right away. To allow the GPU
417 * to potentially optimize series of calls to glCopyTexImage2D, the frame
418 * buffer is left untouched until the first drawing operation. Only when
419 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700420 */
Romain Guyd55a8612010-06-28 17:42:46 -0700421bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700422 float right, float bottom, int alpha, SkXfermode::Mode mode,
423 int flags, GLuint previousFbo) {
424 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700425 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700426
Romain Guyeb993562010-10-05 18:14:38 -0700427 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
428
Romain Guyf607bdc2010-09-10 19:20:06 -0700429 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700430 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700431 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700432 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700433
Romain Guyeb993562010-10-05 18:14:38 -0700434 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700435 if (bounds.intersect(*snapshot->clipRect)) {
436 // We cannot work with sub-pixels in this case
437 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700438
Romain Guyad37cd32011-03-15 11:12:25 -0700439 // When the layer is not an FBO, we may use glCopyTexImage so we
440 // need to make sure the layer does not extend outside the bounds
441 // of the framebuffer
442 if (!bounds.intersect(snapshot->previous->viewport)) {
443 bounds.setEmpty();
444 }
445 } else {
446 bounds.setEmpty();
447 }
Romain Guyeb993562010-10-05 18:14:38 -0700448 }
Romain Guybf434112010-09-16 14:40:17 -0700449
Romain Guy746b7402010-10-26 16:27:31 -0700450 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
451 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800452 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700453 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700454 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700455 }
456
457 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800458 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700459 return false;
460 }
Romain Guyf18fd992010-07-08 11:45:51 -0700461
Romain Guy5b3b3522010-10-27 18:57:51 -0700462 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700463 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700464 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700465 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700466 }
467
Romain Guy9ace8f52011-07-07 20:50:11 -0700468 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700469 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700470 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
471 bounds.getWidth() / float(layer->getWidth()), 0.0f);
472 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700473
Romain Guy8fb95422010-08-17 18:38:51 -0700474 // Save the layer in the snapshot
475 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700476 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700477
Romain Guyeb993562010-10-05 18:14:38 -0700478 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700479 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700480 } else {
481 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700482 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800483 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700484 if (layer->isEmpty()) {
485 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
486 bounds.left, snapshot->height - bounds.bottom,
487 layer->getWidth(), layer->getHeight(), 0);
488 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800489 } else {
490 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
491 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
492 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700493
Romain Guy54be1cd2011-06-13 19:04:27 -0700494 // Enqueue the buffer coordinates to clear the corresponding region later
495 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700496 }
Romain Guyeb993562010-10-05 18:14:38 -0700497 }
Romain Guyf86ef572010-07-01 11:05:42 -0700498
Romain Guyd55a8612010-06-28 17:42:46 -0700499 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700500}
501
Romain Guy5b3b3522010-10-27 18:57:51 -0700502bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
503 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700504 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700505
506#if RENDER_LAYERS_AS_REGIONS
507 snapshot->region = &snapshot->layer->region;
508 snapshot->flags |= Snapshot::kFlagFboTarget;
509#endif
510
511 Rect clip(bounds);
512 snapshot->transform->mapRect(clip);
513 clip.intersect(*snapshot->clipRect);
514 clip.snapToPixelBoundaries();
515 clip.intersect(snapshot->previous->viewport);
516
517 mat4 inverse;
518 inverse.loadInverse(*mSnapshot->transform);
519
520 inverse.mapRect(clip);
521 clip.snapToPixelBoundaries();
522 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700523 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700524
525 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700526 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700527 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700528 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
529 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
530 snapshot->height = bounds.getHeight();
531 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
532 snapshot->orthoMatrix.load(mOrthoMatrix);
533
534 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
536 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700537
538 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700539 if (layer->isEmpty()) {
540 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
541 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700542 }
543
544 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700545 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700546
547#if DEBUG_LAYERS_AS_REGIONS
548 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
549 if (status != GL_FRAMEBUFFER_COMPLETE) {
550 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
551
552 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700553 layer->deleteTexture();
554 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700555
556 delete layer;
557
558 return false;
559 }
560#endif
561
562 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
563 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
564 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700565 glClear(GL_COLOR_BUFFER_BIT);
566
567 dirtyClip();
568
569 // Change the ortho projection
570 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
571 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
572
573 return true;
574}
575
Romain Guy1c740bc2010-09-13 18:00:09 -0700576/**
577 * Read the documentation of createLayer() before doing anything in this method.
578 */
Romain Guy1d83e192010-08-17 11:37:00 -0700579void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
580 if (!current->layer) {
581 LOGE("Attempting to compose a layer that does not exist");
582 return;
583 }
584
Romain Guy5b3b3522010-10-27 18:57:51 -0700585 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700586
587 if (fboLayer) {
588 // Unbind current FBO and restore previous one
589 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
590 }
591
Romain Guy1d83e192010-08-17 11:37:00 -0700592 Layer* layer = current->layer;
593 const Rect& rect = layer->layer;
594
Romain Guy9ace8f52011-07-07 20:50:11 -0700595 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700596 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700597 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700598 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700599 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700600 }
601
Romain Guy03750a02010-10-18 14:06:08 -0700602 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700603
Romain Guy746b7402010-10-26 16:27:31 -0700604 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700605
Romain Guy5b3b3522010-10-27 18:57:51 -0700606 // When the layer is stored in an FBO, we can save a bit of fillrate by
607 // drawing only the dirty region
608 if (fboLayer) {
609 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700610 if (layer->getColorFilter()) {
611 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800612 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700613 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700614 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800615 resetColorFilter();
616 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700617 } else if (!rect.isEmpty()) {
618 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
619 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700620 }
Romain Guy8b55f372010-08-18 17:10:07 -0700621
Romain Guyeb993562010-10-05 18:14:38 -0700622 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800623 // Note: No need to use glDiscardFramebufferEXT() since we never
624 // create/compose layers that are not on screen with this
625 // code path
626 // See LayerRenderer::destroyLayer(Layer*)
627
Romain Guyeb993562010-10-05 18:14:38 -0700628 // Detach the texture from the FBO
629 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
630 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
631 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
632
633 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
634 mCaches.fboCache.put(current->fbo);
635 }
636
Romain Guy746b7402010-10-26 16:27:31 -0700637 dirtyClip();
638
Romain Guyeb993562010-10-05 18:14:38 -0700639 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700640 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700641 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700642 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700643 delete layer;
644 }
645}
646
Romain Guyaa6c24c2011-04-28 18:40:04 -0700647void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700648 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700649
Romain Guy302a9df2011-08-16 13:55:02 -0700650 mat4& transform = layer->getTransform();
651 if (!transform.isIdentity()) {
652 save(0);
653 mSnapshot->transform->multiply(transform);
654 }
655
Romain Guyaa6c24c2011-04-28 18:40:04 -0700656 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700657 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700658 setupDrawWithTexture();
659 } else {
660 setupDrawWithExternalTexture();
661 }
662 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700663 setupDrawColor(alpha, alpha, alpha, alpha);
664 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700666 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700667 setupDrawPureColorUniforms();
668 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700669 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
670 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700671 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700672 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700673 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700674 if (mSnapshot->transform->isPureTranslate() &&
675 layer->getWidth() == (uint32_t) rect.getWidth() &&
676 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
678 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
679
Romain Guyd21b6e12011-11-30 20:21:23 -0800680 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700681 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
682 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800683 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700684 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
685 }
686 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700687 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
688
689 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
690
691 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700692
693 if (!transform.isIdentity()) {
694 restore();
695 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700696}
697
Romain Guy5b3b3522010-10-27 18:57:51 -0700698void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700700 const Rect& texCoords = layer->texCoords;
701 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
702 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700703
Romain Guy9ace8f52011-07-07 20:50:11 -0700704 float x = rect.left;
705 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700706 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700707 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700708 layer->getHeight() == (uint32_t) rect.getHeight();
709
710 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700711 // When we're swapping, the layer is already in screen coordinates
712 if (!swap) {
713 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
714 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
715 }
716
Romain Guyd21b6e12011-11-30 20:21:23 -0800717 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700718 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800719 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700720 }
721
722 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
723 layer->getTexture(), layer->getAlpha() / 255.0f,
724 layer->getMode(), layer->isBlend(),
725 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
726 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700727
Romain Guyaa6c24c2011-04-28 18:40:04 -0700728 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
729 } else {
730 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
731 drawTextureLayer(layer, rect);
732 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
733 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700734}
735
736void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
737#if RENDER_LAYERS_AS_REGIONS
738 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700739 layer->setRegionAsRect();
740
Romain Guy40667672011-03-18 14:34:03 -0700741 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700742
Romain Guy5b3b3522010-10-27 18:57:51 -0700743 layer->region.clear();
744 return;
745 }
746
Romain Guy8a3957d2011-09-07 17:55:15 -0700747 // TODO: See LayerRenderer.cpp::generateMesh() for important
748 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700749 if (!layer->region.isEmpty()) {
750 size_t count;
751 const android::Rect* rects = layer->region.getArray(&count);
752
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 const float alpha = layer->getAlpha() / 255.0f;
754 const float texX = 1.0f / float(layer->getWidth());
755 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800756 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700757
758 TextureVertex* mesh = mCaches.getRegionMesh();
759 GLsizei numQuads = 0;
760
Romain Guy7230a742011-01-10 22:26:16 -0800761 setupDraw();
762 setupDrawWithTexture();
763 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800764 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800766 setupDrawProgram();
767 setupDrawDirtyRegionsDisabled();
768 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800769 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700770 setupDrawTexture(layer->getTexture());
771 if (mSnapshot->transform->isPureTranslate()) {
772 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
773 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
774
Romain Guyd21b6e12011-11-30 20:21:23 -0800775 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700776 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
777 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800778 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700779 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
780 }
Romain Guy7230a742011-01-10 22:26:16 -0800781 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700782
783 for (size_t i = 0; i < count; i++) {
784 const android::Rect* r = &rects[i];
785
786 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800787 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700788 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800789 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700790
791 // TODO: Reject quads outside of the clip
792 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
793 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
794 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
795 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
796
797 numQuads++;
798
799 if (numQuads >= REGION_MESH_QUAD_COUNT) {
800 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
801 numQuads = 0;
802 mesh = mCaches.getRegionMesh();
803 }
804 }
805
806 if (numQuads > 0) {
807 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
808 }
809
810 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800811 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700812
813#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800814 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700815#endif
816
817 layer->region.clear();
818 }
819#else
820 composeLayerRect(layer, rect);
821#endif
822}
823
Romain Guy3a3133d2011-02-01 22:59:58 -0800824void OpenGLRenderer::drawRegionRects(const Region& region) {
825#if DEBUG_LAYERS_AS_REGIONS
826 size_t count;
827 const android::Rect* rects = region.getArray(&count);
828
829 uint32_t colors[] = {
830 0x7fff0000, 0x7f00ff00,
831 0x7f0000ff, 0x7fff00ff,
832 };
833
834 int offset = 0;
835 int32_t top = rects[0].top;
836
837 for (size_t i = 0; i < count; i++) {
838 if (top != rects[i].top) {
839 offset ^= 0x2;
840 top = rects[i].top;
841 }
842
843 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
844 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
845 SkXfermode::kSrcOver_Mode);
846 }
847#endif
848}
849
Romain Guy5b3b3522010-10-27 18:57:51 -0700850void OpenGLRenderer::dirtyLayer(const float left, const float top,
851 const float right, const float bottom, const mat4 transform) {
852#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800853 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700854 Rect bounds(left, top, right, bottom);
855 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800856 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700857 }
858#endif
859}
860
861void OpenGLRenderer::dirtyLayer(const float left, const float top,
862 const float right, const float bottom) {
863#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800864 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800866 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800867 }
868#endif
869}
870
871void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
872#if RENDER_LAYERS_AS_REGIONS
873 if (bounds.intersect(*mSnapshot->clipRect)) {
874 bounds.snapToPixelBoundaries();
875 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
876 if (!dirty.isEmpty()) {
877 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700878 }
879 }
880#endif
881}
882
Romain Guy54be1cd2011-06-13 19:04:27 -0700883void OpenGLRenderer::clearLayerRegions() {
884 const size_t count = mLayers.size();
885 if (count == 0) return;
886
887 if (!mSnapshot->isIgnored()) {
888 // Doing several glScissor/glClear here can negatively impact
889 // GPUs with a tiler architecture, instead we draw quads with
890 // the Clear blending mode
891
892 // The list contains bounds that have already been clipped
893 // against their initial clip rect, and the current clip
894 // is likely different so we need to disable clipping here
895 glDisable(GL_SCISSOR_TEST);
896
897 Vertex mesh[count * 6];
898 Vertex* vertex = mesh;
899
900 for (uint32_t i = 0; i < count; i++) {
901 Rect* bounds = mLayers.itemAt(i);
902
903 Vertex::set(vertex++, bounds->left, bounds->bottom);
904 Vertex::set(vertex++, bounds->left, bounds->top);
905 Vertex::set(vertex++, bounds->right, bounds->top);
906 Vertex::set(vertex++, bounds->left, bounds->bottom);
907 Vertex::set(vertex++, bounds->right, bounds->top);
908 Vertex::set(vertex++, bounds->right, bounds->bottom);
909
910 delete bounds;
911 }
912
913 setupDraw(false);
914 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
915 setupDrawBlending(true, SkXfermode::kClear_Mode);
916 setupDrawProgram();
917 setupDrawPureColorUniforms();
918 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
919
920 mCaches.unbindMeshBuffer();
921 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
922 gVertexStride, &mesh[0].position[0]);
923 glDrawArrays(GL_TRIANGLES, 0, count * 6);
924
925 glEnable(GL_SCISSOR_TEST);
926 } else {
927 for (uint32_t i = 0; i < count; i++) {
928 delete mLayers.itemAt(i);
929 }
930 }
931
932 mLayers.clear();
933}
934
Romain Guybd6b79b2010-06-26 00:13:53 -0700935///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700936// Transforms
937///////////////////////////////////////////////////////////////////////////////
938
939void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700940 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700941}
942
943void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700944 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700945}
946
947void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700948 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700949}
950
Romain Guy807daf72011-01-18 11:19:19 -0800951void OpenGLRenderer::skew(float sx, float sy) {
952 mSnapshot->transform->skew(sx, sy);
953}
954
Romain Guyf6a11b82010-06-23 17:47:49 -0700955void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700956 if (matrix) {
957 mSnapshot->transform->load(*matrix);
958 } else {
959 mSnapshot->transform->loadIdentity();
960 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700961}
962
963void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700964 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700965}
966
967void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700968 SkMatrix transform;
969 mSnapshot->transform->copyTo(transform);
970 transform.preConcat(*matrix);
971 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700972}
973
974///////////////////////////////////////////////////////////////////////////////
975// Clipping
976///////////////////////////////////////////////////////////////////////////////
977
Romain Guybb9524b2010-06-22 18:56:38 -0700978void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700979 Rect clip(*mSnapshot->clipRect);
980 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700981 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700982 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700983}
984
985const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700986 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700987}
988
Romain Guyc7d53492010-06-25 13:41:57 -0700989bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800990 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700991 return true;
992 }
993
Romain Guy1d83e192010-08-17 11:37:00 -0700994 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700995 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700996 r.snapToPixelBoundaries();
997
998 Rect clipRect(*mSnapshot->clipRect);
999 clipRect.snapToPixelBoundaries();
1000
1001 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -07001002}
1003
Romain Guy079ba2c2010-07-16 14:12:24 -07001004bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1005 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001006 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001007 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001008 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001009 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001010}
1011
Romain Guyf6a11b82010-06-23 17:47:49 -07001012///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001013// Drawing commands
1014///////////////////////////////////////////////////////////////////////////////
1015
Romain Guy54be1cd2011-06-13 19:04:27 -07001016void OpenGLRenderer::setupDraw(bool clear) {
1017 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001018 if (mDirtyClip) {
1019 setScissorFromClip();
1020 }
1021 mDescription.reset();
1022 mSetShaderColor = false;
1023 mColorSet = false;
1024 mColorA = mColorR = mColorG = mColorB = 0.0f;
1025 mTextureUnit = 0;
1026 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001027 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001028}
1029
1030void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1031 mDescription.hasTexture = true;
1032 mDescription.hasAlpha8Texture = isAlpha8;
1033}
1034
Romain Guyaa6c24c2011-04-28 18:40:04 -07001035void OpenGLRenderer::setupDrawWithExternalTexture() {
1036 mDescription.hasExternalTexture = true;
1037}
1038
Chet Haase5b0200b2011-04-13 17:58:08 -07001039void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001040 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001041}
1042
Romain Guyed6fcb02011-03-21 13:11:28 -07001043void OpenGLRenderer::setupDrawPoint(float pointSize) {
1044 mDescription.isPoint = true;
1045 mDescription.pointSize = pointSize;
1046}
1047
Romain Guy70ca14e2010-12-13 18:24:33 -08001048void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001049 setupDrawColor(color, (color >> 24) & 0xFF);
1050}
1051
1052void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1053 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001054 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1055 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001056 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001057 mColorR = a * ((color >> 16) & 0xFF);
1058 mColorG = a * ((color >> 8) & 0xFF);
1059 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001060 mColorSet = true;
1061 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1062}
1063
Romain Guy86568192010-12-14 15:55:39 -08001064void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1065 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001066 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1067 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001068 const float a = mColorA / 255.0f;
1069 mColorR = a * ((color >> 16) & 0xFF);
1070 mColorG = a * ((color >> 8) & 0xFF);
1071 mColorB = a * ((color ) & 0xFF);
1072 mColorSet = true;
1073 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1074}
1075
Romain Guy70ca14e2010-12-13 18:24:33 -08001076void OpenGLRenderer::setupDrawColor(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.setColor(r, g, b, a);
1083}
1084
Romain Guy86568192010-12-14 15:55:39 -08001085void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1086 mColorA = a;
1087 mColorR = r;
1088 mColorG = g;
1089 mColorB = b;
1090 mColorSet = true;
1091 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1092}
1093
Romain Guy70ca14e2010-12-13 18:24:33 -08001094void OpenGLRenderer::setupDrawShader() {
1095 if (mShader) {
1096 mShader->describe(mDescription, mCaches.extensions);
1097 }
1098}
1099
1100void OpenGLRenderer::setupDrawColorFilter() {
1101 if (mColorFilter) {
1102 mColorFilter->describe(mDescription, mCaches.extensions);
1103 }
1104}
1105
Romain Guyf09ef512011-05-27 11:43:46 -07001106void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1107 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1108 mColorA = 1.0f;
1109 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001110 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001111 }
1112}
1113
Romain Guy70ca14e2010-12-13 18:24:33 -08001114void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001115 // When the blending mode is kClear_Mode, we need to use a modulate color
1116 // argb=1,0,0,0
1117 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001118 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1119 mDescription, swapSrcDst);
1120}
1121
1122void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001123 // When the blending mode is kClear_Mode, we need to use a modulate color
1124 // argb=1,0,0,0
1125 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001126 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1127 mDescription, swapSrcDst);
1128}
1129
1130void OpenGLRenderer::setupDrawProgram() {
1131 useProgram(mCaches.programCache.get(mDescription));
1132}
1133
1134void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1135 mTrackDirtyRegions = false;
1136}
1137
1138void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1139 bool ignoreTransform) {
1140 mModelView.loadTranslate(left, top, 0.0f);
1141 if (!ignoreTransform) {
1142 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1143 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1144 } else {
1145 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1146 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1147 }
1148}
1149
Chet Haase8a5cc922011-04-26 07:28:09 -07001150void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1151 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001152}
1153
Romain Guy70ca14e2010-12-13 18:24:33 -08001154void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1155 bool ignoreTransform, bool ignoreModelView) {
1156 if (!ignoreModelView) {
1157 mModelView.loadTranslate(left, top, 0.0f);
1158 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001159 } else {
1160 mModelView.loadIdentity();
1161 }
Romain Guy86568192010-12-14 15:55:39 -08001162 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1163 if (!ignoreTransform) {
1164 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1165 if (mTrackDirtyRegions && dirty) {
1166 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1167 }
1168 } else {
1169 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1170 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1171 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001172}
1173
Romain Guyed6fcb02011-03-21 13:11:28 -07001174void OpenGLRenderer::setupDrawPointUniforms() {
1175 int slot = mCaches.currentProgram->getUniform("pointSize");
1176 glUniform1f(slot, mDescription.pointSize);
1177}
1178
Romain Guy70ca14e2010-12-13 18:24:33 -08001179void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001180 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001181 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1182 }
1183}
1184
Romain Guy86568192010-12-14 15:55:39 -08001185void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001186 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001187 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001188 }
1189}
1190
Romain Guy70ca14e2010-12-13 18:24:33 -08001191void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1192 if (mShader) {
1193 if (ignoreTransform) {
1194 mModelView.loadInverse(*mSnapshot->transform);
1195 }
1196 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1197 }
1198}
1199
Romain Guy8d0d4782010-12-14 20:13:35 -08001200void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1201 if (mShader) {
1202 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1203 }
1204}
1205
Romain Guy70ca14e2010-12-13 18:24:33 -08001206void OpenGLRenderer::setupDrawColorFilterUniforms() {
1207 if (mColorFilter) {
1208 mColorFilter->setupProgram(mCaches.currentProgram);
1209 }
1210}
1211
1212void OpenGLRenderer::setupDrawSimpleMesh() {
1213 mCaches.bindMeshBuffer();
1214 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1215 gMeshStride, 0);
1216}
1217
1218void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1219 bindTexture(texture);
1220 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1221
1222 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1223 glEnableVertexAttribArray(mTexCoordsSlot);
1224}
1225
Romain Guyaa6c24c2011-04-28 18:40:04 -07001226void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1227 bindExternalTexture(texture);
1228 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1229
1230 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1231 glEnableVertexAttribArray(mTexCoordsSlot);
1232}
1233
Romain Guy8f0095c2011-05-02 17:24:22 -07001234void OpenGLRenderer::setupDrawTextureTransform() {
1235 mDescription.hasTextureTransform = true;
1236}
1237
1238void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001239 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1240 GL_FALSE, &transform.data[0]);
1241}
1242
Romain Guy70ca14e2010-12-13 18:24:33 -08001243void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1244 if (!vertices) {
1245 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1246 } else {
1247 mCaches.unbindMeshBuffer();
1248 }
1249 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1250 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001251 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001252 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1253 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001254}
1255
Chet Haase5b0200b2011-04-13 17:58:08 -07001256void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1257 mCaches.unbindMeshBuffer();
1258 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1259 gVertexStride, vertices);
1260}
1261
1262/**
1263 * 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 -07001264 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1265 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1266 * attributes (one per vertex) are values from zero to one that tells the fragment
1267 * shader where the fragment is in relation to the line width/length overall; these values are
1268 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1269 * region of the line.
1270 * Note that we only pass down the width values in this setup function. The length coordinates
1271 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001272 */
Chet Haase99585ad2011-05-02 15:00:16 -07001273void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001274 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001275 mCaches.unbindMeshBuffer();
1276 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001277 gAAVertexStride, vertices);
1278 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1279 glEnableVertexAttribArray(widthSlot);
1280 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1281 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1282 glEnableVertexAttribArray(lengthSlot);
1283 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001284 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001285 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001286 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001287 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001288 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001289}
1290
Romain Guy70ca14e2010-12-13 18:24:33 -08001291void OpenGLRenderer::finishDrawTexture() {
1292 glDisableVertexAttribArray(mTexCoordsSlot);
1293}
1294
1295///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001296// Drawing
1297///////////////////////////////////////////////////////////////////////////////
1298
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001299bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1300 Rect& dirty, uint32_t level) {
1301 if (quickReject(0.0f, 0.0f, width, height)) {
1302 return false;
1303 }
1304
Romain Guy0fe478e2010-11-08 12:08:41 -08001305 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1306 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001307 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001308 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001309 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001310
Chet Haasedaf98e92011-01-10 14:10:36 -08001311 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001312}
1313
Chet Haaseed30fd82011-04-22 16:18:45 -07001314void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1315 if (displayList) {
1316 displayList->output(*this, level);
1317 }
1318}
1319
Romain Guya168d732011-03-18 16:50:13 -07001320void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1321 int alpha;
1322 SkXfermode::Mode mode;
1323 getAlphaAndMode(paint, &alpha, &mode);
1324
Romain Guya168d732011-03-18 16:50:13 -07001325 float x = left;
1326 float y = top;
1327
Romain Guye3c26852011-07-25 16:36:01 -07001328 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001329 bool ignoreTransform = false;
1330 if (mSnapshot->transform->isPureTranslate()) {
1331 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1332 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1333 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001334 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001335 } else {
1336 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001337 }
1338
1339 setupDraw();
1340 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001341 if (paint) {
1342 setupDrawAlpha8Color(paint->getColor(), alpha);
1343 }
Romain Guya168d732011-03-18 16:50:13 -07001344 setupDrawColorFilter();
1345 setupDrawShader();
1346 setupDrawBlending(true, mode);
1347 setupDrawProgram();
1348 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001349
Romain Guya168d732011-03-18 16:50:13 -07001350 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001351 texture->setWrap(GL_CLAMP_TO_EDGE);
1352 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001353
Romain Guya168d732011-03-18 16:50:13 -07001354 setupDrawPureColorUniforms();
1355 setupDrawColorFilterUniforms();
1356 setupDrawShaderUniforms();
1357 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1358
1359 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1360
1361 finishDrawTexture();
1362}
1363
Chet Haase5c13d892010-10-08 08:37:55 -07001364void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001365 const float right = left + bitmap->width();
1366 const float bottom = top + bitmap->height();
1367
1368 if (quickReject(left, top, right, bottom)) {
1369 return;
1370 }
1371
Romain Guy86568192010-12-14 15:55:39 -08001372 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001373 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001374 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001375 const AutoTexture autoCleanup(texture);
1376
Romain Guya168d732011-03-18 16:50:13 -07001377 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1378 drawAlphaBitmap(texture, left, top, paint);
1379 } else {
1380 drawTextureRect(left, top, right, bottom, texture, paint);
1381 }
Romain Guyce0537b2010-06-29 21:05:21 -07001382}
1383
Chet Haase5c13d892010-10-08 08:37:55 -07001384void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001385 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1386 const mat4 transform(*matrix);
1387 transform.mapRect(r);
1388
Romain Guy6926c722010-07-12 20:20:03 -07001389 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1390 return;
1391 }
1392
Romain Guy86568192010-12-14 15:55:39 -08001393 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001394 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001395 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001396 const AutoTexture autoCleanup(texture);
1397
Romain Guy5b3b3522010-10-27 18:57:51 -07001398 // This could be done in a cheaper way, all we need is pass the matrix
1399 // to the vertex shader. The save/restore is a bit overkill.
1400 save(SkCanvas::kMatrix_SaveFlag);
1401 concatMatrix(matrix);
1402 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1403 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001404}
1405
Romain Guy5a7b4662011-01-20 19:09:30 -08001406void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1407 float* vertices, int* colors, SkPaint* paint) {
1408 // TODO: Do a quickReject
1409 if (!vertices || mSnapshot->isIgnored()) {
1410 return;
1411 }
1412
1413 glActiveTexture(gTextureUnits[0]);
1414 Texture* texture = mCaches.textureCache.get(bitmap);
1415 if (!texture) return;
1416 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001417
Romain Guyd21b6e12011-11-30 20:21:23 -08001418 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1419 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001420
1421 int alpha;
1422 SkXfermode::Mode mode;
1423 getAlphaAndMode(paint, &alpha, &mode);
1424
Romain Guy5a7b4662011-01-20 19:09:30 -08001425 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001426
Romain Guyb18d2d02011-02-10 15:52:54 -08001427 float left = FLT_MAX;
1428 float top = FLT_MAX;
1429 float right = FLT_MIN;
1430 float bottom = FLT_MIN;
1431
1432#if RENDER_LAYERS_AS_REGIONS
1433 bool hasActiveLayer = hasLayer();
1434#else
1435 bool hasActiveLayer = false;
1436#endif
1437
Romain Guya566b7c2011-01-23 16:36:11 -08001438 // TODO: Support the colors array
1439 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001440 TextureVertex* vertex = mesh;
1441 for (int32_t y = 0; y < meshHeight; y++) {
1442 for (int32_t x = 0; x < meshWidth; x++) {
1443 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1444
1445 float u1 = float(x) / meshWidth;
1446 float u2 = float(x + 1) / meshWidth;
1447 float v1 = float(y) / meshHeight;
1448 float v2 = float(y + 1) / meshHeight;
1449
1450 int ax = i + (meshWidth + 1) * 2;
1451 int ay = ax + 1;
1452 int bx = i;
1453 int by = bx + 1;
1454 int cx = i + 2;
1455 int cy = cx + 1;
1456 int dx = i + (meshWidth + 1) * 2 + 2;
1457 int dy = dx + 1;
1458
1459 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1460 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1461 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1462
1463 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1464 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1465 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001466
1467#if RENDER_LAYERS_AS_REGIONS
1468 if (hasActiveLayer) {
1469 // TODO: This could be optimized to avoid unnecessary ops
1470 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1471 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1472 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1473 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1474 }
1475#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001476 }
1477 }
1478
Romain Guyb18d2d02011-02-10 15:52:54 -08001479#if RENDER_LAYERS_AS_REGIONS
1480 if (hasActiveLayer) {
1481 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1482 }
1483#endif
1484
Romain Guy5a7b4662011-01-20 19:09:30 -08001485 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1486 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001487 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001488}
1489
Romain Guy8ba548f2010-06-30 19:21:21 -07001490void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1491 float srcLeft, float srcTop, float srcRight, float srcBottom,
1492 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001493 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001494 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1495 return;
1496 }
1497
Romain Guy746b7402010-10-26 16:27:31 -07001498 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001499 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001500 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001501 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001502
Romain Guy8ba548f2010-06-30 19:21:21 -07001503 const float width = texture->width;
1504 const float height = texture->height;
1505
Romain Guy68169722011-08-22 17:33:33 -07001506 const float u1 = fmax(0.0f, srcLeft / width);
1507 const float v1 = fmax(0.0f, srcTop / height);
1508 const float u2 = fmin(1.0f, srcRight / width);
1509 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001510
Romain Guy03750a02010-10-18 14:06:08 -07001511 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001512 resetDrawTextureTexCoords(u1, v1, u2, v2);
1513
Romain Guy03750a02010-10-18 14:06:08 -07001514 int alpha;
1515 SkXfermode::Mode mode;
1516 getAlphaAndMode(paint, &alpha, &mode);
1517
Romain Guyd21b6e12011-11-30 20:21:23 -08001518 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1519
Romain Guy6620c6d2010-12-06 18:07:02 -08001520 if (mSnapshot->transform->isPureTranslate()) {
1521 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1522 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1523
Romain Guyb5014982011-07-28 15:39:12 -07001524 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001525 // Enable linear filtering if the source rectangle is scaled
1526 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001527 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001528 }
Romain Guyb5014982011-07-28 15:39:12 -07001529
Romain Guyd21b6e12011-11-30 20:21:23 -08001530 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001531 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1532 texture->id, alpha / 255.0f, mode, texture->blend,
1533 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1534 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1535 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001536 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001537 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1538 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1539 GL_TRIANGLE_STRIP, gMeshCount);
1540 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001541
1542 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1543}
1544
Romain Guy4aa90572010-09-26 18:40:37 -07001545void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001546 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001547 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001548 if (quickReject(left, top, right, bottom)) {
1549 return;
1550 }
1551
Romain Guy746b7402010-10-26 16:27:31 -07001552 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001553 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001554 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001555 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001556 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1557 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001558
1559 int alpha;
1560 SkXfermode::Mode mode;
1561 getAlphaAndMode(paint, &alpha, &mode);
1562
Romain Guy2728f962010-10-08 18:36:15 -07001563 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001564 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001565
Romain Guya5ef39a2010-12-03 16:48:20 -08001566 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001567 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001568#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001569 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001570 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001571 const float offsetX = left + mSnapshot->transform->getTranslateX();
1572 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001573 const size_t count = mesh->quads.size();
1574 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001575 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001576 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001577 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1578 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1579 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001580 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001581 dirtyLayer(left + bounds.left, top + bounds.top,
1582 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001583 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001584 }
1585 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001586#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001587
Romain Guy6620c6d2010-12-06 18:07:02 -08001588 if (pureTranslate) {
1589 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1590 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1591
1592 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1593 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1594 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1595 true, !mesh->hasEmptyQuads);
1596 } else {
1597 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1598 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1599 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1600 true, !mesh->hasEmptyQuads);
1601 }
Romain Guy054dc182010-10-15 17:55:25 -07001602 }
Romain Guyf7f93552010-07-08 19:17:03 -07001603}
1604
Chet Haase99ecdc42011-05-06 12:06:34 -07001605/**
Chet Haase858aa932011-05-12 09:06:00 -07001606 * This function uses a similar approach to that of AA lines in the drawLines() function.
1607 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1608 * shader to compute the translucency of the color, determined by whether a given pixel is
1609 * within that boundary region and how far into the region it is.
1610 */
1611void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001612 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001613 float inverseScaleX = 1.0f;
1614 float inverseScaleY = 1.0f;
1615 // The quad that we use needs to account for scaling.
1616 if (!mSnapshot->transform->isPureTranslate()) {
1617 Matrix4 *mat = mSnapshot->transform;
1618 float m00 = mat->data[Matrix4::kScaleX];
1619 float m01 = mat->data[Matrix4::kSkewY];
1620 float m02 = mat->data[2];
1621 float m10 = mat->data[Matrix4::kSkewX];
1622 float m11 = mat->data[Matrix4::kScaleX];
1623 float m12 = mat->data[6];
1624 float scaleX = sqrt(m00 * m00 + m01 * m01);
1625 float scaleY = sqrt(m10 * m10 + m11 * m11);
1626 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1627 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1628 }
1629
1630 setupDraw();
1631 setupDrawAALine();
1632 setupDrawColor(color);
1633 setupDrawColorFilter();
1634 setupDrawShader();
1635 setupDrawBlending(true, mode);
1636 setupDrawProgram();
1637 setupDrawModelViewIdentity(true);
1638 setupDrawColorUniforms();
1639 setupDrawColorFilterUniforms();
1640 setupDrawShaderIdentityUniforms();
1641
1642 AAVertex rects[4];
1643 AAVertex* aaVertices = &rects[0];
1644 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1645 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1646
1647 float boundarySizeX = .5 * inverseScaleX;
1648 float boundarySizeY = .5 * inverseScaleY;
1649
1650 // Adjust the rect by the AA boundary padding
1651 left -= boundarySizeX;
1652 right += boundarySizeX;
1653 top -= boundarySizeY;
1654 bottom += boundarySizeY;
1655
1656 float width = right - left;
1657 float height = bottom - top;
1658
1659 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1660 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1661 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1662 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1663 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1664 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1665 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1666
1667 if (!quickReject(left, top, right, bottom)) {
1668 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1669 AAVertex::set(aaVertices++, left, top, 1, 0);
1670 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1671 AAVertex::set(aaVertices++, right, top, 0, 0);
1672 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1673 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1674 }
1675}
1676
1677/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001678 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1679 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1680 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1681 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1682 * of the line. Hairlines are more involved because we need to account for transform scaling
1683 * to end up with a one-pixel-wide line in screen space..
1684 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1685 * in combination with values that we calculate and pass down in this method. The basic approach
1686 * is that the quad we create contains both the core line area plus a bounding area in which
1687 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1688 * proportion of the width and the length of a given segment is represented by the boundary
1689 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1690 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1691 * on the inside). This ends up giving the result we want, with pixels that are completely
1692 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1693 * how far into the boundary region they are, which is determined by shader interpolation.
1694 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001695void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1696 if (mSnapshot->isIgnored()) return;
1697
1698 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001699 // We use half the stroke width here because we're going to position the quad
1700 // corner vertices half of the width away from the line endpoints
1701 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001702 // A stroke width of 0 has a special meaning in Skia:
1703 // it draws a line 1 px wide regardless of current transform
1704 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001705 float inverseScaleX = 1.0f;
1706 float inverseScaleY = 1.0f;
1707 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001708 int alpha;
1709 SkXfermode::Mode mode;
1710 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001711 int verticesCount = count;
1712 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001713 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001714 verticesCount += (count - 4);
1715 }
1716
1717 if (isHairLine || isAA) {
1718 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1719 // the line on the screen should always be one pixel wide regardless of scale. For
1720 // AA lines, we only want one pixel of translucent boundary around the quad.
1721 if (!mSnapshot->transform->isPureTranslate()) {
1722 Matrix4 *mat = mSnapshot->transform;
1723 float m00 = mat->data[Matrix4::kScaleX];
1724 float m01 = mat->data[Matrix4::kSkewY];
1725 float m02 = mat->data[2];
1726 float m10 = mat->data[Matrix4::kSkewX];
1727 float m11 = mat->data[Matrix4::kScaleX];
1728 float m12 = mat->data[6];
1729 float scaleX = sqrt(m00*m00 + m01*m01);
1730 float scaleY = sqrt(m10*m10 + m11*m11);
1731 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1732 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1733 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1734 scaled = true;
1735 }
1736 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001737 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001738
1739 getAlphaAndMode(paint, &alpha, &mode);
1740 setupDraw();
1741 if (isAA) {
1742 setupDrawAALine();
1743 }
1744 setupDrawColor(paint->getColor(), alpha);
1745 setupDrawColorFilter();
1746 setupDrawShader();
1747 if (isAA) {
1748 setupDrawBlending(true, mode);
1749 } else {
1750 setupDrawBlending(mode);
1751 }
1752 setupDrawProgram();
1753 setupDrawModelViewIdentity(true);
1754 setupDrawColorUniforms();
1755 setupDrawColorFilterUniforms();
1756 setupDrawShaderIdentityUniforms();
1757
1758 if (isHairLine) {
1759 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001760 halfStrokeWidth = isAA? 1 : .5;
1761 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001762 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001763 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001764 }
1765 Vertex lines[verticesCount];
1766 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001767 AAVertex wLines[verticesCount];
1768 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001769 if (!isAA) {
1770 setupDrawVertices(vertices);
1771 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001772 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1773 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001774 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001775 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1776 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001777 // We will need to calculate the actual width proportion on each segment for
1778 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1779 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1780 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001781 }
1782
Chet Haase99ecdc42011-05-06 12:06:34 -07001783 AAVertex* prevAAVertex = NULL;
1784 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001785
Chet Haase99585ad2011-05-02 15:00:16 -07001786 int boundaryLengthSlot = -1;
1787 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001788 int boundaryWidthSlot = -1;
1789 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001790 for (int i = 0; i < count; i += 4) {
1791 // a = start point, b = end point
1792 vec2 a(points[i], points[i + 1]);
1793 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001794 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001795 float boundaryLengthProportion = 0;
1796 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001797
Chet Haase5b0200b2011-04-13 17:58:08 -07001798 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001799 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001800 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001801 if (isAA) {
1802 float wideningFactor;
1803 if (fabs(n.x) >= fabs(n.y)) {
1804 wideningFactor = fabs(1.0f / n.x);
1805 } else {
1806 wideningFactor = fabs(1.0f / n.y);
1807 }
1808 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001809 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001810 if (scaled) {
1811 n.x *= inverseScaleX;
1812 n.y *= inverseScaleY;
1813 }
1814 } else if (scaled) {
1815 // Extend n by .5 pixel on each side, post-transform
1816 vec2 extendedN = n.copyNormalized();
1817 extendedN /= 2;
1818 extendedN.x *= inverseScaleX;
1819 extendedN.y *= inverseScaleY;
1820 float extendedNLength = extendedN.length();
1821 // We need to set this value on the shader prior to drawing
1822 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1823 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001824 }
1825 float x = n.x;
1826 n.x = -n.y;
1827 n.y = x;
1828
Chet Haase99585ad2011-05-02 15:00:16 -07001829 // aa lines expand the endpoint vertices to encompass the AA boundary
1830 if (isAA) {
1831 vec2 abVector = (b - a);
1832 length = abVector.length();
1833 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001834 if (scaled) {
1835 abVector.x *= inverseScaleX;
1836 abVector.y *= inverseScaleY;
1837 float abLength = abVector.length();
1838 boundaryLengthProportion = abLength / (length + abLength);
1839 } else {
1840 boundaryLengthProportion = .5 / (length + 1);
1841 }
1842 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001843 a -= abVector;
1844 b += abVector;
1845 }
1846
Chet Haase5b0200b2011-04-13 17:58:08 -07001847 // Four corners of the rectangle defining a thick line
1848 vec2 p1 = a - n;
1849 vec2 p2 = a + n;
1850 vec2 p3 = b + n;
1851 vec2 p4 = b - n;
1852
Chet Haase99585ad2011-05-02 15:00:16 -07001853
Chet Haase5b0200b2011-04-13 17:58:08 -07001854 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1855 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1856 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1857 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1858
1859 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001860 if (!isAA) {
1861 if (prevVertex != NULL) {
1862 // Issue two repeat vertices to create degenerate triangles to bridge
1863 // between the previous line and the new one. This is necessary because
1864 // we are creating a single triangle_strip which will contain
1865 // potentially discontinuous line segments.
1866 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1867 Vertex::set(vertices++, p1.x, p1.y);
1868 generatedVerticesCount += 2;
1869 }
1870 Vertex::set(vertices++, p1.x, p1.y);
1871 Vertex::set(vertices++, p2.x, p2.y);
1872 Vertex::set(vertices++, p4.x, p4.y);
1873 Vertex::set(vertices++, p3.x, p3.y);
1874 prevVertex = vertices - 1;
1875 generatedVerticesCount += 4;
1876 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001877 if (!isHairLine && scaled) {
1878 // Must set width proportions per-segment for scaled non-hairlines to use the
1879 // correct AA boundary dimensions
1880 if (boundaryWidthSlot < 0) {
1881 boundaryWidthSlot =
1882 mCaches.currentProgram->getUniform("boundaryWidth");
1883 inverseBoundaryWidthSlot =
1884 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1885 }
1886 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1887 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1888 }
Chet Haase99585ad2011-05-02 15:00:16 -07001889 if (boundaryLengthSlot < 0) {
1890 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1891 inverseBoundaryLengthSlot =
1892 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1893 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001894 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1895 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001896
Chet Haase5b0200b2011-04-13 17:58:08 -07001897 if (prevAAVertex != NULL) {
1898 // Issue two repeat vertices to create degenerate triangles to bridge
1899 // between the previous line and the new one. This is necessary because
1900 // we are creating a single triangle_strip which will contain
1901 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001902 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1903 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1904 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001905 generatedVerticesCount += 2;
1906 }
Chet Haase99585ad2011-05-02 15:00:16 -07001907 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1908 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1909 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1910 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 prevAAVertex = aaVertices - 1;
1912 generatedVerticesCount += 4;
1913 }
Chet Haase99585ad2011-05-02 15:00:16 -07001914 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1915 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1916 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001917 }
1918 }
1919 if (generatedVerticesCount > 0) {
1920 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1921 }
1922}
1923
Romain Guyed6fcb02011-03-21 13:11:28 -07001924void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1925 if (mSnapshot->isIgnored()) return;
1926
1927 // TODO: The paint's cap style defines whether the points are square or circular
1928 // TODO: Handle AA for round points
1929
Chet Haase5b0200b2011-04-13 17:58:08 -07001930 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001931 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001932 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001933 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001934 if (isHairLine) {
1935 // Now that we know it's hairline, we can set the effective width, to be used later
1936 strokeWidth = 1.0f;
1937 }
1938 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001939 int alpha;
1940 SkXfermode::Mode mode;
1941 getAlphaAndMode(paint, &alpha, &mode);
1942
1943 int verticesCount = count >> 1;
1944 int generatedVerticesCount = 0;
1945
1946 TextureVertex pointsData[verticesCount];
1947 TextureVertex* vertex = &pointsData[0];
1948
1949 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001950 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001951 setupDrawColor(paint->getColor(), alpha);
1952 setupDrawColorFilter();
1953 setupDrawShader();
1954 setupDrawBlending(mode);
1955 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001956 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001957 setupDrawColorUniforms();
1958 setupDrawColorFilterUniforms();
1959 setupDrawPointUniforms();
1960 setupDrawShaderIdentityUniforms();
1961 setupDrawMesh(vertex);
1962
1963 for (int i = 0; i < count; i += 2) {
1964 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1965 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001966 float left = points[i] - halfWidth;
1967 float right = points[i] + halfWidth;
1968 float top = points[i + 1] - halfWidth;
1969 float bottom = points [i + 1] + halfWidth;
1970 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001971 }
1972
1973 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1974}
1975
Romain Guy85bf02f2010-06-22 13:11:24 -07001976void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001977 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001978 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001979
Romain Guyae88e5e2010-10-22 17:49:18 -07001980 Rect& clip(*mSnapshot->clipRect);
1981 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001982
Romain Guy3d58c032010-07-14 16:34:53 -07001983 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001984}
Romain Guy9d5316e2010-06-24 19:30:36 -07001985
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001986void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001987 if (!texture) return;
1988 const AutoTexture autoCleanup(texture);
1989
1990 const float x = left + texture->left - texture->offset;
1991 const float y = top + texture->top - texture->offset;
1992
1993 drawPathTexture(texture, x, y, paint);
1994}
1995
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001996void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1997 float rx, float ry, SkPaint* paint) {
1998 if (mSnapshot->isIgnored()) return;
1999
2000 glActiveTexture(gTextureUnits[0]);
2001 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2002 right - left, bottom - top, rx, ry, paint);
2003 drawShape(left, top, texture, paint);
2004}
2005
Romain Guy01d58e42011-01-19 21:54:02 -08002006void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2007 if (mSnapshot->isIgnored()) return;
2008
2009 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002010 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002011 drawShape(x - radius, y - radius, texture, paint);
2012}
Romain Guy01d58e42011-01-19 21:54:02 -08002013
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002014void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2015 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002016
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002017 glActiveTexture(gTextureUnits[0]);
2018 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2019 drawShape(left, top, texture, paint);
2020}
2021
Romain Guy8b2f5262011-01-23 16:15:02 -08002022void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2023 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2024 if (mSnapshot->isIgnored()) return;
2025
2026 if (fabs(sweepAngle) >= 360.0f) {
2027 drawOval(left, top, right, bottom, paint);
2028 return;
2029 }
2030
2031 glActiveTexture(gTextureUnits[0]);
2032 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2033 startAngle, sweepAngle, useCenter, paint);
2034 drawShape(left, top, texture, paint);
2035}
2036
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002037void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2038 SkPaint* paint) {
2039 if (mSnapshot->isIgnored()) return;
2040
2041 glActiveTexture(gTextureUnits[0]);
2042 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2043 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002044}
2045
Chet Haase5c13d892010-10-08 08:37:55 -07002046void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002047 if (p->getStyle() != SkPaint::kFill_Style) {
2048 drawRectAsShape(left, top, right, bottom, p);
2049 return;
2050 }
2051
Romain Guy6926c722010-07-12 20:20:03 -07002052 if (quickReject(left, top, right, bottom)) {
2053 return;
2054 }
2055
Romain Guy026c5e162010-06-28 17:12:22 -07002056 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002057 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002058 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2059 if (!isMode) {
2060 // Assume SRC_OVER
2061 mode = SkXfermode::kSrcOver_Mode;
2062 }
2063 } else {
2064 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002065 }
2066
Romain Guy026c5e162010-06-28 17:12:22 -07002067 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002068 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002069 drawAARect(left, top, right, bottom, color, mode);
2070 } else {
2071 drawColorRect(left, top, right, bottom, color, mode);
2072 }
Romain Guyc7d53492010-06-25 13:41:57 -07002073}
Romain Guy9d5316e2010-06-24 19:30:36 -07002074
Romain Guye8e62a42010-07-23 18:55:21 -07002075void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002076 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002077 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002078 return;
2079 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002080 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002081
Romain Guy8f9a9f62011-12-05 11:53:26 -08002082 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002083
Romain Guye8e62a42010-07-23 18:55:21 -07002084 switch (paint->getTextAlign()) {
2085 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002086 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002087 x -= length / 2.0f;
2088 break;
2089 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002090 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002091 x -= length;
2092 break;
2093 default:
2094 break;
2095 }
2096
Romain Guycac5fd32011-12-01 20:08:50 -08002097 SkPaint::FontMetrics metrics;
2098 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002099 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002100 if (quickReject(x, y + metrics.fTop,
2101 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2102 return;
2103 }
2104
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002105 const float oldX = x;
2106 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002107 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2108 if (pureTranslate) {
2109 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2110 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2111 }
2112
Romain Guyb45c0c92010-08-26 20:35:23 -07002113 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002114#if DEBUG_GLYPHS
2115 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2116#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002117 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002118 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002119
Romain Guy86568192010-12-14 15:55:39 -08002120 int alpha;
2121 SkXfermode::Mode mode;
2122 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002123
Romain Guy1e45aae2010-08-13 19:39:53 -07002124 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002125 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002126 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2127 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002128 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002129
Romain Guy740bf2b2011-04-26 15:33:10 -07002130 const float sx = oldX - shadow->left + mShadowDx;
2131 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002132
Romain Guy86568192010-12-14 15:55:39 -08002133 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002134 int shadowColor = mShadowColor;
2135 if (mShader) {
2136 shadowColor = 0xffffffff;
2137 }
Romain Guy86568192010-12-14 15:55:39 -08002138
2139 glActiveTexture(gTextureUnits[0]);
2140 setupDraw();
2141 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002142 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2143 setupDrawColorFilter();
2144 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002145 setupDrawBlending(true, mode);
2146 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002147 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002148 setupDrawTexture(shadow->id);
2149 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002150 setupDrawColorFilterUniforms();
2151 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002152 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2153
Romain Guy0a417492010-08-16 20:26:20 -07002154 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002155
Romain Guy86568192010-12-14 15:55:39 -08002156 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002157 }
2158
Romain Guyb146b122010-12-15 17:06:45 -08002159 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2160 return;
2161 }
2162
Romain Guy6620c6d2010-12-06 18:07:02 -08002163 // Pick the appropriate texture filtering
2164 bool linearFilter = mSnapshot->transform->changesBounds();
2165 if (pureTranslate && !linearFilter) {
2166 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2167 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002168
Romain Guy86568192010-12-14 15:55:39 -08002169 glActiveTexture(gTextureUnits[0]);
2170 setupDraw();
2171 setupDrawDirtyRegionsDisabled();
2172 setupDrawWithTexture(true);
2173 setupDrawAlpha8Color(paint->getColor(), alpha);
2174 setupDrawColorFilter();
2175 setupDrawShader();
2176 setupDrawBlending(true, mode);
2177 setupDrawProgram();
2178 setupDrawModelView(x, y, x, y, pureTranslate, true);
2179 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2180 setupDrawPureColorUniforms();
2181 setupDrawColorFilterUniforms();
2182 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002183
Romain Guy6620c6d2010-12-06 18:07:02 -08002184 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002185 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2186
2187#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002188 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002189#else
Romain Guyf219da52011-01-16 12:54:25 -08002190 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002191#endif
Romain Guy03750a02010-10-18 14:06:08 -07002192 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002193
2194 // Tell font renderer the locations of position and texture coord
2195 // attributes so it can bind its data properly
2196 int positionSlot = mCaches.currentProgram->position;
2197 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002198 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002199 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002200#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002201 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002202 if (!pureTranslate) {
2203 mSnapshot->transform->mapRect(bounds);
2204 }
Romain Guyf219da52011-01-16 12:54:25 -08002205 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002206 }
2207#endif
2208 }
Romain Guy694b5192010-07-21 21:33:20 -07002209
2210 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002211 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002212
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002213 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002214}
2215
Romain Guy7fbcc042010-08-04 15:40:07 -07002216void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002217 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002218
Romain Guy01d58e42011-01-19 21:54:02 -08002219 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002220
Romain Guyfb8b7632010-08-23 21:05:08 -07002221 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002222 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002223 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002224
Romain Guy8b55f372010-08-18 17:10:07 -07002225 const float x = texture->left - texture->offset;
2226 const float y = texture->top - texture->offset;
2227
Romain Guy01d58e42011-01-19 21:54:02 -08002228 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002229}
2230
Romain Guyada830f2011-01-13 12:13:20 -08002231void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2232 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002233 return;
2234 }
2235
2236 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002237
2238 int alpha;
2239 SkXfermode::Mode mode;
2240 getAlphaAndMode(paint, &alpha, &mode);
2241
Romain Guy9ace8f52011-07-07 20:50:11 -07002242 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002243
Romain Guyf219da52011-01-16 12:54:25 -08002244#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002245 if (!layer->region.isEmpty()) {
2246 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002247 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002248 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002249 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002250 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002251
Romain Guyc88e3572011-01-22 00:32:12 -08002252 setupDraw();
2253 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002254 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002256 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002257 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002258 setupDrawPureColorUniforms();
2259 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002260 setupDrawTexture(layer->getTexture());
2261 if (mSnapshot->transform->isPureTranslate()) {
2262 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2263 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2264
Romain Guyd21b6e12011-11-30 20:21:23 -08002265 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002266 setupDrawModelViewTranslate(x, y,
2267 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2268 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002269 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002270 setupDrawModelViewTranslate(x, y,
2271 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2272 }
Romain Guyc88e3572011-01-22 00:32:12 -08002273 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002274
Romain Guyc88e3572011-01-22 00:32:12 -08002275 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2276 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002277
Romain Guyc88e3572011-01-22 00:32:12 -08002278 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002279
2280#if DEBUG_LAYERS_AS_REGIONS
2281 drawRegionRects(layer->region);
2282#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002283 }
Romain Guyf219da52011-01-16 12:54:25 -08002284 }
2285#else
Romain Guyada830f2011-01-13 12:13:20 -08002286 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2287 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002288#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002289}
2290
Romain Guy6926c722010-07-12 20:20:03 -07002291///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002292// Shaders
2293///////////////////////////////////////////////////////////////////////////////
2294
2295void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002296 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002297}
2298
Romain Guy06f96e22010-07-30 19:18:16 -07002299void OpenGLRenderer::setupShader(SkiaShader* shader) {
2300 mShader = shader;
2301 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002302 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002303 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002304}
2305
Romain Guyd27977d2010-07-14 19:18:51 -07002306///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002307// Color filters
2308///////////////////////////////////////////////////////////////////////////////
2309
2310void OpenGLRenderer::resetColorFilter() {
2311 mColorFilter = NULL;
2312}
2313
2314void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2315 mColorFilter = filter;
2316}
2317
2318///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002319// Drop shadow
2320///////////////////////////////////////////////////////////////////////////////
2321
2322void OpenGLRenderer::resetShadow() {
2323 mHasShadow = false;
2324}
2325
2326void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2327 mHasShadow = true;
2328 mShadowRadius = radius;
2329 mShadowDx = dx;
2330 mShadowDy = dy;
2331 mShadowColor = color;
2332}
2333
2334///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002335// Drawing implementation
2336///////////////////////////////////////////////////////////////////////////////
2337
Romain Guy01d58e42011-01-19 21:54:02 -08002338void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2339 float x, float y, SkPaint* paint) {
2340 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2341 return;
2342 }
2343
2344 int alpha;
2345 SkXfermode::Mode mode;
2346 getAlphaAndMode(paint, &alpha, &mode);
2347
2348 setupDraw();
2349 setupDrawWithTexture(true);
2350 setupDrawAlpha8Color(paint->getColor(), alpha);
2351 setupDrawColorFilter();
2352 setupDrawShader();
2353 setupDrawBlending(true, mode);
2354 setupDrawProgram();
2355 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2356 setupDrawTexture(texture->id);
2357 setupDrawPureColorUniforms();
2358 setupDrawColorFilterUniforms();
2359 setupDrawShaderUniforms();
2360 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2361
2362 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2363
2364 finishDrawTexture();
2365}
2366
Romain Guyf607bdc2010-09-10 19:20:06 -07002367// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002368#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2369#define kStdUnderline_Offset (1.0f / 9.0f)
2370#define kStdUnderline_Thickness (1.0f / 18.0f)
2371
2372void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2373 float x, float y, SkPaint* paint) {
2374 // Handle underline and strike-through
2375 uint32_t flags = paint->getFlags();
2376 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002377 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002378 float underlineWidth = length;
2379 // If length is > 0.0f, we already measured the text for the text alignment
2380 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002381 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002382 }
2383
2384 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002385 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002386 case SkPaint::kCenter_Align:
2387 offsetX = underlineWidth * 0.5f;
2388 break;
2389 case SkPaint::kRight_Align:
2390 offsetX = underlineWidth;
2391 break;
2392 default:
2393 break;
2394 }
2395
2396 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002397 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002398 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002399
Romain Guye20ecbd2010-09-22 19:49:04 -07002400 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002401 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002402
Romain Guyf6834472011-01-23 13:32:12 -08002403 int linesCount = 0;
2404 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2405 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2406
2407 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002408 float points[pointsCount];
2409 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002410
2411 if (flags & SkPaint::kUnderlineText_Flag) {
2412 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002413 points[currentPoint++] = left;
2414 points[currentPoint++] = top;
2415 points[currentPoint++] = left + underlineWidth;
2416 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002417 }
2418
2419 if (flags & SkPaint::kStrikeThruText_Flag) {
2420 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002421 points[currentPoint++] = left;
2422 points[currentPoint++] = top;
2423 points[currentPoint++] = left + underlineWidth;
2424 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002425 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002426
Romain Guy726aeba2011-06-01 14:52:00 -07002427 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002428
Romain Guy726aeba2011-06-01 14:52:00 -07002429 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002430 }
2431 }
2432}
2433
Romain Guy026c5e162010-06-28 17:12:22 -07002434void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002435 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002436 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002437 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002438 color |= 0x00ffffff;
2439 }
2440
Romain Guy70ca14e2010-12-13 18:24:33 -08002441 setupDraw();
2442 setupDrawColor(color);
2443 setupDrawShader();
2444 setupDrawColorFilter();
2445 setupDrawBlending(mode);
2446 setupDrawProgram();
2447 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2448 setupDrawColorUniforms();
2449 setupDrawShaderUniforms(ignoreTransform);
2450 setupDrawColorFilterUniforms();
2451 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002452
Romain Guyc95c8d62010-09-17 15:31:32 -07002453 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2454}
2455
Romain Guy82ba8142010-07-09 13:25:56 -07002456void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002457 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002458 int alpha;
2459 SkXfermode::Mode mode;
2460 getAlphaAndMode(paint, &alpha, &mode);
2461
Romain Guyd21b6e12011-11-30 20:21:23 -08002462 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002463
Romain Guy6620c6d2010-12-06 18:07:02 -08002464 if (mSnapshot->transform->isPureTranslate()) {
2465 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2466 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2467
Romain Guyd21b6e12011-11-30 20:21:23 -08002468 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002469 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2470 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2471 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2472 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002473 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002474 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2475 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2476 GL_TRIANGLE_STRIP, gMeshCount);
2477 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002478}
2479
Romain Guybd6b79b2010-06-26 00:13:53 -07002480void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002481 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2482 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002483 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002484}
2485
2486void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002487 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002488 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002489 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
Romain Guy746b7402010-10-26 16:27:31 -07002491 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002492 setupDrawWithTexture();
2493 setupDrawColor(alpha, alpha, alpha, alpha);
2494 setupDrawColorFilter();
2495 setupDrawBlending(blend, mode, swapSrcDst);
2496 setupDrawProgram();
2497 if (!dirty) {
2498 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002499 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002500 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002501 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002502 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002503 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002504 }
Romain Guy86568192010-12-14 15:55:39 -08002505 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002506 setupDrawColorFilterUniforms();
2507 setupDrawTexture(texture);
2508 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002509
Romain Guy6820ac82010-09-15 18:11:50 -07002510 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002511
2512 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002513}
2514
Romain Guya5aed0d2010-09-09 14:42:43 -07002515void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002516 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002517 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2518 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002519 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002520 if (!mCaches.blend) {
2521 glEnable(GL_BLEND);
2522 }
Romain Guy82ba8142010-07-09 13:25:56 -07002523
Romain Guyf607bdc2010-09-10 19:20:06 -07002524 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2525 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002526
Romain Guya5aed0d2010-09-09 14:42:43 -07002527 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2528 glBlendFunc(sourceMode, destMode);
2529 mCaches.lastSrcMode = sourceMode;
2530 mCaches.lastDstMode = destMode;
2531 }
2532 } else {
2533 // These blend modes are not supported by OpenGL directly and have
2534 // to be implemented using shaders. Since the shader will perform
2535 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002536 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002537 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002538 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002539 }
2540
2541 if (mCaches.blend) {
2542 glDisable(GL_BLEND);
2543 }
2544 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002545 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002546 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002547 glDisable(GL_BLEND);
2548 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002549 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002550}
2551
Romain Guy889f8d12010-07-29 14:37:42 -07002552bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002553 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002554 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002555 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002556 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002557 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002558 }
Romain Guy6926c722010-07-12 20:20:03 -07002559 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002560}
2561
Romain Guy026c5e162010-06-28 17:12:22 -07002562void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002563 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002564 TextureVertex::setUV(v++, u1, v1);
2565 TextureVertex::setUV(v++, u2, v1);
2566 TextureVertex::setUV(v++, u1, v2);
2567 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002568}
2569
Chet Haase5c13d892010-10-08 08:37:55 -07002570void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002571 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002572 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002573
2574 // Skia draws using the color's alpha channel if < 255
2575 // Otherwise, it uses the paint's alpha
2576 int color = paint->getColor();
2577 *alpha = (color >> 24) & 0xFF;
2578 if (*alpha == 255) {
2579 *alpha = paint->getAlpha();
2580 }
2581 } else {
2582 *mode = SkXfermode::kSrcOver_Mode;
2583 *alpha = 255;
2584 }
Romain Guy026c5e162010-06-28 17:12:22 -07002585}
2586
Romain Guya5aed0d2010-09-09 14:42:43 -07002587SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002588 SkXfermode::Mode resultMode;
2589 if (!SkXfermode::AsMode(mode, &resultMode)) {
2590 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002591 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002592 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002593}
2594
Romain Guy9d5316e2010-06-24 19:30:36 -07002595}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002596}; // namespace android