blob: a60ac0804af4d40ec4eb2a3839fbcec4383afb2f [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 Guyf2fc4602011-07-19 15:20:03 -0700136 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700137 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700138 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700139
140 mWidth = width;
141 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700142
143 mFirstSnapshot->height = height;
144 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700145
146 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700147}
148
Romain Guy6b7bd242010-10-06 19:49:23 -0700149void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800150 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
151}
152
153void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800154 mCaches.clearGarbage();
155
Romain Guy8aef54f2010-09-01 15:13:49 -0700156 mSnapshot = new Snapshot(mFirstSnapshot,
157 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800158 mSnapshot->fbo = getTargetFbo();
159
Romain Guy8fb95422010-08-17 18:38:51 -0700160 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700161
Romain Guyfb8b7632010-08-23 21:05:08 -0700162 glViewport(0, 0, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700163
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 glEnable(GL_SCISSOR_TEST);
165 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
166 mSnapshot->setClip(left, top, right, bottom);
167
Romain Guy6b7bd242010-10-06 19:49:23 -0700168 if (!opaque) {
169 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
170 glClear(GL_COLOR_BUFFER_BIT);
171 }
Romain Guybb9524b2010-06-22 18:56:38 -0700172}
173
Romain Guyb025b9c2010-09-16 14:16:48 -0700174void OpenGLRenderer::finish() {
175#if DEBUG_OPENGL
176 GLenum status = GL_NO_ERROR;
177 while ((status = glGetError()) != GL_NO_ERROR) {
178 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800179 switch (status) {
180 case GL_OUT_OF_MEMORY:
181 LOGE(" OpenGLRenderer is out of memory!");
182 break;
183 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700184 }
185#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800186#if DEBUG_MEMORY_USAGE
187 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800188#else
189 if (mCaches.getDebugLevel() & kDebugMemory) {
190 mCaches.dumpMemoryUsage();
191 }
Romain Guyc15008e2010-11-10 11:59:15 -0800192#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700193}
194
Romain Guy6c319ca2011-01-11 14:29:25 -0800195void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700196 if (mCaches.currentProgram) {
197 if (mCaches.currentProgram->isInUse()) {
198 mCaches.currentProgram->remove();
199 mCaches.currentProgram = NULL;
200 }
201 }
Romain Guy50c0f092010-10-19 11:42:22 -0700202 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700203}
204
Romain Guy6c319ca2011-01-11 14:29:25 -0800205void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800206 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
207
208 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700209
210 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700211 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700212
Romain Guyf607bdc2010-09-10 19:20:06 -0700213 glDisable(GL_DITHER);
214
Chet Haase08837c22011-11-28 11:53:21 -0800215 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy03750a02010-10-18 14:06:08 -0700216 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700217
Romain Guy50c0f092010-10-19 11:42:22 -0700218 mCaches.blend = true;
219 glEnable(GL_BLEND);
220 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
221 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700222}
223
Romain Guycabfcc12011-03-07 18:06:46 -0800224bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800225 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800226 if (mDirtyClip) {
227 setScissorFromClip();
228 }
Romain Guyd643bb52011-03-01 14:55:21 -0800229
Romain Guy80911b82011-03-16 15:30:12 -0700230 Rect clip(*mSnapshot->clipRect);
231 clip.snapToPixelBoundaries();
232
Romain Guyd643bb52011-03-01 14:55:21 -0800233#if RENDER_LAYERS_AS_REGIONS
234 // Since we don't know what the functor will draw, let's dirty
235 // tne entire clip region
236 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800237 dirtyLayerUnchecked(clip, getRegion());
238 }
239#endif
240
Romain Guy08aa2cb2011-03-17 11:06:57 -0700241 DrawGlInfo info;
242 info.clipLeft = clip.left;
243 info.clipTop = clip.top;
244 info.clipRight = clip.right;
245 info.clipBottom = clip.bottom;
246 info.isLayer = hasLayer();
247 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700248
Romain Guy08aa2cb2011-03-17 11:06:57 -0700249 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800250
251 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700252 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800253 dirty.unionWith(localDirty);
254 }
255
Chet Haasedaf98e92011-01-10 14:10:36 -0800256 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800257 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800258}
259
Romain Guyf6a11b82010-06-23 17:47:49 -0700260///////////////////////////////////////////////////////////////////////////////
261// State management
262///////////////////////////////////////////////////////////////////////////////
263
Romain Guybb9524b2010-06-22 18:56:38 -0700264int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700265 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
268int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700269 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700270}
271
272void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700273 if (mSaveCount > 1) {
274 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700275 }
Romain Guybb9524b2010-06-22 18:56:38 -0700276}
277
278void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700279 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700280
Romain Guy8fb95422010-08-17 18:38:51 -0700281 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700282 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy8aef54f2010-09-01 15:13:49 -0700286int OpenGLRenderer::saveSnapshot(int flags) {
287 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700288 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700289}
290
291bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700292 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700293 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700294 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700295
Romain Guybd6b79b2010-06-26 00:13:53 -0700296 sp<Snapshot> current = mSnapshot;
297 sp<Snapshot> previous = mSnapshot->previous;
298
Romain Guyeb993562010-10-05 18:14:38 -0700299 if (restoreOrtho) {
300 Rect& r = previous->viewport;
301 glViewport(r.left, r.top, r.right, r.bottom);
302 mOrthoMatrix.load(current->orthoMatrix);
303 }
304
Romain Guy8b55f372010-08-18 17:10:07 -0700305 mSaveCount--;
306 mSnapshot = previous;
307
Romain Guy2542d192010-08-18 11:47:12 -0700308 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700309 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700310 }
Romain Guy2542d192010-08-18 11:47:12 -0700311
Romain Guy5ec99242010-11-03 16:19:08 -0700312 if (restoreLayer) {
313 composeLayer(current, previous);
314 }
315
Romain Guy2542d192010-08-18 11:47:12 -0700316 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700317}
318
Romain Guyf6a11b82010-06-23 17:47:49 -0700319///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700320// Layers
321///////////////////////////////////////////////////////////////////////////////
322
323int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700324 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700325 const GLuint previousFbo = mSnapshot->fbo;
326 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700327
Romain Guyaf636eb2010-12-09 17:47:21 -0800328 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700329 int alpha = 255;
330 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700331
Romain Guye45362c2010-11-03 19:58:32 -0700332 if (p) {
333 alpha = p->getAlpha();
334 if (!mCaches.extensions.hasFramebufferFetch()) {
335 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
336 if (!isMode) {
337 // Assume SRC_OVER
338 mode = SkXfermode::kSrcOver_Mode;
339 }
340 } else {
341 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700342 }
343 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700344 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700345 }
Romain Guyd55a8612010-06-28 17:42:46 -0700346
Romain Guydbc26d22010-10-11 17:58:29 -0700347 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
348 }
Romain Guyd55a8612010-06-28 17:42:46 -0700349
350 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700351}
352
353int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
354 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700355 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700356 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700357 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700358 SkPaint paint;
359 paint.setAlpha(alpha);
360 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700361 }
Romain Guyd55a8612010-06-28 17:42:46 -0700362}
Romain Guybd6b79b2010-06-26 00:13:53 -0700363
Romain Guy1c740bc2010-09-13 18:00:09 -0700364/**
365 * Layers are viewed by Skia are slightly different than layers in image editing
366 * programs (for instance.) When a layer is created, previously created layers
367 * and the frame buffer still receive every drawing command. For instance, if a
368 * layer is created and a shape intersecting the bounds of the layers and the
369 * framebuffer is draw, the shape will be drawn on both (unless the layer was
370 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
371 *
372 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
373 * texture. Unfortunately, this is inefficient as it requires every primitive to
374 * be drawn n + 1 times, where n is the number of active layers. In practice this
375 * means, for every primitive:
376 * - Switch active frame buffer
377 * - Change viewport, clip and projection matrix
378 * - Issue the drawing
379 *
380 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700381 * To avoid this, layers are implemented in a different way here, at least in the
382 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
383 * is set. When this flag is set we can redirect all drawing operations into a
384 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700385 *
386 * This implementation relies on the frame buffer being at least RGBA 8888. When
387 * a layer is created, only a texture is created, not an FBO. The content of the
388 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700389 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700390 * buffer and drawing continues as normal. This technique therefore treats the
391 * frame buffer as a scratch buffer for the layers.
392 *
393 * To compose the layers back onto the frame buffer, each layer texture
394 * (containing the original frame buffer data) is drawn as a simple quad over
395 * the frame buffer. The trick is that the quad is set as the composition
396 * destination in the blending equation, and the frame buffer becomes the source
397 * of the composition.
398 *
399 * Drawing layers with an alpha value requires an extra step before composition.
400 * An empty quad is drawn over the layer's region in the frame buffer. This quad
401 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
402 * quad is used to multiply the colors in the frame buffer. This is achieved by
403 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
404 * GL_ZERO, GL_SRC_ALPHA.
405 *
406 * Because glCopyTexImage2D() can be slow, an alternative implementation might
407 * be use to draw a single clipped layer. The implementation described above
408 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700409 *
410 * (1) The frame buffer is actually not cleared right away. To allow the GPU
411 * to potentially optimize series of calls to glCopyTexImage2D, the frame
412 * buffer is left untouched until the first drawing operation. Only when
413 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700414 */
Romain Guyd55a8612010-06-28 17:42:46 -0700415bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700416 float right, float bottom, int alpha, SkXfermode::Mode mode,
417 int flags, GLuint previousFbo) {
418 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700419 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700420
Romain Guyeb993562010-10-05 18:14:38 -0700421 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
422
Romain Guyf607bdc2010-09-10 19:20:06 -0700423 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700424 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700425 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700426 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700427
Romain Guyeb993562010-10-05 18:14:38 -0700428 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700429 if (bounds.intersect(*snapshot->clipRect)) {
430 // We cannot work with sub-pixels in this case
431 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700432
Romain Guyad37cd32011-03-15 11:12:25 -0700433 // When the layer is not an FBO, we may use glCopyTexImage so we
434 // need to make sure the layer does not extend outside the bounds
435 // of the framebuffer
436 if (!bounds.intersect(snapshot->previous->viewport)) {
437 bounds.setEmpty();
438 }
439 } else {
440 bounds.setEmpty();
441 }
Romain Guyeb993562010-10-05 18:14:38 -0700442 }
Romain Guybf434112010-09-16 14:40:17 -0700443
Romain Guy746b7402010-10-26 16:27:31 -0700444 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
445 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800446 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700447 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700448 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700449 }
450
451 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800452 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700453 return false;
454 }
Romain Guyf18fd992010-07-08 11:45:51 -0700455
Romain Guy5b3b3522010-10-27 18:57:51 -0700456 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700457 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700458 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700459 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700460 }
461
Romain Guy9ace8f52011-07-07 20:50:11 -0700462 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700463 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700464 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
465 bounds.getWidth() / float(layer->getWidth()), 0.0f);
466 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700467
Romain Guy8fb95422010-08-17 18:38:51 -0700468 // Save the layer in the snapshot
469 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700470 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700471
Romain Guyeb993562010-10-05 18:14:38 -0700472 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700473 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700474 } else {
475 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700476 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800477 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700478 if (layer->isEmpty()) {
479 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
480 bounds.left, snapshot->height - bounds.bottom,
481 layer->getWidth(), layer->getHeight(), 0);
482 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800483 } else {
484 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
485 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
486 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700487
Romain Guy54be1cd2011-06-13 19:04:27 -0700488 // Enqueue the buffer coordinates to clear the corresponding region later
489 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700490 }
Romain Guyeb993562010-10-05 18:14:38 -0700491 }
Romain Guyf86ef572010-07-01 11:05:42 -0700492
Romain Guyd55a8612010-06-28 17:42:46 -0700493 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700494}
495
Romain Guy5b3b3522010-10-27 18:57:51 -0700496bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
497 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700498 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700499
500#if RENDER_LAYERS_AS_REGIONS
501 snapshot->region = &snapshot->layer->region;
502 snapshot->flags |= Snapshot::kFlagFboTarget;
503#endif
504
505 Rect clip(bounds);
506 snapshot->transform->mapRect(clip);
507 clip.intersect(*snapshot->clipRect);
508 clip.snapToPixelBoundaries();
509 clip.intersect(snapshot->previous->viewport);
510
511 mat4 inverse;
512 inverse.loadInverse(*mSnapshot->transform);
513
514 inverse.mapRect(clip);
515 clip.snapToPixelBoundaries();
516 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700517 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518
519 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700520 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700521 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700522 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
523 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
524 snapshot->height = bounds.getHeight();
525 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
526 snapshot->orthoMatrix.load(mOrthoMatrix);
527
528 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
530 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700531
532 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700533 if (layer->isEmpty()) {
534 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
535 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700536 }
537
538 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700539 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700540
541#if DEBUG_LAYERS_AS_REGIONS
542 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
543 if (status != GL_FRAMEBUFFER_COMPLETE) {
544 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
545
546 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700547 layer->deleteTexture();
548 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700549
550 delete layer;
551
552 return false;
553 }
554#endif
555
556 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
557 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
558 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
559 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
560 glClear(GL_COLOR_BUFFER_BIT);
561
562 dirtyClip();
563
564 // Change the ortho projection
565 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
566 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
567
568 return true;
569}
570
Romain Guy1c740bc2010-09-13 18:00:09 -0700571/**
572 * Read the documentation of createLayer() before doing anything in this method.
573 */
Romain Guy1d83e192010-08-17 11:37:00 -0700574void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
575 if (!current->layer) {
576 LOGE("Attempting to compose a layer that does not exist");
577 return;
578 }
579
Romain Guy5b3b3522010-10-27 18:57:51 -0700580 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700581
582 if (fboLayer) {
583 // Unbind current FBO and restore previous one
584 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
585 }
586
Romain Guy1d83e192010-08-17 11:37:00 -0700587 Layer* layer = current->layer;
588 const Rect& rect = layer->layer;
589
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700591 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700593 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700594 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700595 }
596
Romain Guy03750a02010-10-18 14:06:08 -0700597 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700598
Romain Guy746b7402010-10-26 16:27:31 -0700599 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700600
Romain Guy5b3b3522010-10-27 18:57:51 -0700601 // When the layer is stored in an FBO, we can save a bit of fillrate by
602 // drawing only the dirty region
603 if (fboLayer) {
604 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 if (layer->getColorFilter()) {
606 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800607 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800610 resetColorFilter();
611 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700612 } else if (!rect.isEmpty()) {
613 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
614 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 }
Romain Guy8b55f372010-08-18 17:10:07 -0700616
Romain Guyeb993562010-10-05 18:14:38 -0700617 if (fboLayer) {
618 // Detach the texture from the FBO
619 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
620 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
621 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
622
623 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
624 mCaches.fboCache.put(current->fbo);
625 }
626
Romain Guy746b7402010-10-26 16:27:31 -0700627 dirtyClip();
628
Romain Guyeb993562010-10-05 18:14:38 -0700629 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700630 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700631 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700632 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700633 delete layer;
634 }
635}
636
Romain Guyaa6c24c2011-04-28 18:40:04 -0700637void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700638 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700639
Romain Guy302a9df2011-08-16 13:55:02 -0700640 mat4& transform = layer->getTransform();
641 if (!transform.isIdentity()) {
642 save(0);
643 mSnapshot->transform->multiply(transform);
644 }
645
Romain Guyaa6c24c2011-04-28 18:40:04 -0700646 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700647 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700648 setupDrawWithTexture();
649 } else {
650 setupDrawWithExternalTexture();
651 }
652 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 setupDrawColor(alpha, alpha, alpha, alpha);
654 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700655 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700656 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657 setupDrawPureColorUniforms();
658 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700659 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
660 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700661 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700662 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700663 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700664 if (mSnapshot->transform->isPureTranslate() &&
665 layer->getWidth() == (uint32_t) rect.getWidth() &&
666 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700667 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
668 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
669
Romain Guyd21b6e12011-11-30 20:21:23 -0800670 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700671 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
672 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800673 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
675 }
676 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700677 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
678
679 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
680
681 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700682
683 if (!transform.isIdentity()) {
684 restore();
685 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700686}
687
Romain Guy5b3b3522010-10-27 18:57:51 -0700688void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700690 const Rect& texCoords = layer->texCoords;
691 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
692 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700693
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 float x = rect.left;
695 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700696 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700697 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700698 layer->getHeight() == (uint32_t) rect.getHeight();
699
700 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700701 // When we're swapping, the layer is already in screen coordinates
702 if (!swap) {
703 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
704 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
705 }
706
Romain Guyd21b6e12011-11-30 20:21:23 -0800707 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800709 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 }
711
712 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
713 layer->getTexture(), layer->getAlpha() / 255.0f,
714 layer->getMode(), layer->isBlend(),
715 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
716 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700717
Romain Guyaa6c24c2011-04-28 18:40:04 -0700718 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
719 } else {
720 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
721 drawTextureLayer(layer, rect);
722 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
723 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700724}
725
726void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
727#if RENDER_LAYERS_AS_REGIONS
728 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700729 layer->setRegionAsRect();
730
Romain Guy40667672011-03-18 14:34:03 -0700731 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700732
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 layer->region.clear();
734 return;
735 }
736
Romain Guy8a3957d2011-09-07 17:55:15 -0700737 // TODO: See LayerRenderer.cpp::generateMesh() for important
738 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700739 if (!layer->region.isEmpty()) {
740 size_t count;
741 const android::Rect* rects = layer->region.getArray(&count);
742
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 const float alpha = layer->getAlpha() / 255.0f;
744 const float texX = 1.0f / float(layer->getWidth());
745 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800746 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700747
748 TextureVertex* mesh = mCaches.getRegionMesh();
749 GLsizei numQuads = 0;
750
Romain Guy7230a742011-01-10 22:26:16 -0800751 setupDraw();
752 setupDrawWithTexture();
753 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800754 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800756 setupDrawProgram();
757 setupDrawDirtyRegionsDisabled();
758 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800759 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawTexture(layer->getTexture());
761 if (mSnapshot->transform->isPureTranslate()) {
762 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
763 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
764
Romain Guyd21b6e12011-11-30 20:21:23 -0800765 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
767 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
770 }
Romain Guy7230a742011-01-10 22:26:16 -0800771 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700772
773 for (size_t i = 0; i < count; i++) {
774 const android::Rect* r = &rects[i];
775
776 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800777 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800779 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
781 // TODO: Reject quads outside of the clip
782 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
783 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
784 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
785 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
786
787 numQuads++;
788
789 if (numQuads >= REGION_MESH_QUAD_COUNT) {
790 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
791 numQuads = 0;
792 mesh = mCaches.getRegionMesh();
793 }
794 }
795
796 if (numQuads > 0) {
797 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
798 }
799
800 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800801 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700802
803#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800804 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700805#endif
806
807 layer->region.clear();
808 }
809#else
810 composeLayerRect(layer, rect);
811#endif
812}
813
Romain Guy3a3133d2011-02-01 22:59:58 -0800814void OpenGLRenderer::drawRegionRects(const Region& region) {
815#if DEBUG_LAYERS_AS_REGIONS
816 size_t count;
817 const android::Rect* rects = region.getArray(&count);
818
819 uint32_t colors[] = {
820 0x7fff0000, 0x7f00ff00,
821 0x7f0000ff, 0x7fff00ff,
822 };
823
824 int offset = 0;
825 int32_t top = rects[0].top;
826
827 for (size_t i = 0; i < count; i++) {
828 if (top != rects[i].top) {
829 offset ^= 0x2;
830 top = rects[i].top;
831 }
832
833 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
834 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
835 SkXfermode::kSrcOver_Mode);
836 }
837#endif
838}
839
Romain Guy5b3b3522010-10-27 18:57:51 -0700840void OpenGLRenderer::dirtyLayer(const float left, const float top,
841 const float right, const float bottom, const mat4 transform) {
842#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800843 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 Rect bounds(left, top, right, bottom);
845 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800846 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 }
848#endif
849}
850
851void OpenGLRenderer::dirtyLayer(const float left, const float top,
852 const float right, const float bottom) {
853#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800854 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700855 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800856 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800857 }
858#endif
859}
860
861void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
862#if RENDER_LAYERS_AS_REGIONS
863 if (bounds.intersect(*mSnapshot->clipRect)) {
864 bounds.snapToPixelBoundaries();
865 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
866 if (!dirty.isEmpty()) {
867 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700868 }
869 }
870#endif
871}
872
Romain Guy54be1cd2011-06-13 19:04:27 -0700873void OpenGLRenderer::clearLayerRegions() {
874 const size_t count = mLayers.size();
875 if (count == 0) return;
876
877 if (!mSnapshot->isIgnored()) {
878 // Doing several glScissor/glClear here can negatively impact
879 // GPUs with a tiler architecture, instead we draw quads with
880 // the Clear blending mode
881
882 // The list contains bounds that have already been clipped
883 // against their initial clip rect, and the current clip
884 // is likely different so we need to disable clipping here
885 glDisable(GL_SCISSOR_TEST);
886
887 Vertex mesh[count * 6];
888 Vertex* vertex = mesh;
889
890 for (uint32_t i = 0; i < count; i++) {
891 Rect* bounds = mLayers.itemAt(i);
892
893 Vertex::set(vertex++, bounds->left, bounds->bottom);
894 Vertex::set(vertex++, bounds->left, bounds->top);
895 Vertex::set(vertex++, bounds->right, bounds->top);
896 Vertex::set(vertex++, bounds->left, bounds->bottom);
897 Vertex::set(vertex++, bounds->right, bounds->top);
898 Vertex::set(vertex++, bounds->right, bounds->bottom);
899
900 delete bounds;
901 }
902
903 setupDraw(false);
904 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
905 setupDrawBlending(true, SkXfermode::kClear_Mode);
906 setupDrawProgram();
907 setupDrawPureColorUniforms();
908 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
909
910 mCaches.unbindMeshBuffer();
911 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
912 gVertexStride, &mesh[0].position[0]);
913 glDrawArrays(GL_TRIANGLES, 0, count * 6);
914
915 glEnable(GL_SCISSOR_TEST);
916 } else {
917 for (uint32_t i = 0; i < count; i++) {
918 delete mLayers.itemAt(i);
919 }
920 }
921
922 mLayers.clear();
923}
924
Romain Guybd6b79b2010-06-26 00:13:53 -0700925///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700926// Transforms
927///////////////////////////////////////////////////////////////////////////////
928
929void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700930 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700931}
932
933void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700934 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700935}
936
937void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700938 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700939}
940
Romain Guy807daf72011-01-18 11:19:19 -0800941void OpenGLRenderer::skew(float sx, float sy) {
942 mSnapshot->transform->skew(sx, sy);
943}
944
Romain Guyf6a11b82010-06-23 17:47:49 -0700945void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700946 if (matrix) {
947 mSnapshot->transform->load(*matrix);
948 } else {
949 mSnapshot->transform->loadIdentity();
950 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700951}
952
953void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700954 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700955}
956
957void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700958 SkMatrix transform;
959 mSnapshot->transform->copyTo(transform);
960 transform.preConcat(*matrix);
961 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700962}
963
964///////////////////////////////////////////////////////////////////////////////
965// Clipping
966///////////////////////////////////////////////////////////////////////////////
967
Romain Guybb9524b2010-06-22 18:56:38 -0700968void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700969 Rect clip(*mSnapshot->clipRect);
970 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700971 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700972 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700973}
974
975const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700976 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700977}
978
Romain Guyc7d53492010-06-25 13:41:57 -0700979bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800980 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700981 return true;
982 }
983
Romain Guy1d83e192010-08-17 11:37:00 -0700984 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700985 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700986 r.snapToPixelBoundaries();
987
988 Rect clipRect(*mSnapshot->clipRect);
989 clipRect.snapToPixelBoundaries();
990
991 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700992}
993
Romain Guy079ba2c2010-07-16 14:12:24 -0700994bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
995 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700996 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700997 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700998 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700999 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001000}
1001
Romain Guyf6a11b82010-06-23 17:47:49 -07001002///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001003// Drawing commands
1004///////////////////////////////////////////////////////////////////////////////
1005
Romain Guy54be1cd2011-06-13 19:04:27 -07001006void OpenGLRenderer::setupDraw(bool clear) {
1007 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001008 if (mDirtyClip) {
1009 setScissorFromClip();
1010 }
1011 mDescription.reset();
1012 mSetShaderColor = false;
1013 mColorSet = false;
1014 mColorA = mColorR = mColorG = mColorB = 0.0f;
1015 mTextureUnit = 0;
1016 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001017 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001018}
1019
1020void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1021 mDescription.hasTexture = true;
1022 mDescription.hasAlpha8Texture = isAlpha8;
1023}
1024
Romain Guyaa6c24c2011-04-28 18:40:04 -07001025void OpenGLRenderer::setupDrawWithExternalTexture() {
1026 mDescription.hasExternalTexture = true;
1027}
1028
Chet Haase5b0200b2011-04-13 17:58:08 -07001029void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001030 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001031}
1032
Romain Guyed6fcb02011-03-21 13:11:28 -07001033void OpenGLRenderer::setupDrawPoint(float pointSize) {
1034 mDescription.isPoint = true;
1035 mDescription.pointSize = pointSize;
1036}
1037
Romain Guy70ca14e2010-12-13 18:24:33 -08001038void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001039 setupDrawColor(color, (color >> 24) & 0xFF);
1040}
1041
1042void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1043 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001044 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1045 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001046 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001047 mColorR = a * ((color >> 16) & 0xFF);
1048 mColorG = a * ((color >> 8) & 0xFF);
1049 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001050 mColorSet = true;
1051 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1052}
1053
Romain Guy86568192010-12-14 15:55:39 -08001054void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1055 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001056 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1057 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001058 const float a = mColorA / 255.0f;
1059 mColorR = a * ((color >> 16) & 0xFF);
1060 mColorG = a * ((color >> 8) & 0xFF);
1061 mColorB = a * ((color ) & 0xFF);
1062 mColorSet = true;
1063 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1064}
1065
Romain Guy70ca14e2010-12-13 18:24:33 -08001066void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1067 mColorA = a;
1068 mColorR = r;
1069 mColorG = g;
1070 mColorB = b;
1071 mColorSet = true;
1072 mSetShaderColor = mDescription.setColor(r, g, b, a);
1073}
1074
Romain Guy86568192010-12-14 15:55:39 -08001075void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1076 mColorA = a;
1077 mColorR = r;
1078 mColorG = g;
1079 mColorB = b;
1080 mColorSet = true;
1081 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1082}
1083
Romain Guy70ca14e2010-12-13 18:24:33 -08001084void OpenGLRenderer::setupDrawShader() {
1085 if (mShader) {
1086 mShader->describe(mDescription, mCaches.extensions);
1087 }
1088}
1089
1090void OpenGLRenderer::setupDrawColorFilter() {
1091 if (mColorFilter) {
1092 mColorFilter->describe(mDescription, mCaches.extensions);
1093 }
1094}
1095
Romain Guyf09ef512011-05-27 11:43:46 -07001096void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1097 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1098 mColorA = 1.0f;
1099 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001100 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001101 }
1102}
1103
Romain Guy70ca14e2010-12-13 18:24:33 -08001104void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001105 // When the blending mode is kClear_Mode, we need to use a modulate color
1106 // argb=1,0,0,0
1107 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001108 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1109 mDescription, swapSrcDst);
1110}
1111
1112void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001113 // When the blending mode is kClear_Mode, we need to use a modulate color
1114 // argb=1,0,0,0
1115 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001116 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1117 mDescription, swapSrcDst);
1118}
1119
1120void OpenGLRenderer::setupDrawProgram() {
1121 useProgram(mCaches.programCache.get(mDescription));
1122}
1123
1124void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1125 mTrackDirtyRegions = false;
1126}
1127
1128void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1129 bool ignoreTransform) {
1130 mModelView.loadTranslate(left, top, 0.0f);
1131 if (!ignoreTransform) {
1132 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1133 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1134 } else {
1135 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1136 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1137 }
1138}
1139
Chet Haase8a5cc922011-04-26 07:28:09 -07001140void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1141 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001142}
1143
Romain Guy70ca14e2010-12-13 18:24:33 -08001144void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1145 bool ignoreTransform, bool ignoreModelView) {
1146 if (!ignoreModelView) {
1147 mModelView.loadTranslate(left, top, 0.0f);
1148 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001149 } else {
1150 mModelView.loadIdentity();
1151 }
Romain Guy86568192010-12-14 15:55:39 -08001152 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1153 if (!ignoreTransform) {
1154 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1155 if (mTrackDirtyRegions && dirty) {
1156 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1157 }
1158 } else {
1159 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1160 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1161 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001162}
1163
Romain Guyed6fcb02011-03-21 13:11:28 -07001164void OpenGLRenderer::setupDrawPointUniforms() {
1165 int slot = mCaches.currentProgram->getUniform("pointSize");
1166 glUniform1f(slot, mDescription.pointSize);
1167}
1168
Romain Guy70ca14e2010-12-13 18:24:33 -08001169void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001170 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001171 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1172 }
1173}
1174
Romain Guy86568192010-12-14 15:55:39 -08001175void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001176 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001177 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001178 }
1179}
1180
Romain Guy70ca14e2010-12-13 18:24:33 -08001181void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1182 if (mShader) {
1183 if (ignoreTransform) {
1184 mModelView.loadInverse(*mSnapshot->transform);
1185 }
1186 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1187 }
1188}
1189
Romain Guy8d0d4782010-12-14 20:13:35 -08001190void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1191 if (mShader) {
1192 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1193 }
1194}
1195
Romain Guy70ca14e2010-12-13 18:24:33 -08001196void OpenGLRenderer::setupDrawColorFilterUniforms() {
1197 if (mColorFilter) {
1198 mColorFilter->setupProgram(mCaches.currentProgram);
1199 }
1200}
1201
1202void OpenGLRenderer::setupDrawSimpleMesh() {
1203 mCaches.bindMeshBuffer();
1204 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1205 gMeshStride, 0);
1206}
1207
1208void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1209 bindTexture(texture);
1210 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1211
1212 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1213 glEnableVertexAttribArray(mTexCoordsSlot);
1214}
1215
Romain Guyaa6c24c2011-04-28 18:40:04 -07001216void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1217 bindExternalTexture(texture);
1218 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1219
1220 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1221 glEnableVertexAttribArray(mTexCoordsSlot);
1222}
1223
Romain Guy8f0095c2011-05-02 17:24:22 -07001224void OpenGLRenderer::setupDrawTextureTransform() {
1225 mDescription.hasTextureTransform = true;
1226}
1227
1228void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001229 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1230 GL_FALSE, &transform.data[0]);
1231}
1232
Romain Guy70ca14e2010-12-13 18:24:33 -08001233void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1234 if (!vertices) {
1235 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1236 } else {
1237 mCaches.unbindMeshBuffer();
1238 }
1239 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1240 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001241 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001242 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1243 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001244}
1245
Chet Haase5b0200b2011-04-13 17:58:08 -07001246void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1247 mCaches.unbindMeshBuffer();
1248 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1249 gVertexStride, vertices);
1250}
1251
1252/**
1253 * 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 -07001254 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1255 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1256 * attributes (one per vertex) are values from zero to one that tells the fragment
1257 * shader where the fragment is in relation to the line width/length overall; these values are
1258 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1259 * region of the line.
1260 * Note that we only pass down the width values in this setup function. The length coordinates
1261 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001262 */
Chet Haase99585ad2011-05-02 15:00:16 -07001263void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001264 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001265 mCaches.unbindMeshBuffer();
1266 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001267 gAAVertexStride, vertices);
1268 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1269 glEnableVertexAttribArray(widthSlot);
1270 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1271 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1272 glEnableVertexAttribArray(lengthSlot);
1273 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001274 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001275 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001276 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001277 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001278 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001279}
1280
Romain Guy70ca14e2010-12-13 18:24:33 -08001281void OpenGLRenderer::finishDrawTexture() {
1282 glDisableVertexAttribArray(mTexCoordsSlot);
1283}
1284
1285///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001286// Drawing
1287///////////////////////////////////////////////////////////////////////////////
1288
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001289bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1290 Rect& dirty, uint32_t level) {
1291 if (quickReject(0.0f, 0.0f, width, height)) {
1292 return false;
1293 }
1294
Romain Guy0fe478e2010-11-08 12:08:41 -08001295 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1296 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001297 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001298 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001299 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001300
Chet Haasedaf98e92011-01-10 14:10:36 -08001301 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001302}
1303
Chet Haaseed30fd82011-04-22 16:18:45 -07001304void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1305 if (displayList) {
1306 displayList->output(*this, level);
1307 }
1308}
1309
Romain Guya168d732011-03-18 16:50:13 -07001310void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1311 int alpha;
1312 SkXfermode::Mode mode;
1313 getAlphaAndMode(paint, &alpha, &mode);
1314
Romain Guya168d732011-03-18 16:50:13 -07001315 float x = left;
1316 float y = top;
1317
Romain Guye3c26852011-07-25 16:36:01 -07001318 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001319 bool ignoreTransform = false;
1320 if (mSnapshot->transform->isPureTranslate()) {
1321 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1322 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1323 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001324 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001325 } else {
1326 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001327 }
1328
1329 setupDraw();
1330 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001331 if (paint) {
1332 setupDrawAlpha8Color(paint->getColor(), alpha);
1333 }
Romain Guya168d732011-03-18 16:50:13 -07001334 setupDrawColorFilter();
1335 setupDrawShader();
1336 setupDrawBlending(true, mode);
1337 setupDrawProgram();
1338 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001339
Romain Guya168d732011-03-18 16:50:13 -07001340 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001341 texture->setWrap(GL_CLAMP_TO_EDGE);
1342 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001343
Romain Guya168d732011-03-18 16:50:13 -07001344 setupDrawPureColorUniforms();
1345 setupDrawColorFilterUniforms();
1346 setupDrawShaderUniforms();
1347 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1348
1349 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1350
1351 finishDrawTexture();
1352}
1353
Chet Haase5c13d892010-10-08 08:37:55 -07001354void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001355 const float right = left + bitmap->width();
1356 const float bottom = top + bitmap->height();
1357
1358 if (quickReject(left, top, right, bottom)) {
1359 return;
1360 }
1361
Romain Guy86568192010-12-14 15:55:39 -08001362 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001363 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001364 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001365 const AutoTexture autoCleanup(texture);
1366
Romain Guya168d732011-03-18 16:50:13 -07001367 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1368 drawAlphaBitmap(texture, left, top, paint);
1369 } else {
1370 drawTextureRect(left, top, right, bottom, texture, paint);
1371 }
Romain Guyce0537b2010-06-29 21:05:21 -07001372}
1373
Chet Haase5c13d892010-10-08 08:37:55 -07001374void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001375 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1376 const mat4 transform(*matrix);
1377 transform.mapRect(r);
1378
Romain Guy6926c722010-07-12 20:20:03 -07001379 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1380 return;
1381 }
1382
Romain Guy86568192010-12-14 15:55:39 -08001383 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001384 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001385 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001386 const AutoTexture autoCleanup(texture);
1387
Romain Guy5b3b3522010-10-27 18:57:51 -07001388 // This could be done in a cheaper way, all we need is pass the matrix
1389 // to the vertex shader. The save/restore is a bit overkill.
1390 save(SkCanvas::kMatrix_SaveFlag);
1391 concatMatrix(matrix);
1392 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1393 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001394}
1395
Romain Guy5a7b4662011-01-20 19:09:30 -08001396void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1397 float* vertices, int* colors, SkPaint* paint) {
1398 // TODO: Do a quickReject
1399 if (!vertices || mSnapshot->isIgnored()) {
1400 return;
1401 }
1402
1403 glActiveTexture(gTextureUnits[0]);
1404 Texture* texture = mCaches.textureCache.get(bitmap);
1405 if (!texture) return;
1406 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001407
Romain Guyd21b6e12011-11-30 20:21:23 -08001408 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1409 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001410
1411 int alpha;
1412 SkXfermode::Mode mode;
1413 getAlphaAndMode(paint, &alpha, &mode);
1414
Romain Guy5a7b4662011-01-20 19:09:30 -08001415 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001416
Romain Guyb18d2d02011-02-10 15:52:54 -08001417 float left = FLT_MAX;
1418 float top = FLT_MAX;
1419 float right = FLT_MIN;
1420 float bottom = FLT_MIN;
1421
1422#if RENDER_LAYERS_AS_REGIONS
1423 bool hasActiveLayer = hasLayer();
1424#else
1425 bool hasActiveLayer = false;
1426#endif
1427
Romain Guya566b7c2011-01-23 16:36:11 -08001428 // TODO: Support the colors array
1429 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001430 TextureVertex* vertex = mesh;
1431 for (int32_t y = 0; y < meshHeight; y++) {
1432 for (int32_t x = 0; x < meshWidth; x++) {
1433 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1434
1435 float u1 = float(x) / meshWidth;
1436 float u2 = float(x + 1) / meshWidth;
1437 float v1 = float(y) / meshHeight;
1438 float v2 = float(y + 1) / meshHeight;
1439
1440 int ax = i + (meshWidth + 1) * 2;
1441 int ay = ax + 1;
1442 int bx = i;
1443 int by = bx + 1;
1444 int cx = i + 2;
1445 int cy = cx + 1;
1446 int dx = i + (meshWidth + 1) * 2 + 2;
1447 int dy = dx + 1;
1448
1449 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1450 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1451 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1452
1453 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1454 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1455 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001456
1457#if RENDER_LAYERS_AS_REGIONS
1458 if (hasActiveLayer) {
1459 // TODO: This could be optimized to avoid unnecessary ops
1460 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1461 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1462 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1463 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1464 }
1465#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001466 }
1467 }
1468
Romain Guyb18d2d02011-02-10 15:52:54 -08001469#if RENDER_LAYERS_AS_REGIONS
1470 if (hasActiveLayer) {
1471 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1472 }
1473#endif
1474
Romain Guy5a7b4662011-01-20 19:09:30 -08001475 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1476 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001477 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001478}
1479
Romain Guy8ba548f2010-06-30 19:21:21 -07001480void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1481 float srcLeft, float srcTop, float srcRight, float srcBottom,
1482 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001483 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001484 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1485 return;
1486 }
1487
Romain Guy746b7402010-10-26 16:27:31 -07001488 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001489 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001490 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001491 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001492
Romain Guy8ba548f2010-06-30 19:21:21 -07001493 const float width = texture->width;
1494 const float height = texture->height;
1495
Romain Guy68169722011-08-22 17:33:33 -07001496 const float u1 = fmax(0.0f, srcLeft / width);
1497 const float v1 = fmax(0.0f, srcTop / height);
1498 const float u2 = fmin(1.0f, srcRight / width);
1499 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001500
Romain Guy03750a02010-10-18 14:06:08 -07001501 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001502 resetDrawTextureTexCoords(u1, v1, u2, v2);
1503
Romain Guy03750a02010-10-18 14:06:08 -07001504 int alpha;
1505 SkXfermode::Mode mode;
1506 getAlphaAndMode(paint, &alpha, &mode);
1507
Romain Guyd21b6e12011-11-30 20:21:23 -08001508 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1509
Romain Guy6620c6d2010-12-06 18:07:02 -08001510 if (mSnapshot->transform->isPureTranslate()) {
1511 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1512 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1513
Romain Guyb5014982011-07-28 15:39:12 -07001514 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001515 // Enable linear filtering if the source rectangle is scaled
1516 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001517 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001518 }
Romain Guyb5014982011-07-28 15:39:12 -07001519
Romain Guyd21b6e12011-11-30 20:21:23 -08001520 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001521 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1522 texture->id, alpha / 255.0f, mode, texture->blend,
1523 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1524 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1525 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001526 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001527 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1528 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1529 GL_TRIANGLE_STRIP, gMeshCount);
1530 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001531
1532 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1533}
1534
Romain Guy4aa90572010-09-26 18:40:37 -07001535void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001536 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001537 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001538 if (quickReject(left, top, right, bottom)) {
1539 return;
1540 }
1541
Romain Guy746b7402010-10-26 16:27:31 -07001542 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001543 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001544 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001545 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001546 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1547 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001548
1549 int alpha;
1550 SkXfermode::Mode mode;
1551 getAlphaAndMode(paint, &alpha, &mode);
1552
Romain Guy2728f962010-10-08 18:36:15 -07001553 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001554 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001555
Romain Guya5ef39a2010-12-03 16:48:20 -08001556 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001557 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001558#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001559 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001560 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001561 const float offsetX = left + mSnapshot->transform->getTranslateX();
1562 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001563 const size_t count = mesh->quads.size();
1564 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001565 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001566 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001567 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1568 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1569 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001570 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001571 dirtyLayer(left + bounds.left, top + bounds.top,
1572 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001573 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001574 }
1575 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001576#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001577
Romain Guy6620c6d2010-12-06 18:07:02 -08001578 if (pureTranslate) {
1579 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1580 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1581
1582 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1583 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1584 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1585 true, !mesh->hasEmptyQuads);
1586 } else {
1587 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1588 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1589 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1590 true, !mesh->hasEmptyQuads);
1591 }
Romain Guy054dc182010-10-15 17:55:25 -07001592 }
Romain Guyf7f93552010-07-08 19:17:03 -07001593}
1594
Chet Haase99ecdc42011-05-06 12:06:34 -07001595/**
Chet Haase858aa932011-05-12 09:06:00 -07001596 * This function uses a similar approach to that of AA lines in the drawLines() function.
1597 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1598 * shader to compute the translucency of the color, determined by whether a given pixel is
1599 * within that boundary region and how far into the region it is.
1600 */
1601void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001602 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001603 float inverseScaleX = 1.0f;
1604 float inverseScaleY = 1.0f;
1605 // The quad that we use needs to account for scaling.
1606 if (!mSnapshot->transform->isPureTranslate()) {
1607 Matrix4 *mat = mSnapshot->transform;
1608 float m00 = mat->data[Matrix4::kScaleX];
1609 float m01 = mat->data[Matrix4::kSkewY];
1610 float m02 = mat->data[2];
1611 float m10 = mat->data[Matrix4::kSkewX];
1612 float m11 = mat->data[Matrix4::kScaleX];
1613 float m12 = mat->data[6];
1614 float scaleX = sqrt(m00 * m00 + m01 * m01);
1615 float scaleY = sqrt(m10 * m10 + m11 * m11);
1616 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1617 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1618 }
1619
1620 setupDraw();
1621 setupDrawAALine();
1622 setupDrawColor(color);
1623 setupDrawColorFilter();
1624 setupDrawShader();
1625 setupDrawBlending(true, mode);
1626 setupDrawProgram();
1627 setupDrawModelViewIdentity(true);
1628 setupDrawColorUniforms();
1629 setupDrawColorFilterUniforms();
1630 setupDrawShaderIdentityUniforms();
1631
1632 AAVertex rects[4];
1633 AAVertex* aaVertices = &rects[0];
1634 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1635 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1636
1637 float boundarySizeX = .5 * inverseScaleX;
1638 float boundarySizeY = .5 * inverseScaleY;
1639
1640 // Adjust the rect by the AA boundary padding
1641 left -= boundarySizeX;
1642 right += boundarySizeX;
1643 top -= boundarySizeY;
1644 bottom += boundarySizeY;
1645
1646 float width = right - left;
1647 float height = bottom - top;
1648
1649 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1650 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1651 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1652 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1653 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1654 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1655 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1656
1657 if (!quickReject(left, top, right, bottom)) {
1658 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1659 AAVertex::set(aaVertices++, left, top, 1, 0);
1660 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1661 AAVertex::set(aaVertices++, right, top, 0, 0);
1662 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1663 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1664 }
1665}
1666
1667/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001668 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1669 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1670 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1671 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1672 * of the line. Hairlines are more involved because we need to account for transform scaling
1673 * to end up with a one-pixel-wide line in screen space..
1674 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1675 * in combination with values that we calculate and pass down in this method. The basic approach
1676 * is that the quad we create contains both the core line area plus a bounding area in which
1677 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1678 * proportion of the width and the length of a given segment is represented by the boundary
1679 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1680 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1681 * on the inside). This ends up giving the result we want, with pixels that are completely
1682 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1683 * how far into the boundary region they are, which is determined by shader interpolation.
1684 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001685void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1686 if (mSnapshot->isIgnored()) return;
1687
1688 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001689 // We use half the stroke width here because we're going to position the quad
1690 // corner vertices half of the width away from the line endpoints
1691 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001692 // A stroke width of 0 has a special meaning in Skia:
1693 // it draws a line 1 px wide regardless of current transform
1694 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001695 float inverseScaleX = 1.0f;
1696 float inverseScaleY = 1.0f;
1697 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001698 int alpha;
1699 SkXfermode::Mode mode;
1700 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001701 int verticesCount = count;
1702 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001703 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001704 verticesCount += (count - 4);
1705 }
1706
1707 if (isHairLine || isAA) {
1708 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1709 // the line on the screen should always be one pixel wide regardless of scale. For
1710 // AA lines, we only want one pixel of translucent boundary around the quad.
1711 if (!mSnapshot->transform->isPureTranslate()) {
1712 Matrix4 *mat = mSnapshot->transform;
1713 float m00 = mat->data[Matrix4::kScaleX];
1714 float m01 = mat->data[Matrix4::kSkewY];
1715 float m02 = mat->data[2];
1716 float m10 = mat->data[Matrix4::kSkewX];
1717 float m11 = mat->data[Matrix4::kScaleX];
1718 float m12 = mat->data[6];
1719 float scaleX = sqrt(m00*m00 + m01*m01);
1720 float scaleY = sqrt(m10*m10 + m11*m11);
1721 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1722 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1723 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1724 scaled = true;
1725 }
1726 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001727 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001728
1729 getAlphaAndMode(paint, &alpha, &mode);
1730 setupDraw();
1731 if (isAA) {
1732 setupDrawAALine();
1733 }
1734 setupDrawColor(paint->getColor(), alpha);
1735 setupDrawColorFilter();
1736 setupDrawShader();
1737 if (isAA) {
1738 setupDrawBlending(true, mode);
1739 } else {
1740 setupDrawBlending(mode);
1741 }
1742 setupDrawProgram();
1743 setupDrawModelViewIdentity(true);
1744 setupDrawColorUniforms();
1745 setupDrawColorFilterUniforms();
1746 setupDrawShaderIdentityUniforms();
1747
1748 if (isHairLine) {
1749 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001750 halfStrokeWidth = isAA? 1 : .5;
1751 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001752 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001753 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001754 }
1755 Vertex lines[verticesCount];
1756 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001757 AAVertex wLines[verticesCount];
1758 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001759 if (!isAA) {
1760 setupDrawVertices(vertices);
1761 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001762 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1763 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001764 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001765 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1766 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001767 // We will need to calculate the actual width proportion on each segment for
1768 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1769 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1770 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001771 }
1772
Chet Haase99ecdc42011-05-06 12:06:34 -07001773 AAVertex* prevAAVertex = NULL;
1774 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001775
Chet Haase99585ad2011-05-02 15:00:16 -07001776 int boundaryLengthSlot = -1;
1777 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001778 int boundaryWidthSlot = -1;
1779 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001780 for (int i = 0; i < count; i += 4) {
1781 // a = start point, b = end point
1782 vec2 a(points[i], points[i + 1]);
1783 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001784 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001785 float boundaryLengthProportion = 0;
1786 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001787
Chet Haase5b0200b2011-04-13 17:58:08 -07001788 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001789 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001790 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001791 if (isAA) {
1792 float wideningFactor;
1793 if (fabs(n.x) >= fabs(n.y)) {
1794 wideningFactor = fabs(1.0f / n.x);
1795 } else {
1796 wideningFactor = fabs(1.0f / n.y);
1797 }
1798 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001799 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001800 if (scaled) {
1801 n.x *= inverseScaleX;
1802 n.y *= inverseScaleY;
1803 }
1804 } else if (scaled) {
1805 // Extend n by .5 pixel on each side, post-transform
1806 vec2 extendedN = n.copyNormalized();
1807 extendedN /= 2;
1808 extendedN.x *= inverseScaleX;
1809 extendedN.y *= inverseScaleY;
1810 float extendedNLength = extendedN.length();
1811 // We need to set this value on the shader prior to drawing
1812 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1813 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001814 }
1815 float x = n.x;
1816 n.x = -n.y;
1817 n.y = x;
1818
Chet Haase99585ad2011-05-02 15:00:16 -07001819 // aa lines expand the endpoint vertices to encompass the AA boundary
1820 if (isAA) {
1821 vec2 abVector = (b - a);
1822 length = abVector.length();
1823 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001824 if (scaled) {
1825 abVector.x *= inverseScaleX;
1826 abVector.y *= inverseScaleY;
1827 float abLength = abVector.length();
1828 boundaryLengthProportion = abLength / (length + abLength);
1829 } else {
1830 boundaryLengthProportion = .5 / (length + 1);
1831 }
1832 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001833 a -= abVector;
1834 b += abVector;
1835 }
1836
Chet Haase5b0200b2011-04-13 17:58:08 -07001837 // Four corners of the rectangle defining a thick line
1838 vec2 p1 = a - n;
1839 vec2 p2 = a + n;
1840 vec2 p3 = b + n;
1841 vec2 p4 = b - n;
1842
Chet Haase99585ad2011-05-02 15:00:16 -07001843
Chet Haase5b0200b2011-04-13 17:58:08 -07001844 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1845 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1846 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1847 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1848
1849 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001850 if (!isAA) {
1851 if (prevVertex != NULL) {
1852 // Issue two repeat vertices to create degenerate triangles to bridge
1853 // between the previous line and the new one. This is necessary because
1854 // we are creating a single triangle_strip which will contain
1855 // potentially discontinuous line segments.
1856 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1857 Vertex::set(vertices++, p1.x, p1.y);
1858 generatedVerticesCount += 2;
1859 }
1860 Vertex::set(vertices++, p1.x, p1.y);
1861 Vertex::set(vertices++, p2.x, p2.y);
1862 Vertex::set(vertices++, p4.x, p4.y);
1863 Vertex::set(vertices++, p3.x, p3.y);
1864 prevVertex = vertices - 1;
1865 generatedVerticesCount += 4;
1866 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001867 if (!isHairLine && scaled) {
1868 // Must set width proportions per-segment for scaled non-hairlines to use the
1869 // correct AA boundary dimensions
1870 if (boundaryWidthSlot < 0) {
1871 boundaryWidthSlot =
1872 mCaches.currentProgram->getUniform("boundaryWidth");
1873 inverseBoundaryWidthSlot =
1874 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1875 }
1876 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1877 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1878 }
Chet Haase99585ad2011-05-02 15:00:16 -07001879 if (boundaryLengthSlot < 0) {
1880 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1881 inverseBoundaryLengthSlot =
1882 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1883 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001884 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1885 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001886
Chet Haase5b0200b2011-04-13 17:58:08 -07001887 if (prevAAVertex != NULL) {
1888 // Issue two repeat vertices to create degenerate triangles to bridge
1889 // between the previous line and the new one. This is necessary because
1890 // we are creating a single triangle_strip which will contain
1891 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001892 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1893 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1894 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 generatedVerticesCount += 2;
1896 }
Chet Haase99585ad2011-05-02 15:00:16 -07001897 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1898 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1899 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1900 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 prevAAVertex = aaVertices - 1;
1902 generatedVerticesCount += 4;
1903 }
Chet Haase99585ad2011-05-02 15:00:16 -07001904 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1905 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1906 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001907 }
1908 }
1909 if (generatedVerticesCount > 0) {
1910 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1911 }
1912}
1913
Romain Guyed6fcb02011-03-21 13:11:28 -07001914void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1915 if (mSnapshot->isIgnored()) return;
1916
1917 // TODO: The paint's cap style defines whether the points are square or circular
1918 // TODO: Handle AA for round points
1919
Chet Haase5b0200b2011-04-13 17:58:08 -07001920 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001921 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001922 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001923 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001924 if (isHairLine) {
1925 // Now that we know it's hairline, we can set the effective width, to be used later
1926 strokeWidth = 1.0f;
1927 }
1928 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001929 int alpha;
1930 SkXfermode::Mode mode;
1931 getAlphaAndMode(paint, &alpha, &mode);
1932
1933 int verticesCount = count >> 1;
1934 int generatedVerticesCount = 0;
1935
1936 TextureVertex pointsData[verticesCount];
1937 TextureVertex* vertex = &pointsData[0];
1938
1939 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001940 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001941 setupDrawColor(paint->getColor(), alpha);
1942 setupDrawColorFilter();
1943 setupDrawShader();
1944 setupDrawBlending(mode);
1945 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001947 setupDrawColorUniforms();
1948 setupDrawColorFilterUniforms();
1949 setupDrawPointUniforms();
1950 setupDrawShaderIdentityUniforms();
1951 setupDrawMesh(vertex);
1952
1953 for (int i = 0; i < count; i += 2) {
1954 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1955 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001956 float left = points[i] - halfWidth;
1957 float right = points[i] + halfWidth;
1958 float top = points[i + 1] - halfWidth;
1959 float bottom = points [i + 1] + halfWidth;
1960 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001961 }
1962
1963 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1964}
1965
Romain Guy85bf02f2010-06-22 13:11:24 -07001966void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001967 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001968 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001969
Romain Guyae88e5e2010-10-22 17:49:18 -07001970 Rect& clip(*mSnapshot->clipRect);
1971 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001972
Romain Guy3d58c032010-07-14 16:34:53 -07001973 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001974}
Romain Guy9d5316e2010-06-24 19:30:36 -07001975
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001976void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001977 if (!texture) return;
1978 const AutoTexture autoCleanup(texture);
1979
1980 const float x = left + texture->left - texture->offset;
1981 const float y = top + texture->top - texture->offset;
1982
1983 drawPathTexture(texture, x, y, paint);
1984}
1985
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001986void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1987 float rx, float ry, SkPaint* paint) {
1988 if (mSnapshot->isIgnored()) return;
1989
1990 glActiveTexture(gTextureUnits[0]);
1991 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1992 right - left, bottom - top, rx, ry, paint);
1993 drawShape(left, top, texture, paint);
1994}
1995
Romain Guy01d58e42011-01-19 21:54:02 -08001996void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1997 if (mSnapshot->isIgnored()) return;
1998
1999 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002000 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002001 drawShape(x - radius, y - radius, texture, paint);
2002}
Romain Guy01d58e42011-01-19 21:54:02 -08002003
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002004void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2005 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002006
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002007 glActiveTexture(gTextureUnits[0]);
2008 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2009 drawShape(left, top, texture, paint);
2010}
2011
Romain Guy8b2f5262011-01-23 16:15:02 -08002012void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2013 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2014 if (mSnapshot->isIgnored()) return;
2015
2016 if (fabs(sweepAngle) >= 360.0f) {
2017 drawOval(left, top, right, bottom, paint);
2018 return;
2019 }
2020
2021 glActiveTexture(gTextureUnits[0]);
2022 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2023 startAngle, sweepAngle, useCenter, paint);
2024 drawShape(left, top, texture, paint);
2025}
2026
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002027void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2028 SkPaint* paint) {
2029 if (mSnapshot->isIgnored()) return;
2030
2031 glActiveTexture(gTextureUnits[0]);
2032 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2033 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002034}
2035
Chet Haase5c13d892010-10-08 08:37:55 -07002036void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002037 if (p->getStyle() != SkPaint::kFill_Style) {
2038 drawRectAsShape(left, top, right, bottom, p);
2039 return;
2040 }
2041
Romain Guy6926c722010-07-12 20:20:03 -07002042 if (quickReject(left, top, right, bottom)) {
2043 return;
2044 }
2045
Romain Guy026c5e162010-06-28 17:12:22 -07002046 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002047 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002048 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2049 if (!isMode) {
2050 // Assume SRC_OVER
2051 mode = SkXfermode::kSrcOver_Mode;
2052 }
2053 } else {
2054 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002055 }
2056
Romain Guy026c5e162010-06-28 17:12:22 -07002057 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002058 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002059 drawAARect(left, top, right, bottom, color, mode);
2060 } else {
2061 drawColorRect(left, top, right, bottom, color, mode);
2062 }
Romain Guyc7d53492010-06-25 13:41:57 -07002063}
Romain Guy9d5316e2010-06-24 19:30:36 -07002064
Romain Guye8e62a42010-07-23 18:55:21 -07002065void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002066 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002067 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002068 return;
2069 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002070 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002071
Romain Guy67ffc362011-06-03 18:50:11 -07002072 // TODO: We should probably make a copy of the paint instead of modifying
2073 // it; modifying the paint will change its generationID the first
2074 // time, which might impact caches. More investigation needed to
2075 // see if it matters.
2076 // If we make a copy, then drawTextDecorations() should *not* make
2077 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002078 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002079#if RENDER_TEXT_AS_GLYPHS
2080 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2081#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002082
Romain Guye8e62a42010-07-23 18:55:21 -07002083 switch (paint->getTextAlign()) {
2084 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002085 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002086 x -= length / 2.0f;
2087 break;
2088 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002089 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002090 x -= length;
2091 break;
2092 default:
2093 break;
2094 }
2095
Romain Guycac5fd32011-12-01 20:08:50 -08002096 SkPaint::FontMetrics metrics;
2097 paint->getFontMetrics(&metrics, 0.0f);
2098 if (quickReject(x, y + metrics.fTop,
2099 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2100 return;
2101 }
2102
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002103 const float oldX = x;
2104 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002105 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2106 if (pureTranslate) {
2107 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2108 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2109 }
2110
Romain Guyb45c0c92010-08-26 20:35:23 -07002111 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002112#if DEBUG_GLYPHS
2113 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2114#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002115 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002116 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002117
Romain Guy86568192010-12-14 15:55:39 -08002118 int alpha;
2119 SkXfermode::Mode mode;
2120 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002121
Romain Guy1e45aae2010-08-13 19:39:53 -07002122 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002123 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002124 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2125 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002126 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002127
Romain Guy740bf2b2011-04-26 15:33:10 -07002128 const float sx = oldX - shadow->left + mShadowDx;
2129 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002130
Romain Guy86568192010-12-14 15:55:39 -08002131 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002132 int shadowColor = mShadowColor;
2133 if (mShader) {
2134 shadowColor = 0xffffffff;
2135 }
Romain Guy86568192010-12-14 15:55:39 -08002136
2137 glActiveTexture(gTextureUnits[0]);
2138 setupDraw();
2139 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002140 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2141 setupDrawColorFilter();
2142 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002143 setupDrawBlending(true, mode);
2144 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002145 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002146 setupDrawTexture(shadow->id);
2147 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002148 setupDrawColorFilterUniforms();
2149 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002150 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2151
Romain Guy0a417492010-08-16 20:26:20 -07002152 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002153
Romain Guy86568192010-12-14 15:55:39 -08002154 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002155 }
2156
Romain Guyb146b122010-12-15 17:06:45 -08002157 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2158 return;
2159 }
2160
Romain Guy6620c6d2010-12-06 18:07:02 -08002161 // Pick the appropriate texture filtering
2162 bool linearFilter = mSnapshot->transform->changesBounds();
2163 if (pureTranslate && !linearFilter) {
2164 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2165 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002166
Romain Guy86568192010-12-14 15:55:39 -08002167 glActiveTexture(gTextureUnits[0]);
2168 setupDraw();
2169 setupDrawDirtyRegionsDisabled();
2170 setupDrawWithTexture(true);
2171 setupDrawAlpha8Color(paint->getColor(), alpha);
2172 setupDrawColorFilter();
2173 setupDrawShader();
2174 setupDrawBlending(true, mode);
2175 setupDrawProgram();
2176 setupDrawModelView(x, y, x, y, pureTranslate, true);
2177 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2178 setupDrawPureColorUniforms();
2179 setupDrawColorFilterUniforms();
2180 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002181
Romain Guy6620c6d2010-12-06 18:07:02 -08002182 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002183 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2184
2185#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002186 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002187#else
Romain Guyf219da52011-01-16 12:54:25 -08002188 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002189#endif
Romain Guy03750a02010-10-18 14:06:08 -07002190 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002191
2192 // Tell font renderer the locations of position and texture coord
2193 // attributes so it can bind its data properly
2194 int positionSlot = mCaches.currentProgram->position;
2195 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002196 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002197 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002198#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002199 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002200 if (!pureTranslate) {
2201 mSnapshot->transform->mapRect(bounds);
2202 }
Romain Guyf219da52011-01-16 12:54:25 -08002203 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002204 }
2205#endif
2206 }
Romain Guy694b5192010-07-21 21:33:20 -07002207
2208 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002209 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002210
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002211 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002212}
2213
Romain Guy7fbcc042010-08-04 15:40:07 -07002214void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002215 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002216
Romain Guy01d58e42011-01-19 21:54:02 -08002217 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002218
Romain Guyfb8b7632010-08-23 21:05:08 -07002219 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002220 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002221 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002222
Romain Guy8b55f372010-08-18 17:10:07 -07002223 const float x = texture->left - texture->offset;
2224 const float y = texture->top - texture->offset;
2225
Romain Guy01d58e42011-01-19 21:54:02 -08002226 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002227}
2228
Romain Guyada830f2011-01-13 12:13:20 -08002229void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2230 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002231 return;
2232 }
2233
2234 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002235
2236 int alpha;
2237 SkXfermode::Mode mode;
2238 getAlphaAndMode(paint, &alpha, &mode);
2239
Romain Guy9ace8f52011-07-07 20:50:11 -07002240 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002241
Romain Guyf219da52011-01-16 12:54:25 -08002242#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002243 if (!layer->region.isEmpty()) {
2244 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002245 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002246 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002247 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002248 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002249
Romain Guyc88e3572011-01-22 00:32:12 -08002250 setupDraw();
2251 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002252 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002253 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002254 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002256 setupDrawPureColorUniforms();
2257 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002258 setupDrawTexture(layer->getTexture());
2259 if (mSnapshot->transform->isPureTranslate()) {
2260 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2261 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2262
Romain Guyd21b6e12011-11-30 20:21:23 -08002263 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002264 setupDrawModelViewTranslate(x, y,
2265 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2266 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002267 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002268 setupDrawModelViewTranslate(x, y,
2269 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2270 }
Romain Guyc88e3572011-01-22 00:32:12 -08002271 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002272
Romain Guyc88e3572011-01-22 00:32:12 -08002273 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2274 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002275
Romain Guyc88e3572011-01-22 00:32:12 -08002276 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002277
2278#if DEBUG_LAYERS_AS_REGIONS
2279 drawRegionRects(layer->region);
2280#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002281 }
Romain Guyf219da52011-01-16 12:54:25 -08002282 }
2283#else
Romain Guyada830f2011-01-13 12:13:20 -08002284 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2285 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002286#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002287}
2288
Romain Guy6926c722010-07-12 20:20:03 -07002289///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002290// Shaders
2291///////////////////////////////////////////////////////////////////////////////
2292
2293void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002294 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002295}
2296
Romain Guy06f96e22010-07-30 19:18:16 -07002297void OpenGLRenderer::setupShader(SkiaShader* shader) {
2298 mShader = shader;
2299 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002300 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002301 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002302}
2303
Romain Guyd27977d2010-07-14 19:18:51 -07002304///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002305// Color filters
2306///////////////////////////////////////////////////////////////////////////////
2307
2308void OpenGLRenderer::resetColorFilter() {
2309 mColorFilter = NULL;
2310}
2311
2312void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2313 mColorFilter = filter;
2314}
2315
2316///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002317// Drop shadow
2318///////////////////////////////////////////////////////////////////////////////
2319
2320void OpenGLRenderer::resetShadow() {
2321 mHasShadow = false;
2322}
2323
2324void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2325 mHasShadow = true;
2326 mShadowRadius = radius;
2327 mShadowDx = dx;
2328 mShadowDy = dy;
2329 mShadowColor = color;
2330}
2331
2332///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002333// Drawing implementation
2334///////////////////////////////////////////////////////////////////////////////
2335
Romain Guy01d58e42011-01-19 21:54:02 -08002336void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2337 float x, float y, SkPaint* paint) {
2338 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2339 return;
2340 }
2341
2342 int alpha;
2343 SkXfermode::Mode mode;
2344 getAlphaAndMode(paint, &alpha, &mode);
2345
2346 setupDraw();
2347 setupDrawWithTexture(true);
2348 setupDrawAlpha8Color(paint->getColor(), alpha);
2349 setupDrawColorFilter();
2350 setupDrawShader();
2351 setupDrawBlending(true, mode);
2352 setupDrawProgram();
2353 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2354 setupDrawTexture(texture->id);
2355 setupDrawPureColorUniforms();
2356 setupDrawColorFilterUniforms();
2357 setupDrawShaderUniforms();
2358 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2359
2360 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2361
2362 finishDrawTexture();
2363}
2364
Romain Guyf607bdc2010-09-10 19:20:06 -07002365// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002366#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2367#define kStdUnderline_Offset (1.0f / 9.0f)
2368#define kStdUnderline_Thickness (1.0f / 18.0f)
2369
2370void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2371 float x, float y, SkPaint* paint) {
2372 // Handle underline and strike-through
2373 uint32_t flags = paint->getFlags();
2374 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002375 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002376 float underlineWidth = length;
2377 // If length is > 0.0f, we already measured the text for the text alignment
2378 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002379 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002380 }
2381
2382 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002383 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002384 case SkPaint::kCenter_Align:
2385 offsetX = underlineWidth * 0.5f;
2386 break;
2387 case SkPaint::kRight_Align:
2388 offsetX = underlineWidth;
2389 break;
2390 default:
2391 break;
2392 }
2393
2394 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002395 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002396 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002397
Romain Guye20ecbd2010-09-22 19:49:04 -07002398 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002399 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002400
Romain Guyf6834472011-01-23 13:32:12 -08002401 int linesCount = 0;
2402 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2403 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2404
2405 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002406 float points[pointsCount];
2407 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002408
2409 if (flags & SkPaint::kUnderlineText_Flag) {
2410 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002411 points[currentPoint++] = left;
2412 points[currentPoint++] = top;
2413 points[currentPoint++] = left + underlineWidth;
2414 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002415 }
2416
2417 if (flags & SkPaint::kStrikeThruText_Flag) {
2418 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002419 points[currentPoint++] = left;
2420 points[currentPoint++] = top;
2421 points[currentPoint++] = left + underlineWidth;
2422 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002423 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002424
Romain Guy726aeba2011-06-01 14:52:00 -07002425 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002426
Romain Guy726aeba2011-06-01 14:52:00 -07002427 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002428 }
2429 }
2430}
2431
Romain Guy026c5e162010-06-28 17:12:22 -07002432void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002433 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002434 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002435 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002436 color |= 0x00ffffff;
2437 }
2438
Romain Guy70ca14e2010-12-13 18:24:33 -08002439 setupDraw();
2440 setupDrawColor(color);
2441 setupDrawShader();
2442 setupDrawColorFilter();
2443 setupDrawBlending(mode);
2444 setupDrawProgram();
2445 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2446 setupDrawColorUniforms();
2447 setupDrawShaderUniforms(ignoreTransform);
2448 setupDrawColorFilterUniforms();
2449 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002450
Romain Guyc95c8d62010-09-17 15:31:32 -07002451 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2452}
2453
Romain Guy82ba8142010-07-09 13:25:56 -07002454void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002455 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002456 int alpha;
2457 SkXfermode::Mode mode;
2458 getAlphaAndMode(paint, &alpha, &mode);
2459
Romain Guyd21b6e12011-11-30 20:21:23 -08002460 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002461
Romain Guy6620c6d2010-12-06 18:07:02 -08002462 if (mSnapshot->transform->isPureTranslate()) {
2463 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2464 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2465
Romain Guyd21b6e12011-11-30 20:21:23 -08002466 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002467 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2468 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2469 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2470 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002471 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002472 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2473 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2474 GL_TRIANGLE_STRIP, gMeshCount);
2475 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002476}
2477
Romain Guybd6b79b2010-06-26 00:13:53 -07002478void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002479 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2480 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002481 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002482}
2483
2484void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002485 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002486 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002487 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002488
Romain Guy746b7402010-10-26 16:27:31 -07002489 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490 setupDrawWithTexture();
2491 setupDrawColor(alpha, alpha, alpha, alpha);
2492 setupDrawColorFilter();
2493 setupDrawBlending(blend, mode, swapSrcDst);
2494 setupDrawProgram();
2495 if (!dirty) {
2496 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002497 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002498 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002499 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002500 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002501 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002502 }
Romain Guy86568192010-12-14 15:55:39 -08002503 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002504 setupDrawColorFilterUniforms();
2505 setupDrawTexture(texture);
2506 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002507
Romain Guy6820ac82010-09-15 18:11:50 -07002508 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002509
2510 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002511}
2512
Romain Guya5aed0d2010-09-09 14:42:43 -07002513void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002514 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002515 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2516 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002517 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002518 if (!mCaches.blend) {
2519 glEnable(GL_BLEND);
2520 }
Romain Guy82ba8142010-07-09 13:25:56 -07002521
Romain Guyf607bdc2010-09-10 19:20:06 -07002522 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2523 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002524
Romain Guya5aed0d2010-09-09 14:42:43 -07002525 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2526 glBlendFunc(sourceMode, destMode);
2527 mCaches.lastSrcMode = sourceMode;
2528 mCaches.lastDstMode = destMode;
2529 }
2530 } else {
2531 // These blend modes are not supported by OpenGL directly and have
2532 // to be implemented using shaders. Since the shader will perform
2533 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002534 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002535 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002536 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002537 }
2538
2539 if (mCaches.blend) {
2540 glDisable(GL_BLEND);
2541 }
2542 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002543 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002544 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002545 glDisable(GL_BLEND);
2546 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002547 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002548}
2549
Romain Guy889f8d12010-07-29 14:37:42 -07002550bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002551 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002552 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002553 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002554 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002555 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002556 }
Romain Guy6926c722010-07-12 20:20:03 -07002557 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002558}
2559
Romain Guy026c5e162010-06-28 17:12:22 -07002560void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002561 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002562 TextureVertex::setUV(v++, u1, v1);
2563 TextureVertex::setUV(v++, u2, v1);
2564 TextureVertex::setUV(v++, u1, v2);
2565 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002566}
2567
Chet Haase5c13d892010-10-08 08:37:55 -07002568void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002569 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002570 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002571
2572 // Skia draws using the color's alpha channel if < 255
2573 // Otherwise, it uses the paint's alpha
2574 int color = paint->getColor();
2575 *alpha = (color >> 24) & 0xFF;
2576 if (*alpha == 255) {
2577 *alpha = paint->getAlpha();
2578 }
2579 } else {
2580 *mode = SkXfermode::kSrcOver_Mode;
2581 *alpha = 255;
2582 }
Romain Guy026c5e162010-06-28 17:12:22 -07002583}
2584
Romain Guya5aed0d2010-09-09 14:42:43 -07002585SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002586 SkXfermode::Mode resultMode;
2587 if (!SkXfermode::AsMode(mode, &resultMode)) {
2588 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002589 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002590 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002591}
2592
Romain Guy9d5316e2010-06-24 19:30:36 -07002593}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002594}; // namespace android